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");
35 class moodle_enrol_external
extends external_api
{
39 * Returns description of method parameters
40 * @return external_function_parameters
42 public static function get_enrolled_users_parameters() {
43 return new external_function_parameters(
45 'courseid' => new external_value(PARAM_INT
, 'Course id'),
46 'withcapability' => new external_value(PARAM_CAPABILITY
, 'User should have this capability', VALUE_DEFAULT
, null),
47 'groupid' => new external_value(PARAM_INT
, 'Group id, null means all groups', VALUE_DEFAULT
, null),
48 'onlyactive' => new external_value(PARAM_INT
, 'True means only active, false means all participants', VALUE_DEFAULT
, 0),
54 * Get list of course participants.
56 * @param int $courseid
57 * @param text $withcapability
59 * @param bool $onlyactive
60 * @return array of course participants
62 public static function get_enrolled_users($courseid, $withcapability = null, $groupid = null, $onlyactive = false) {
63 global $DB, $CFG, $USER;
65 // Do basic automatic PARAM checks on incoming data, using params description
66 // If any problems are found then exceptions are thrown with helpful error messages
67 $params = self
::validate_parameters(self
::get_enrolled_users_parameters(), array(
68 'courseid'=>$courseid,
69 'withcapability'=>$withcapability,
71 'onlyactive'=>$onlyactive)
74 $coursecontext = get_context_instance(CONTEXT_COURSE
, $params['courseid']);
75 if ($courseid == SITEID
) {
76 $context = get_context_instance(CONTEXT_SYSTEM
);
78 $context = $coursecontext;
82 self
::validate_context($context);
83 } catch (Exception
$e) {
84 $exceptionparam = new stdClass();
85 $exceptionparam->message
= $e->getMessage();
86 $exceptionparam->courseid
= $params['courseid'];
87 throw new moodle_exception(get_string('errorcoursecontextnotvalid' , 'webservice', $exceptionparam));
90 if ($courseid == SITEID
) {
91 require_capability('moodle/site:viewparticipants', $context);
93 require_capability('moodle/course:viewparticipants', $context);
96 if ($withcapability) {
97 require_capability('moodle/role:review', $coursecontext);
99 if ($groupid && groups_is_member($groupid)) {
100 require_capability('moodle/site:accessallgroups', $coursecontext);
103 require_capability('moodle/course:enrolreview', $coursecontext);
106 list($sqlparams, $params) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
107 $sql = "SELECT ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id as usercontextid
108 FROM {user_enrolments} ue
109 JOIN {enrol} e ON (e.id = ue.enrolid)
110 JOIN {user} u ON (ue.userid = u.id)
111 JOIN {context} c ON (u.id = c.instanceid AND contextlevel = " . CONTEXT_USER
. ")
112 WHERE e.courseid = :courseid AND ue.userid IN ($sqlparams)
113 GROUP BY ue.userid, e.courseid, u.firstname, u.lastname, u.username, c.id";
114 $params['courseid'] = $courseid;
115 $enrolledusers = $DB->get_records_sql($sql, $params);
117 $isadmin = is_siteadmin($USER);
118 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
119 foreach ($enrolledusers as $enrolleduser) {
120 $profilimgurl = moodle_url
::make_pluginfile_url($enrolleduser->usercontextid
, 'user', 'icon', NULL, '/', 'f1');
121 $profilimgurlsmall = moodle_url
::make_pluginfile_url($enrolleduser->usercontextid
, 'user', 'icon', NULL, '/', 'f2');
123 'courseid' => $enrolleduser->courseid
,
124 'userid' => $enrolleduser->userid
,
125 'fullname' => fullname($enrolleduser),
126 'profileimgurl' => $profilimgurl->out(false),
127 'profileimgurlsmall' => $profilimgurlsmall->out(false)
129 // check if we can return username
131 $resultuser['username'] = $enrolleduser->username
;
133 // check if we can return first and last name
134 if ($isadmin or $canviewfullnames) {
135 $resultuser['firstname'] = $enrolleduser->firstname
;
136 $resultuser['lastname'] = $enrolleduser->lastname
;
138 $result[] = $resultuser;
145 * Returns description of method result value
146 * @return external_description
148 public static function get_enrolled_users_returns() {
149 return new external_multiple_structure(
150 new external_single_structure(
152 'courseid' => new external_value(PARAM_INT
, 'id of course'),
153 'userid' => new external_value(PARAM_INT
, 'id of user'),
154 'firstname' => new external_value(PARAM_RAW
, 'first name of user', VALUE_OPTIONAL
),
155 'lastname' => new external_value(PARAM_RAW
, 'last name of user', VALUE_OPTIONAL
),
156 'fullname' => new external_value(PARAM_RAW
, 'fullname of user'),
157 'username' => new external_value(PARAM_RAW
, 'username of user', VALUE_OPTIONAL
),
158 'profileimgurl' => new external_value(PARAM_URL
, 'url of the profile image'),
159 'profileimgurlsmall' => new external_value(PARAM_URL
, 'url of the profile image (small version)')
166 * Returns description of method parameters
167 * @return external_function_parameters
169 public static function get_users_courses_parameters() {
170 return new external_function_parameters(
172 'userid' => new external_value(PARAM_INT
, 'user id'),
178 * Get list of courses user is enrolled in (only active enrolments are returned).
180 * Please note the current user must be able to access the course, otherwise the course is not included.
183 * @return array of courses
185 public static function get_users_courses($userid) {
188 // Do basic automatic PARAM checks on incoming data, using params description
189 // If any problems are found then exceptions are thrown with helpful error messages
190 $params = self
::validate_parameters(self
::get_users_courses_parameters(), array('userid'=>$userid));
192 $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible');
195 foreach ($courses as $course) {
196 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
198 self
::validate_context($context);
199 } catch (Exception
$e) {
200 // current user can not access this course, sorry we can not disclose who is enrolled in this course!
203 if ($userid != $USER->id
and !has_capability('moodle/course:viewparticipants', $context)) {
204 // we need capability to view participants
208 $result[] = array('id'=>$course->id
, 'shortname'=>$course->shortname
, 'fullname'=>$course->fullname
, 'idnumber'=>$course->idnumber
,'visible'=>$course->visible
);
215 * Returns description of method result value
216 * @return external_description
218 public static function get_users_courses_returns() {
219 return new external_multiple_structure(
220 new external_single_structure(
222 'id' => new external_value(PARAM_INT
, 'id of course'),
223 'shortname' => new external_value(PARAM_RAW
, 'short name of course'),
224 'fullname' => new external_value(PARAM_RAW
, 'long name of course'),
225 'idnumber' => new external_value(PARAM_RAW
, 'id number of course'),
226 'visible' => new external_value(PARAM_INT
, '1 means visible, 0 means hidden course'),
234 * Returns description of method parameters
235 * @return external_function_parameters
237 public static function role_assign_parameters() {
238 return new external_function_parameters(
240 'assignments' => new external_multiple_structure(
241 new external_single_structure(
243 'roleid' => new external_value(PARAM_INT
, 'Role to assign to the user'),
244 'userid' => new external_value(PARAM_INT
, 'The user that is going to be assigned'),
245 'contextid' => new external_value(PARAM_INT
, 'The context to assign the user role in'),
254 * Manual role assignments to users
256 * @param array $assignment An array of manual role assignment
259 public static function role_assign($assignments) {
262 // Do basic automatic PARAM checks on incoming data, using params description
263 // If any problems are found then exceptions are thrown with helpful error messages
264 $params = self
::validate_parameters(self
::role_assign_parameters(), array('assignments'=>$assignments));
266 $transaction = $DB->start_delegated_transaction();
268 foreach ($params['assignments'] as $assignment) {
269 // Ensure the current user is allowed to run this function in the enrolment context
270 $context = get_context_instance_by_id($assignment['contextid']);
271 self
::validate_context($context);
272 require_capability('moodle/role:assign', $context);
274 // throw an exception if user is not able to assign the role in this context
275 $roles = get_assignable_roles($context, ROLENAME_SHORT
);
276 if (!key_exists($assignment['roleid'], $roles)) {
277 throw new invalid_parameter_exception('Can not assign roleid='.$assignment['roleid'].' in contextid='.$assignment['contextid']);
280 role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);
283 $transaction->allow_commit();
287 * Returns description of method result value
288 * @return external_description
290 public static function role_assign_returns() {
296 * Returns description of method parameters
297 * @return external_function_parameters
299 public static function role_unassign_parameters() {
300 return new external_function_parameters(
302 'unassignments' => new external_multiple_structure(
303 new external_single_structure(
305 'roleid' => new external_value(PARAM_INT
, 'Role to assign to the user'),
306 'userid' => new external_value(PARAM_INT
, 'The user that is going to be assigned'),
307 'contextid' => new external_value(PARAM_INT
, 'The context to unassign the user role from'),
316 * Unassign roles from users
318 * @param array $unassignment An array of unassignment
321 public static function role_unassign($unassignments) {
324 // Do basic automatic PARAM checks on incoming data, using params description
325 // If any problems are found then exceptions are thrown with helpful error messages
326 $params = self
::validate_parameters(self
::role_unassign_parameters(), array('unassignments'=>$unassignments));
328 $transaction = $DB->start_delegated_transaction();
330 foreach ($params['unassignments'] as $unassignment) {
331 // Ensure the current user is allowed to run this function in the unassignment context
332 $context = get_context_instance_by_id($unassignment['contextid']);
333 self
::validate_context($context);
334 require_capability('moodle/role:assign', $context);
336 // throw an exception if user is not able to unassign the role in this context
337 $roles = get_assignable_roles($context, ROLENAME_SHORT
);
338 if (!key_exists($unassignment['roleid'], $roles)) {
339 throw new invalid_parameter_exception('Can not unassign roleid='.$unassignment['roleid'].' in contextid='.$unassignment['contextid']);
342 role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']);
345 $transaction->allow_commit();
349 * Returns description of method result value
350 * @return external_description
352 public static function role_unassign_returns() {