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/>.
22 * @copyright 2009 Petr Skodak
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once("$CFG->libdir/externallib.php");
31 * User external functions
35 * @copyright 2011 Jerome Mouneyrac
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_user_external
extends external_api
{
42 * Returns description of method parameters
44 * @return external_function_parameters
47 public static function create_users_parameters() {
50 'createpassword' => new external_value(PARAM_BOOL
, 'True if password should be created and mailed to user.',
53 'username' => new external_value(core_user
::get_property_type('username'),
54 'Username policy is defined in Moodle security config.'),
55 'auth' => new external_value(core_user
::get_property_type('auth'), 'Auth plugins include manual, ldap, etc',
56 VALUE_DEFAULT
, 'manual', core_user
::get_property_null('auth')),
57 'password' => new external_value(core_user
::get_property_type('password'),
58 'Plain text password consisting of any characters', VALUE_OPTIONAL
),
59 'firstname' => new external_value(core_user
::get_property_type('firstname'), 'The first name(s) of the user'),
60 'lastname' => new external_value(core_user
::get_property_type('lastname'), 'The family name of the user'),
61 'email' => new external_value(core_user
::get_property_type('email'), 'A valid and unique email address'),
62 'maildisplay' => new external_value(core_user
::get_property_type('maildisplay'), 'Email display', VALUE_OPTIONAL
),
63 'city' => new external_value(core_user
::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL
),
64 'country' => new external_value(core_user
::get_property_type('country'),
65 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
66 'timezone' => new external_value(core_user
::get_property_type('timezone'),
67 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL
),
68 'description' => new external_value(core_user
::get_property_type('description'), 'User profile description, no HTML',
71 'firstnamephonetic' => new external_value(core_user
::get_property_type('firstnamephonetic'),
72 'The first name(s) phonetically of the user', VALUE_OPTIONAL
),
73 'lastnamephonetic' => new external_value(core_user
::get_property_type('lastnamephonetic'),
74 'The family name phonetically of the user', VALUE_OPTIONAL
),
75 'middlename' => new external_value(core_user
::get_property_type('middlename'), 'The middle name of the user',
77 'alternatename' => new external_value(core_user
::get_property_type('alternatename'), 'The alternate name of the user',
80 'interests' => new external_value(PARAM_TEXT
, 'User interests (separated by commas)', VALUE_OPTIONAL
),
82 'url' => new external_value(core_user
::get_property_type('url'), 'User web page', VALUE_OPTIONAL
),
83 'icq' => new external_value(core_user
::get_property_type('icq'), 'ICQ number', VALUE_OPTIONAL
),
84 'skype' => new external_value(core_user
::get_property_type('skype'), 'Skype ID', VALUE_OPTIONAL
),
85 'aim' => new external_value(core_user
::get_property_type('aim'), 'AIM ID', VALUE_OPTIONAL
),
86 'yahoo' => new external_value(core_user
::get_property_type('yahoo'), 'Yahoo ID', VALUE_OPTIONAL
),
87 'msn' => new external_value(core_user
::get_property_type('msn'), 'MSN ID', VALUE_OPTIONAL
),
88 'idnumber' => new external_value(core_user
::get_property_type('idnumber'),
89 'An arbitrary ID code number perhaps from the institution', VALUE_DEFAULT
, ''),
90 'institution' => new external_value(core_user
::get_property_type('institution'), 'institution', VALUE_OPTIONAL
),
91 'department' => new external_value(core_user
::get_property_type('department'), 'department', VALUE_OPTIONAL
),
92 'phone1' => new external_value(core_user
::get_property_type('phone1'), 'Phone 1', VALUE_OPTIONAL
),
93 'phone2' => new external_value(core_user
::get_property_type('phone2'), 'Phone 2', VALUE_OPTIONAL
),
94 'address' => new external_value(core_user
::get_property_type('address'), 'Postal address', VALUE_OPTIONAL
),
95 // Other user preferences stored in the user table.
96 'lang' => new external_value(core_user
::get_property_type('lang'), 'Language code such as "en", must exist on server',
97 VALUE_DEFAULT
, core_user
::get_property_default('lang'), core_user
::get_property_null('lang')),
98 'calendartype' => new external_value(core_user
::get_property_type('calendartype'),
99 'Calendar type such as "gregorian", must exist on server', VALUE_DEFAULT
, $CFG->calendartype
, VALUE_OPTIONAL
),
100 'theme' => new external_value(core_user
::get_property_type('theme'),
101 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL
),
102 'mailformat' => new external_value(core_user
::get_property_type('mailformat'),
103 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL
),
104 // Custom user profile fields.
105 'customfields' => new external_multiple_structure(
106 new external_single_structure(
108 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the custom field'),
109 'value' => new external_value(PARAM_RAW
, 'The value of the custom field')
111 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL
),
113 'preferences' => new external_multiple_structure(
114 new external_single_structure(
116 'type' => new external_value(PARAM_RAW
, 'The name of the preference'),
117 'value' => new external_value(PARAM_RAW
, 'The value of the preference')
119 ), 'User preferences', VALUE_OPTIONAL
),
121 return new external_function_parameters(
123 'users' => new external_multiple_structure(
124 new external_single_structure($userfields)
131 * Create one or more users.
133 * @throws invalid_parameter_exception
134 * @param array $users An array of users to create.
135 * @return array An array of arrays
138 public static function create_users($users) {
140 require_once($CFG->dirroot
."/lib/weblib.php");
141 require_once($CFG->dirroot
."/user/lib.php");
142 require_once($CFG->dirroot
."/user/editlib.php");
143 require_once($CFG->dirroot
."/user/profile/lib.php"); // Required for customfields related function.
145 // Ensure the current user is allowed to run this function.
146 $context = context_system
::instance();
147 self
::validate_context($context);
148 require_capability('moodle/user:create', $context);
150 // Do basic automatic PARAM checks on incoming data, using params description.
151 // If any problems are found then exceptions are thrown with helpful error messages.
152 $params = self
::validate_parameters(self
::create_users_parameters(), array('users' => $users));
154 $availableauths = core_component
::get_plugin_list('auth');
155 unset($availableauths['mnet']); // These would need mnethostid too.
156 unset($availableauths['webservice']); // We do not want new webservice users for now.
158 $availablethemes = core_component
::get_plugin_list('theme');
159 $availablelangs = get_string_manager()->get_list_of_translations();
161 $transaction = $DB->start_delegated_transaction();
164 foreach ($params['users'] as $user) {
165 // Make sure that the username, firstname and lastname are not blank.
166 foreach (array('username', 'firstname', 'lastname') as $fieldname) {
167 if (trim($user[$fieldname]) === '') {
168 throw new invalid_parameter_exception('The field '.$fieldname.' cannot be blank');
172 // Make sure that the username doesn't already exist.
173 if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id
))) {
174 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
177 // Make sure auth is valid.
178 if (empty($availableauths[$user['auth']])) {
179 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
182 // Make sure lang is valid.
183 if (empty($availablelangs[$user['lang']])) {
184 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
187 // Make sure lang is valid.
188 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { // Theme is VALUE_OPTIONAL,
189 // so no default value
190 // We need to test if the client sent it
191 // => !empty($user['theme']).
192 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
195 // Make sure we have a password or have to create one.
196 $authplugin = get_auth_plugin($user['auth']);
197 if ($authplugin->is_internal() && empty($user['password']) && empty($user['createpassword'])) {
198 throw new invalid_parameter_exception('Invalid password: you must provide a password, or set createpassword.');
201 $user['confirmed'] = true;
202 $user['mnethostid'] = $CFG->mnet_localhost_id
;
204 // Start of user info validation.
205 // Make sure we validate current user info as handled by current GUI. See user/editadvanced_form.php func validation().
206 if (!validate_email($user['email'])) {
207 throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
208 } else if (empty($CFG->allowaccountssameemail
)) {
209 // Make a case-insensitive query for the given email address.
210 $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid';
212 'email' => $user['email'],
213 'mnethostid' => $user['mnethostid']
215 // If there are other user(s) that already have the same email, throw an error.
216 if ($DB->record_exists_select('user', $select, $params)) {
217 throw new invalid_parameter_exception('Email address already exists: '.$user['email']);
220 // End of user info validation.
222 $createpassword = !empty($user['createpassword']);
223 unset($user['createpassword']);
224 $updatepassword = false;
225 if ($authplugin->is_internal()) {
226 if ($createpassword) {
227 $user['password'] = '';
229 $updatepassword = true;
232 $user['password'] = AUTH_PASSWORD_NOT_CACHED
;
235 // Create the user data now!
236 $user['id'] = user_create_user($user, $updatepassword, false);
238 $userobject = (object)$user;
240 // Set user interests.
241 if (!empty($user['interests'])) {
242 $trimmedinterests = array_map('trim', explode(',', $user['interests']));
243 $interests = array_filter($trimmedinterests, function($value) {
244 return !empty($value);
246 useredit_update_interests($userobject, $interests);
250 if (!empty($user['customfields'])) {
251 foreach ($user['customfields'] as $customfield) {
252 // Profile_save_data() saves profile file it's expecting a user with the correct id,
253 // and custom field to be named profile_field_"shortname".
254 $user["profile_field_".$customfield['type']] = $customfield['value'];
256 profile_save_data((object) $user);
259 if ($createpassword) {
260 setnew_password_and_mail($userobject);
261 unset_user_preference('create_password', $userobject);
262 set_user_preference('auth_forcepasswordchange', 1, $userobject);
266 \core\event\user_created
::create_from_userid($user['id'])->trigger();
269 if (!empty($user['preferences'])) {
270 $userpref = (object)$user;
271 foreach ($user['preferences'] as $preference) {
272 $userpref->{'preference_'.$preference['type']} = $preference['value'];
274 useredit_update_user_preference($userpref);
277 $userids[] = array('id' => $user['id'], 'username' => $user['username']);
280 $transaction->allow_commit();
286 * Returns description of method result value
288 * @return external_description
291 public static function create_users_returns() {
292 return new external_multiple_structure(
293 new external_single_structure(
295 'id' => new external_value(core_user
::get_property_type('id'), 'user id'),
296 'username' => new external_value(core_user
::get_property_type('username'), 'user name'),
304 * Returns description of method parameters
306 * @return external_function_parameters
309 public static function delete_users_parameters() {
310 return new external_function_parameters(
312 'userids' => new external_multiple_structure(new external_value(core_user
::get_property_type('id'), 'user ID')),
320 * @throws moodle_exception
321 * @param array $userids
325 public static function delete_users($userids) {
326 global $CFG, $DB, $USER;
327 require_once($CFG->dirroot
."/user/lib.php");
329 // Ensure the current user is allowed to run this function.
330 $context = context_system
::instance();
331 require_capability('moodle/user:delete', $context);
332 self
::validate_context($context);
334 $params = self
::validate_parameters(self
::delete_users_parameters(), array('userids' => $userids));
336 $transaction = $DB->start_delegated_transaction();
338 foreach ($params['userids'] as $userid) {
339 $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST
);
340 // Must not allow deleting of admins or self!!!
341 if (is_siteadmin($user)) {
342 throw new moodle_exception('useradminodelete', 'error');
344 if ($USER->id
== $user->id
) {
345 throw new moodle_exception('usernotdeletederror', 'error');
347 user_delete_user($user);
350 $transaction->allow_commit();
356 * Returns description of method result value
361 public static function delete_users_returns() {
366 * Returns description of method parameters.
368 * @return external_function_parameters
371 public static function update_user_preferences_parameters() {
372 return new external_function_parameters(
374 'userid' => new external_value(PARAM_INT
, 'id of the user, default to current user', VALUE_DEFAULT
, 0),
375 'emailstop' => new external_value(core_user
::get_property_type('emailstop'),
376 'Enable or disable notifications for this user', VALUE_DEFAULT
, null),
377 'preferences' => new external_multiple_structure(
378 new external_single_structure(
380 'type' => new external_value(PARAM_RAW
, 'The name of the preference'),
381 'value' => new external_value(PARAM_RAW
, 'The value of the preference, do not set this field if you
382 want to remove (unset) the current value.', VALUE_DEFAULT
, null),
384 ), 'User preferences', VALUE_DEFAULT
, array()
391 * Update the user's preferences.
394 * @param bool|null $emailstop
395 * @param array $preferences
399 public static function update_user_preferences($userid = 0, $emailstop = null, $preferences = array()) {
402 require_once($CFG->dirroot
. '/user/lib.php');
403 require_once($CFG->dirroot
. '/user/editlib.php');
404 require_once($CFG->dirroot
. '/message/lib.php');
406 if (empty($userid)) {
410 $systemcontext = context_system
::instance();
411 self
::validate_context($systemcontext);
414 'emailstop' => $emailstop,
415 'preferences' => $preferences
417 $params = self
::validate_parameters(self
::update_user_preferences_parameters(), $params);
418 $preferences = $params['preferences'];
421 if (!empty($preferences)) {
422 $userpref = ['id' => $userid];
423 foreach ($preferences as $preference) {
424 $userpref['preference_' . $preference['type']] = $preference['value'];
426 useredit_update_user_preference($userpref);
429 // Check if they want to update the email.
430 if ($emailstop !== null) {
431 $otheruser = ($userid == $USER->id
) ?
$USER : core_user
::get_user($userid, '*', MUST_EXIST
);
432 core_user
::require_active_user($otheruser);
433 if (core_message_can_edit_message_profile($otheruser) && $otheruser->emailstop
!= $emailstop) {
434 $user = new stdClass();
436 $user->emailstop
= $emailstop;
437 user_update_user($user);
439 // Update the $USER if we should.
440 if ($userid == $USER->id
) {
441 $USER->emailstop
= $emailstop;
450 * Returns description of method result value
455 public static function update_user_preferences_returns() {
460 * Returns description of method parameters
462 * @return external_function_parameters
465 public static function update_users_parameters() {
467 'id' => new external_value(core_user
::get_property_type('id'), 'ID of the user'),
469 'username' => new external_value(core_user
::get_property_type('username'),
470 'Username policy is defined in Moodle security config.', VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
471 'auth' => new external_value(core_user
::get_property_type('auth'), 'Auth plugins include manual, ldap, etc',
472 VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
473 'suspended' => new external_value(core_user
::get_property_type('suspended'),
474 'Suspend user account, either false to enable user login or true to disable it', VALUE_OPTIONAL
),
475 'password' => new external_value(core_user
::get_property_type('password'),
476 'Plain text password consisting of any characters', VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
477 'firstname' => new external_value(core_user
::get_property_type('firstname'), 'The first name(s) of the user',
478 VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
479 'lastname' => new external_value(core_user
::get_property_type('lastname'), 'The family name of the user',
481 'email' => new external_value(core_user
::get_property_type('email'), 'A valid and unique email address', VALUE_OPTIONAL
,
482 '', NULL_NOT_ALLOWED
),
483 'maildisplay' => new external_value(core_user
::get_property_type('maildisplay'), 'Email display', VALUE_OPTIONAL
),
484 'city' => new external_value(core_user
::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL
),
485 'country' => new external_value(core_user
::get_property_type('country'),
486 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
487 'timezone' => new external_value(core_user
::get_property_type('timezone'),
488 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL
),
489 'description' => new external_value(core_user
::get_property_type('description'), 'User profile description, no HTML',
492 'userpicture' => new external_value(PARAM_INT
,
493 'The itemid where the new user picture has been uploaded to, 0 to delete', VALUE_OPTIONAL
),
495 'firstnamephonetic' => new external_value(core_user
::get_property_type('firstnamephonetic'),
496 'The first name(s) phonetically of the user', VALUE_OPTIONAL
),
497 'lastnamephonetic' => new external_value(core_user
::get_property_type('lastnamephonetic'),
498 'The family name phonetically of the user', VALUE_OPTIONAL
),
499 'middlename' => new external_value(core_user
::get_property_type('middlename'), 'The middle name of the user',
501 'alternatename' => new external_value(core_user
::get_property_type('alternatename'), 'The alternate name of the user',
504 'interests' => new external_value(PARAM_TEXT
, 'User interests (separated by commas)', VALUE_OPTIONAL
),
506 'url' => new external_value(core_user
::get_property_type('url'), 'User web page', VALUE_OPTIONAL
),
507 'icq' => new external_value(core_user
::get_property_type('icq'), 'ICQ number', VALUE_OPTIONAL
),
508 'skype' => new external_value(core_user
::get_property_type('skype'), 'Skype ID', VALUE_OPTIONAL
),
509 'aim' => new external_value(core_user
::get_property_type('aim'), 'AIM ID', VALUE_OPTIONAL
),
510 'yahoo' => new external_value(core_user
::get_property_type('yahoo'), 'Yahoo ID', VALUE_OPTIONAL
),
511 'msn' => new external_value(core_user
::get_property_type('msn'), 'MSN ID', VALUE_OPTIONAL
),
512 'idnumber' => new external_value(core_user
::get_property_type('idnumber'),
513 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL
),
514 'institution' => new external_value(core_user
::get_property_type('institution'), 'Institution', VALUE_OPTIONAL
),
515 'department' => new external_value(core_user
::get_property_type('department'), 'Department', VALUE_OPTIONAL
),
516 'phone1' => new external_value(core_user
::get_property_type('phone1'), 'Phone', VALUE_OPTIONAL
),
517 'phone2' => new external_value(core_user
::get_property_type('phone2'), 'Mobile phone', VALUE_OPTIONAL
),
518 'address' => new external_value(core_user
::get_property_type('address'), 'Postal address', VALUE_OPTIONAL
),
519 // Other user preferences stored in the user table.
520 'lang' => new external_value(core_user
::get_property_type('lang'), 'Language code such as "en", must exist on server',
521 VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
522 'calendartype' => new external_value(core_user
::get_property_type('calendartype'),
523 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
524 'theme' => new external_value(core_user
::get_property_type('theme'),
525 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL
),
526 'mailformat' => new external_value(core_user
::get_property_type('mailformat'),
527 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL
),
528 // Custom user profile fields.
529 'customfields' => new external_multiple_structure(
530 new external_single_structure(
532 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the custom field'),
533 'value' => new external_value(PARAM_RAW
, 'The value of the custom field')
535 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL
),
537 'preferences' => new external_multiple_structure(
538 new external_single_structure(
540 'type' => new external_value(PARAM_RAW
, 'The name of the preference'),
541 'value' => new external_value(PARAM_RAW
, 'The value of the preference')
543 ), 'User preferences', VALUE_OPTIONAL
),
545 return new external_function_parameters(
547 'users' => new external_multiple_structure(
548 new external_single_structure($userfields)
557 * @param array $users
561 public static function update_users($users) {
562 global $CFG, $DB, $USER;
563 require_once($CFG->dirroot
."/user/lib.php");
564 require_once($CFG->dirroot
."/user/profile/lib.php"); // Required for customfields related function.
565 require_once($CFG->dirroot
.'/user/editlib.php');
567 // Ensure the current user is allowed to run this function.
568 $context = context_system
::instance();
569 require_capability('moodle/user:update', $context);
570 self
::validate_context($context);
572 $params = self
::validate_parameters(self
::update_users_parameters(), array('users' => $users));
574 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
577 'accepted_types' => 'web_image');
579 $transaction = $DB->start_delegated_transaction();
581 foreach ($params['users'] as $user) {
582 // First check the user exists.
583 if (!$existinguser = core_user
::get_user($user['id'])) {
586 // Check if we are trying to update an admin.
587 if ($existinguser->id
!= $USER->id
and is_siteadmin($existinguser) and !is_siteadmin($USER)) {
590 // Other checks (deleted, remote or guest users).
591 if ($existinguser->deleted
or is_mnet_remote_user($existinguser) or isguestuser($existinguser->id
)) {
594 // Check duplicated emails.
595 if (isset($user['email']) && $user['email'] !== $existinguser->email
) {
596 if (!validate_email($user['email'])) {
598 } else if (empty($CFG->allowaccountssameemail
)) {
599 // Make a case-insensitive query for the given email address and make sure to exclude the user being updated.
600 $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
602 'email' => $user['email'],
603 'mnethostid' => $CFG->mnet_localhost_id
,
604 'userid' => $user['id']
606 // Skip if there are other user(s) that already have the same email.
607 if ($DB->record_exists_select('user', $select, $params)) {
613 user_update_user($user, true, false);
615 $userobject = (object)$user;
617 // Update user picture if it was specified for this user.
618 if (empty($CFG->disableuserimages
) && isset($user['userpicture'])) {
619 $userobject->deletepicture
= null;
621 if ($user['userpicture'] == 0) {
622 $userobject->deletepicture
= true;
624 $userobject->imagefile
= $user['userpicture'];
627 core_user
::update_picture($userobject, $filemanageroptions);
630 // Update user interests.
631 if (!empty($user['interests'])) {
632 $trimmedinterests = array_map('trim', explode(',', $user['interests']));
633 $interests = array_filter($trimmedinterests, function($value) {
634 return !empty($value);
636 useredit_update_interests($userobject, $interests);
639 // Update user custom fields.
640 if (!empty($user['customfields'])) {
642 foreach ($user['customfields'] as $customfield) {
643 // Profile_save_data() saves profile file it's expecting a user with the correct id,
644 // and custom field to be named profile_field_"shortname".
645 $user["profile_field_".$customfield['type']] = $customfield['value'];
647 profile_save_data((object) $user);
651 \core\event\user_updated
::create_from_userid($user['id'])->trigger();
654 if (!empty($user['preferences'])) {
655 $userpref = clone($existinguser);
656 foreach ($user['preferences'] as $preference) {
657 $userpref->{'preference_'.$preference['type']} = $preference['value'];
659 useredit_update_user_preference($userpref);
661 if (isset($user['suspended']) and $user['suspended']) {
662 \core\session\manager
::kill_user_sessions($user['id']);
666 $transaction->allow_commit();
672 * Returns description of method result value
677 public static function update_users_returns() {
682 * Returns description of method parameters
684 * @return external_function_parameters
687 public static function get_users_by_field_parameters() {
688 return new external_function_parameters(
690 'field' => new external_value(PARAM_ALPHA
, 'the search field can be
691 \'id\' or \'idnumber\' or \'username\' or \'email\''),
692 'values' => new external_multiple_structure(
693 new external_value(PARAM_RAW
, 'the value to match'))
699 * Get user information for a unique field.
701 * @throws coding_exception
702 * @throws invalid_parameter_exception
703 * @param string $field
704 * @param array $values
705 * @return array An array of arrays containg user profiles.
708 public static function get_users_by_field($field, $values) {
709 global $CFG, $USER, $DB;
710 require_once($CFG->dirroot
. "/user/lib.php");
712 $params = self
::validate_parameters(self
::get_users_by_field_parameters(),
713 array('field' => $field, 'values' => $values));
715 // This array will keep all the users that are allowed to be searched,
716 // according to the current user's privileges.
717 $cleanedvalues = array();
721 $paramtype = core_user
::get_property_type('id');
724 $paramtype = core_user
::get_property_type('idnumber');
727 $paramtype = core_user
::get_property_type('username');
730 $paramtype = core_user
::get_property_type('email');
733 throw new coding_exception('invalid field parameter',
734 'The search field \'' . $field . '\' is not supported, look at the web service documentation');
738 foreach ($values as $value) {
739 $cleanedvalue = clean_param($value, $paramtype);
740 if ( $value != $cleanedvalue) {
741 throw new invalid_parameter_exception('The field \'' . $field .
742 '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')');
744 $cleanedvalues[] = $cleanedvalue;
747 // Retrieve the users.
748 $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id');
750 $context = context_system
::instance();
751 self
::validate_context($context);
753 // Finally retrieve each users information.
754 $returnedusers = array();
755 foreach ($users as $user) {
756 $userdetails = user_get_user_details_courses($user);
758 // Return the user only if the searched field is returned.
759 // Otherwise it means that the $USER was not allowed to search the returned user.
760 if (!empty($userdetails) and !empty($userdetails[$field])) {
761 $returnedusers[] = $userdetails;
765 return $returnedusers;
769 * Returns description of method result value
771 * @return external_multiple_structure
774 public static function get_users_by_field_returns() {
775 return new external_multiple_structure(self
::user_description());
780 * Returns description of get_users() parameters.
782 * @return external_function_parameters
785 public static function get_users_parameters() {
786 return new external_function_parameters(
788 'criteria' => new external_multiple_structure(
789 new external_single_structure(
791 'key' => new external_value(PARAM_ALPHA
, 'the user column to search, expected keys (value format) are:
792 "id" (int) matching user id,
793 "lastname" (string) user last name (Note: you can use % for searching but it may be considerably slower!),
794 "firstname" (string) user first name (Note: you can use % for searching but it may be considerably slower!),
795 "idnumber" (string) matching user idnumber,
796 "username" (string) matching user username,
797 "email" (string) user email (Note: you can use % for searching but it may be considerably slower!),
798 "auth" (string) matching user auth plugin'),
799 'value' => new external_value(PARAM_RAW
, 'the value to search')
801 ), 'the key/value pairs to be considered in user search. Values can not be empty.
802 Specify different keys only once (fullname => \'user1\', auth => \'manual\', ...) -
803 key occurences are forbidden.
804 The search is executed with AND operator on the criterias. Invalid criterias (keys) are ignored,
805 the search is still executed on the valid criterias.
806 You can search without criteria, but the function is not designed for it.
807 It could very slow or timeout. The function is designed to search some specific users.'
814 * Retrieve matching user.
816 * @throws moodle_exception
817 * @param array $criteria the allowed array keys are id/lastname/firstname/idnumber/username/email/auth.
818 * @return array An array of arrays containing user profiles.
821 public static function get_users($criteria = array()) {
822 global $CFG, $USER, $DB;
824 require_once($CFG->dirroot
. "/user/lib.php");
826 $params = self
::validate_parameters(self
::get_users_parameters(),
827 array('criteria' => $criteria));
829 // Validate the criteria and retrieve the users.
832 $sqlparams = array();
835 // Do not retrieve deleted users.
836 $sql = ' deleted = 0';
838 foreach ($params['criteria'] as $criteriaindex => $criteria) {
840 // Check that the criteria has never been used.
841 if (array_key_exists($criteria['key'], $usedkeys)) {
842 throw new moodle_exception('keyalreadyset', '', '', null, 'The key ' . $criteria['key'] . ' can only be sent once');
844 $usedkeys[$criteria['key']] = true;
847 $invalidcriteria = false;
848 // Clean the parameters.
849 $paramtype = PARAM_RAW
;
850 switch ($criteria['key']) {
852 $paramtype = core_user
::get_property_type('id');
855 $paramtype = core_user
::get_property_type('idnumber');
858 $paramtype = core_user
::get_property_type('username');
861 // We use PARAM_RAW to allow searches with %.
862 $paramtype = core_user
::get_property_type('email');
865 $paramtype = core_user
::get_property_type('auth');
869 $paramtype = core_user
::get_property_type('firstname');
872 // Send back a warning that this search key is not supported in this version.
873 // This warning will make the function extandable without breaking clients.
875 'item' => $criteria['key'],
876 'warningcode' => 'invalidfieldparameter',
878 'The search key \'' . $criteria['key'] . '\' is not supported, look at the web service documentation'
880 // Do not add this invalid criteria to the created SQL request.
881 $invalidcriteria = true;
882 unset($params['criteria'][$criteriaindex]);
886 if (!$invalidcriteria) {
887 $cleanedvalue = clean_param($criteria['value'], $paramtype);
892 switch ($criteria['key']) {
897 $sql .= $criteria['key'] . ' = :' . $criteria['key'];
898 $sqlparams[$criteria['key']] = $cleanedvalue;
903 $sql .= $DB->sql_like($criteria['key'], ':' . $criteria['key'], false);
904 $sqlparams[$criteria['key']] = $cleanedvalue;
912 $users = $DB->get_records_select('user', $sql, $sqlparams, 'id ASC');
914 // Finally retrieve each users information.
915 $returnedusers = array();
916 foreach ($users as $user) {
917 $userdetails = user_get_user_details_courses($user);
919 // Return the user only if all the searched fields are returned.
920 // Otherwise it means that the $USER was not allowed to search the returned user.
921 if (!empty($userdetails)) {
924 foreach ($params['criteria'] as $criteria) {
925 if (empty($userdetails[$criteria['key']])) {
931 $returnedusers[] = $userdetails;
936 return array('users' => $returnedusers, 'warnings' => $warnings);
940 * Returns description of get_users result value.
942 * @return external_description
945 public static function get_users_returns() {
946 return new external_single_structure(
947 array('users' => new external_multiple_structure(
948 self
::user_description()
950 'warnings' => new external_warnings('always set to \'key\'', 'faulty key name')
956 * Returns description of method parameters
958 * @return external_function_parameters
961 public static function get_course_user_profiles_parameters() {
962 return new external_function_parameters(
964 'userlist' => new external_multiple_structure(
965 new external_single_structure(
967 'userid' => new external_value(core_user
::get_property_type('id'), 'userid'),
968 'courseid' => new external_value(PARAM_INT
, 'courseid'),
977 * Get course participant's details
979 * @param array $userlist array of user ids and according course ids
980 * @return array An array of arrays describing course participants
983 public static function get_course_user_profiles($userlist) {
984 global $CFG, $USER, $DB;
985 require_once($CFG->dirroot
. "/user/lib.php");
986 $params = self
::validate_parameters(self
::get_course_user_profiles_parameters(), array('userlist' => $userlist));
989 $courseids = array();
990 foreach ($params['userlist'] as $value) {
991 $userids[] = $value['userid'];
992 $courseids[$value['userid']] = $value['courseid'];
995 // Cache all courses.
997 list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids), SQL_PARAMS_NAMED
);
998 $cselect = ', ' . context_helper
::get_preload_record_columns_sql('ctx');
999 $cjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
1000 $params['contextlevel'] = CONTEXT_COURSE
;
1001 $coursesql = "SELECT c.* $cselect
1002 FROM {course} c $cjoin
1003 WHERE c.id $sqlcourseids";
1004 $rs = $DB->get_recordset_sql($coursesql, $params);
1005 foreach ($rs as $course) {
1006 // Adding course contexts to cache.
1007 context_helper
::preload_from_record($course);
1009 $courses[$course->id
] = $course;
1013 list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED
);
1014 $uselect = ', ' . context_helper
::get_preload_record_columns_sql('ctx');
1015 $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
1016 $params['contextlevel'] = CONTEXT_USER
;
1017 $usersql = "SELECT u.* $uselect
1018 FROM {user} u $ujoin
1019 WHERE u.id $sqluserids";
1020 $users = $DB->get_recordset_sql($usersql, $params);
1022 foreach ($users as $user) {
1023 if (!empty($user->deleted
)) {
1026 context_helper
::preload_from_record($user);
1027 $course = $courses[$courseids[$user->id
]];
1028 $context = context_course
::instance($courseids[$user->id
], IGNORE_MISSING
);
1029 self
::validate_context($context);
1030 if ($userarray = user_get_user_details($user, $course)) {
1031 $result[] = $userarray;
1041 * Returns description of method result value
1043 * @return external_description
1046 public static function get_course_user_profiles_returns() {
1047 $additionalfields = array(
1048 'groups' => new external_multiple_structure(
1049 new external_single_structure(
1051 'id' => new external_value(PARAM_INT
, 'group id'),
1052 'name' => new external_value(PARAM_RAW
, 'group name'),
1053 'description' => new external_value(PARAM_RAW
, 'group description'),
1054 'descriptionformat' => new external_format_value('description'),
1056 ), 'user groups', VALUE_OPTIONAL
),
1057 'roles' => new external_multiple_structure(
1058 new external_single_structure(
1060 'roleid' => new external_value(PARAM_INT
, 'role id'),
1061 'name' => new external_value(PARAM_RAW
, 'role name'),
1062 'shortname' => new external_value(PARAM_ALPHANUMEXT
, 'role shortname'),
1063 'sortorder' => new external_value(PARAM_INT
, 'role sortorder')
1065 ), 'user roles', VALUE_OPTIONAL
),
1066 'enrolledcourses' => new external_multiple_structure(
1067 new external_single_structure(
1069 'id' => new external_value(PARAM_INT
, 'Id of the course'),
1070 'fullname' => new external_value(PARAM_RAW
, 'Fullname of the course'),
1071 'shortname' => new external_value(PARAM_RAW
, 'Shortname of the course')
1073 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL
)
1076 return new external_multiple_structure(self
::user_description($additionalfields));
1080 * Create user return value description.
1082 * @param array $additionalfields some additional field
1083 * @return single_structure_description
1085 public static function user_description($additionalfields = array()) {
1086 $userfields = array(
1087 'id' => new external_value(core_user
::get_property_type('id'), 'ID of the user'),
1088 'username' => new external_value(core_user
::get_property_type('username'), 'The username', VALUE_OPTIONAL
),
1089 'firstname' => new external_value(core_user
::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL
),
1090 'lastname' => new external_value(core_user
::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL
),
1091 'fullname' => new external_value(core_user
::get_property_type('firstname'), 'The fullname of the user'),
1092 'email' => new external_value(core_user
::get_property_type('email'), 'An email address - allow email as root@localhost', VALUE_OPTIONAL
),
1093 'address' => new external_value(core_user
::get_property_type('address'), 'Postal address', VALUE_OPTIONAL
),
1094 'phone1' => new external_value(core_user
::get_property_type('phone1'), 'Phone 1', VALUE_OPTIONAL
),
1095 'phone2' => new external_value(core_user
::get_property_type('phone2'), 'Phone 2', VALUE_OPTIONAL
),
1096 'icq' => new external_value(core_user
::get_property_type('icq'), 'icq number', VALUE_OPTIONAL
),
1097 'skype' => new external_value(core_user
::get_property_type('skype'), 'skype id', VALUE_OPTIONAL
),
1098 'yahoo' => new external_value(core_user
::get_property_type('yahoo'), 'yahoo id', VALUE_OPTIONAL
),
1099 'aim' => new external_value(core_user
::get_property_type('aim'), 'aim id', VALUE_OPTIONAL
),
1100 'msn' => new external_value(core_user
::get_property_type('msn'), 'msn number', VALUE_OPTIONAL
),
1101 'department' => new external_value(core_user
::get_property_type('department'), 'department', VALUE_OPTIONAL
),
1102 'institution' => new external_value(core_user
::get_property_type('institution'), 'institution', VALUE_OPTIONAL
),
1103 'idnumber' => new external_value(core_user
::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL
),
1104 'interests' => new external_value(PARAM_TEXT
, 'user interests (separated by commas)', VALUE_OPTIONAL
),
1105 'firstaccess' => new external_value(core_user
::get_property_type('firstaccess'), 'first access to the site (0 if never)', VALUE_OPTIONAL
),
1106 'lastaccess' => new external_value(core_user
::get_property_type('lastaccess'), 'last access to the site (0 if never)', VALUE_OPTIONAL
),
1107 'auth' => new external_value(core_user
::get_property_type('auth'), 'Auth plugins include manual, ldap, etc', VALUE_OPTIONAL
),
1108 'suspended' => new external_value(core_user
::get_property_type('suspended'), 'Suspend user account, either false to enable user login or true to disable it', VALUE_OPTIONAL
),
1109 'confirmed' => new external_value(core_user
::get_property_type('confirmed'), 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL
),
1110 'lang' => new external_value(core_user
::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_OPTIONAL
),
1111 'calendartype' => new external_value(core_user
::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL
),
1112 'theme' => new external_value(core_user
::get_property_type('theme'), 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL
),
1113 'timezone' => new external_value(core_user
::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL
),
1114 'mailformat' => new external_value(core_user
::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL
),
1115 'description' => new external_value(core_user
::get_property_type('description'), 'User profile description', VALUE_OPTIONAL
),
1116 'descriptionformat' => new external_format_value(core_user
::get_property_type('descriptionformat'), VALUE_OPTIONAL
),
1117 'city' => new external_value(core_user
::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL
),
1118 'url' => new external_value(core_user
::get_property_type('url'), 'URL of the user', VALUE_OPTIONAL
),
1119 'country' => new external_value(core_user
::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
1120 'profileimageurlsmall' => new external_value(PARAM_URL
, 'User image profile URL - small version'),
1121 'profileimageurl' => new external_value(PARAM_URL
, 'User image profile URL - big version'),
1122 'customfields' => new external_multiple_structure(
1123 new external_single_structure(
1125 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The type of the custom field - text field, checkbox...'),
1126 'value' => new external_value(PARAM_RAW
, 'The value of the custom field'),
1127 'name' => new external_value(PARAM_RAW
, 'The name of the custom field'),
1128 'shortname' => new external_value(PARAM_RAW
, 'The shortname of the custom field - to be able to build the field class in the code'),
1130 ), 'User custom fields (also known as user profile fields)', VALUE_OPTIONAL
),
1131 'preferences' => new external_multiple_structure(
1132 new external_single_structure(
1134 'name' => new external_value(PARAM_RAW
, 'The name of the preferences'),
1135 'value' => new external_value(PARAM_RAW
, 'The value of the preference'),
1137 ), 'Users preferences', VALUE_OPTIONAL
)
1139 if (!empty($additionalfields)) {
1140 $userfields = array_merge($userfields, $additionalfields);
1142 return new external_single_structure($userfields);
1146 * Returns description of method parameters
1148 * @return external_function_parameters
1151 public static function add_user_private_files_parameters() {
1152 return new external_function_parameters(
1154 'draftid' => new external_value(PARAM_INT
, 'draft area id')
1160 * Copy files from a draft area to users private files area.
1162 * @throws invalid_parameter_exception
1163 * @param int $draftid Id of a draft area containing files.
1164 * @return array An array of warnings
1167 public static function add_user_private_files($draftid) {
1169 require_once($CFG->libdir
. "/filelib.php");
1171 $params = self
::validate_parameters(self
::add_user_private_files_parameters(), array('draftid' => $draftid));
1173 if (isguestuser()) {
1174 throw new invalid_parameter_exception('Guest users cannot upload files');
1177 $context = context_user
::instance($USER->id
);
1178 require_capability('moodle/user:manageownfiles', $context);
1180 $maxbytes = $CFG->userquota
;
1181 $maxareabytes = $CFG->userquota
;
1182 if (has_capability('moodle/user:ignoreuserquota', $context)) {
1183 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS
;
1184 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED
;
1187 $options = array('subdirs' => 1,
1188 'maxbytes' => $maxbytes,
1190 'areamaxbytes' => $maxareabytes);
1192 file_merge_files_from_draft_area_into_filearea($draftid, $context->id
, 'user', 'private', 0, $options);
1198 * Returns description of method result value
1200 * @return external_description
1203 public static function add_user_private_files_returns() {
1208 * Returns description of method parameters.
1210 * @return external_function_parameters
1213 public static function add_user_device_parameters() {
1214 return new external_function_parameters(
1216 'appid' => new external_value(PARAM_NOTAGS
, 'the app id, usually something like com.moodle.moodlemobile'),
1217 'name' => new external_value(PARAM_NOTAGS
, 'the device name, \'occam\' or \'iPhone\' etc.'),
1218 'model' => new external_value(PARAM_NOTAGS
, 'the device model \'Nexus4\' or \'iPad1,1\' etc.'),
1219 'platform' => new external_value(PARAM_NOTAGS
, 'the device platform \'iOS\' or \'Android\' etc.'),
1220 'version' => new external_value(PARAM_NOTAGS
, 'the device version \'6.1.2\' or \'4.2.2\' etc.'),
1221 'pushid' => new external_value(PARAM_RAW
, 'the device PUSH token/key/identifier/registration id'),
1222 'uuid' => new external_value(PARAM_RAW
, 'the device UUID')
1228 * Add a user device in Moodle database (for PUSH notifications usually).
1230 * @throws moodle_exception
1231 * @param string $appid The app id, usually something like com.moodle.moodlemobile.
1232 * @param string $name The device name, occam or iPhone etc.
1233 * @param string $model The device model Nexus4 or iPad1.1 etc.
1234 * @param string $platform The device platform iOs or Android etc.
1235 * @param string $version The device version 6.1.2 or 4.2.2 etc.
1236 * @param string $pushid The device PUSH token/key/identifier/registration id.
1237 * @param string $uuid The device UUID.
1238 * @return array List of possible warnings.
1241 public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) {
1242 global $CFG, $USER, $DB;
1243 require_once($CFG->dirroot
. "/user/lib.php");
1245 $params = self
::validate_parameters(self
::add_user_device_parameters(),
1246 array('appid' => $appid,
1249 'platform' => $platform,
1250 'version' => $version,
1251 'pushid' => $pushid,
1255 $warnings = array();
1257 // Prevent duplicate keys for users.
1258 if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'userid' => $USER->id
))) {
1259 $warnings['warning'][] = array(
1260 'item' => $params['pushid'],
1261 'warningcode' => 'existingkeyforthisuser',
1262 'message' => 'This key is already stored for this user'
1267 // Notice that we can have multiple devices because previously it was allowed to have repeated ones.
1268 // Since we don't have a clear way to decide which one is the more appropiate, we update all.
1269 if ($userdevices = $DB->get_records('user_devices', array('uuid' => $params['uuid'],
1270 'appid' => $params['appid'], 'userid' => $USER->id
))) {
1272 foreach ($userdevices as $userdevice) {
1273 $userdevice->version
= $params['version']; // Maybe the user upgraded the device.
1274 $userdevice->pushid
= $params['pushid'];
1275 $userdevice->timemodified
= time();
1276 $DB->update_record('user_devices', $userdevice);
1280 $userdevice = new stdclass
;
1281 $userdevice->userid
= $USER->id
;
1282 $userdevice->appid
= $params['appid'];
1283 $userdevice->name
= $params['name'];
1284 $userdevice->model
= $params['model'];
1285 $userdevice->platform
= $params['platform'];
1286 $userdevice->version
= $params['version'];
1287 $userdevice->pushid
= $params['pushid'];
1288 $userdevice->uuid
= $params['uuid'];
1289 $userdevice->timecreated
= time();
1290 $userdevice->timemodified
= $userdevice->timecreated
;
1292 if (!$DB->insert_record('user_devices', $userdevice)) {
1293 throw new moodle_exception("There was a problem saving in the database the device with key: " . $params['pushid']);
1301 * Returns description of method result value.
1303 * @return external_multiple_structure
1306 public static function add_user_device_returns() {
1307 return new external_multiple_structure(
1308 new external_warnings()
1313 * Returns description of method parameters.
1315 * @return external_function_parameters
1318 public static function remove_user_device_parameters() {
1319 return new external_function_parameters(
1321 'uuid' => new external_value(PARAM_RAW
, 'the device UUID'),
1322 'appid' => new external_value(PARAM_NOTAGS
,
1323 'the app id, if empty devices matching the UUID for the user will be removed',
1330 * Remove a user device from the Moodle database (for PUSH notifications usually).
1332 * @param string $uuid The device UUID.
1333 * @param string $appid The app id, opitonal parameter. If empty all the devices fmatching the UUID or the user will be removed.
1334 * @return array List of possible warnings and removal status.
1337 public static function remove_user_device($uuid, $appid = "") {
1339 require_once($CFG->dirroot
. "/user/lib.php");
1341 $params = self
::validate_parameters(self
::remove_user_device_parameters(), array('uuid' => $uuid, 'appid' => $appid));
1343 $context = context_system
::instance();
1344 self
::validate_context($context);
1346 // Warnings array, it can be empty at the end but is mandatory.
1347 $warnings = array();
1349 $removed = user_remove_user_device($params['uuid'], $params['appid']);
1352 $warnings[] = array(
1353 'item' => $params['uuid'],
1354 'warningcode' => 'devicedoesnotexist',
1355 'message' => 'The device doesn\'t exists in the database'
1360 'removed' => $removed,
1361 'warnings' => $warnings
1368 * Returns description of method result value.
1370 * @return external_multiple_structure
1373 public static function remove_user_device_returns() {
1374 return new external_single_structure(
1376 'removed' => new external_value(PARAM_BOOL
, 'True if removed, false if not removed because it doesn\'t exists'),
1377 'warnings' => new external_warnings(),
1383 * Returns description of method parameters
1385 * @return external_function_parameters
1388 public static function view_user_list_parameters() {
1389 return new external_function_parameters(
1391 'courseid' => new external_value(PARAM_INT
, 'id of the course, 0 for site')
1397 * Trigger the user_list_viewed event.
1399 * @param int $courseid id of course
1400 * @return array of warnings and status result
1402 * @throws moodle_exception
1404 public static function view_user_list($courseid) {
1406 require_once($CFG->dirroot
. "/user/lib.php");
1407 require_once($CFG->dirroot
. '/course/lib.php');
1409 $params = self
::validate_parameters(self
::view_user_list_parameters(),
1411 'courseid' => $courseid
1414 $warnings = array();
1416 if (empty($params['courseid'])) {
1417 $params['courseid'] = SITEID
;
1420 $course = get_course($params['courseid']);
1422 if ($course->id
== SITEID
) {
1423 $context = context_system
::instance();
1425 $context = context_course
::instance($course->id
);
1427 self
::validate_context($context);
1429 course_require_view_participants($context);
1431 user_list_view($course, $context);
1434 $result['status'] = true;
1435 $result['warnings'] = $warnings;
1440 * Returns description of method result value
1442 * @return external_description
1445 public static function view_user_list_returns() {
1446 return new external_single_structure(
1448 'status' => new external_value(PARAM_BOOL
, 'status: true if success'),
1449 'warnings' => new external_warnings()
1455 * Returns description of method parameters
1457 * @return external_function_parameters
1460 public static function view_user_profile_parameters() {
1461 return new external_function_parameters(
1463 'userid' => new external_value(PARAM_INT
, 'id of the user, 0 for current user', VALUE_REQUIRED
),
1464 'courseid' => new external_value(PARAM_INT
, 'id of the course, default site course', VALUE_DEFAULT
, 0)
1470 * Trigger the user profile viewed event.
1472 * @param int $userid id of user
1473 * @param int $courseid id of course
1474 * @return array of warnings and status result
1476 * @throws moodle_exception
1478 public static function view_user_profile($userid, $courseid = 0) {
1480 require_once($CFG->dirroot
. "/user/profile/lib.php");
1482 $params = self
::validate_parameters(self
::view_user_profile_parameters(),
1484 'userid' => $userid,
1485 'courseid' => $courseid
1488 $warnings = array();
1490 if (empty($params['userid'])) {
1491 $params['userid'] = $USER->id
;
1494 if (empty($params['courseid'])) {
1495 $params['courseid'] = SITEID
;
1498 $course = get_course($params['courseid']);
1499 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1500 core_user
::require_active_user($user);
1502 if ($course->id
== SITEID
) {
1503 $coursecontext = context_system
::instance();;
1505 $coursecontext = context_course
::instance($course->id
);
1507 self
::validate_context($coursecontext);
1509 $currentuser = $USER->id
== $user->id
;
1510 $usercontext = context_user
::instance($user->id
);
1512 if (!$currentuser and
1513 !has_capability('moodle/user:viewdetails', $coursecontext) and
1514 !has_capability('moodle/user:viewdetails', $usercontext)) {
1515 throw new moodle_exception('cannotviewprofile');
1518 // Case like user/profile.php.
1519 if ($course->id
== SITEID
) {
1520 profile_view($user, $usercontext);
1522 // Case like user/view.php.
1523 if (!$currentuser and !can_access_course($course, $user, '', true)) {
1524 throw new moodle_exception('notenrolledprofile');
1527 profile_view($user, $coursecontext, $course);
1531 $result['status'] = true;
1532 $result['warnings'] = $warnings;
1537 * Returns description of method result value
1539 * @return external_description
1542 public static function view_user_profile_returns() {
1543 return new external_single_structure(
1545 'status' => new external_value(PARAM_BOOL
, 'status: true if success'),
1546 'warnings' => new external_warnings()
1552 * Returns description of method parameters
1554 * @return external_function_parameters
1557 public static function get_user_preferences_parameters() {
1558 return new external_function_parameters(
1560 'name' => new external_value(PARAM_RAW
, 'preference name, empty for all', VALUE_DEFAULT
, ''),
1561 'userid' => new external_value(PARAM_INT
, 'id of the user, default to current user', VALUE_DEFAULT
, 0)
1567 * Return user preferences.
1569 * @param string $name preference name, empty for all
1570 * @param int $userid id of the user, 0 for current user
1571 * @return array of warnings and preferences
1573 * @throws moodle_exception
1575 public static function get_user_preferences($name = '', $userid = 0) {
1578 $params = self
::validate_parameters(self
::get_user_preferences_parameters(),
1583 $preferences = array();
1584 $warnings = array();
1586 $context = context_system
::instance();
1587 self
::validate_context($context);
1589 if (empty($params['name'])) {
1592 if (empty($params['userid'])) {
1595 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1596 core_user
::require_active_user($user);
1597 if ($user->id
!= $USER->id
) {
1598 // Only admins can retrieve other users preferences.
1599 require_capability('moodle/site:config', $context);
1603 $userpreferences = get_user_preferences($name, null, $user);
1604 // Check if we received just one preference.
1605 if (!is_array($userpreferences)) {
1606 $userpreferences = array($name => $userpreferences);
1609 foreach ($userpreferences as $name => $value) {
1610 $preferences[] = array(
1617 $result['preferences'] = $preferences;
1618 $result['warnings'] = $warnings;
1623 * Returns description of method result value
1625 * @return external_description
1628 public static function get_user_preferences_returns() {
1629 return new external_single_structure(
1631 'preferences' => new external_multiple_structure(
1632 new external_single_structure(
1634 'name' => new external_value(PARAM_RAW
, 'The name of the preference'),
1635 'value' => new external_value(PARAM_RAW
, 'The value of the preference'),
1638 'User custom fields (also known as user profile fields)'
1640 'warnings' => new external_warnings()
1646 * Returns description of method parameters
1648 * @return external_function_parameters
1651 public static function update_picture_parameters() {
1652 return new external_function_parameters(
1654 'draftitemid' => new external_value(PARAM_INT
, 'Id of the user draft file to use as image'),
1655 'delete' => new external_value(PARAM_BOOL
, 'If we should delete the user picture', VALUE_DEFAULT
, false),
1656 'userid' => new external_value(PARAM_INT
, 'Id of the user, 0 for current user', VALUE_DEFAULT
, 0)
1662 * Update or delete the user picture in the site
1664 * @param int $draftitemid id of the user draft file to use as image
1665 * @param bool $delete if we should delete the user picture
1666 * @param int $userid id of the user, 0 for current user
1667 * @return array warnings and success status
1669 * @throws moodle_exception
1671 public static function update_picture($draftitemid, $delete = false, $userid = 0) {
1672 global $CFG, $USER, $PAGE;
1674 $params = self
::validate_parameters(
1675 self
::update_picture_parameters(),
1677 'draftitemid' => $draftitemid,
1678 'delete' => $delete,
1683 $context = context_system
::instance();
1684 self
::validate_context($context);
1686 if (!empty($CFG->disableuserimages
)) {
1687 throw new moodle_exception('userimagesdisabled', 'admin');
1690 if (empty($params['userid']) or $params['userid'] == $USER->id
) {
1692 require_capability('moodle/user:editownprofile', $context);
1694 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1695 core_user
::require_active_user($user);
1696 $personalcontext = context_user
::instance($user->id
);
1698 require_capability('moodle/user:editprofile', $personalcontext);
1699 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
1700 throw new moodle_exception('useradmineditadmin');
1704 // Load the appropriate auth plugin.
1705 $userauth = get_auth_plugin($user->auth
);
1706 if (is_mnet_remote_user($user) or !$userauth->can_edit_profile() or $userauth->edit_profile_url()) {
1707 throw new moodle_exception('noprofileedit', 'auth');
1710 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
, 'subdirs' => 0, 'maxfiles' => 1, 'accepted_types' => 'web_image');
1711 $user->deletepicture
= $params['delete'];
1712 $user->imagefile
= $params['draftitemid'];
1713 $success = core_user
::update_picture($user, $filemanageroptions);
1716 'success' => $success,
1717 'warnings' => array(),
1720 $userpicture = new user_picture(core_user
::get_user($user->id
));
1721 $userpicture->size
= 1; // Size f1.
1722 $result['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
1728 * Returns description of method result value
1730 * @return external_description
1733 public static function update_picture_returns() {
1734 return new external_single_structure(
1736 'success' => new external_value(PARAM_BOOL
, 'True if the image was updated, false otherwise.'),
1737 'profileimageurl' => new external_value(PARAM_URL
, 'New profile user image url', VALUE_OPTIONAL
),
1738 'warnings' => new external_warnings()
1744 * Returns description of method parameters
1746 * @return external_function_parameters
1749 public static function set_user_preferences_parameters() {
1750 return new external_function_parameters(
1752 'preferences' => new external_multiple_structure(
1753 new external_single_structure(
1755 'name' => new external_value(PARAM_RAW
, 'The name of the preference'),
1756 'value' => new external_value(PARAM_RAW
, 'The value of the preference'),
1757 'userid' => new external_value(PARAM_INT
, 'Id of the user to set the preference'),
1766 * Set user preferences.
1768 * @param array $preferences list of preferences including name, value and userid
1769 * @return array of warnings and preferences saved
1771 * @throws moodle_exception
1773 public static function set_user_preferences($preferences) {
1776 $params = self
::validate_parameters(self
::set_user_preferences_parameters(), array('preferences' => $preferences));
1777 $warnings = array();
1780 $context = context_system
::instance();
1781 self
::validate_context($context);
1783 $userscache = array();
1784 foreach ($params['preferences'] as $pref) {
1785 // Check to which user set the preference.
1786 if (!empty($userscache[$pref['userid']])) {
1787 $user = $userscache[$pref['userid']];
1790 $user = core_user
::get_user($pref['userid'], '*', MUST_EXIST
);
1791 core_user
::require_active_user($user);
1792 $userscache[$pref['userid']] = $user;
1793 } catch (Exception
$e) {
1794 $warnings[] = array(
1796 'itemid' => $pref['userid'],
1797 'warningcode' => 'invaliduser',
1798 'message' => $e->getMessage()
1805 if (core_user
::can_edit_preference($pref['name'], $user)) {
1806 $value = core_user
::clean_preference($pref['value'], $pref['name']);
1807 set_user_preference($pref['name'], $value, $user->id
);
1809 'name' => $pref['name'],
1810 'userid' => $user->id
,
1813 $warnings[] = array(
1815 'itemid' => $user->id
,
1816 'warningcode' => 'nopermission',
1817 'message' => 'You are not allowed to change the preference '.s($pref['name']).' for user '.$user->id
1820 } catch (Exception
$e) {
1821 $warnings[] = array(
1823 'itemid' => $user->id
,
1824 'warningcode' => 'errorsavingpreference',
1825 'message' => $e->getMessage()
1831 $result['saved'] = $saved;
1832 $result['warnings'] = $warnings;
1837 * Returns description of method result value
1839 * @return external_description
1842 public static function set_user_preferences_returns() {
1843 return new external_single_structure(
1845 'saved' => new external_multiple_structure(
1846 new external_single_structure(
1848 'name' => new external_value(PARAM_RAW
, 'The name of the preference'),
1849 'userid' => new external_value(PARAM_INT
, 'The user the preference was set for'),
1851 ), 'Preferences saved'
1853 'warnings' => new external_warnings()
1859 * Returns description of method parameters.
1861 * @return external_function_parameters
1864 public static function agree_site_policy_parameters() {
1865 return new external_function_parameters(array());
1869 * Agree the site policy for the current user.
1871 * @return array of warnings and status result
1873 * @throws moodle_exception
1875 public static function agree_site_policy() {
1876 global $CFG, $DB, $USER;
1878 $warnings = array();
1880 $context = context_system
::instance();
1882 // We expect an exception here since the user didn't agree the site policy yet.
1883 self
::validate_context($context);
1884 } catch (Exception
$e) {
1885 // We are expecting only a sitepolicynotagreed exception.
1886 if (!($e instanceof moodle_exception
) or $e->errorcode
!= 'sitepolicynotagreed') {
1887 // In case we receive a different exception, throw it.
1892 $manager = new \core_privacy\local\sitepolicy\
manager();
1893 if (!empty($USER->policyagreed
)) {
1895 $warnings[] = array(
1897 'itemid' => $USER->id
,
1898 'warningcode' => 'alreadyagreed',
1899 'message' => 'The user already agreed the site policy.'
1901 } else if (!$manager->is_defined()) {
1903 $warnings[] = array(
1905 'itemid' => $USER->id
,
1906 'warningcode' => 'nositepolicy',
1907 'message' => 'The site does not have a site policy configured.'
1910 $status = $manager->accept();
1914 $result['status'] = $status;
1915 $result['warnings'] = $warnings;
1920 * Returns description of method result value.
1922 * @return external_description
1925 public static function agree_site_policy_returns() {
1926 return new external_single_structure(
1928 'status' => new external_value(PARAM_BOOL
, 'Status: true only if we set the policyagreed to 1 for the user'),
1929 'warnings' => new external_warnings()
1935 * Returns description of method parameters.
1937 * @return external_function_parameters
1940 public static function get_private_files_info_parameters() {
1941 return new external_function_parameters(
1943 'userid' => new external_value(PARAM_INT
, 'Id of the user, default to current user.', VALUE_DEFAULT
, 0)
1949 * Returns general information about files in the user private files area.
1951 * @param int $userid Id of the user, default to current user.
1952 * @return array of warnings and file area information
1954 * @throws moodle_exception
1956 public static function get_private_files_info($userid = 0) {
1958 require_once($CFG->libdir
. '/filelib.php');
1960 $params = self
::validate_parameters(self
::get_private_files_info_parameters(), array('userid' => $userid));
1961 $warnings = array();
1963 $context = context_system
::instance();
1964 self
::validate_context($context);
1966 if (empty($params['userid']) ||
$params['userid'] == $USER->id
) {
1967 $usercontext = context_user
::instance($USER->id
);
1968 require_capability('moodle/user:manageownfiles', $usercontext);
1970 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1971 core_user
::require_active_user($user);
1972 // Only admins can retrieve other users information.
1973 require_capability('moodle/site:config', $context);
1974 $usercontext = context_user
::instance($user->id
);
1977 $fileareainfo = file_get_file_area_info($usercontext->id
, 'user', 'private');
1980 $result['filecount'] = $fileareainfo['filecount'];
1981 $result['foldercount'] = $fileareainfo['foldercount'];
1982 $result['filesize'] = $fileareainfo['filesize'];
1983 $result['filesizewithoutreferences'] = $fileareainfo['filesize_without_references'];
1984 $result['warnings'] = $warnings;
1989 * Returns description of method result value.
1991 * @return external_description
1994 public static function get_private_files_info_returns() {
1995 return new external_single_structure(
1997 'filecount' => new external_value(PARAM_INT
, 'Number of files in the area.'),
1998 'foldercount' => new external_value(PARAM_INT
, 'Number of folders in the area.'),
1999 'filesize' => new external_value(PARAM_INT
, 'Total size of the files in the area.'),
2000 'filesizewithoutreferences' => new external_value(PARAM_INT
, 'Total size of the area excluding file references'),
2001 'warnings' => new external_warnings()