Hey folks,
I'm experiencing some difficulty writing a new webservice for Moodle 2.5.1.
I'm trying to set up a webservice to return some course information along with a list of enrolment types available to that course. For some reason I can't figure out, the only enrolment types to show up in the 'enrolments' array is self enrolment. None of the other types show up.
I think my query is correct. I've included my code below.
class local_droodle_external extends external_api {
/**
* Returns desription of method parameters
* @return external_function_parameters
*/
public static function get_course_parameters() {
return new external_function_parameters(
array(
'courseid' => new external_value(PARAM_TEXT, 'The ID of the course for which you want the fullname')
)
);
}
/**
* Returns course fullname
* @return string course fullname
*/
public static function get_course($courseID) {
// Parameter Validation
$params = self::validate_parameters(self::get_course_fullname_parameters(), array('courseid' => $courseID));
global $CFG, $DB;
$query =
"SELECT
course.id,
course.fullname,
course.shortname,
course.idnumber,
course.summary,
course.format,
course.startdate,
course.visible,
course.lang,
course.timecreated,
course.timemodified,
categories.name AS cat_name,
categories.id AS cat_id,
categories.description AS cat_desc,
enrol.id AS enrol_id,
enrol.enrol AS enrol_type,
enrol.enrolstartdate AS enrol_startdate,
enrol.enrolenddate AS enrol_enddate,
enrol.enrolperiod AS enrol_period,
enrol.status AS enrol_status,
enrol.password AS enrol_password,
enrol.timecreated AS enrol_created,
enrol.timemodified AS enrol_modified,
user.username AS user_username
FROM {$CFG->prefix}course course
LEFT JOIN {$CFG->prefix}course_categories categories ON course.category = categories.id
LEFT JOIN {$CFG->prefix}enrol enrol ON course.id = enrol.courseid
LEFT JOIN {$CFG->prefix}user_enrolments user_enrol ON enrol.id = user_enrol.enrolid
LEFT JOIN {$CFG->prefix}user user ON user_enrol.userid = user.id";
$records = $DB->get_records_sql($query);
foreach ($records as $course) {
if(!isset($return[$course->id])) {
$return[$course->id]['courseid'] = $course->id;
$return[$course->id]['fullname'] = $course->fullname;
$return[$course->id]['shortname'] = $course->shortname;
$return[$course->id]['idnumber'] = $course->idnumber;
$return[$course->id]['summary'] = $course->summary;
$return[$course->id]['format'] = $course->format;
$return[$course->id]['startdate'] = $course->startdate;
$return[$course->id]['visible'] = $course->visible;
$return[$course->id]['lang'] = $course->lang;
$return[$course->id]['timecreated'] = $course->timecreated;
$return[$course->id]['timemodified'] = $course->timemodified;
$return[$course->id]['cat_name'] = $course->cat_name;
$return[$course->id]['cat_id'] = $course->cat_id;
$return[$course->id]['cat_desc'] = $course->cat_desc;
$return[$course->id]['enrolments'] = array();
}
$return[$course->id]['enrolments'][$course->enrol_type]['type'] = $course->enrol_type;
$return[$course->id]['enrolments'][$course->enrol_type]['id'] = $course->enrol_id;
$return[$course->id]['enrolments'][$course->enrol_type]['startdate'] = $course->enrol_startdate;
$return[$course->id]['enrolments'][$course->enrol_type]['enddate'] = $course->enrol_enddate;
$return[$course->id]['enrolments'][$course->enrol_type]['period'] = $course->enrol_period;
$return[$course->id]['enrolments'][$course->enrol_type]['status'] = $course->enrol_status;
$return[$course->id]['enrolments'][$course->enrol_type]['password'] = $course->enrol_password;
}
return $return;
}
public static function get_course_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'courseid' => new external_value(PARAM_INT, 'Course id'),
'fullname' => new external_value(PARAM_TEXT, 'Course fullname'),
'shortname' => new external_value(PARAM_TEXT, 'Course shortname'),
'idnumber' => new external_value(PARAM_TEXT, 'Course ID Number (Not the same as Course id)'),
'summary' => new external_value(PARAM_RAW, 'Course summary'),
'format' => new external_value(PARAM_TEXT, 'Course Format'),
'startdate' => new external_value(PARAM_INT, 'Course start date'),
'visible' => new external_value(PARAM_INT, 'Course visibility'),
'lang' => new external_value(PARAM_TEXT, 'Course language'),
'timecreated' => new external_value(PARAM_INT, "Course creation date"),
'timemodified' => new external_value(PARAM_INT, "Course last modified"),
'cat_name' => new external_value(PARAM_TEXT, 'Category Name'),
'cat_id' => new external_value(PARAM_INT, 'Category ID'),
'cat_desc' => new external_value(PARAM_RAW, 'Category description'),
'enrolments' => new external_multiple_structure(
new external_single_structure(
array(
'type' => new external_value(PARAM_TEXT, 'Enrolment type'),
'id' => new external_value(PARAM_INT, 'Enrolment ID'),
'startdate' => new external_value(PARAM_TEXT, 'Enrolment start date'),
'enddate' => new external_value(PARAM_INT, 'Enrolment end date'),
'period' => new external_value(PARAM_TEXT, 'Enrolment period'),
'status' => new external_value(PARAM_INT, 'Enrolment status'),
'password' => new external_value(PARAM_RAW, 'Enrolment password'),
)
)
)
)
)
);
}
}
I'm at a loss and could use some help.
Thank you!