invalid_response_exception 在 moodle 本地插件中

invalid_response_exception in moodle local plugin

我已经安装了本地插件并设法检索了一个数组。但是当我尝试加载数组列表时它会抛出错误。

'coursename' 存储在结果数组中的第二个,看起来像这样。

class local_get_completion_overview_external extends external_api {

    public static function get_completion_overview_parameters() {
        return new external_function_parameters(
            array(
                'field' => new external_value(PARAM_ALPHA, 'The field to search can be left empty for all courses or:
                    id: course id', VALUE_DEFAULT, '')
            )
        );
    }

    public static function get_completion_overview(){
        global $CFG, $DB;
        require_once($CFG->dirroot . '/course/lib.php');
        require_once($CFG->libdir . '/filterlib.php');

        $params = self::validate_parameters(self::get_completion_overview_parameters($field = ''),
            array(
                'field' => $field
            )
        );

        $sql = "SELECT DISTINCT cr.id AS courseid,
                cr.fullname AS coursename,
                COUNT(DISTINCT ra.id ) AS enrols,
                COUNT(DISTINCT cc.timecompleted) AS completed
                FROM {course} cr
                JOIN {context} ct ON ( ct.instanceid = cr.id )
                LEFT JOIN {role_assignments} ra ON ( ra.contextid = ct.id ) and ra.roleid = 5
                LEFT JOIN {course_completions} cc ON cc.course = cr.id
                GROUP BY  cr.fullname, cr.id
                ORDER BY coursename";

        $courseids = array();
        $warnings = array();
        // $requestedcourseids = $params['field'];

        if (empty($params['field'])) {
            $courses = $DB->get_records_sql($sql, array());
        }        
if(!empty($courses)){

            $coursesdata = array();
            $currentcourseid = null;
            $course = null;

            foreach($courses as $completion) {
                $context = context_system::instance();
                has_capability('moodle/site:config', $context);

                $crs = array();
                $crs['courseid'] = $completion->courseid;
                $crs['coursename'] = (string)$completion->coursename;
                $crs['enrols'] = $completion->enrols;
                $crs['completed'] = $completion->completed;

                if(is_null($currentcourseid) || ($completion->courseid != $currentcourseid)) {
                    if(!is_null($course)) {
                        $coursesdata[] = $course;
                    }
                    $course = array();
                    $course['courseid'] = $completion->courseid;
                    $course['results'] = array();
                }

                $course['results'][] = $crs;

                $currentcourseid = $completion->courseid;
            }

            if(!is_null($course)){
                $coursesdata[] = $course;
            }

            // $courses->close();
        }

        $result = array();
        $result['course'] = $coursesdata;

        return $result;
        // return array(
        //     'courseid' => $crs['courseid'],
        //     'coursename' => $crs['coursename'],
        //     'enrols' => $crs['enrols'],
        //     'completed' => $crs['completed'],
        // );
    }

    public static function get_completion_overview_returns() {
        return new external_single_structure(
            array(
                'course' => new external_multiple_structure(
                    new external_single_structure(
                        array(
                            'courseid' => new external_value(PARAM_INT, ''),
                            'coursename' => new external_value(PARAM_TEXT, ''),
                            'enrols' => new external_value(PARAM_INT, ''),
                            'completed' => new external_value(PARAM_INT, ''),
                        )
                        )
                ),
            )
        );
    }

我发现一个 moodle 论坛说错误的 lang type 会导致这个问题,所以应该改变它。 (https://moodle.org/mod/forum/discuss.php?d=355334) 我想这可能是这种情况,因为错误从 return() 中的第二个数组抛出,这是一个文本值,但我可能是错的,最好在我做下一步之前问一下。

在此先致谢,如有任何帮助,我们将不胜感激!

您可以通过将 VALUE_OPTIONAL 子句添加到 return 数据来解决此问题:

public static function get_completion_overview_returns() {
        return new external_single_structure(
            array(
                'course' => new external_multiple_structure(
                    new external_single_structure(
                        array(
                            'courseid' => new external_value(PARAM_INT, ''),
                            'coursename' => new external_value(PARAM_TEXT, '', VALUE_OPTIONAL),
                            'enrols' => new external_value(PARAM_INT, '', VALUE_OPTIONAL),
                            'completed' => new external_value(PARAM_INT, '', VALUE_OPTIONAL),
                        )
                    )
                ),
            )
        );
    }

然后您可能应该检查空数据案例。