3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // 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 2009 Moodle Pty Ltd (http://moodle.com)
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");
37 class core_enrol_external
extends external_api
{
40 * Returns description of method parameters
41 * @return external_function_parameters
43 public static function get_users_courses_parameters() {
44 return new external_function_parameters(
46 'userid' => new external_value(PARAM_INT
, 'user id'),
52 * Get list of courses user is enrolled in (only active enrolments are returned).
54 * Please note the current user must be able to access the course, otherwise the course is not included.
57 * @return array of courses
59 public static function get_users_courses($userid) {
62 // Do basic automatic PARAM checks on incoming data, using params description
63 // If any problems are found then exceptions are thrown with helpful error messages
64 $params = self
::validate_parameters(self
::get_users_courses_parameters(), array('userid'=>$userid));
66 $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible');
69 foreach ($courses as $course) {
70 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
72 self
::validate_context($context);
73 } catch (Exception
$e) {
74 // current user can not access this course, sorry we can not disclose who is enrolled in this course!
78 if ($userid != $USER->id
and !has_capability('moodle/course:viewparticipants', $context)) {
79 // we need capability to view participants
83 list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context);
84 $enrolledsql = "SELECT COUNT(*) FROM ($enrolledsqlselect) AS enrolleduserids";
85 $enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams);
87 $result[] = array('id'=>$course->id
, 'shortname'=>$course->shortname
, 'fullname'=>$course->fullname
, 'idnumber'=>$course->idnumber
,'visible'=>$course->visible
, 'enrolledusercount'=>$enrolledusercount);
94 * Returns description of method result value
95 * @return external_description
97 public static function get_users_courses_returns() {
98 return new external_multiple_structure(
99 new external_single_structure(
101 'id' => new external_value(PARAM_INT
, 'id of course'),
102 'shortname' => new external_value(PARAM_RAW
, 'short name of course'),
103 'fullname' => new external_value(PARAM_RAW
, 'long name of course'),
104 'enrolledusercount' => new external_value(PARAM_INT
, 'Number of enrolled users in this course'),
105 'idnumber' => new external_value(PARAM_RAW
, 'id number of course'),
106 'visible' => new external_value(PARAM_INT
, '1 means visible, 0 means hidden course'),
113 * Returns description of method parameters
114 * @return external_function_parameters
116 public static function get_enrolled_users_parameters() {
117 return new external_function_parameters(
119 'courseid' => new external_value(PARAM_INT
, 'course id'),
120 'options' => new external_multiple_structure(
121 new external_single_structure(
123 'name' => new external_value(PARAM_ALPHANUMEXT
, 'option name'),
124 'value' => new external_value(PARAM_RAW
, 'option value')
127 * withcapability (string) return only users with this capability. This option requires \'moodle/role:review\' on the course context.
128 * groupid (integer) return only users in this group id. This option requires \'moodle/site:accessallgroups\' on the course context.
129 * onlyactive (integer) return only users with active enrolments and matching time restrictions. This option requires \'moodle/course:enrolreview\' on the course context.
130 * userfields (\'string, string, ...\') return only the values of these user fields.
131 * limitfrom (integer) sql limit from.
132 * limitnumber (integer) maximum number of returned users.', VALUE_DEFAULT
, array()),
138 * Get course participants details
139 * @param int $courseid course id
140 * @param array $options options {
141 * 'name' => option name
142 * 'value' => option value
144 * @return array An array of users
146 public static function get_enrolled_users($courseid, $options) {
147 global $CFG, $USER, $DB;
148 require_once($CFG->dirroot
. "/user/lib.php");
150 $params = self
::validate_parameters(
151 self
::get_enrolled_users_parameters(),
153 'courseid'=>$courseid,
157 $withcapability = '';
160 $userfields = array();
163 foreach ($options as $option) {
164 switch ($option['name']) {
165 case 'withcapability':
166 $withcapability = $option['value'];
169 $groupid = (int)$option['value'];
172 $onlyactive = !empty($option['value']);
175 $thefields = explode(',', $option['value']);
176 foreach ($thefields as $f) {
177 $userfields[] = clean_param($f, PARAM_ALPHANUMEXT
);
180 $limitfrom = clean_param($option['value'], PARAM_INT
);
183 $limitnumber = clean_param($option['value'], PARAM_INT
);
188 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST
);
189 $coursecontext = get_context_instance(CONTEXT_COURSE
, $courseid);
190 if ($courseid == SITEID
) {
191 $context = get_system_context();
193 $context = $coursecontext;
196 self
::validate_context($context);
197 } catch (Exception
$e) {
198 $exceptionparam = new stdClass();
199 $exceptionparam->message
= $e->getMessage();
200 $exceptionparam->courseid
= $params['courseid'];
201 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
204 if ($courseid == SITEID
) {
205 require_capability('moodle/site:viewparticipants', $context);
207 require_capability('moodle/course:viewparticipants', $context);
209 // to overwrite this parameter, you need role:review capability
210 if ($withcapability) {
211 require_capability('moodle/role:review', $coursecontext);
213 // need accessallgroups capability if you want to overwrite this option
214 if (!empty($groupid) && groups_is_member($groupid)) {
215 require_capability('moodle/site:accessallgroups', $coursecontext);
217 // to overwrite this option, you need course:enrolereview permission
219 require_capability('moodle/course:enrolreview', $coursecontext);
222 list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
223 list($ctxselect, $ctxjoin) = context_instance_preload_sql('u.id', CONTEXT_USER
, 'ctx');
224 $sqlparams['courseid'] = $courseid;
225 $sql = "SELECT u.* $ctxselect
226 FROM {user} u $ctxjoin
227 WHERE u.id IN ($enrolledsql)
229 $enrolledusers = $DB->get_recordset_sql($sql, $enrolledparams, $limitfrom, $limitnumber);
231 foreach ($enrolledusers as $user) {
232 context_instance_preload($user);
233 if ($userdetails = user_get_user_details($user, $course, $userfields)) {
234 $users[] = $userdetails;
237 $enrolledusers->close();
243 * Returns description of method result value
244 * @return external_description
246 public static function get_enrolled_users_returns() {
247 return new external_multiple_structure(
248 new external_single_structure(
250 'id' => new external_value(PARAM_NUMBER
, 'ID of the user'),
251 'username' => new external_value(PARAM_RAW
, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL
),
252 'firstname' => new external_value(PARAM_NOTAGS
, 'The first name(s) of the user', VALUE_OPTIONAL
),
253 'lastname' => new external_value(PARAM_NOTAGS
, 'The family name of the user', VALUE_OPTIONAL
),
254 'fullname' => new external_value(PARAM_NOTAGS
, 'The fullname of the user'),
255 'email' => new external_value(PARAM_TEXT
, 'An email address - allow email as root@localhost', VALUE_OPTIONAL
),
256 'address' => new external_value(PARAM_MULTILANG
, 'Postal address', VALUE_OPTIONAL
),
257 'phone1' => new external_value(PARAM_NOTAGS
, 'Phone 1', VALUE_OPTIONAL
),
258 'phone2' => new external_value(PARAM_NOTAGS
, 'Phone 2', VALUE_OPTIONAL
),
259 'icq' => new external_value(PARAM_NOTAGS
, 'icq number', VALUE_OPTIONAL
),
260 'skype' => new external_value(PARAM_NOTAGS
, 'skype id', VALUE_OPTIONAL
),
261 'yahoo' => new external_value(PARAM_NOTAGS
, 'yahoo id', VALUE_OPTIONAL
),
262 'aim' => new external_value(PARAM_NOTAGS
, 'aim id', VALUE_OPTIONAL
),
263 'msn' => new external_value(PARAM_NOTAGS
, 'msn number', VALUE_OPTIONAL
),
264 'department' => new external_value(PARAM_TEXT
, 'department', VALUE_OPTIONAL
),
265 'institution' => new external_value(PARAM_TEXT
, 'institution', VALUE_OPTIONAL
),
266 'interests' => new external_value(PARAM_TEXT
, 'user interests (separated by commas)', VALUE_OPTIONAL
),
267 'firstaccess' => new external_value(PARAM_INT
, 'first access to the site (0 if never)', VALUE_OPTIONAL
),
268 'lastaccess' => new external_value(PARAM_INT
, 'last access to the site (0 if never)', VALUE_OPTIONAL
),
269 'description' => new external_value(PARAM_RAW
, 'User profile description', VALUE_OPTIONAL
),
270 'descriptionformat' => new external_value(PARAM_INT
, 'User profile description format', VALUE_OPTIONAL
),
271 'city' => new external_value(PARAM_NOTAGS
, 'Home city of the user', VALUE_OPTIONAL
),
272 'url' => new external_value(PARAM_URL
, 'URL of the user', VALUE_OPTIONAL
),
273 'country' => new external_value(PARAM_ALPHA
, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
274 'profileimageurlsmall' => new external_value(PARAM_URL
, 'User image profile URL - small version', VALUE_OPTIONAL
),
275 'profileimageurl' => new external_value(PARAM_URL
, 'User image profile URL - big version', VALUE_OPTIONAL
),
276 'customfields' => new external_multiple_structure(
277 new external_single_structure(
279 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The type of the custom field - text field, checkbox...'),
280 'value' => new external_value(PARAM_RAW
, 'The value of the custom field'),
281 'name' => new external_value(PARAM_RAW
, 'The name of the custom field'),
282 'shortname' => new external_value(PARAM_RAW
, 'The shortname of the custom field - to be able to build the field class in the code'),
284 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL
),
285 'groups' => new external_multiple_structure(
286 new external_single_structure(
288 'id' => new external_value(PARAM_INT
, 'group id'),
289 'name' => new external_value(PARAM_RAW
, 'group name'),
290 'description' => new external_value(PARAM_RAW
, 'group description'),
292 ), 'user groups', VALUE_OPTIONAL
),
293 'roles' => new external_multiple_structure(
294 new external_single_structure(
296 'roleid' => new external_value(PARAM_INT
, 'role id'),
297 'name' => new external_value(PARAM_RAW
, 'role name'),
298 'shortname' => new external_value(PARAM_ALPHANUMEXT
, 'role shortname'),
299 'sortorder' => new external_value(PARAM_INT
, 'role sortorder')
301 ), 'user roles', VALUE_OPTIONAL
),
302 'preferences' => new external_multiple_structure(
303 new external_single_structure(
305 'name' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the preferences'),
306 'value' => new external_value(PARAM_RAW
, 'The value of the custom field'),
308 ), 'User preferences', VALUE_OPTIONAL
),
309 'enrolledcourses' => new external_multiple_structure(
310 new external_single_structure(
312 'id' => new external_value(PARAM_INT
, 'Id of the course'),
313 'fullname' => new external_value(PARAM_RAW
, 'Fullname of the course'),
314 'shortname' => new external_value(PARAM_RAW
, 'Shortname of the course')
316 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL
)
327 class core_role_external
extends external_api
{
330 * Returns description of method parameters
331 * @return external_function_parameters
333 public static function assign_roles_parameters() {
334 return new external_function_parameters(
336 'assignments' => new external_multiple_structure(
337 new external_single_structure(
339 'roleid' => new external_value(PARAM_INT
, 'Role to assign to the user'),
340 'userid' => new external_value(PARAM_INT
, 'The user that is going to be assigned'),
341 'contextid' => new external_value(PARAM_INT
, 'The context to assign the user role in'),
350 * Manual role assignments to users
352 * @param array $assignment An array of manual role assignment
355 public static function assign_roles($assignments) {
358 // Do basic automatic PARAM checks on incoming data, using params description
359 // If any problems are found then exceptions are thrown with helpful error messages
360 $params = self
::validate_parameters(self
::assign_roles_parameters(), array('assignments'=>$assignments));
362 $transaction = $DB->start_delegated_transaction();
364 foreach ($params['assignments'] as $assignment) {
365 // Ensure the current user is allowed to run this function in the enrolment context
366 $context = get_context_instance_by_id($assignment['contextid']);
367 self
::validate_context($context);
368 require_capability('moodle/role:assign', $context);
370 // throw an exception if user is not able to assign the role in this context
371 $roles = get_assignable_roles($context, ROLENAME_SHORT
);
373 if (!key_exists($assignment['roleid'], $roles)) {
374 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
377 role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);
380 $transaction->allow_commit();
384 * Returns description of method result value
385 * @return external_description
387 public static function assign_roles_returns() {
393 * Returns description of method parameters
394 * @return external_function_parameters
396 public static function unassign_roles_parameters() {
397 return new external_function_parameters(
399 'unassignments' => new external_multiple_structure(
400 new external_single_structure(
402 'roleid' => new external_value(PARAM_INT
, 'Role to assign to the user'),
403 'userid' => new external_value(PARAM_INT
, 'The user that is going to be assigned'),
404 'contextid' => new external_value(PARAM_INT
, 'The context to unassign the user role from'),
413 * Unassign roles from users
415 * @param array $unassignment An array of unassignment
418 public static function unassign_roles($unassignments) {
421 // Do basic automatic PARAM checks on incoming data, using params description
422 // If any problems are found then exceptions are thrown with helpful error messages
423 $params = self
::validate_parameters(self
::unassign_roles_parameters(), array('unassignments'=>$unassignments));
425 $transaction = $DB->start_delegated_transaction();
427 foreach ($params['unassignments'] as $unassignment) {
428 // Ensure the current user is allowed to run this function in the unassignment context
429 $context = get_context_instance_by_id($unassignment['contextid']);
430 self
::validate_context($context);
431 require_capability('moodle/role:assign', $context);
433 // throw an exception if user is not able to unassign the role in this context
434 $roles = get_assignable_roles($context, ROLENAME_SHORT
);
435 if (!key_exists($unassignment['roleid'], $roles)) {
436 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
439 role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']);
442 $transaction->allow_commit();
446 * Returns description of method result value
449 public static function unassign_roles_returns() {
456 * Deprecated enroll and role functions
457 * @deprecated since Moodle 2.2 please use core_enrol_external or core_role_external instead
459 class moodle_enrol_external
extends external_api
{
463 * Returns description of method parameters
464 * @deprecated since Moodle 2.2 please use core_enrol_external::get_enrolled_users_parameters() instead
465 * @return external_function_parameters
467 public static function get_enrolled_users_parameters() {
468 return new external_function_parameters(
470 'courseid' => new external_value(PARAM_INT
, 'Course id'),
471 'withcapability' => new external_value(PARAM_CAPABILITY
, 'User should have this capability', VALUE_DEFAULT
, null),
472 'groupid' => new external_value(PARAM_INT
, 'Group id, null means all groups', VALUE_DEFAULT
, null),
473 'onlyactive' => new external_value(PARAM_INT
, 'True means only active, false means all participants', VALUE_DEFAULT
, 0),
479 * Get list of course participants.
480 * @deprecated since Moodle 2.2 please use core_enrol_external::get_enrolled_users() instead
481 * @param int $courseid
482 * @param text $withcapability
483 * @param int $groupid
484 * @param bool $onlyactive
485 * @return array of course participants
487 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) {
488 global $DB, $CFG, $USER;
490 // Do basic automatic PARAM checks on incoming data, using params description
491 // If any problems are found then exceptions are thrown with helpful error messages
492 $params = self
::validate_parameters(self
::get_enrolled_users_parameters(), array(
493 'courseid'=>$courseid,
494 'withcapability'=>$withcapability,
496 'onlyactive'=>$onlyactive)
499 $coursecontext = get_context_instance(CONTEXT_COURSE
, $params['courseid']);
500 if ($courseid == SITEID
) {
501 $context = get_context_instance(CONTEXT_SYSTEM
);
503 $context = $coursecontext;
507 self
::validate_context($context);
508 } catch (Exception
$e) {
509 $exceptionparam = new stdClass();
510 $exceptionparam->message
= $e->getMessage();
511 $exceptionparam->courseid
= $params['courseid'];
512 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
515 if ($courseid == SITEID
) {
516 require_capability('moodle/site:viewparticipants', $context);
518 require_capability('moodle/course:viewparticipants', $context);
521 if ($withcapability) {
522 require_capability('moodle/role:review', $coursecontext);
524 if ($groupid && groups_is_member($groupid)) {
525 require_capability('moodle/site:accessallgroups', $coursecontext);
528 require_capability('moodle/course:enrolreview', $coursecontext);
531 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
532 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid
533 FROM {user_enrolments} ue
534 JOIN {enrol} e ON (e.id = ue.enrolid)
535 JOIN {user} u ON (ue.userid = u.id)
536 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER
. ")
537 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams)
538 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id";
539 $params['courseid'] = $courseid;
540 $enrolledusers = $DB->get_records_sql($sql, $params);
542 $isadmin = is_siteadmin($USER);
543 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
544 foreach ($enrolledusers as $enrolleduser) {
545 $profilimgurl = moodle_url
::make_pluginfile_url($enrolleduser->usercontextid
, 'user', 'icon', NULL, '/', 'f1');
546 $profilimgurlsmall = moodle_url
::make_pluginfile_url($enrolleduser->usercontextid
, 'user', 'icon', NULL, '/', 'f2');
548 'courseid' => $enrolleduser->courseid
,
549 'userid' => $enrolleduser->userid
,
550 'fullname' => fullname($enrolleduser),
551 'profileimgurl' => $profilimgurl->out(false),
552 'profileimgurlsmall' => $profilimgurlsmall->out(false)
554 // check if we can return username
556 $resultuser['username'] = $enrolleduser->username
;
558 // check if we can return first and last name
559 if ($isadmin or $canviewfullnames) {
560 $resultuser['firstname'] = $enrolleduser->firstname
;
561 $resultuser['lastname'] = $enrolleduser->lastname
;
563 $result[] = $resultuser;
570 * Returns description of method result value
571 * @deprecated since Moodle 2.2 please use core_enrol_external::get_enrolled_users_returns() instead
572 * @return external_description
574 public static function get_enrolled_users_returns() {
575 return new external_multiple_structure(
576 new external_single_structure(
578 'courseid' => new external_value(PARAM_INT
, 'id of course'),
579 'userid' => new external_value(PARAM_INT
, 'id of user'),
580 'firstname' => new external_value(PARAM_RAW
, 'first name of user', VALUE_OPTIONAL
),
581 'lastname' => new external_value(PARAM_RAW
, 'last name of user', VALUE_OPTIONAL
),
582 'fullname' => new external_value(PARAM_RAW
, 'fullname of user'),
583 'username' => new external_value(PARAM_RAW
, 'username of user', VALUE_OPTIONAL
),
584 'profileimgurl' => new external_value(PARAM_URL
, 'url of the profile image'),
585 'profileimgurlsmall' => new external_value(PARAM_URL
, 'url of the profile image (small version)')
592 * Returns description of method parameters
593 * @deprecated since Moodle 2.2 please use core_enrol_external::get_users_courses_parameters() instead
594 * @return external_function_parameters
596 public static function get_users_courses_parameters() {
597 return core_enrol_external
::get_users_courses_parameters();
601 * Get list of courses user is enrolled in (only active enrolments are returned).
603 * Please note the current user must be able to access the course, otherwise the course is not included.
604 * @deprecated since Moodle 2.2 please use core_enrol_external::get_users_courses() instead
606 * @return array of courses
608 public static function get_users_courses($userid) {
609 return core_enrol_external
::get_users_courses($userid);
613 * Returns description of method result value
614 * @deprecated since Moodle 2.2 please use core_enrol_external::get_users_courses_returns() instead
615 * @return external_description
617 public static function get_users_courses_returns() {
618 return core_enrol_external
::get_users_courses_returns();
623 * Returns description of method parameters
624 * @deprecated since Moodle 2.2 please use core_role_external::assign_roles_parameters() instead
625 * @return external_function_parameters
627 public static function role_assign_parameters() {
628 return core_role_external
::assign_roles_parameters();
632 * Manual role assignments to users
633 * @deprecated since Moodle 2.2 please use core_role_external::assign_roles() instead
634 * @param array $assignment An array of manual role assignment
637 public static function role_assign($assignments) {
638 return core_role_external
::assign_roles($assignments);
642 * Returns description of method result value
643 * @deprecated since Moodle 2.2 please use core_role_external::assign_roles_returns() instead
644 * @return external_description
646 public static function role_assign_returns() {
647 return core_role_external
::assign_roles_returns();
652 * Returns description of method parameters
653 * @deprecated since Moodle 2.2 please use core_role_external::unassign_roles_parameters() instead
654 * @return external_function_parameters
656 public static function role_unassign_parameters() {
657 return core_role_external
::unassign_roles_parameters();
661 * Unassign roles from users
662 * @deprecated since Moodle 2.2 please use core_role_external::unassign_roles() instead
663 * @param array $unassignment An array of unassignment
666 public static function role_unassign($unassignments) {
667 return core_role_external
::unassign_roles($unassignments);
671 * Returns description of method result value
672 * @deprecated since Moodle 2.2 please use core_role_external::unassign_roles_returns() instead
673 * @return external_description
675 public static function role_unassign_returns() {
676 return core_role_external
::unassign_roles_returns();