Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / enrol / externallib.php
blob0a93c9020a07ff6febd8c1a68d5a6804d1d2801a
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 case 'limitfrom' :
117 $limitfrom = clean_param($option['value'], PARAM_INT);
118 break;
119 case 'limitnumber' :
120 $limitnumber = clean_param($option['value'], PARAM_INT);
121 break;
125 foreach ($params['coursecapabilities'] as $coursecapability) {
126 $courseid = $coursecapability['courseid'];
127 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
128 $coursecontext = context_course::instance($courseid);
129 if (!$coursecontext) {
130 throw new moodle_exception('cannotfindcourse', 'error', '', null,
131 'The course id ' . $courseid . ' doesn\'t exist.');
133 if ($courseid == SITEID) {
134 $context = context_system::instance();
135 } else {
136 $context = $coursecontext;
138 try {
139 self::validate_context($context);
140 } catch (Exception $e) {
141 $exceptionparam = new stdClass();
142 $exceptionparam->message = $e->getMessage();
143 $exceptionparam->courseid = $params['courseid'];
144 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
147 if ($courseid == SITEID) {
148 require_capability('moodle/site:viewparticipants', $context);
149 } else {
150 require_capability('moodle/course:viewparticipants', $context);
152 // The accessallgroups capability is needed to use this option.
153 if (!empty($groupid) && groups_is_member($groupid)) {
154 require_capability('moodle/site:accessallgroups', $coursecontext);
156 // The course:enrolereview capability is needed to use this option.
157 if ($onlyactive) {
158 require_capability('moodle/course:enrolreview', $coursecontext);
161 // To see the permissions of others role:review capability is required.
162 require_capability('moodle/role:review', $coursecontext);
163 foreach ($coursecapability['capabilities'] as $capability) {
164 $courseusers['courseid'] = $courseid;
165 $courseusers['capability'] = $capability;
167 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $capability, $groupid, $onlyactive);
169 $sql = "SELECT u.* FROM {user} u WHERE u.id IN ($enrolledsql) ORDER BY u.id ASC";
171 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
172 $users = array();
173 foreach ($enrolledusers as $courseuser) {
174 if ($userdetails = user_get_user_details($courseuser, $course, $userfields)) {
175 $users[] = $userdetails;
178 $enrolledusers->close();
179 $courseusers['users'] = $users;
180 $result[] = $courseusers;
183 return $result;
187 * Returns description of method result value
189 * @return external_multiple_structure
190 * @since Moodle 2.4
192 public static function get_enrolled_users_with_capability_returns() {
193 return new external_multiple_structure( new external_single_structure (
194 array (
195 'courseid' => new external_value(PARAM_INT, 'Course ID number in the Moodle course table'),
196 'capability' => new external_value(PARAM_CAPABILITY, 'Capability name'),
197 'users' => new external_multiple_structure(
198 new external_single_structure(
199 array(
200 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
201 'username' => new external_value(PARAM_RAW, 'Username', VALUE_OPTIONAL),
202 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
203 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
204 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
205 'email' => new external_value(PARAM_TEXT, 'Email address', VALUE_OPTIONAL),
206 'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL),
207 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
208 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
209 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
210 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
211 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
212 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
213 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
214 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
215 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
216 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
217 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
218 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
219 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
220 'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL),
221 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
222 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
223 'country' => new external_value(PARAM_ALPHA, 'Country code of the user, such as AU or CZ', VALUE_OPTIONAL),
224 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small', VALUE_OPTIONAL),
225 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big', VALUE_OPTIONAL),
226 'customfields' => new external_multiple_structure(
227 new external_single_structure(
228 array(
229 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field'),
230 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
231 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
232 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field'),
234 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
235 'groups' => new external_multiple_structure(
236 new external_single_structure(
237 array(
238 'id' => new external_value(PARAM_INT, 'group id'),
239 'name' => new external_value(PARAM_RAW, 'group name'),
240 'description' => new external_value(PARAM_RAW, 'group description'),
242 ), 'user groups', VALUE_OPTIONAL),
243 'roles' => new external_multiple_structure(
244 new external_single_structure(
245 array(
246 'roleid' => new external_value(PARAM_INT, 'role id'),
247 'name' => new external_value(PARAM_RAW, 'role name'),
248 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
249 'sortorder' => new external_value(PARAM_INT, 'role sortorder')
251 ), 'user roles', VALUE_OPTIONAL),
252 'preferences' => new external_multiple_structure(
253 new external_single_structure(
254 array(
255 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
256 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
258 ), 'User preferences', VALUE_OPTIONAL),
259 'enrolledcourses' => new external_multiple_structure(
260 new external_single_structure(
261 array(
262 'id' => new external_value(PARAM_INT, 'Id of the course'),
263 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
264 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
266 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
268 ), 'List of users that are enrolled in the course and have the specified capability'),
275 * Returns description of method parameters
277 * @return external_function_parameters
279 public static function get_users_courses_parameters() {
280 return new external_function_parameters(
281 array(
282 'userid' => new external_value(PARAM_INT, 'user id'),
288 * Get list of courses user is enrolled in (only active enrolments are returned).
289 * Please note the current user must be able to access the course, otherwise the course is not included.
291 * @param int $userid
292 * @return array of courses
294 public static function get_users_courses($userid) {
295 global $USER, $DB;
297 // Do basic automatic PARAM checks on incoming data, using params description
298 // If any problems are found then exceptions are thrown with helpful error messages
299 $params = self::validate_parameters(self::get_users_courses_parameters(), array('userid'=>$userid));
301 $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible');
302 $result = array();
304 foreach ($courses as $course) {
305 $context = context_course::instance($course->id, IGNORE_MISSING);
306 try {
307 self::validate_context($context);
308 } catch (Exception $e) {
309 // current user can not access this course, sorry we can not disclose who is enrolled in this course!
310 continue;
313 if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) {
314 // we need capability to view participants
315 continue;
318 list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context);
319 $enrolledsql = "SELECT COUNT(*) FROM ($enrolledsqlselect) AS enrolleduserids";
320 $enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams);
322 $result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible, 'enrolledusercount'=>$enrolledusercount);
325 return $result;
329 * Returns description of method result value
331 * @return external_description
333 public static function get_users_courses_returns() {
334 return new external_multiple_structure(
335 new external_single_structure(
336 array(
337 'id' => new external_value(PARAM_INT, 'id of course'),
338 'shortname' => new external_value(PARAM_RAW, 'short name of course'),
339 'fullname' => new external_value(PARAM_RAW, 'long name of course'),
340 'enrolledusercount' => new external_value(PARAM_INT, 'Number of enrolled users in this course'),
341 'idnumber' => new external_value(PARAM_RAW, 'id number of course'),
342 'visible' => new external_value(PARAM_INT, '1 means visible, 0 means hidden course'),
349 * Returns description of method parameters
351 * @return external_function_parameters
353 public static function get_enrolled_users_parameters() {
354 return new external_function_parameters(
355 array(
356 'courseid' => new external_value(PARAM_INT, 'course id'),
357 'options' => new external_multiple_structure(
358 new external_single_structure(
359 array(
360 'name' => new external_value(PARAM_ALPHANUMEXT, 'option name'),
361 'value' => new external_value(PARAM_RAW, 'option value')
363 ), 'Option names:
364 * withcapability (string) return only users with this capability. This option requires \'moodle/role:review\' on the course context.
365 * groupid (integer) return only users in this group id. This option requires \'moodle/site:accessallgroups\' on the course context.
366 * onlyactive (integer) return only users with active enrolments and matching time restrictions. This option requires \'moodle/course:enrolreview\' on the course context.
367 * userfields (\'string, string, ...\') return only the values of these user fields.
368 * limitfrom (integer) sql limit from.
369 * limitnumber (integer) maximum number of returned users.', VALUE_DEFAULT, array()),
375 * Get course participants details
377 * @param int $courseid course id
378 * @param array $options options {
379 * 'name' => option name
380 * 'value' => option value
382 * @return array An array of users
384 public static function get_enrolled_users($courseid, $options = array()) {
385 global $CFG, $USER, $DB;
386 require_once($CFG->dirroot . "/user/lib.php");
388 $params = self::validate_parameters(
389 self::get_enrolled_users_parameters(),
390 array(
391 'courseid'=>$courseid,
392 'options'=>$options
395 $withcapability = '';
396 $groupid = 0;
397 $onlyactive = false;
398 $userfields = array();
399 $limitfrom = 0;
400 $limitnumber = 0;
401 foreach ($options as $option) {
402 switch ($option['name']) {
403 case 'withcapability':
404 $withcapability = $option['value'];
405 break;
406 case 'groupid':
407 $groupid = (int)$option['value'];
408 break;
409 case 'onlyactive':
410 $onlyactive = !empty($option['value']);
411 break;
412 case 'userfields':
413 $thefields = explode(',', $option['value']);
414 foreach ($thefields as $f) {
415 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT);
417 case 'limitfrom' :
418 $limitfrom = clean_param($option['value'], PARAM_INT);
419 break;
420 case 'limitnumber' :
421 $limitnumber = clean_param($option['value'], PARAM_INT);
422 break;
426 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
427 $coursecontext = context_course::instance($courseid, IGNORE_MISSING);
428 if ($courseid == SITEID) {
429 $context = get_system_context();
430 } else {
431 $context = $coursecontext;
433 try {
434 self::validate_context($context);
435 } catch (Exception $e) {
436 $exceptionparam = new stdClass();
437 $exceptionparam->message = $e->getMessage();
438 $exceptionparam->courseid = $params['courseid'];
439 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
442 if ($courseid == SITEID) {
443 require_capability('moodle/site:viewparticipants', $context);
444 } else {
445 require_capability('moodle/course:viewparticipants', $context);
447 // to overwrite this parameter, you need role:review capability
448 if ($withcapability) {
449 require_capability('moodle/role:review', $coursecontext);
451 // need accessallgroups capability if you want to overwrite this option
452 if (!empty($groupid) && groups_is_member($groupid)) {
453 require_capability('moodle/site:accessallgroups', $coursecontext);
455 // to overwrite this option, you need course:enrolereview permission
456 if ($onlyactive) {
457 require_capability('moodle/course:enrolreview', $coursecontext);
460 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
461 list($ctxselect, $ctxjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
462 $sqlparams['courseid'] = $courseid;
463 $sql = "SELECT u.* $ctxselect
464 FROM {user} u $ctxjoin
465 WHERE u.id IN ($enrolledsql)
466 ORDER BY u.id ASC";
467 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
468 $users = array();
469 foreach ($enrolledusers as $user) {
470 context_instance_preload($user);
471 if ($userdetails = user_get_user_details($user, $course, $userfields)) {
472 $users[] = $userdetails;
475 $enrolledusers->close();
477 return $users;
481 * Returns description of method result value
483 * @return external_description
485 public static function get_enrolled_users_returns() {
486 return new external_multiple_structure(
487 new external_single_structure(
488 array(
489 'id' => new external_value(PARAM_INT, 'ID of the user'),
490 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL),
491 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
492 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
493 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
494 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
495 'address' => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL),
496 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
497 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
498 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
499 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
500 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
501 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
502 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
503 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
504 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
505 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
506 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
507 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
508 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
509 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
510 'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
511 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
512 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
513 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
514 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version', VALUE_OPTIONAL),
515 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version', VALUE_OPTIONAL),
516 'customfields' => new external_multiple_structure(
517 new external_single_structure(
518 array(
519 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
520 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
521 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
522 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
524 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
525 'groups' => new external_multiple_structure(
526 new external_single_structure(
527 array(
528 'id' => new external_value(PARAM_INT, 'group id'),
529 'name' => new external_value(PARAM_RAW, 'group name'),
530 'description' => new external_value(PARAM_RAW, 'group description'),
531 'descriptionformat' => new external_format_value('description'),
533 ), 'user groups', VALUE_OPTIONAL),
534 'roles' => new external_multiple_structure(
535 new external_single_structure(
536 array(
537 'roleid' => new external_value(PARAM_INT, 'role id'),
538 'name' => new external_value(PARAM_RAW, 'role name'),
539 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
540 'sortorder' => new external_value(PARAM_INT, 'role sortorder')
542 ), 'user roles', VALUE_OPTIONAL),
543 'preferences' => new external_multiple_structure(
544 new external_single_structure(
545 array(
546 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
547 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
549 ), 'User preferences', VALUE_OPTIONAL),
550 'enrolledcourses' => new external_multiple_structure(
551 new external_single_structure(
552 array(
553 'id' => new external_value(PARAM_INT, 'Id of the course'),
554 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
555 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
557 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
566 * Role external functions
568 * @package core_role
569 * @category external
570 * @copyright 2011 Jerome Mouneyrac
571 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
572 * @since Moodle 2.2
574 class core_role_external extends external_api {
577 * Returns description of method parameters
579 * @return external_function_parameters
581 public static function assign_roles_parameters() {
582 return new external_function_parameters(
583 array(
584 'assignments' => new external_multiple_structure(
585 new external_single_structure(
586 array(
587 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
588 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
589 'contextid' => new external_value(PARAM_INT, 'The context to assign the user role in'),
598 * Manual role assignments to users
600 * @param array $assignments An array of manual role assignment
602 public static function assign_roles($assignments) {
603 global $DB;
605 // Do basic automatic PARAM checks on incoming data, using params description
606 // If any problems are found then exceptions are thrown with helpful error messages
607 $params = self::validate_parameters(self::assign_roles_parameters(), array('assignments'=>$assignments));
609 $transaction = $DB->start_delegated_transaction();
611 foreach ($params['assignments'] as $assignment) {
612 // Ensure the current user is allowed to run this function in the enrolment context
613 $context = context::instance_by_id($assignment['contextid'], IGNORE_MISSING);
614 self::validate_context($context);
615 require_capability('moodle/role:assign', $context);
617 // throw an exception if user is not able to assign the role in this context
618 $roles = get_assignable_roles($context, ROLENAME_SHORT);
620 if (!array_key_exists($assignment['roleid'], $roles)) {
621 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
624 role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);
627 $transaction->allow_commit();
631 * Returns description of method result value
633 * @return null
635 public static function assign_roles_returns() {
636 return null;
641 * Returns description of method parameters
643 * @return external_function_parameters
645 public static function unassign_roles_parameters() {
646 return new external_function_parameters(
647 array(
648 'unassignments' => new external_multiple_structure(
649 new external_single_structure(
650 array(
651 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
652 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
653 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from'),
662 * Unassign roles from users
664 * @param array $unassignments An array of unassignment
666 public static function unassign_roles($unassignments) {
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::unassign_roles_parameters(), array('unassignments'=>$unassignments));
673 $transaction = $DB->start_delegated_transaction();
675 foreach ($params['unassignments'] as $unassignment) {
676 // Ensure the current user is allowed to run this function in the unassignment context
677 $context = context::instance_by_id($unassignment['contextid'], IGNORE_MISSING);
678 self::validate_context($context);
679 require_capability('moodle/role:assign', $context);
681 // throw an exception if user is not able to unassign the role in this context
682 $roles = get_assignable_roles($context, ROLENAME_SHORT);
683 if (!array_key_exists($unassignment['roleid'], $roles)) {
684 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
687 role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']);
690 $transaction->allow_commit();
694 * Returns description of method result value
696 * @return null
698 public static function unassign_roles_returns() {
699 return null;
705 * Deprecated enrol and role external functions
707 * @package core_enrol
708 * @copyright 2010 Jerome Mouneyrac
709 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
710 * @since Moodle 2.0
711 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
712 * @todo MDL-31194 This will be deleted in Moodle 2.5.
713 * @see core_enrol_external
714 * @see core_role_external
716 class moodle_enrol_external extends external_api {
720 * Returns description of method parameters
722 * @return external_function_parameters
723 * @since Moodle 2.0
724 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
725 * @todo MDL-31194 This will be deleted in Moodle 2.5.
726 * @see core_enrol_external::get_enrolled_users_parameters()
728 public static function get_enrolled_users_parameters() {
729 return new external_function_parameters(
730 array(
731 'courseid' => new external_value(PARAM_INT, 'Course id'),
732 'withcapability' => new external_value(PARAM_CAPABILITY, 'User should have this capability', VALUE_DEFAULT, null),
733 'groupid' => new external_value(PARAM_INT, 'Group id, null means all groups', VALUE_DEFAULT, null),
734 'onlyactive' => new external_value(PARAM_INT, 'True means only active, false means all participants', VALUE_DEFAULT, 0),
740 * Get list of course participants.
742 * @param int $courseid
743 * @param text $withcapability
744 * @param int $groupid
745 * @param bool $onlyactive
746 * @return array of course participants
747 * @since Moodle 2.0
748 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
749 * @todo MDL-31194 This will be deleted in Moodle 2.5.
750 * @see core_enrol_external::get_enrolled_users()
752 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) {
753 global $DB, $CFG, $USER;
755 // Do basic automatic PARAM checks on incoming data, using params description
756 // If any problems are found then exceptions are thrown with helpful error messages
757 $params = self::validate_parameters(self::get_enrolled_users_parameters(), array(
758 'courseid'=>$courseid,
759 'withcapability'=>$withcapability,
760 'groupid'=>$groupid,
761 'onlyactive'=>$onlyactive)
764 $coursecontext = context_course::instance($params['courseid'], IGNORE_MISSING);
765 if ($courseid == SITEID) {
766 $context = context_system::instance();
767 } else {
768 $context = $coursecontext;
771 try {
772 self::validate_context($context);
773 } catch (Exception $e) {
774 $exceptionparam = new stdClass();
775 $exceptionparam->message = $e->getMessage();
776 $exceptionparam->courseid = $params['courseid'];
777 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
780 if ($courseid == SITEID) {
781 require_capability('moodle/site:viewparticipants', $context);
782 } else {
783 require_capability('moodle/course:viewparticipants', $context);
786 if ($withcapability) {
787 require_capability('moodle/role:review', $coursecontext);
789 if ($groupid && groups_is_member($groupid)) {
790 require_capability('moodle/site:accessallgroups', $coursecontext);
792 if ($onlyactive) {
793 require_capability('moodle/course:enrolreview', $coursecontext);
796 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
797 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid
798 FROM {user_enrolments} ue
799 JOIN {enrol} e ON (e.id = ue.enrolid)
800 JOIN {user} u ON (ue.userid = u.id)
801 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER . ")
802 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams)
803 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id";
804 $params['courseid'] = $courseid;
805 $enrolledusers = $DB->get_records_sql($sql, $params);
806 $result = array();
807 $isadmin = is_siteadmin($USER);
808 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
809 foreach ($enrolledusers as $enrolleduser) {
810 $profilimgurl = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f1');
811 $profilimgurlsmall = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f2');
812 $resultuser = array(
813 'courseid' => $enrolleduser->courseid,
814 'userid' => $enrolleduser->userid,
815 'fullname' => fullname($enrolleduser),
816 'profileimgurl' => $profilimgurl->out(false),
817 'profileimgurlsmall' => $profilimgurlsmall->out(false)
819 // check if we can return username
820 if ($isadmin) {
821 $resultuser['username'] = $enrolleduser->username;
823 // check if we can return first and last name
824 if ($isadmin or $canviewfullnames) {
825 $resultuser['firstname'] = $enrolleduser->firstname;
826 $resultuser['lastname'] = $enrolleduser->lastname;
828 $result[] = $resultuser;
831 return $result;
835 * Returns description of method result value
837 * @return external_description
838 * @since Moodle 2.0
839 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
840 * @todo MDL-31194 This will be deleted in Moodle 2.5.
841 * @see core_enrol_external::get_enrolled_users_returns()
843 public static function get_enrolled_users_returns() {
844 return new external_multiple_structure(
845 new external_single_structure(
846 array(
847 'courseid' => new external_value(PARAM_INT, 'id of course'),
848 'userid' => new external_value(PARAM_INT, 'id of user'),
849 'firstname' => new external_value(PARAM_RAW, 'first name of user', VALUE_OPTIONAL),
850 'lastname' => new external_value(PARAM_RAW, 'last name of user', VALUE_OPTIONAL),
851 'fullname' => new external_value(PARAM_RAW, 'fullname of user'),
852 'username' => new external_value(PARAM_RAW, 'username of user', VALUE_OPTIONAL),
853 'profileimgurl' => new external_value(PARAM_URL, 'url of the profile image'),
854 'profileimgurlsmall' => new external_value(PARAM_URL, 'url of the profile image (small version)')
861 * Returns description of method parameters
863 * @return external_function_parameters
864 * @since Moodle 2.1
865 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
866 * @todo MDL-31194 This will be deleted in Moodle 2.5.
867 * @see core_enrol_external::get_users_courses_parameters()
869 public static function get_users_courses_parameters() {
870 return core_enrol_external::get_users_courses_parameters();
874 * Get list of courses user is enrolled in (only active enrolments are returned).
875 * Please note the current user must be able to access the course, otherwise the course is not included.
877 * @param int $userid
878 * @return array of courses
879 * @since Moodle 2.1
880 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
881 * @todo MDL-31194 This will be deleted in Moodle 2.5.
882 * @see use core_enrol_external::get_users_courses()
884 public static function get_users_courses($userid) {
885 return core_enrol_external::get_users_courses($userid);
889 * Returns description of method result value
891 * @return external_description
892 * @since Moodle 2.1
893 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
894 * @todo MDL-31194 This will be deleted in Moodle 2.5.
895 * @see core_enrol_external::get_users_courses_returns()
897 public static function get_users_courses_returns() {
898 return core_enrol_external::get_users_courses_returns();
903 * Returns description of method parameters
905 * @return external_function_parameters
906 * @since Moodle 2.0
907 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
908 * @todo MDL-31194 This will be deleted in Moodle 2.5.
909 * @see core_role_external::assign_roles_parameters()
911 public static function role_assign_parameters() {
912 return core_role_external::assign_roles_parameters();
916 * Manual role assignments to users
918 * @param array $assignments An array of manual role assignment
919 * @since Moodle 2.0
920 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
921 * @todo MDL-31194 This will be deleted in Moodle 2.5.
922 * @see core_role_external::assign_roles()
924 public static function role_assign($assignments) {
925 return core_role_external::assign_roles($assignments);
929 * Returns description of method result value
931 * @return null
932 * @since Moodle 2.0
933 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
934 * @todo MDL-31194 This will be deleted in Moodle 2.5.
935 * @see core_role_external::assign_roles_returns()
937 public static function role_assign_returns() {
938 return core_role_external::assign_roles_returns();
943 * Returns description of method parameters
945 * @return external_function_parameters
946 * @since Moodle 2.0
947 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
948 * @todo MDL-31194 This will be deleted in Moodle 2.5.
949 * @see core_role_external::unassign_roles_parameters()
951 public static function role_unassign_parameters() {
952 return core_role_external::unassign_roles_parameters();
956 * Unassign roles from users
958 * @param array $unassignments An array of unassignment
959 * @since Moodle 2.0
960 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
961 * @todo MDL-31194 This will be deleted in Moodle 2.5.
962 * @see core_role_external::unassign_roles()
964 public static function role_unassign($unassignments) {
965 return core_role_external::unassign_roles($unassignments);
969 * Returns description of method result value
971 * @return null
972 * @since Moodle 2.0
973 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
974 * @todo MDL-31194 This will be deleted in Moodle 2.5.
975 * @see core_role_external::unassign_roles_returns()
977 public static function role_unassign_returns() {
978 return core_role_external::unassign_roles_returns();