简体   繁体   中英

warning messages not returning from local plugin in Moodle

I am trying to pull each course completion data through API. Pulling all data and the course data by entering courseid working okay, but when I enter courseid doesn't exist in Moodle, it doesn't return the warning message.

在此处输入图像描述

在此处输入图像描述

courseid 5 doesn't exist and it should throw warning messages but it returns an error.

Looks like it recognises whether $courseid is empty or not and shows the right value based on entered request. I've looked at other plugin files and saw how other people passed the warning message in their code, it looks the same and I can't figure out why this doesn't throw the error message. Am I missing importing a class maybe?

Any help would be appreciated.

Here is my code.

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, ''),
                'value' => new external_value(PARAM_RAW, 'The value to match', VALUE_DEFAULT, '')
            )
        );
    }

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

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

        $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";

        $warnings = array();
        $requestedcourseids = $params['value'];

        if (empty($params['field'])) {
            $courses = $DB->get_records_sql($sql, array());
        } else {
            $value = clean_param($params['id'], PARAM_INT);

            if (count($value) > 0) {
                $placeholders = array();

                $sql_2 = "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) 
                            WHERE cr.id = ".$requestedcourseids." GROUP BY cr.fullname, cr.id";

                $courses = $DB->get_records_sql($sql_2, $placeholders);
            }
        }

        if(!empty($courses)) {

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

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

                if(is_null($currentcourseid) || ($completion->courseid != $currentcourseid)) {
                    if(!is_null($course)) {
                        $coursesdata[] = $course;
                    }
                    $course = array();
                    $course['courseid'] = $completion->courseid;
                    $course['coursename'] = $completion->coursename;
                    $course['enrols'] = $completion->enrols;
                    $course['completed'] = $completion->completed;
                    $course['totalcourses'] = count($course);

                }

                $currentcourseid = $completion->courseid;
            }

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

        } else {
            $warning = array();
            $warning['item'] = 'course';
            $warning['itemid'] = $requestedcourseids;
            $warning['warningcode'] = '1';
            $warning['message'] = 'No course found';

            $warnings[] = $warning;
        }

        $result['course'] = $coursesdata;
        $result['warnings'] = $warnings;

        return $result;
    }

    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, 'description'),
                            'coursename' => new external_value(PARAM_TEXT, ''),
                            'enrols' => new external_value(PARAM_INT, '', VALUE_OPTIONAL),
                            'completed' => new external_value(PARAM_INT, '', VALUE_OPTIONAL),
                            'totalcourses' => new external_value(PARAM_INT, '', VALUE_OPTIONAL),
                        )
                    )
                ),
                'warnings' => new external_warnings()
            )
        );
    }
}

In your code, the variable $coursesdata is only initialised as an array if at least one requested course is found in the database.

So, the line:

$result['courses'] = $coursesdata;

Will produce a warning message (if debugging is on) and set $result['courses'] to null. The expected type here is an array, so the webservice code correctly complains that the 'courses' value is not the correct type.

To fix, make sure you add:

$coursesdata = [];

Somewhere near the top of your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM