MDL-40589 - Theme/CLEAN - Flip YUI3 tree item icon in RTL mode in folder resource
[moodle.git] / enrol / externallib.php
blobf7f08046533ac5581c4eb618bd91da739397d2bc
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('x') FROM ($enrolledsqlselect) 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 = context_system::instance();
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_helper::preload_from_record($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', VALUE_OPTIONAL),
590 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to assign the user role in
591 (block, course, coursecat, system, user, module)', VALUE_OPTIONAL),
592 'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be assigned', VALUE_OPTIONAL),
601 * Manual role assignments to users
603 * @param array $assignments An array of manual role assignment
605 public static function assign_roles($assignments) {
606 global $DB;
608 // Do basic automatic PARAM checks on incoming data, using params description
609 // If any problems are found then exceptions are thrown with helpful error messages
610 $params = self::validate_parameters(self::assign_roles_parameters(), array('assignments'=>$assignments));
612 $transaction = $DB->start_delegated_transaction();
614 foreach ($params['assignments'] as $assignment) {
615 // Ensure correct context level with a instance id or contextid is passed.
616 $context = self::get_context_from_params($assignment);
618 // Ensure the current user is allowed to run this function in the enrolment context.
619 self::validate_context($context);
620 require_capability('moodle/role:assign', $context);
622 // throw an exception if user is not able to assign the role in this context
623 $roles = get_assignable_roles($context, ROLENAME_SHORT);
625 if (!array_key_exists($assignment['roleid'], $roles)) {
626 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
629 role_assign($assignment['roleid'], $assignment['userid'], $context->id);
632 $transaction->allow_commit();
636 * Returns description of method result value
638 * @return null
640 public static function assign_roles_returns() {
641 return null;
646 * Returns description of method parameters
648 * @return external_function_parameters
650 public static function unassign_roles_parameters() {
651 return new external_function_parameters(
652 array(
653 'unassignments' => new external_multiple_structure(
654 new external_single_structure(
655 array(
656 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
657 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
658 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from', VALUE_OPTIONAL),
659 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level to unassign the user role in
660 + (block, course, coursecat, system, user, module)', VALUE_OPTIONAL),
661 'instanceid' => new external_value(PARAM_INT, 'The Instance id of item where the role needs to be unassigned', VALUE_OPTIONAL),
670 * Unassign roles from users
672 * @param array $unassignments An array of unassignment
674 public static function unassign_roles($unassignments) {
675 global $DB;
677 // Do basic automatic PARAM checks on incoming data, using params description
678 // If any problems are found then exceptions are thrown with helpful error messages
679 $params = self::validate_parameters(self::unassign_roles_parameters(), array('unassignments'=>$unassignments));
681 $transaction = $DB->start_delegated_transaction();
683 foreach ($params['unassignments'] as $unassignment) {
684 // Ensure the current user is allowed to run this function in the unassignment context
685 $context = self::get_context_from_params($unassignment);
686 self::validate_context($context);
687 require_capability('moodle/role:assign', $context);
689 // throw an exception if user is not able to unassign the role in this context
690 $roles = get_assignable_roles($context, ROLENAME_SHORT);
691 if (!array_key_exists($unassignment['roleid'], $roles)) {
692 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
695 role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id);
698 $transaction->allow_commit();
702 * Returns description of method result value
704 * @return null
706 public static function unassign_roles_returns() {
707 return null;
713 * Deprecated enrol and role external functions
715 * @package core_enrol
716 * @copyright 2010 Jerome Mouneyrac
717 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
718 * @since Moodle 2.0
719 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
720 * @see core_enrol_external
721 * @see core_role_external
723 class moodle_enrol_external extends external_api {
727 * Returns description of method parameters
729 * @return external_function_parameters
730 * @since Moodle 2.0
731 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
732 * @see core_enrol_external::get_enrolled_users_parameters()
734 public static function get_enrolled_users_parameters() {
735 return new external_function_parameters(
736 array(
737 'courseid' => new external_value(PARAM_INT, 'Course id'),
738 'withcapability' => new external_value(PARAM_CAPABILITY, 'User should have this capability', VALUE_DEFAULT, null),
739 'groupid' => new external_value(PARAM_INT, 'Group id, null means all groups', VALUE_DEFAULT, null),
740 'onlyactive' => new external_value(PARAM_INT, 'True means only active, false means all participants', VALUE_DEFAULT, 0),
746 * Get list of course participants.
748 * @param int $courseid
749 * @param text $withcapability
750 * @param int $groupid
751 * @param bool $onlyactive
752 * @return array of course participants
753 * @since Moodle 2.0
754 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
755 * @see core_enrol_external::get_enrolled_users()
757 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) {
758 global $DB, $CFG, $USER;
760 // Do basic automatic PARAM checks on incoming data, using params description
761 // If any problems are found then exceptions are thrown with helpful error messages
762 $params = self::validate_parameters(self::get_enrolled_users_parameters(), array(
763 'courseid'=>$courseid,
764 'withcapability'=>$withcapability,
765 'groupid'=>$groupid,
766 'onlyactive'=>$onlyactive)
769 $coursecontext = context_course::instance($params['courseid'], IGNORE_MISSING);
770 if ($courseid == SITEID) {
771 $context = context_system::instance();
772 } else {
773 $context = $coursecontext;
776 try {
777 self::validate_context($context);
778 } catch (Exception $e) {
779 $exceptionparam = new stdClass();
780 $exceptionparam->message = $e->getMessage();
781 $exceptionparam->courseid = $params['courseid'];
782 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
785 if ($courseid == SITEID) {
786 require_capability('moodle/site:viewparticipants', $context);
787 } else {
788 require_capability('moodle/course:viewparticipants', $context);
791 if ($withcapability) {
792 require_capability('moodle/role:review', $coursecontext);
794 if ($groupid && groups_is_member($groupid)) {
795 require_capability('moodle/site:accessallgroups', $coursecontext);
797 if ($onlyactive) {
798 require_capability('moodle/course:enrolreview', $coursecontext);
801 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
802 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid
803 FROM {user_enrolments} ue
804 JOIN {enrol} e ON (e.id = ue.enrolid)
805 JOIN {user} u ON (ue.userid = u.id)
806 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER . ")
807 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams)
808 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id";
809 $params['courseid'] = $courseid;
810 $enrolledusers = $DB->get_records_sql($sql, $params);
811 $result = array();
812 $isadmin = is_siteadmin($USER);
813 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
814 foreach ($enrolledusers as $enrolleduser) {
815 $profilimgurl = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f1');
816 $profilimgurlsmall = moodle_url::make_pluginfile_url($enrolleduser->usercontextid, 'user', 'icon', NULL, '/', 'f2');
817 $resultuser = array(
818 'courseid' => $enrolleduser->courseid,
819 'userid' => $enrolleduser->userid,
820 'fullname' => fullname($enrolleduser),
821 'profileimgurl' => $profilimgurl->out(false),
822 'profileimgurlsmall' => $profilimgurlsmall->out(false)
824 // check if we can return username
825 if ($isadmin) {
826 $resultuser['username'] = $enrolleduser->username;
828 // check if we can return first and last name
829 if ($isadmin or $canviewfullnames) {
830 $resultuser['firstname'] = $enrolleduser->firstname;
831 $resultuser['lastname'] = $enrolleduser->lastname;
833 $result[] = $resultuser;
836 return $result;
840 * Returns description of method result value
842 * @return external_description
843 * @since Moodle 2.0
844 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
845 * @see core_enrol_external::get_enrolled_users_returns()
847 public static function get_enrolled_users_returns() {
848 return new external_multiple_structure(
849 new external_single_structure(
850 array(
851 'courseid' => new external_value(PARAM_INT, 'id of course'),
852 'userid' => new external_value(PARAM_INT, 'id of user'),
853 'firstname' => new external_value(PARAM_RAW, 'first name of user', VALUE_OPTIONAL),
854 'lastname' => new external_value(PARAM_RAW, 'last name of user', VALUE_OPTIONAL),
855 'fullname' => new external_value(PARAM_RAW, 'fullname of user'),
856 'username' => new external_value(PARAM_RAW, 'username of user', VALUE_OPTIONAL),
857 'profileimgurl' => new external_value(PARAM_URL, 'url of the profile image'),
858 'profileimgurlsmall' => new external_value(PARAM_URL, 'url of the profile image (small version)')
865 * Returns description of method parameters
867 * @return external_function_parameters
868 * @since Moodle 2.1
869 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
870 * @see core_enrol_external::get_users_courses_parameters()
872 public static function get_users_courses_parameters() {
873 return core_enrol_external::get_users_courses_parameters();
877 * Get list of courses user is enrolled in (only active enrolments are returned).
878 * Please note the current user must be able to access the course, otherwise the course is not included.
880 * @param int $userid
881 * @return array of courses
882 * @since Moodle 2.1
883 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
884 * @see use core_enrol_external::get_users_courses()
886 public static function get_users_courses($userid) {
887 return core_enrol_external::get_users_courses($userid);
891 * Returns description of method result value
893 * @return external_description
894 * @since Moodle 2.1
895 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
896 * @see core_enrol_external::get_users_courses_returns()
898 public static function get_users_courses_returns() {
899 return core_enrol_external::get_users_courses_returns();
904 * Returns description of method parameters
906 * @return external_function_parameters
907 * @since Moodle 2.0
908 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
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 * @see core_role_external::assign_roles()
923 public static function role_assign($assignments) {
924 return core_role_external::assign_roles($assignments);
928 * Returns description of method result value
930 * @return null
931 * @since Moodle 2.0
932 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
933 * @see core_role_external::assign_roles_returns()
935 public static function role_assign_returns() {
936 return core_role_external::assign_roles_returns();
941 * Returns description of method parameters
943 * @return external_function_parameters
944 * @since Moodle 2.0
945 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
946 * @see core_role_external::unassign_roles_parameters()
948 public static function role_unassign_parameters() {
949 return core_role_external::unassign_roles_parameters();
953 * Unassign roles from users
955 * @param array $unassignments An array of unassignment
956 * @since Moodle 2.0
957 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
958 * @see core_role_external::unassign_roles()
960 public static function role_unassign($unassignments) {
961 return core_role_external::unassign_roles($unassignments);
965 * Returns description of method result value
967 * @return null
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::unassign_roles_returns()
972 public static function role_unassign_returns() {
973 return core_role_external::unassign_roles_returns();