2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * External course participation api.
21 * This api is mostly read only, the actual enrol and unenrol
22 * support is in each enrol plugin.
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");
35 * Enrol external functions
39 * @copyright 2011 Jerome Mouneyrac
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class core_enrol_external
extends external_api
{
46 * Returns description of method parameters
48 * @return external_function_parameters
51 public static function get_enrolled_users_with_capability_parameters() {
52 return new external_function_parameters(
54 'coursecapabilities' => new external_multiple_structure(
55 new external_single_structure(
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(
66 'name' => new external_value(PARAM_ALPHANUMEXT
, 'option name'),
67 'value' => new external_value(PARAM_RAW
, 'option value')
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())
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
87 public static function get_enrolled_users_with_capability($coursecapabilities, $options) {
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));
100 $userfields = array();
103 foreach ($params['options'] as $option) {
104 switch ($option['name']) {
106 $groupid = (int)$option['value'];
109 $onlyactive = !empty($option['value']);
112 $thefields = explode(',', $option['value']);
113 foreach ($thefields as $f) {
114 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT
);
117 $limitfrom = clean_param($option['value'], PARAM_INT
);
120 $limitnumber = clean_param($option['value'], PARAM_INT
);
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();
136 $context = $coursecontext;
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);
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.
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);
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;
187 * Returns description of method result value
189 * @return external_multiple_structure
192 public static function get_enrolled_users_with_capability_returns() {
193 return new external_multiple_structure( new external_single_structure (
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(
200 'id' => new external_value(PARAM_INT
, '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(
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(
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(
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(
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(
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(
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.
292 * @return array of courses
294 public static function get_users_courses($userid) {
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');
304 foreach ($courses as $course) {
305 $context = context_course
::instance($course->id
, IGNORE_MISSING
);
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!
313 if ($userid != $USER->id
and !has_capability('moodle/course:viewparticipants', $context)) {
314 // we need capability to view participants
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);
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(
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(
356 'courseid' => new external_value(PARAM_INT
, 'course id'),
357 'options' => new external_multiple_structure(
358 new external_single_structure(
360 'name' => new external_value(PARAM_ALPHANUMEXT
, 'option name'),
361 'value' => new external_value(PARAM_RAW
, 'option value')
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(),
391 'courseid'=>$courseid,
395 $withcapability = '';
398 $userfields = array();
401 foreach ($options as $option) {
402 switch ($option['name']) {
403 case 'withcapability':
404 $withcapability = $option['value'];
407 $groupid = (int)$option['value'];
410 $onlyactive = !empty($option['value']);
413 $thefields = explode(',', $option['value']);
414 foreach ($thefields as $f) {
415 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT
);
418 $limitfrom = clean_param($option['value'], PARAM_INT
);
421 $limitnumber = clean_param($option['value'], PARAM_INT
);
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();
431 $context = $coursecontext;
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);
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
457 require_capability('moodle/course:enrolreview', $coursecontext);
460 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
461 $ctxselect = ', ' . context_helper
::get_preload_record_columns_sql('ctx');
462 $ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
463 $enrolledparams['contextlevel'] = CONTEXT_USER
;
464 $sql = "SELECT u.* $ctxselect
465 FROM {user} u $ctxjoin
466 WHERE u.id IN ($enrolledsql)
468 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
470 foreach ($enrolledusers as $user) {
471 context_helper
::preload_from_record($user);
472 if ($userdetails = user_get_user_details($user, $course, $userfields)) {
473 $users[] = $userdetails;
476 $enrolledusers->close();
482 * Returns description of method result value
484 * @return external_description
486 public static function get_enrolled_users_returns() {
487 return new external_multiple_structure(
488 new external_single_structure(
490 'id' => new external_value(PARAM_INT
, 'ID of the user'),
491 'username' => new external_value(PARAM_RAW
, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL
),
492 'firstname' => new external_value(PARAM_NOTAGS
, 'The first name(s) of the user', VALUE_OPTIONAL
),
493 'lastname' => new external_value(PARAM_NOTAGS
, 'The family name of the user', VALUE_OPTIONAL
),
494 'fullname' => new external_value(PARAM_NOTAGS
, 'The fullname of the user'),
495 'email' => new external_value(PARAM_TEXT
, 'An email address - allow email as root@localhost', VALUE_OPTIONAL
),
496 'address' => new external_value(PARAM_TEXT
, 'Postal address', VALUE_OPTIONAL
),
497 'phone1' => new external_value(PARAM_NOTAGS
, 'Phone 1', VALUE_OPTIONAL
),
498 'phone2' => new external_value(PARAM_NOTAGS
, 'Phone 2', VALUE_OPTIONAL
),
499 'icq' => new external_value(PARAM_NOTAGS
, 'icq number', VALUE_OPTIONAL
),
500 'skype' => new external_value(PARAM_NOTAGS
, 'skype id', VALUE_OPTIONAL
),
501 'yahoo' => new external_value(PARAM_NOTAGS
, 'yahoo id', VALUE_OPTIONAL
),
502 'aim' => new external_value(PARAM_NOTAGS
, 'aim id', VALUE_OPTIONAL
),
503 'msn' => new external_value(PARAM_NOTAGS
, 'msn number', VALUE_OPTIONAL
),
504 'department' => new external_value(PARAM_TEXT
, 'department', VALUE_OPTIONAL
),
505 'institution' => new external_value(PARAM_TEXT
, 'institution', VALUE_OPTIONAL
),
506 'idnumber' => new external_value(PARAM_RAW
, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL
),
507 'interests' => new external_value(PARAM_TEXT
, 'user interests (separated by commas)', VALUE_OPTIONAL
),
508 'firstaccess' => new external_value(PARAM_INT
, 'first access to the site (0 if never)', VALUE_OPTIONAL
),
509 'lastaccess' => new external_value(PARAM_INT
, 'last access to the site (0 if never)', VALUE_OPTIONAL
),
510 'description' => new external_value(PARAM_RAW
, 'User profile description', VALUE_OPTIONAL
),
511 'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL
),
512 'city' => new external_value(PARAM_NOTAGS
, 'Home city of the user', VALUE_OPTIONAL
),
513 'url' => new external_value(PARAM_URL
, 'URL of the user', VALUE_OPTIONAL
),
514 'country' => new external_value(PARAM_ALPHA
, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
515 'profileimageurlsmall' => new external_value(PARAM_URL
, 'User image profile URL - small version', VALUE_OPTIONAL
),
516 'profileimageurl' => new external_value(PARAM_URL
, 'User image profile URL - big version', VALUE_OPTIONAL
),
517 'customfields' => new external_multiple_structure(
518 new external_single_structure(
520 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The type of the custom field - text field, checkbox...'),
521 'value' => new external_value(PARAM_RAW
, 'The value of the custom field'),
522 'name' => new external_value(PARAM_RAW
, 'The name of the custom field'),
523 'shortname' => new external_value(PARAM_RAW
, 'The shortname of the custom field - to be able to build the field class in the code'),
525 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL
),
526 'groups' => new external_multiple_structure(
527 new external_single_structure(
529 'id' => new external_value(PARAM_INT
, 'group id'),
530 'name' => new external_value(PARAM_RAW
, 'group name'),
531 'description' => new external_value(PARAM_RAW
, 'group description'),
532 'descriptionformat' => new external_format_value('description'),
534 ), 'user groups', VALUE_OPTIONAL
),
535 'roles' => new external_multiple_structure(
536 new external_single_structure(
538 'roleid' => new external_value(PARAM_INT
, 'role id'),
539 'name' => new external_value(PARAM_RAW
, 'role name'),
540 'shortname' => new external_value(PARAM_ALPHANUMEXT
, 'role shortname'),
541 'sortorder' => new external_value(PARAM_INT
, 'role sortorder')
543 ), 'user roles', VALUE_OPTIONAL
),
544 'preferences' => new external_multiple_structure(
545 new external_single_structure(
547 'name' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the preferences'),
548 'value' => new external_value(PARAM_RAW
, 'The value of the custom field'),
550 ), 'User preferences', VALUE_OPTIONAL
),
551 'enrolledcourses' => new external_multiple_structure(
552 new external_single_structure(
554 'id' => new external_value(PARAM_INT
, 'Id of the course'),
555 'fullname' => new external_value(PARAM_RAW
, 'Fullname of the course'),
556 'shortname' => new external_value(PARAM_RAW
, 'Shortname of the course')
558 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL
)
565 * Returns description of get_course_enrolment_methods() parameters
567 * @return external_function_parameters
569 public static function get_course_enrolment_methods_parameters() {
570 return new external_function_parameters(
572 'courseid' => new external_value(PARAM_INT
, 'Course id')
578 * Get list of active course enrolment methods for current user.
580 * @param int $courseid
581 * @return array of course enrolment methods
583 public static function get_course_enrolment_methods($courseid) {
585 $params = self
::validate_parameters(self
::get_course_enrolment_methods_parameters(), array('courseid' => $courseid));
587 $coursecontext = context_course
::instance($params['courseid']);
588 $categorycontext = $coursecontext->get_parent_context();
589 self
::validate_context($categorycontext);
592 $enrolinstances = enrol_get_instances($params['courseid'], true);
593 foreach ($enrolinstances as $enrolinstance) {
594 if ($enrolplugin = enrol_get_plugin($enrolinstance->enrol
)) {
595 if ($instanceinfo = $enrolplugin->get_enrol_info($enrolinstance)) {
596 $result[] = (array) $instanceinfo;
604 * Returns description of get_course_enrolment_methods() result value
606 * @return external_description
608 public static function get_course_enrolment_methods_returns() {
609 return new external_multiple_structure(
610 new external_single_structure(
612 'id' => new external_value(PARAM_INT
, 'id of course enrolment instance'),
613 'courseid' => new external_value(PARAM_INT
, 'id of course'),
614 'type' => new external_value(PARAM_PLUGIN
, 'type of enrolment plugin'),
615 'name' => new external_value(PARAM_RAW
, 'name of enrolment plugin'),
616 'status' => new external_value(PARAM_RAW
, 'status of enrolment plugin'),
617 'wsfunction' => new external_value(PARAM_ALPHANUMEXT
, 'webservice function to get more information', VALUE_OPTIONAL
),
625 * Role external functions
629 * @copyright 2011 Jerome Mouneyrac
630 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
633 class core_role_external
extends external_api
{
636 * Returns description of method parameters
638 * @return external_function_parameters
640 public static function assign_roles_parameters() {
641 return new external_function_parameters(
643 'assignments' => new external_multiple_structure(
644 new external_single_structure(
646 'roleid' => new external_value(PARAM_INT
, 'Role to assign to the user'),
647 'userid' => new external_value(PARAM_INT
, 'The user that is going to be assigned'),
648 'contextid' => new external_value(PARAM_INT
, 'The context to assign the user role in', VALUE_OPTIONAL
),
649 'contextlevel' => new external_value(PARAM_ALPHA
, 'The context level to assign the user role in
650 (block, course, coursecat, system, user, module)', VALUE_OPTIONAL
),
651 'instanceid' => new external_value(PARAM_INT
, 'The Instance id of item where the role needs to be assigned', VALUE_OPTIONAL
),
660 * Manual role assignments to users
662 * @param array $assignments An array of manual role assignment
664 public static function assign_roles($assignments) {
667 // Do basic automatic PARAM checks on incoming data, using params description
668 // If any problems are found then exceptions are thrown with helpful error messages
669 $params = self
::validate_parameters(self
::assign_roles_parameters(), array('assignments'=>$assignments));
671 $transaction = $DB->start_delegated_transaction();
673 foreach ($params['assignments'] as $assignment) {
674 // Ensure correct context level with a instance id or contextid is passed.
675 $context = self
::get_context_from_params($assignment);
677 // Ensure the current user is allowed to run this function in the enrolment context.
678 self
::validate_context($context);
679 require_capability('moodle/role:assign', $context);
681 // throw an exception if user is not able to assign the role in this context
682 $roles = get_assignable_roles($context, ROLENAME_SHORT
);
684 if (!array_key_exists($assignment['roleid'], $roles)) {
685 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
688 role_assign($assignment['roleid'], $assignment['userid'], $context->id
);
691 $transaction->allow_commit();
695 * Returns description of method result value
699 public static function assign_roles_returns() {
705 * Returns description of method parameters
707 * @return external_function_parameters
709 public static function unassign_roles_parameters() {
710 return new external_function_parameters(
712 'unassignments' => new external_multiple_structure(
713 new external_single_structure(
715 'roleid' => new external_value(PARAM_INT
, 'Role to assign to the user'),
716 'userid' => new external_value(PARAM_INT
, 'The user that is going to be assigned'),
717 'contextid' => new external_value(PARAM_INT
, 'The context to unassign the user role from', VALUE_OPTIONAL
),
718 'contextlevel' => new external_value(PARAM_ALPHA
, 'The context level to unassign the user role in
719 + (block, course, coursecat, system, user, module)', VALUE_OPTIONAL
),
720 'instanceid' => new external_value(PARAM_INT
, 'The Instance id of item where the role needs to be unassigned', VALUE_OPTIONAL
),
729 * Unassign roles from users
731 * @param array $unassignments An array of unassignment
733 public static function unassign_roles($unassignments) {
736 // Do basic automatic PARAM checks on incoming data, using params description
737 // If any problems are found then exceptions are thrown with helpful error messages
738 $params = self
::validate_parameters(self
::unassign_roles_parameters(), array('unassignments'=>$unassignments));
740 $transaction = $DB->start_delegated_transaction();
742 foreach ($params['unassignments'] as $unassignment) {
743 // Ensure the current user is allowed to run this function in the unassignment context
744 $context = self
::get_context_from_params($unassignment);
745 self
::validate_context($context);
746 require_capability('moodle/role:assign', $context);
748 // throw an exception if user is not able to unassign the role in this context
749 $roles = get_assignable_roles($context, ROLENAME_SHORT
);
750 if (!array_key_exists($unassignment['roleid'], $roles)) {
751 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
754 role_unassign($unassignment['roleid'], $unassignment['userid'], $context->id
);
757 $transaction->allow_commit();
761 * Returns description of method result value
765 public static function unassign_roles_returns() {
772 * Deprecated enrol and role external functions
774 * @package core_enrol
775 * @copyright 2010 Jerome Mouneyrac
776 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
778 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
779 * @see core_enrol_external
780 * @see core_role_external
782 class moodle_enrol_external
extends external_api
{
786 * Returns description of method parameters
788 * @return external_function_parameters
790 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
791 * @see core_enrol_external::get_enrolled_users_parameters()
793 public static function get_enrolled_users_parameters() {
794 return new external_function_parameters(
796 'courseid' => new external_value(PARAM_INT
, 'Course id'),
797 'withcapability' => new external_value(PARAM_CAPABILITY
, 'User should have this capability', VALUE_DEFAULT
, null),
798 'groupid' => new external_value(PARAM_INT
, 'Group id, null means all groups', VALUE_DEFAULT
, null),
799 'onlyactive' => new external_value(PARAM_INT
, 'True means only active, false means all participants', VALUE_DEFAULT
, 0),
805 * Get list of course participants.
807 * @param int $courseid
808 * @param text $withcapability
809 * @param int $groupid
810 * @param bool $onlyactive
811 * @return array of course participants
813 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
814 * @see core_enrol_external::get_enrolled_users()
816 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) {
817 global $DB, $CFG, $USER;
819 // Do basic automatic PARAM checks on incoming data, using params description
820 // If any problems are found then exceptions are thrown with helpful error messages
821 $params = self
::validate_parameters(self
::get_enrolled_users_parameters(), array(
822 'courseid'=>$courseid,
823 'withcapability'=>$withcapability,
825 'onlyactive'=>$onlyactive)
828 $coursecontext = context_course
::instance($params['courseid'], IGNORE_MISSING
);
829 if ($courseid == SITEID
) {
830 $context = context_system
::instance();
832 $context = $coursecontext;
836 self
::validate_context($context);
837 } catch (Exception
$e) {
838 $exceptionparam = new stdClass();
839 $exceptionparam->message
= $e->getMessage();
840 $exceptionparam->courseid
= $params['courseid'];
841 throw new moodle_exception('errorcoursecontextnotvalid' , 'webservice', '', $exceptionparam);
844 if ($courseid == SITEID
) {
845 require_capability('moodle/site:viewparticipants', $context);
847 require_capability('moodle/course:viewparticipants', $context);
850 if ($withcapability) {
851 require_capability('moodle/role:review', $coursecontext);
853 if ($groupid && groups_is_member($groupid)) {
854 require_capability('moodle/site:accessallgroups', $coursecontext);
857 require_capability('moodle/course:enrolreview', $coursecontext);
860 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
861 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid
862 FROM {user_enrolments} ue
863 JOIN {enrol} e ON (e.id = ue.enrolid)
864 JOIN {user} u ON (ue.userid = u.id)
865 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER
. ")
866 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams)
867 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id";
868 $params['courseid'] = $courseid;
869 $enrolledusers = $DB->get_records_sql($sql, $params);
871 $isadmin = is_siteadmin($USER);
872 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
873 foreach ($enrolledusers as $enrolleduser) {
874 $profilimgurl = moodle_url
::make_pluginfile_url($enrolleduser->usercontextid
, 'user', 'icon', NULL, '/', 'f1');
875 $profilimgurlsmall = moodle_url
::make_pluginfile_url($enrolleduser->usercontextid
, 'user', 'icon', NULL, '/', 'f2');
877 'courseid' => $enrolleduser->courseid
,
878 'userid' => $enrolleduser->userid
,
879 'fullname' => fullname($enrolleduser),
880 'profileimgurl' => $profilimgurl->out(false),
881 'profileimgurlsmall' => $profilimgurlsmall->out(false)
883 // check if we can return username
885 $resultuser['username'] = $enrolleduser->username
;
887 // check if we can return first and last name
888 if ($isadmin or $canviewfullnames) {
889 $resultuser['firstname'] = $enrolleduser->firstname
;
890 $resultuser['lastname'] = $enrolleduser->lastname
;
892 $result[] = $resultuser;
899 * Returns description of method result value
901 * @return external_description
903 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
904 * @see core_enrol_external::get_enrolled_users_returns()
906 public static function get_enrolled_users_returns() {
907 return new external_multiple_structure(
908 new external_single_structure(
910 'courseid' => new external_value(PARAM_INT
, 'id of course'),
911 'userid' => new external_value(PARAM_INT
, 'id of user'),
912 'firstname' => new external_value(PARAM_RAW
, 'first name of user', VALUE_OPTIONAL
),
913 'lastname' => new external_value(PARAM_RAW
, 'last name of user', VALUE_OPTIONAL
),
914 'fullname' => new external_value(PARAM_RAW
, 'fullname of user'),
915 'username' => new external_value(PARAM_RAW
, 'username of user', VALUE_OPTIONAL
),
916 'profileimgurl' => new external_value(PARAM_URL
, 'url of the profile image'),
917 'profileimgurlsmall' => new external_value(PARAM_URL
, 'url of the profile image (small version)')
924 * Returns description of method parameters
926 * @return external_function_parameters
928 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
929 * @see core_enrol_external::get_users_courses_parameters()
931 public static function get_users_courses_parameters() {
932 return core_enrol_external
::get_users_courses_parameters();
936 * Get list of courses user is enrolled in (only active enrolments are returned).
937 * Please note the current user must be able to access the course, otherwise the course is not included.
940 * @return array of courses
942 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
943 * @see use core_enrol_external::get_users_courses()
945 public static function get_users_courses($userid) {
946 return core_enrol_external
::get_users_courses($userid);
950 * Returns description of method result value
952 * @return external_description
954 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
955 * @see core_enrol_external::get_users_courses_returns()
957 public static function get_users_courses_returns() {
958 return core_enrol_external
::get_users_courses_returns();
963 * Returns description of method parameters
965 * @return external_function_parameters
967 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
968 * @see core_role_external::assign_roles_parameters()
970 public static function role_assign_parameters() {
971 return core_role_external
::assign_roles_parameters();
975 * Manual role assignments to users
977 * @param array $assignments An array of manual role assignment
979 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
980 * @see core_role_external::assign_roles()
982 public static function role_assign($assignments) {
983 return core_role_external
::assign_roles($assignments);
987 * Returns description of method result value
991 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
992 * @see core_role_external::assign_roles_returns()
994 public static function role_assign_returns() {
995 return core_role_external
::assign_roles_returns();
1000 * Returns description of method parameters
1002 * @return external_function_parameters
1004 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1005 * @see core_role_external::unassign_roles_parameters()
1007 public static function role_unassign_parameters() {
1008 return core_role_external
::unassign_roles_parameters();
1012 * Unassign roles from users
1014 * @param array $unassignments An array of unassignment
1016 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1017 * @see core_role_external::unassign_roles()
1019 public static function role_unassign($unassignments) {
1020 return core_role_external
::unassign_roles($unassignments);
1024 * Returns description of method result value
1028 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1029 * @see core_role_external::unassign_roles_returns()
1031 public static function role_unassign_returns() {
1032 return core_role_external
::unassign_roles_returns();