MDL-45214 always try to use ID sort in get_events_select()
[moodle.git] / enrol / externallib.php
blob619cea0d9c77c8688935178540694a2a923af7f3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * External course participation api.
21 * This api is mostly read only, the actual enrol and unenrol
22 * support is in each enrol plugin.
24 * @package core_enrol
25 * @category external
26 * @copyright 2010 Jerome Mouneyrac
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
32 require_once("$CFG->libdir/externallib.php");
34 /**
35 * Enrol external functions
37 * @package core_enrol
38 * @category external
39 * @copyright 2011 Jerome Mouneyrac
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 * @since Moodle 2.2
43 class core_enrol_external extends external_api {
45 /**
46 * Returns description of method parameters
48 * @return external_function_parameters
49 * @since Moodle 2.4
51 public static function get_enrolled_users_with_capability_parameters() {
52 return new external_function_parameters(
53 array (
54 'coursecapabilities' => new external_multiple_structure(
55 new external_single_structure(
56 array (
57 'courseid' => new external_value(PARAM_INT, 'Course ID number in the Moodle course table'),
58 'capabilities' => new external_multiple_structure(
59 new external_value(PARAM_CAPABILITY, 'Capability name, such as mod/forum:viewdiscussion')),
62 , 'course id and associated capability name'),
63 'options' => new external_multiple_structure(
64 new external_single_structure(
65 array(
66 'name' => new external_value(PARAM_ALPHANUMEXT, 'option name'),
67 'value' => new external_value(PARAM_RAW, 'option value')
69 ), 'Option names:
70 * groupid (integer) return only users in this group id. Requires \'moodle/site:accessallgroups\' .
71 * onlyactive (integer) only users with active enrolments. Requires \'moodle/course:enrolreview\' .
72 * userfields (\'string, string, ...\') return only the values of these user fields.
73 * limitfrom (integer) sql limit from.
74 * limitnumber (integer) max number of users per course and capability.', VALUE_DEFAULT, array())
79 /**
80 * Return users that have the capabilities for each course specified. For each course and capability specified,
81 * a list of the users that are enrolled in the course and have that capability are returned.
83 * @param array $coursecapabilities array of course ids and associated capability names {courseid, {capabilities}}
84 * @return array An array of arrays describing users for each associated courseid and capability
85 * @since Moodle 2.4
87 public static function get_enrolled_users_with_capability($coursecapabilities, $options) {
88 global $CFG, $DB;
89 require_once($CFG->dirroot . "/user/lib.php");
91 if (empty($coursecapabilities)) {
92 throw new invalid_parameter_exception('Parameter can not be empty');
94 $params = self::validate_parameters(self::get_enrolled_users_with_capability_parameters(),
95 array ('coursecapabilities' => $coursecapabilities, 'options'=>$options));
96 $result = array();
97 $userlist = array();
98 $groupid = 0;
99 $onlyactive = false;
100 $userfields = array();
101 $limitfrom = 0;
102 $limitnumber = 0;
103 foreach ($params['options'] as $option) {
104 switch ($option['name']) {
105 case 'groupid':
106 $groupid = (int)$option['value'];
107 break;
108 case 'onlyactive':
109 $onlyactive = !empty($option['value']);
110 break;
111 case 'userfields':
112 $thefields = explode(',', $option['value']);
113 foreach ($thefields as $f) {
114 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT);
116 break;
117 case 'limitfrom' :
118 $limitfrom = clean_param($option['value'], PARAM_INT);
119 break;
120 case 'limitnumber' :
121 $limitnumber = clean_param($option['value'], PARAM_INT);
122 break;
126 foreach ($params['coursecapabilities'] as $coursecapability) {
127 $courseid = $coursecapability['courseid'];
128 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
129 $coursecontext = context_course::instance($courseid);
130 if (!$coursecontext) {
131 throw new moodle_exception('cannotfindcourse', 'error', '', null,
132 'The course id ' . $courseid . ' doesn\'t exist.');
134 if ($courseid == SITEID) {
135 $context = context_system::instance();
136 } else {
137 $context = $coursecontext;
139 try {
140 self::validate_context($context);
141 } catch (Exception $e) {
142 $exceptionparam = new stdClass();
143 $exceptionparam->message = $e->getMessage();
144 $exceptionparam->courseid = $params['courseid'];
145 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
148 if ($courseid == SITEID) {
149 require_capability('moodle/site:viewparticipants', $context);
150 } else {
151 require_capability('moodle/course:viewparticipants', $context);
153 // The accessallgroups capability is needed to use this option.
154 if (!empty($groupid) && groups_is_member($groupid)) {
155 require_capability('moodle/site:accessallgroups', $coursecontext);
157 // The course:enrolereview capability is needed to use this option.
158 if ($onlyactive) {
159 require_capability('moodle/course:enrolreview', $coursecontext);
162 // To see the permissions of others role:review capability is required.
163 require_capability('moodle/role:review', $coursecontext);
164 foreach ($coursecapability['capabilities'] as $capability) {
165 $courseusers['courseid'] = $courseid;
166 $courseusers['capability'] = $capability;
168 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $capability, $groupid, $onlyactive);
170 $sql = "SELECT u.* FROM {user} u WHERE u.id IN ($enrolledsql) ORDER BY u.id ASC";
172 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
173 $users = array();
174 foreach ($enrolledusers as $courseuser) {
175 if ($userdetails = user_get_user_details($courseuser, $course, $userfields)) {
176 $users[] = $userdetails;
179 $enrolledusers->close();
180 $courseusers['users'] = $users;
181 $result[] = $courseusers;
184 return $result;
188 * Returns description of method result value
190 * @return external_multiple_structure
191 * @since Moodle 2.4
193 public static function get_enrolled_users_with_capability_returns() {
194 return new external_multiple_structure( new external_single_structure (
195 array (
196 'courseid' => new external_value(PARAM_INT, 'Course ID number in the Moodle course table'),
197 'capability' => new external_value(PARAM_CAPABILITY, 'Capability name'),
198 'users' => new external_multiple_structure(
199 new external_single_structure(
200 array(
201 'id' => new external_value(PARAM_INT, 'ID of the user'),
202 'username' => new external_value(PARAM_RAW, 'Username', VALUE_OPTIONAL),
203 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
204 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
205 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
206 'email' => new external_value(PARAM_TEXT, 'Email address', VALUE_OPTIONAL),
207 'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL),
208 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
209 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
210 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
211 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
212 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
213 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
214 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
215 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
216 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
217 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
218 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
219 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
220 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
221 'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL),
222 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
223 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
224 'country' => new external_value(PARAM_ALPHA, 'Country code of the user, such as AU or CZ', VALUE_OPTIONAL),
225 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small', VALUE_OPTIONAL),
226 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big', VALUE_OPTIONAL),
227 'customfields' => new external_multiple_structure(
228 new external_single_structure(
229 array(
230 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field'),
231 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
232 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
233 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field'),
235 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
236 'groups' => new external_multiple_structure(
237 new external_single_structure(
238 array(
239 'id' => new external_value(PARAM_INT, 'group id'),
240 'name' => new external_value(PARAM_RAW, 'group name'),
241 'description' => new external_value(PARAM_RAW, 'group description'),
243 ), 'user groups', VALUE_OPTIONAL),
244 'roles' => new external_multiple_structure(
245 new external_single_structure(
246 array(
247 'roleid' => new external_value(PARAM_INT, 'role id'),
248 'name' => new external_value(PARAM_RAW, 'role name'),
249 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
250 'sortorder' => new external_value(PARAM_INT, 'role sortorder')
252 ), 'user roles', VALUE_OPTIONAL),
253 'preferences' => new external_multiple_structure(
254 new external_single_structure(
255 array(
256 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
257 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
259 ), 'User preferences', VALUE_OPTIONAL),
260 'enrolledcourses' => new external_multiple_structure(
261 new external_single_structure(
262 array(
263 'id' => new external_value(PARAM_INT, 'Id of the course'),
264 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
265 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
267 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
269 ), 'List of users that are enrolled in the course and have the specified capability'),
276 * Returns description of method parameters
278 * @return external_function_parameters
280 public static function get_users_courses_parameters() {
281 return new external_function_parameters(
282 array(
283 'userid' => new external_value(PARAM_INT, 'user id'),
289 * Get list of courses user is enrolled in (only active enrolments are returned).
290 * Please note the current user must be able to access the course, otherwise the course is not included.
292 * @param int $userid
293 * @return array of courses
295 public static function get_users_courses($userid) {
296 global $USER, $DB;
298 // Do basic automatic PARAM checks on incoming data, using params description
299 // If any problems are found then exceptions are thrown with helpful error messages
300 $params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid));
302 $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible');
303 $result = array();
305 foreach ($courses as $course) {
306 $context = context_course::instance($course->id, IGNORE_MISSING);
307 try {
308 self::validate_context($context);
309 } catch (Exception $e) {
310 // current user can not access this course, sorry we can not disclose who is enrolled in this course!
311 continue;
314 if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) {
315 // we need capability to view participants
316 continue;
319 list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context);
320 $enrolledsql = "SELECT COUNT('x') FROM ($enrolledsqlselect) enrolleduserids";
321 $enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams);
323 $result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible, 'enrolledusercount'=>$enrolledusercount);
326 return $result;
330 * Returns description of method result value
332 * @return external_description
334 public static function get_users_courses_returns() {
335 return new external_multiple_structure(
336 new external_single_structure(
337 array(
338 'id' => new external_value(PARAM_INT, 'id of course'),
339 'shortname' => new external_value(PARAM_RAW, 'short name of course'),
340 'fullname' => new external_value(PARAM_RAW, 'long name of course'),
341 'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'),
342 'idnumber' => new external_value(PARAM_RAW, 'id number of course'),
343 'visible' => new external_value(PARAM_INT, '1 means visible, 0 means hidden course'),
350 * Returns description of method parameters
352 * @return external_function_parameters
354 public static function get_enrolled_users_parameters() {
355 return new external_function_parameters(
356 array(
357 'courseid' => new external_value(PARAM_INT, 'course id'),
358 'options' => new external_multiple_structure(
359 new external_single_structure(
360 array(
361 'name' => new external_value(PARAM_ALPHANUMEXT, 'option name'),
362 'value' => new external_value(PARAM_RAW, 'option value')
364 ), 'Option names:
365 * withcapability (string) return only users with this capability. This option requires \'moodle/role:review\' on the course context.
366 * groupid (integer) return only users in this group id. This option requires \'moodle/site:accessallgroups\' on the course context.
367 * onlyactive (integer) return only users with active enrolments and matching time restrictions. This option requires \'moodle/course:enrolreview\' on the course context.
368 * userfields (\'string, string, ...\') return only the values of these user fields.
369 * limitfrom (integer) sql limit from.
370 * limitnumber (integer) maximum number of returned users.', VALUE_DEFAULT, array()),
376 * Get course participants details
378 * @param int $courseid course id
379 * @param array $options options {
380 * 'name' => option name
381 * 'value' => option value
383 * @return array An array of users
385 public static function get_enrolled_users($courseid, $options = array()) {
386 global $CFG, $USER, $DB;
387 require_once($CFG->dirroot . "/user/lib.php");
389 $params = self::validate_parameters(
390 self::get_enrolled_users_parameters(),
391 array(
392 'courseid'=>$courseid,
393 'options'=>$options
396 $withcapability = '';
397 $groupid = 0;
398 $onlyactive = false;
399 $userfields = array();
400 $limitfrom = 0;
401 $limitnumber = 0;
402 foreach ($options as $option) {
403 switch ($option['name']) {
404 case 'withcapability':
405 $withcapability = $option['value'];
406 break;
407 case 'groupid':
408 $groupid = (int)$option['value'];
409 break;
410 case 'onlyactive':
411 $onlyactive = !empty($option['value']);
412 break;
413 case 'userfields':
414 $thefields = explode(',', $option['value']);
415 foreach ($thefields as $f) {
416 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT);
418 break;
419 case 'limitfrom' :
420 $limitfrom = clean_param($option['value'], PARAM_INT);
421 break;
422 case 'limitnumber' :
423 $limitnumber = clean_param($option['value'], PARAM_INT);
424 break;
428 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
429 $coursecontext = context_course::instance($courseid, IGNORE_MISSING);
430 if ($courseid == SITEID) {
431 $context = context_system::instance();
432 } else {
433 $context = $coursecontext;
435 try {
436 self::validate_context($context);
437 } catch (Exception $e) {
438 $exceptionparam = new stdClass();
439 $exceptionparam->message = $e->getMessage();
440 $exceptionparam->courseid = $params['courseid'];
441 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
444 if ($courseid == SITEID) {
445 require_capability('moodle/site:viewparticipants', $context);
446 } else {
447 require_capability('moodle/course:viewparticipants', $context);
449 // to overwrite this parameter, you need role:review capability
450 if ($withcapability) {
451 require_capability('moodle/role:review', $coursecontext);
453 // need accessallgroups capability if you want to overwrite this option
454 if (!empty($groupid) && groups_is_member($groupid)) {
455 require_capability('moodle/site:accessallgroups', $coursecontext);
457 // to overwrite this option, you need course:enrolereview permission
458 if ($onlyactive) {
459 require_capability('moodle/course:enrolreview', $coursecontext);
462 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
463 $ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
464 $ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
465 $enrolledparams['contextlevel'] = CONTEXT_USER;
466 $sql = "SELECT u.* $ctxselect
467 FROM {user} u $ctxjoin
468 WHERE u.id IN ($enrolledsql)
469 ORDER BY u.id ASC";
470 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
471 $users = array();
472 foreach ($enrolledusers as $user) {
473 context_helper::preload_from_record($user);
474 if ($userdetails = user_get_user_details($user, $course, $userfields)) {
475 $users[] = $userdetails;
478 $enrolledusers->close();
480 return $users;
484 * Returns description of method result value
486 * @return external_description
488 public static function get_enrolled_users_returns() {
489 return new external_multiple_structure(
490 new external_single_structure(
491 array(
492 'id' => new external_value(PARAM_INT, 'ID of the user'),
493 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL),
494 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
495 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
496 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
497 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
498 'address' => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL),
499 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
500 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
501 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
502 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
503 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
504 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
505 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
506 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
507 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
508 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
509 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
510 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
511 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
512 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
513 'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
514 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
515 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
516 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
517 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version', VALUE_OPTIONAL),
518 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version', VALUE_OPTIONAL),
519 'customfields' => new external_multiple_structure(
520 new external_single_structure(
521 array(
522 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
523 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
524 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
525 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
527 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
528 'groups' => new external_multiple_structure(
529 new external_single_structure(
530 array(
531 'id' => new external_value(PARAM_INT, 'group id'),
532 'name' => new external_value(PARAM_RAW, 'group name'),
533 'description' => new external_value(PARAM_RAW, 'group description'),
534 'descriptionformat' => new external_format_value('description'),
536 ), 'user groups', VALUE_OPTIONAL),
537 'roles' => new external_multiple_structure(
538 new external_single_structure(
539 array(
540 'roleid' => new external_value(PARAM_INT, 'role id'),
541 'name' => new external_value(PARAM_RAW, 'role name'),
542 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
543 'sortorder' => new external_value(PARAM_INT, 'role sortorder')
545 ), 'user roles', VALUE_OPTIONAL),
546 'preferences' => new external_multiple_structure(
547 new external_single_structure(
548 array(
549 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
550 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
552 ), 'User preferences', VALUE_OPTIONAL),
553 'enrolledcourses' => new external_multiple_structure(
554 new external_single_structure(
555 array(
556 'id' => new external_value(PARAM_INT, 'Id of the course'),
557 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
558 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
560 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
567 * Returns description of get_course_enrolment_methods() parameters
569 * @return external_function_parameters
571 public static function get_course_enrolment_methods_parameters() {
572 return new external_function_parameters(
573 array(
574 'courseid' => new external_value(PARAM_INT, 'Course id')
580 * Get list of active course enrolment methods for current user.
582 * @param int $courseid
583 * @return array of course enrolment methods
585 public static function get_course_enrolment_methods($courseid) {
587 $params = self::validate_parameters(self::get_course_enrolment_methods_parameters(), array('courseid' => $courseid));
589 $coursecontext = context_course::instance($params['courseid']);
590 $categorycontext = $coursecontext->get_parent_context();
591 self::validate_context($categorycontext);
593 $result = array();
594 $enrolinstances = enrol_get_instances($params['courseid'], true);
595 foreach ($enrolinstances as $enrolinstance) {
596 if ($enrolplugin = enrol_get_plugin($enrolinstance->enrol)) {
597 if ($instanceinfo = $enrolplugin->get_enrol_info($enrolinstance)) {
598 $result[] = (array) $instanceinfo;
602 return $result;
606 * Returns description of get_course_enrolment_methods() result value
608 * @return external_description
610 public static function get_course_enrolment_methods_returns() {
611 return new external_multiple_structure(
612 new external_single_structure(
613 array(
614 'id' => new external_value(PARAM_INT, 'id of course enrolment instance'),
615 'courseid' => new external_value(PARAM_INT, 'id of course'),
616 'type' => new external_value(PARAM_PLUGIN, 'type of enrolment plugin'),
617 'name' => new external_value(PARAM_RAW, 'name of enrolment plugin'),
618 'status' => new external_value(PARAM_RAW, 'status of enrolment plugin'),
619 'wsfunction' => new external_value(PARAM_ALPHANUMEXT, 'webservice function to get more information', VALUE_OPTIONAL),
627 * Role external functions
629 * @package core_role
630 * @category external
631 * @copyright 2011 Jerome Mouneyrac
632 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
633 * @since Moodle 2.2
635 class core_role_external extends external_api {
638 * Returns description of method parameters
640 * @return external_function_parameters
642 public static function assign_roles_parameters() {
643 return new external_function_parameters(
644 array(
645 'assignments' => new external_multiple_structure(
646 new external_single_structure(
647 array(
648 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
649 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
650 'contextid' => new external_value(PARAM_INT, 'The context to assign the user role in', VALUE_OPTIONAL),
651 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to assign the user role in
652 (block, course, coursecat, system, user, module)', VALUE_OPTIONAL),
653 'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be assigned', VALUE_OPTIONAL),
662 * Manual role assignments to users
664 * @param array $assignments An array of manual role assignment
666 public static function assign_roles($assignments) {
667 global $DB;
669 // Do basic automatic PARAM checks on incoming data, using params description
670 // If any problems are found then exceptions are thrown with helpful error messages
671 $params = self::validate_parameters(self::assign_roles_parameters(), array('assignments'=>$assignments));
673 $transaction = $DB->start_delegated_transaction();
675 foreach ($params['assignments'] as $assignment) {
676 // Ensure correct context level with a instance id or contextid is passed.
677 $context = self::get_context_from_params($assignment);
679 // Ensure the current user is allowed to run this function in the enrolment context.
680 self::validate_context($context);
681 require_capability('moodle/role:assign', $context);
683 // throw an exception if user is not able to assign the role in this context
684 $roles = get_assignable_roles($context, ROLENAME_SHORT);
686 if (!array_key_exists($assignment['roleid'], $roles)) {
687 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
690 role_assign($assignment['roleid'], $assignment['userid'], $context->id);
693 $transaction->allow_commit();
697 * Returns description of method result value
699 * @return null
701 public static function assign_roles_returns() {
702 return null;
707 * Returns description of method parameters
709 * @return external_function_parameters
711 public static function unassign_roles_parameters() {
712 return new external_function_parameters(
713 array(
714 'unassignments' => new external_multiple_structure(
715 new external_single_structure(
716 array(
717 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
718 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
719 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from', VALUE_OPTIONAL),
720 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to unassign the user role in
721 + (block, course, coursecat, system, user, module)', VALUE_OPTIONAL),
722 'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be unassigned', VALUE_OPTIONAL),
731 * Unassign roles from users
733 * @param array $unassignments An array of unassignment
735 public static function unassign_roles($unassignments) {
736 global $DB;
738 // Do basic automatic PARAM checks on incoming data, using params description
739 // If any problems are found then exceptions are thrown with helpful error messages
740 $params = self::validate_parameters(self::unassign_roles_parameters(), array('unassignments'=>$unassignments));
742 $transaction = $DB->start_delegated_transaction();
744 foreach ($params['unassignments'] as $unassignment) {
745 // Ensure the current user is allowed to run this function in the unassignment context
746 $context = self::get_context_from_params($unassignment);
747 self::validate_context($context);
748 require_capability('moodle/role:assign', $context);
750 // throw an exception if user is not able to unassign the role in this context
751 $roles = get_assignable_roles($context, ROLENAME_SHORT);
752 if (!array_key_exists($unassignment['roleid'], $roles)) {
753 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
756 role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id);
759 $transaction->allow_commit();
763 * Returns description of method result value
765 * @return null
767 public static function unassign_roles_returns() {
768 return null;
774 * Deprecated enrol and role external functions
776 * @package core_enrol
777 * @copyright 2010 Jerome Mouneyrac
778 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
779 * @since Moodle 2.0
780 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
781 * @see core_enrol_external
782 * @see core_role_external
784 class moodle_enrol_external extends external_api {
788 * Returns description of method parameters
790 * @return external_function_parameters
791 * @since Moodle 2.0
792 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
793 * @see core_enrol_external::get_enrolled_users_parameters()
795 public static function get_enrolled_users_parameters() {
796 return new external_function_parameters(
797 array(
798 'courseid' => new external_value(PARAM_INT, 'Course id'),
799 'withcapability' => new external_value(PARAM_CAPABILITY, 'User should have this capability', VALUE_DEFAULT, null),
800 'groupid' => new external_value(PARAM_INT, 'Group id, null means all groups', VALUE_DEFAULT, null),
801 'onlyactive' => new external_value(PARAM_INT, 'True means only active, false means all participants', VALUE_DEFAULT, 0),
807 * Get list of course participants.
809 * @param int $courseid
810 * @param text $withcapability
811 * @param int $groupid
812 * @param bool $onlyactive
813 * @return array of course participants
814 * @since Moodle 2.0
815 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
816 * @see core_enrol_external::get_enrolled_users()
818 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) {
819 global $DB, $CFG, $USER;
821 // Do basic automatic PARAM checks on incoming data, using params description
822 // If any problems are found then exceptions are thrown with helpful error messages
823 $params = self::validate_parameters(self::get_enrolled_users_parameters(), array(
824 'courseid'=>$courseid,
825 'withcapability'=>$withcapability,
826 'groupid'=>$groupid,
827 'onlyactive'=>$onlyactive)
830 $coursecontext = context_course::instance($params['courseid'], IGNORE_MISSING);
831 if ($courseid == SITEID) {
832 $context = context_system::instance();
833 } else {
834 $context = $coursecontext;
837 try {
838 self::validate_context($context);
839 } catch (Exception $e) {
840 $exceptionparam = new stdClass();
841 $exceptionparam->message = $e->getMessage();
842 $exceptionparam->courseid = $params['courseid'];
843 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
846 if ($courseid == SITEID) {
847 require_capability('moodle/site:viewparticipants', $context);
848 } else {
849 require_capability('moodle/course:viewparticipants', $context);
852 if ($withcapability) {
853 require_capability('moodle/role:review', $coursecontext);
855 if ($groupid && groups_is_member($groupid)) {
856 require_capability('moodle/site:accessallgroups', $coursecontext);
858 if ($onlyactive) {
859 require_capability('moodle/course:enrolreview', $coursecontext);
862 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
863 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid
864 FROM {user_enrolments} ue
865 JOIN {enrol} e ON (e.id = ue.enrolid)
866 JOIN {user} u ON (ue.userid = u.id)
867 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER . ")
868 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams)
869 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id";
870 $params['courseid'] = $courseid;
871 $enrolledusers = $DB->get_records_sql($sql, $params);
872 $result = array();
873 $isadmin = is_siteadmin($USER);
874 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
875 foreach ($enrolledusers as $enrolleduser) {
876 $profilimgurl = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f1');
877 $profilimgurlsmall = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f2');
878 $resultuser = array(
879 'courseid' => $enrolleduser->courseid,
880 'userid' => $enrolleduser->userid,
881 'fullname' => fullname($enrolleduser),
882 'profileimgurl' => $profilimgurl->out(false),
883 'profileimgurlsmall' => $profilimgurlsmall->out(false)
885 // check if we can return username
886 if ($isadmin) {
887 $resultuser['username'] = $enrolleduser->username;
889 // check if we can return first and last name
890 if ($isadmin or $canviewfullnames) {
891 $resultuser['firstname'] = $enrolleduser->firstname;
892 $resultuser['lastname'] = $enrolleduser->lastname;
894 $result[] = $resultuser;
897 return $result;
901 * Returns description of method result value
903 * @return external_description
904 * @since Moodle 2.0
905 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
906 * @see core_enrol_external::get_enrolled_users_returns()
908 public static function get_enrolled_users_returns() {
909 return new external_multiple_structure(
910 new external_single_structure(
911 array(
912 'courseid' => new external_value(PARAM_INT, 'id of course'),
913 'userid' => new external_value(PARAM_INT, 'id of user'),
914 'firstname' => new external_value(PARAM_RAW, 'first name of user', VALUE_OPTIONAL),
915 'lastname' => new external_value(PARAM_RAW, 'last name of user', VALUE_OPTIONAL),
916 'fullname' => new external_value(PARAM_RAW, 'fullname of user'),
917 'username' => new external_value(PARAM_RAW, 'username of user', VALUE_OPTIONAL),
918 'profileimgurl' => new external_value(PARAM_URL, 'url of the profile image'),
919 'profileimgurlsmall' => new external_value(PARAM_URL, 'url of the profile image (small version)')
926 * Returns description of method parameters
928 * @return external_function_parameters
929 * @since Moodle 2.1
930 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
931 * @see core_enrol_external::get_users_courses_parameters()
933 public static function get_users_courses_parameters() {
934 return core_enrol_external::get_users_courses_parameters();
938 * Get list of courses user is enrolled in (only active enrolments are returned).
939 * Please note the current user must be able to access the course, otherwise the course is not included.
941 * @param int $userid
942 * @return array of courses
943 * @since Moodle 2.1
944 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
945 * @see use core_enrol_external::get_users_courses()
947 public static function get_users_courses($userid) {
948 return core_enrol_external::get_users_courses($userid);
952 * Returns description of method result value
954 * @return external_description
955 * @since Moodle 2.1
956 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
957 * @see core_enrol_external::get_users_courses_returns()
959 public static function get_users_courses_returns() {
960 return core_enrol_external::get_users_courses_returns();
965 * Returns description of method parameters
967 * @return external_function_parameters
968 * @since Moodle 2.0
969 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
970 * @see core_role_external::assign_roles_parameters()
972 public static function role_assign_parameters() {
973 return core_role_external::assign_roles_parameters();
977 * Manual role assignments to users
979 * @param array $assignments An array of manual role assignment
980 * @since Moodle 2.0
981 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
982 * @see core_role_external::assign_roles()
984 public static function role_assign($assignments) {
985 return core_role_external::assign_roles($assignments);
989 * Returns description of method result value
991 * @return null
992 * @since Moodle 2.0
993 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
994 * @see core_role_external::assign_roles_returns()
996 public static function role_assign_returns() {
997 return core_role_external::assign_roles_returns();
1002 * Returns description of method parameters
1004 * @return external_function_parameters
1005 * @since Moodle 2.0
1006 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1007 * @see core_role_external::unassign_roles_parameters()
1009 public static function role_unassign_parameters() {
1010 return core_role_external::unassign_roles_parameters();
1014 * Unassign roles from users
1016 * @param array $unassignments An array of unassignment
1017 * @since Moodle 2.0
1018 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1019 * @see core_role_external::unassign_roles()
1021 public static function role_unassign($unassignments) {
1022 return core_role_external::unassign_roles($unassignments);
1026 * Returns description of method result value
1028 * @return null
1029 * @since Moodle 2.0
1030 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1031 * @see core_role_external::unassign_roles_returns()
1033 public static function role_unassign_returns() {
1034 return core_role_external::unassign_roles_returns();