MDL-27759 fix whitespace
[moodle.git] / enrol / externallib.php
blob9813ef517885e4f80f1b7fbaa30ca8815ec327ae
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
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
25 * @subpackage enrol
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 {
38 /**
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(
44 array(
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),
53 /**
54 * Get list of course participants.
56 * @param int $courseid
57 * @param text $withcapability
58 * @param int $groupid
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,
70 'groupid'=>$groupid,
71 'onlyactive'=>$onlyactive)
74 $coursecontext = get_context_instance(CONTEXT_COURSE, $params['courseid']);
75 if ($courseid == SITEID) {
76 $context = get_context_instance(CONTEXT_SYSTEM);
77 } else {
78 $context = $coursecontext;
81 try {
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);
92 } else {
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);
102 if ($onlyactive) {
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);
116 $result = array();
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');
122 $resultuser = array(
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
130 if ($isadmin) {
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;
141 return $result;
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(
151 array(
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(
171 array(
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.
182 * @param int $userid
183 * @return array of courses
185 public static function get_users_courses($userid) {
186 global $USER;
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');
193 $result = array();
195 foreach ($courses as $course) {
196 $context = get_context_instance(CONTEXT_COURSE, $course->id);
197 try {
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!
201 continue;
203 if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) {
204 // we need capability to view participants
205 continue;
208 $result[] = array('id'=>$course->id, 'shortname'=>$course->shortname, 'fullname'=>$course->fullname, 'idnumber'=>$course->idnumber,'visible'=>$course->visible);
211 return $result;
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(
221 array(
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(
239 array(
240 'assignments' => new external_multiple_structure(
241 new external_single_structure(
242 array(
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
257 * @return null
259 public static function role_assign($assignments) {
260 global $DB;
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 role_assign($assignment['roleid'], $assignment['userid'], $assignment['contextid']);
277 $transaction->allow_commit();
281 * Returns description of method result value
282 * @return external_description
284 public static function role_assign_returns() {
285 return null;
290 * Returns description of method parameters
291 * @return external_function_parameters
293 public static function role_unassign_parameters() {
294 return new external_function_parameters(
295 array(
296 'unassignments' => new external_multiple_structure(
297 new external_single_structure(
298 array(
299 'roleid' => new external_value(PARAM_INT, 'Role to assign to the user'),
300 'userid' => new external_value(PARAM_INT, 'The user that is going to be assigned'),
301 'contextid' => new external_value(PARAM_INT, 'The context to unassign the user role from'),
310 * Unassign roles from users
312 * @param array $unassignment An array of unassignment
313 * @return null
315 public static function role_unassign($unassignments) {
316 global $DB;
318 // Do basic automatic PARAM checks on incoming data, using params description
319 // If any problems are found then exceptions are thrown with helpful error messages
320 $params = self::validate_parameters(self::role_unassign_parameters(), array('unassignments'=>$unassignments));
322 $transaction = $DB->start_delegated_transaction();
324 foreach ($params['unassignments'] as $unassignment) {
325 // Ensure the current user is allowed to run this function in the unassignment context
326 $context = get_context_instance_by_id($unassignment['contextid']);
327 self::validate_context($context);
328 require_capability('moodle/role:assign', $context);
330 role_unassign($unassignment['roleid'], $unassignment['userid'], $unassignment['contextid']);
333 $transaction->allow_commit();
337 * Returns description of method result value
338 * @return external_description
340 public static function role_unassign_returns() {
341 return null;