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 return new external_function_parameters(
52 'users' => new external_multiple_structure(
53 new external_single_structure(
56 new external_value(core_user
::get_property_type('username'), 'Username policy is defined in Moodle security config.'),
58 new external_value(core_user
::get_property_type('password'), 'Plain text password consisting of any characters', VALUE_OPTIONAL
),
60 new external_value(PARAM_BOOL
, 'True if password should be created and mailed to user.',
63 new external_value(core_user
::get_property_type('firstname'), 'The first name(s) of the user'),
65 new external_value(core_user
::get_property_type('lastname'), 'The family name of the user'),
67 new external_value(core_user
::get_property_type('email'), 'A valid and unique email address'),
69 new external_value(core_user
::get_property_type('auth'), 'Auth plugins include manual, ldap, etc', VALUE_DEFAULT
,
70 'manual', core_user
::get_property_null('auth')),
72 new external_value(core_user
::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution',
75 new external_value(core_user
::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_DEFAULT
,
76 core_user
::get_property_default('lang'), core_user
::get_property_null('lang')),
78 new external_value(core_user
::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server',
79 VALUE_DEFAULT
, $CFG->calendartype
, VALUE_OPTIONAL
),
81 new external_value(core_user
::get_property_type('theme'), 'Theme name such as "standard", must exist on server',
84 new external_value(core_user
::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default',
87 new external_value(core_user
::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc',
90 new external_value(core_user
::get_property_type('description'), 'User profile description, no HTML', VALUE_OPTIONAL
),
92 new external_value(core_user
::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL
),
94 new external_value(core_user
::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
95 'firstnamephonetic' =>
96 new external_value(core_user
::get_property_type('firstnamephonetic'), 'The first name(s) phonetically of the user', VALUE_OPTIONAL
),
98 new external_value(core_user
::get_property_type('lastnamephonetic'), 'The family name phonetically of the user', VALUE_OPTIONAL
),
100 new external_value(core_user
::get_property_type('middlename'), 'The middle name of the user', VALUE_OPTIONAL
),
102 new external_value(core_user
::get_property_type('alternatename'), 'The alternate name of the user', VALUE_OPTIONAL
),
103 'preferences' => new external_multiple_structure(
104 new external_single_structure(
106 'type' => new external_value(PARAM_RAW
, 'The name of the preference'),
107 'value' => new external_value(PARAM_RAW
, 'The value of the preference')
109 ), 'User preferences', VALUE_OPTIONAL
),
110 'customfields' => new external_multiple_structure(
111 new external_single_structure(
113 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the custom field'),
114 'value' => new external_value(PARAM_RAW
, 'The value of the custom field')
116 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL
)
125 * Create one or more users.
127 * @throws invalid_parameter_exception
128 * @param array $users An array of users to create.
129 * @return array An array of arrays
132 public static function create_users($users) {
134 require_once($CFG->dirroot
."/lib/weblib.php");
135 require_once($CFG->dirroot
."/user/lib.php");
136 require_once($CFG->dirroot
."/user/editlib.php");
137 require_once($CFG->dirroot
."/user/profile/lib.php"); // Required for customfields related function.
139 // Ensure the current user is allowed to run this function.
140 $context = context_system
::instance();
141 self
::validate_context($context);
142 require_capability('moodle/user:create', $context);
144 // Do basic automatic PARAM checks on incoming data, using params description.
145 // If any problems are found then exceptions are thrown with helpful error messages.
146 $params = self
::validate_parameters(self
::create_users_parameters(), array('users' => $users));
148 $availableauths = core_component
::get_plugin_list('auth');
149 unset($availableauths['mnet']); // These would need mnethostid too.
150 unset($availableauths['webservice']); // We do not want new webservice users for now.
152 $availablethemes = core_component
::get_plugin_list('theme');
153 $availablelangs = get_string_manager()->get_list_of_translations();
155 $transaction = $DB->start_delegated_transaction();
158 foreach ($params['users'] as $user) {
159 // Make sure that the username, firstname and lastname are not blank.
160 foreach (array('username', 'firstname', 'lastname') as $fieldname) {
161 if (trim($user[$fieldname]) === '') {
162 throw new invalid_parameter_exception('The field '.$fieldname.' cannot be blank');
166 // Make sure that the username doesn't already exist.
167 if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id
))) {
168 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
171 // Make sure auth is valid.
172 if (empty($availableauths[$user['auth']])) {
173 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
176 // Make sure lang is valid.
177 if (empty($availablelangs[$user['lang']])) {
178 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
181 // Make sure lang is valid.
182 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { // Theme is VALUE_OPTIONAL,
183 // so no default value
184 // We need to test if the client sent it
185 // => !empty($user['theme']).
186 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
189 // Make sure we have a password or have to create one.
190 $authplugin = get_auth_plugin($user['auth']);
191 if ($authplugin->is_internal() && empty($user['password']) && empty($user['createpassword'])) {
192 throw new invalid_parameter_exception('Invalid password: you must provide a password, or set createpassword.');
195 $user['confirmed'] = true;
196 $user['mnethostid'] = $CFG->mnet_localhost_id
;
198 // Start of user info validation.
199 // Make sure we validate current user info as handled by current GUI. See user/editadvanced_form.php func validation().
200 if (!validate_email($user['email'])) {
201 throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
202 } else if (empty($CFG->allowaccountssameemail
)) {
203 // Make a case-insensitive query for the given email address.
204 $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid';
206 'email' => $user['email'],
207 'mnethostid' => $user['mnethostid']
209 // If there are other user(s) that already have the same email, throw an error.
210 if ($DB->record_exists_select('user', $select, $params)) {
211 throw new invalid_parameter_exception('Email address already exists: '.$user['email']);
214 // End of user info validation.
216 $createpassword = !empty($user['createpassword']);
217 unset($user['createpassword']);
218 $updatepassword = false;
219 if ($authplugin->is_internal()) {
220 if ($createpassword) {
221 $user['password'] = '';
223 $updatepassword = true;
226 $user['password'] = AUTH_PASSWORD_NOT_CACHED
;
229 // Create the user data now!
230 $user['id'] = user_create_user($user, $updatepassword, false);
233 if (!empty($user['customfields'])) {
234 foreach ($user['customfields'] as $customfield) {
235 // Profile_save_data() saves profile file it's expecting a user with the correct id,
236 // and custom field to be named profile_field_"shortname".
237 $user["profile_field_".$customfield['type']] = $customfield['value'];
239 profile_save_data((object) $user);
242 if ($createpassword) {
243 $userobject = (object)$user;
244 setnew_password_and_mail($userobject);
245 unset_user_preference('create_password', $userobject);
246 set_user_preference('auth_forcepasswordchange', 1, $userobject);
250 \core\event\user_created
::create_from_userid($user['id'])->trigger();
253 if (!empty($user['preferences'])) {
254 $userpref = (object)$user;
255 foreach ($user['preferences'] as $preference) {
256 $userpref->{'preference_'.$preference['type']} = $preference['value'];
258 useredit_update_user_preference($userpref);
261 $userids[] = array('id' => $user['id'], 'username' => $user['username']);
264 $transaction->allow_commit();
270 * Returns description of method result value
272 * @return external_description
275 public static function create_users_returns() {
276 return new external_multiple_structure(
277 new external_single_structure(
279 'id' => new external_value(core_user
::get_property_type('id'), 'user id'),
280 'username' => new external_value(core_user
::get_property_type('username'), 'user name'),
288 * Returns description of method parameters
290 * @return external_function_parameters
293 public static function delete_users_parameters() {
294 return new external_function_parameters(
296 'userids' => new external_multiple_structure(new external_value(core_user
::get_property_type('id'), 'user ID')),
304 * @throws moodle_exception
305 * @param array $userids
309 public static function delete_users($userids) {
310 global $CFG, $DB, $USER;
311 require_once($CFG->dirroot
."/user/lib.php");
313 // Ensure the current user is allowed to run this function.
314 $context = context_system
::instance();
315 require_capability('moodle/user:delete', $context);
316 self
::validate_context($context);
318 $params = self
::validate_parameters(self
::delete_users_parameters(), array('userids' => $userids));
320 $transaction = $DB->start_delegated_transaction();
322 foreach ($params['userids'] as $userid) {
323 $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST
);
324 // Must not allow deleting of admins or self!!!
325 if (is_siteadmin($user)) {
326 throw new moodle_exception('useradminodelete', 'error');
328 if ($USER->id
== $user->id
) {
329 throw new moodle_exception('usernotdeletederror', 'error');
331 user_delete_user($user);
334 $transaction->allow_commit();
340 * Returns description of method result value
345 public static function delete_users_returns() {
350 * Returns description of method parameters.
352 * @return external_function_parameters
355 public static function update_user_preferences_parameters() {
356 return new external_function_parameters(
358 'userid' => new external_value(PARAM_INT
, 'id of the user, default to current user', VALUE_DEFAULT
, 0),
359 'emailstop' => new external_value(core_user
::get_property_type('emailstop'),
360 'Enable or disable notifications for this user', VALUE_DEFAULT
, null),
361 'preferences' => new external_multiple_structure(
362 new external_single_structure(
364 'type' => new external_value(PARAM_RAW
, 'The name of the preference'),
365 'value' => new external_value(PARAM_RAW
, 'The value of the preference')
367 ), 'User preferences', VALUE_DEFAULT
, array()
374 * Update the user's preferences.
377 * @param bool|null $emailstop
378 * @param array $preferences
382 public static function update_user_preferences($userid, $emailstop = null, $preferences = array()) {
385 require_once($CFG->dirroot
. '/user/lib.php');
386 require_once($CFG->dirroot
. '/user/editlib.php');
387 require_once($CFG->dirroot
. '/message/lib.php');
389 if (empty($userid)) {
393 $systemcontext = context_system
::instance();
394 self
::validate_context($systemcontext);
397 'emailstop' => $emailstop,
398 'preferences' => $preferences
400 self
::validate_parameters(self
::update_user_preferences_parameters(), $params);
403 if (!empty($preferences)) {
404 $userpref = ['id' => $userid];
405 foreach ($preferences as $preference) {
406 $userpref['preference_' . $preference['type']] = $preference['value'];
408 useredit_update_user_preference($userpref);
411 // Check if they want to update the email.
412 if ($emailstop !== null) {
413 $otheruser = ($userid == $USER->id
) ?
$USER : core_user
::get_user($userid, '*', MUST_EXIST
);
414 core_user
::require_active_user($otheruser);
415 if (core_message_can_edit_message_profile($otheruser) && $otheruser->emailstop
!= $emailstop) {
416 $user = new stdClass();
418 $user->emailstop
= $emailstop;
419 user_update_user($user);
421 // Update the $USER if we should.
422 if ($userid == $USER->id
) {
423 $USER->emailstop
= $emailstop;
432 * Returns description of method result value
437 public static function update_user_preferences_returns() {
442 * Returns description of method parameters
444 * @return external_function_parameters
447 public static function update_users_parameters() {
448 return new external_function_parameters(
450 'users' => new external_multiple_structure(
451 new external_single_structure(
454 new external_value(core_user
::get_property_type('id'), 'ID of the user'),
456 new external_value(core_user
::get_property_type('username'), 'Username policy is defined in Moodle security config.',
457 VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
459 new external_value(core_user
::get_property_type('password'), 'Plain text password consisting of any characters', VALUE_OPTIONAL
,
460 '', NULL_NOT_ALLOWED
),
462 new external_value(core_user
::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL
, '',
465 new external_value(core_user
::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL
),
467 new external_value(core_user
::get_property_type('email'), 'A valid and unique email address', VALUE_OPTIONAL
, '',
470 new external_value(core_user
::get_property_type('auth'), 'Auth plugins include manual, ldap, etc', VALUE_OPTIONAL
, '',
473 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
),
475 new external_value(core_user
::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution',
478 new external_value(core_user
::get_property_type('lang'), 'Language code such as "en", must exist on server',
479 VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
481 new external_value(core_user
::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server',
482 VALUE_OPTIONAL
, '', NULL_NOT_ALLOWED
),
484 new external_value(core_user
::get_property_type('theme'), 'Theme name such as "standard", must exist on server',
487 new external_value(core_user
::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default',
490 new external_value(core_user
::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc',
493 new external_value(core_user
::get_property_type('description'), 'User profile description, no HTML', VALUE_OPTIONAL
),
495 new external_value(core_user
::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL
),
497 new external_value(core_user
::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
498 'firstnamephonetic' =>
499 new external_value(core_user
::get_property_type('firstnamephonetic'), 'The first name(s) phonetically of the user', VALUE_OPTIONAL
),
500 'lastnamephonetic' =>
501 new external_value(core_user
::get_property_type('lastnamephonetic'), 'The family name phonetically of the user', VALUE_OPTIONAL
),
503 new external_value(core_user
::get_property_type('middlename'), 'The middle name of the user', VALUE_OPTIONAL
),
505 new external_value(core_user
::get_property_type('alternatename'), 'The alternate name of the user', VALUE_OPTIONAL
),
507 new external_value(PARAM_INT
, 'The itemid where the new user picture '.
508 'has been uploaded to, 0 to delete', VALUE_OPTIONAL
),
509 'customfields' => new external_multiple_structure(
510 new external_single_structure(
512 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The name of the custom field'),
513 'value' => new external_value(PARAM_RAW
, 'The value of the custom field')
515 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL
),
516 'preferences' => new external_multiple_structure(
517 new external_single_structure(
519 'type' => new external_value(PARAM_RAW
, 'The name of the preference'),
520 'value' => new external_value(PARAM_RAW
, 'The value of the preference')
522 ), 'User preferences', VALUE_OPTIONAL
),
533 * @param array $users
537 public static function update_users($users) {
538 global $CFG, $DB, $USER;
539 require_once($CFG->dirroot
."/user/lib.php");
540 require_once($CFG->dirroot
."/user/profile/lib.php"); // Required for customfields related function.
541 require_once($CFG->dirroot
.'/user/editlib.php');
543 // Ensure the current user is allowed to run this function.
544 $context = context_system
::instance();
545 require_capability('moodle/user:update', $context);
546 self
::validate_context($context);
548 $params = self
::validate_parameters(self
::update_users_parameters(), array('users' => $users));
550 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
553 'accepted_types' => 'web_image');
555 $transaction = $DB->start_delegated_transaction();
557 foreach ($params['users'] as $user) {
558 // First check the user exists.
559 if (!$existinguser = core_user
::get_user($user['id'])) {
562 // Check if we are trying to update an admin.
563 if ($existinguser->id
!= $USER->id
and is_siteadmin($existinguser) and !is_siteadmin($USER)) {
566 // Other checks (deleted, remote or guest users).
567 if ($existinguser->deleted
or is_mnet_remote_user($existinguser) or isguestuser($existinguser->id
)) {
570 // Check duplicated emails.
571 if (isset($user['email']) && $user['email'] !== $existinguser->email
) {
572 if (!validate_email($user['email'])) {
574 } else if (empty($CFG->allowaccountssameemail
)) {
575 // Make a case-insensitive query for the given email address and make sure to exclude the user being updated.
576 $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
578 'email' => $user['email'],
579 'mnethostid' => $CFG->mnet_localhost_id
,
580 'userid' => $user['id']
582 // Skip if there are other user(s) that already have the same email.
583 if ($DB->record_exists_select('user', $select, $params)) {
589 user_update_user($user, true, false);
591 // Update user picture if it was specified for this user.
592 if (empty($CFG->disableuserimages
) && isset($user['userpicture'])) {
593 $userobject = (object)$user;
595 $userobject->deletepicture
= null;
597 if ($user['userpicture'] == 0) {
598 $userobject->deletepicture
= true;
600 $userobject->imagefile
= $user['userpicture'];
603 core_user
::update_picture($userobject, $filemanageroptions);
606 // Update user custom fields.
607 if (!empty($user['customfields'])) {
609 foreach ($user['customfields'] as $customfield) {
610 // Profile_save_data() saves profile file it's expecting a user with the correct id,
611 // and custom field to be named profile_field_"shortname".
612 $user["profile_field_".$customfield['type']] = $customfield['value'];
614 profile_save_data((object) $user);
618 \core\event\user_updated
::create_from_userid($user['id'])->trigger();
621 if (!empty($user['preferences'])) {
622 $userpref = clone($existinguser);
623 foreach ($user['preferences'] as $preference) {
624 $userpref->{'preference_'.$preference['type']} = $preference['value'];
626 useredit_update_user_preference($userpref);
628 if (isset($user['suspended']) and $user['suspended']) {
629 \core\session\manager
::kill_user_sessions($user['id']);
633 $transaction->allow_commit();
639 * Returns description of method result value
644 public static function update_users_returns() {
649 * Returns description of method parameters
651 * @return external_function_parameters
654 public static function get_users_by_field_parameters() {
655 return new external_function_parameters(
657 'field' => new external_value(PARAM_ALPHA
, 'the search field can be
658 \'id\' or \'idnumber\' or \'username\' or \'email\''),
659 'values' => new external_multiple_structure(
660 new external_value(PARAM_RAW
, 'the value to match'))
666 * Get user information for a unique field.
668 * @throws coding_exception
669 * @throws invalid_parameter_exception
670 * @param string $field
671 * @param array $values
672 * @return array An array of arrays containg user profiles.
675 public static function get_users_by_field($field, $values) {
676 global $CFG, $USER, $DB;
677 require_once($CFG->dirroot
. "/user/lib.php");
679 $params = self
::validate_parameters(self
::get_users_by_field_parameters(),
680 array('field' => $field, 'values' => $values));
682 // This array will keep all the users that are allowed to be searched,
683 // according to the current user's privileges.
684 $cleanedvalues = array();
688 $paramtype = core_user
::get_property_type('id');
691 $paramtype = core_user
::get_property_type('idnumber');
694 $paramtype = core_user
::get_property_type('username');
697 $paramtype = core_user
::get_property_type('email');
700 throw new coding_exception('invalid field parameter',
701 'The search field \'' . $field . '\' is not supported, look at the web service documentation');
705 foreach ($values as $value) {
706 $cleanedvalue = clean_param($value, $paramtype);
707 if ( $value != $cleanedvalue) {
708 throw new invalid_parameter_exception('The field \'' . $field .
709 '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')');
711 $cleanedvalues[] = $cleanedvalue;
714 // Retrieve the users.
715 $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id');
717 $context = context_system
::instance();
718 self
::validate_context($context);
720 // Finally retrieve each users information.
721 $returnedusers = array();
722 foreach ($users as $user) {
723 $userdetails = user_get_user_details_courses($user);
725 // Return the user only if the searched field is returned.
726 // Otherwise it means that the $USER was not allowed to search the returned user.
727 if (!empty($userdetails) and !empty($userdetails[$field])) {
728 $returnedusers[] = $userdetails;
732 return $returnedusers;
736 * Returns description of method result value
738 * @return external_multiple_structure
741 public static function get_users_by_field_returns() {
742 return new external_multiple_structure(self
::user_description());
747 * Returns description of get_users() parameters.
749 * @return external_function_parameters
752 public static function get_users_parameters() {
753 return new external_function_parameters(
755 'criteria' => new external_multiple_structure(
756 new external_single_structure(
758 'key' => new external_value(PARAM_ALPHA
, 'the user column to search, expected keys (value format) are:
759 "id" (int) matching user id,
760 "lastname" (string) user last name (Note: you can use % for searching but it may be considerably slower!),
761 "firstname" (string) user first name (Note: you can use % for searching but it may be considerably slower!),
762 "idnumber" (string) matching user idnumber,
763 "username" (string) matching user username,
764 "email" (string) user email (Note: you can use % for searching but it may be considerably slower!),
765 "auth" (string) matching user auth plugin'),
766 'value' => new external_value(PARAM_RAW
, 'the value to search')
768 ), 'the key/value pairs to be considered in user search. Values can not be empty.
769 Specify different keys only once (fullname => \'user1\', auth => \'manual\', ...) -
770 key occurences are forbidden.
771 The search is executed with AND operator on the criterias. Invalid criterias (keys) are ignored,
772 the search is still executed on the valid criterias.
773 You can search without criteria, but the function is not designed for it.
774 It could very slow or timeout. The function is designed to search some specific users.'
781 * Retrieve matching user.
783 * @throws moodle_exception
784 * @param array $criteria the allowed array keys are id/lastname/firstname/idnumber/username/email/auth.
785 * @return array An array of arrays containing user profiles.
788 public static function get_users($criteria = array()) {
789 global $CFG, $USER, $DB;
791 require_once($CFG->dirroot
. "/user/lib.php");
793 $params = self
::validate_parameters(self
::get_users_parameters(),
794 array('criteria' => $criteria));
796 // Validate the criteria and retrieve the users.
799 $sqlparams = array();
802 // Do not retrieve deleted users.
803 $sql = ' deleted = 0';
805 foreach ($params['criteria'] as $criteriaindex => $criteria) {
807 // Check that the criteria has never been used.
808 if (array_key_exists($criteria['key'], $usedkeys)) {
809 throw new moodle_exception('keyalreadyset', '', '', null, 'The key ' . $criteria['key'] . ' can only be sent once');
811 $usedkeys[$criteria['key']] = true;
814 $invalidcriteria = false;
815 // Clean the parameters.
816 $paramtype = PARAM_RAW
;
817 switch ($criteria['key']) {
819 $paramtype = core_user
::get_property_type('id');
822 $paramtype = core_user
::get_property_type('idnumber');
825 $paramtype = core_user
::get_property_type('username');
828 // We use PARAM_RAW to allow searches with %.
829 $paramtype = core_user
::get_property_type('email');
832 $paramtype = core_user
::get_property_type('auth');
836 $paramtype = core_user
::get_property_type('firstname');
839 // Send back a warning that this search key is not supported in this version.
840 // This warning will make the function extandable without breaking clients.
842 'item' => $criteria['key'],
843 'warningcode' => 'invalidfieldparameter',
845 'The search key \'' . $criteria['key'] . '\' is not supported, look at the web service documentation'
847 // Do not add this invalid criteria to the created SQL request.
848 $invalidcriteria = true;
849 unset($params['criteria'][$criteriaindex]);
853 if (!$invalidcriteria) {
854 $cleanedvalue = clean_param($criteria['value'], $paramtype);
859 switch ($criteria['key']) {
864 $sql .= $criteria['key'] . ' = :' . $criteria['key'];
865 $sqlparams[$criteria['key']] = $cleanedvalue;
870 $sql .= $DB->sql_like($criteria['key'], ':' . $criteria['key'], false);
871 $sqlparams[$criteria['key']] = $cleanedvalue;
879 $users = $DB->get_records_select('user', $sql, $sqlparams, 'id ASC');
881 // Finally retrieve each users information.
882 $returnedusers = array();
883 foreach ($users as $user) {
884 $userdetails = user_get_user_details_courses($user);
886 // Return the user only if all the searched fields are returned.
887 // Otherwise it means that the $USER was not allowed to search the returned user.
888 if (!empty($userdetails)) {
891 foreach ($params['criteria'] as $criteria) {
892 if (empty($userdetails[$criteria['key']])) {
898 $returnedusers[] = $userdetails;
903 return array('users' => $returnedusers, 'warnings' => $warnings);
907 * Returns description of get_users result value.
909 * @return external_description
912 public static function get_users_returns() {
913 return new external_single_structure(
914 array('users' => new external_multiple_structure(
915 self
::user_description()
917 'warnings' => new external_warnings('always set to \'key\'', 'faulty key name')
923 * Returns description of method parameters
925 * @return external_function_parameters
928 public static function get_course_user_profiles_parameters() {
929 return new external_function_parameters(
931 'userlist' => new external_multiple_structure(
932 new external_single_structure(
934 'userid' => new external_value(core_user
::get_property_type('id'), 'userid'),
935 'courseid' => new external_value(PARAM_INT
, 'courseid'),
944 * Get course participant's details
946 * @param array $userlist array of user ids and according course ids
947 * @return array An array of arrays describing course participants
950 public static function get_course_user_profiles($userlist) {
951 global $CFG, $USER, $DB;
952 require_once($CFG->dirroot
. "/user/lib.php");
953 $params = self
::validate_parameters(self
::get_course_user_profiles_parameters(), array('userlist' => $userlist));
956 $courseids = array();
957 foreach ($params['userlist'] as $value) {
958 $userids[] = $value['userid'];
959 $courseids[$value['userid']] = $value['courseid'];
962 // Cache all courses.
964 list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids), SQL_PARAMS_NAMED
);
965 $cselect = ', ' . context_helper
::get_preload_record_columns_sql('ctx');
966 $cjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
967 $params['contextlevel'] = CONTEXT_COURSE
;
968 $coursesql = "SELECT c.* $cselect
969 FROM {course} c $cjoin
970 WHERE c.id $sqlcourseids";
971 $rs = $DB->get_recordset_sql($coursesql, $params);
972 foreach ($rs as $course) {
973 // Adding course contexts to cache.
974 context_helper
::preload_from_record($course);
976 $courses[$course->id
] = $course;
980 list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED
);
981 $uselect = ', ' . context_helper
::get_preload_record_columns_sql('ctx');
982 $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
983 $params['contextlevel'] = CONTEXT_USER
;
984 $usersql = "SELECT u.* $uselect
986 WHERE u.id $sqluserids";
987 $users = $DB->get_recordset_sql($usersql, $params);
989 foreach ($users as $user) {
990 if (!empty($user->deleted
)) {
993 context_helper
::preload_from_record($user);
994 $course = $courses[$courseids[$user->id
]];
995 $context = context_course
::instance($courseids[$user->id
], IGNORE_MISSING
);
996 self
::validate_context($context);
997 if ($userarray = user_get_user_details($user, $course)) {
998 $result[] = $userarray;
1008 * Returns description of method result value
1010 * @return external_description
1013 public static function get_course_user_profiles_returns() {
1014 $additionalfields = array(
1015 'groups' => new external_multiple_structure(
1016 new external_single_structure(
1018 'id' => new external_value(PARAM_INT
, 'group id'),
1019 'name' => new external_value(PARAM_RAW
, 'group name'),
1020 'description' => new external_value(PARAM_RAW
, 'group description'),
1021 'descriptionformat' => new external_format_value('description'),
1023 ), 'user groups', VALUE_OPTIONAL
),
1024 'roles' => new external_multiple_structure(
1025 new external_single_structure(
1027 'roleid' => new external_value(PARAM_INT
, 'role id'),
1028 'name' => new external_value(PARAM_RAW
, 'role name'),
1029 'shortname' => new external_value(PARAM_ALPHANUMEXT
, 'role shortname'),
1030 'sortorder' => new external_value(PARAM_INT
, 'role sortorder')
1032 ), 'user roles', VALUE_OPTIONAL
),
1033 'enrolledcourses' => new external_multiple_structure(
1034 new external_single_structure(
1036 'id' => new external_value(PARAM_INT
, 'Id of the course'),
1037 'fullname' => new external_value(PARAM_RAW
, 'Fullname of the course'),
1038 'shortname' => new external_value(PARAM_RAW
, 'Shortname of the course')
1040 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL
)
1043 return new external_multiple_structure(self
::user_description($additionalfields));
1047 * Create user return value description.
1049 * @param array $additionalfields some additional field
1050 * @return single_structure_description
1052 public static function user_description($additionalfields = array()) {
1053 $userfields = array(
1054 'id' => new external_value(core_user
::get_property_type('id'), 'ID of the user'),
1055 'username' => new external_value(core_user
::get_property_type('username'), 'The username', VALUE_OPTIONAL
),
1056 'firstname' => new external_value(core_user
::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL
),
1057 'lastname' => new external_value(core_user
::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL
),
1058 'fullname' => new external_value(core_user
::get_property_type('firstname'), 'The fullname of the user'),
1059 'email' => new external_value(core_user
::get_property_type('email'), 'An email address - allow email as root@localhost', VALUE_OPTIONAL
),
1060 'address' => new external_value(core_user
::get_property_type('address'), 'Postal address', VALUE_OPTIONAL
),
1061 'phone1' => new external_value(core_user
::get_property_type('phone1'), 'Phone 1', VALUE_OPTIONAL
),
1062 'phone2' => new external_value(core_user
::get_property_type('phone2'), 'Phone 2', VALUE_OPTIONAL
),
1063 'icq' => new external_value(core_user
::get_property_type('icq'), 'icq number', VALUE_OPTIONAL
),
1064 'skype' => new external_value(core_user
::get_property_type('skype'), 'skype id', VALUE_OPTIONAL
),
1065 'yahoo' => new external_value(core_user
::get_property_type('yahoo'), 'yahoo id', VALUE_OPTIONAL
),
1066 'aim' => new external_value(core_user
::get_property_type('aim'), 'aim id', VALUE_OPTIONAL
),
1067 'msn' => new external_value(core_user
::get_property_type('msn'), 'msn number', VALUE_OPTIONAL
),
1068 'department' => new external_value(core_user
::get_property_type('department'), 'department', VALUE_OPTIONAL
),
1069 'institution' => new external_value(core_user
::get_property_type('institution'), 'institution', VALUE_OPTIONAL
),
1070 'idnumber' => new external_value(core_user
::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL
),
1071 'interests' => new external_value(PARAM_TEXT
, 'user interests (separated by commas)', VALUE_OPTIONAL
),
1072 'firstaccess' => new external_value(core_user
::get_property_type('firstaccess'), 'first access to the site (0 if never)', VALUE_OPTIONAL
),
1073 'lastaccess' => new external_value(core_user
::get_property_type('lastaccess'), 'last access to the site (0 if never)', VALUE_OPTIONAL
),
1074 'auth' => new external_value(core_user
::get_property_type('auth'), 'Auth plugins include manual, ldap, etc', VALUE_OPTIONAL
),
1075 '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
),
1076 'confirmed' => new external_value(core_user
::get_property_type('confirmed'), 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL
),
1077 'lang' => new external_value(core_user
::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_OPTIONAL
),
1078 'calendartype' => new external_value(core_user
::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL
),
1079 'theme' => new external_value(core_user
::get_property_type('theme'), 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL
),
1080 'timezone' => new external_value(core_user
::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL
),
1081 'mailformat' => new external_value(core_user
::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL
),
1082 'description' => new external_value(core_user
::get_property_type('description'), 'User profile description', VALUE_OPTIONAL
),
1083 'descriptionformat' => new external_format_value(core_user
::get_property_type('descriptionformat'), VALUE_OPTIONAL
),
1084 'city' => new external_value(core_user
::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL
),
1085 'url' => new external_value(core_user
::get_property_type('url'), 'URL of the user', VALUE_OPTIONAL
),
1086 'country' => new external_value(core_user
::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL
),
1087 'profileimageurlsmall' => new external_value(PARAM_URL
, 'User image profile URL - small version'),
1088 'profileimageurl' => new external_value(PARAM_URL
, 'User image profile URL - big version'),
1089 'customfields' => new external_multiple_structure(
1090 new external_single_structure(
1092 'type' => new external_value(PARAM_ALPHANUMEXT
, 'The type of the custom field - text field, checkbox...'),
1093 'value' => new external_value(PARAM_RAW
, 'The value of the custom field'),
1094 'name' => new external_value(PARAM_RAW
, 'The name of the custom field'),
1095 'shortname' => new external_value(PARAM_RAW
, 'The shortname of the custom field - to be able to build the field class in the code'),
1097 ), 'User custom fields (also known as user profile fields)', VALUE_OPTIONAL
),
1098 'preferences' => new external_multiple_structure(
1099 new external_single_structure(
1101 'name' => new external_value(PARAM_RAW
, 'The name of the preferences'),
1102 'value' => new external_value(PARAM_RAW
, 'The value of the preference'),
1104 ), 'Users preferences', VALUE_OPTIONAL
)
1106 if (!empty($additionalfields)) {
1107 $userfields = array_merge($userfields, $additionalfields);
1109 return new external_single_structure($userfields);
1113 * Returns description of method parameters
1115 * @return external_function_parameters
1118 public static function add_user_private_files_parameters() {
1119 return new external_function_parameters(
1121 'draftid' => new external_value(PARAM_INT
, 'draft area id')
1127 * Copy files from a draft area to users private files area.
1129 * @throws invalid_parameter_exception
1130 * @param int $draftid Id of a draft area containing files.
1131 * @return array An array of warnings
1134 public static function add_user_private_files($draftid) {
1136 require_once($CFG->libdir
. "/filelib.php");
1138 $params = self
::validate_parameters(self
::add_user_private_files_parameters(), array('draftid' => $draftid));
1140 if (isguestuser()) {
1141 throw new invalid_parameter_exception('Guest users cannot upload files');
1144 $context = context_user
::instance($USER->id
);
1145 require_capability('moodle/user:manageownfiles', $context);
1147 $maxbytes = $CFG->userquota
;
1148 $maxareabytes = $CFG->userquota
;
1149 if (has_capability('moodle/user:ignoreuserquota', $context)) {
1150 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS
;
1151 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED
;
1154 $options = array('subdirs' => 1,
1155 'maxbytes' => $maxbytes,
1157 'areamaxbytes' => $maxareabytes);
1159 file_merge_files_from_draft_area_into_filearea($draftid, $context->id
, 'user', 'private', 0, $options);
1165 * Returns description of method result value
1167 * @return external_description
1170 public static function add_user_private_files_returns() {
1175 * Returns description of method parameters.
1177 * @return external_function_parameters
1180 public static function add_user_device_parameters() {
1181 return new external_function_parameters(
1183 'appid' => new external_value(PARAM_NOTAGS
, 'the app id, usually something like com.moodle.moodlemobile'),
1184 'name' => new external_value(PARAM_NOTAGS
, 'the device name, \'occam\' or \'iPhone\' etc.'),
1185 'model' => new external_value(PARAM_NOTAGS
, 'the device model \'Nexus4\' or \'iPad1,1\' etc.'),
1186 'platform' => new external_value(PARAM_NOTAGS
, 'the device platform \'iOS\' or \'Android\' etc.'),
1187 'version' => new external_value(PARAM_NOTAGS
, 'the device version \'6.1.2\' or \'4.2.2\' etc.'),
1188 'pushid' => new external_value(PARAM_RAW
, 'the device PUSH token/key/identifier/registration id'),
1189 'uuid' => new external_value(PARAM_RAW
, 'the device UUID')
1195 * Add a user device in Moodle database (for PUSH notifications usually).
1197 * @throws moodle_exception
1198 * @param string $appid The app id, usually something like com.moodle.moodlemobile.
1199 * @param string $name The device name, occam or iPhone etc.
1200 * @param string $model The device model Nexus4 or iPad1.1 etc.
1201 * @param string $platform The device platform iOs or Android etc.
1202 * @param string $version The device version 6.1.2 or 4.2.2 etc.
1203 * @param string $pushid The device PUSH token/key/identifier/registration id.
1204 * @param string $uuid The device UUID.
1205 * @return array List of possible warnings.
1208 public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) {
1209 global $CFG, $USER, $DB;
1210 require_once($CFG->dirroot
. "/user/lib.php");
1212 $params = self
::validate_parameters(self
::add_user_device_parameters(),
1213 array('appid' => $appid,
1216 'platform' => $platform,
1217 'version' => $version,
1218 'pushid' => $pushid,
1222 $warnings = array();
1224 // Prevent duplicate keys for users.
1225 if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'userid' => $USER->id
))) {
1226 $warnings['warning'][] = array(
1227 'item' => $params['pushid'],
1228 'warningcode' => 'existingkeyforthisuser',
1229 'message' => 'This key is already stored for this user'
1234 // Notice that we can have multiple devices because previously it was allowed to have repeated ones.
1235 // Since we don't have a clear way to decide which one is the more appropiate, we update all.
1236 if ($userdevices = $DB->get_records('user_devices', array('uuid' => $params['uuid'],
1237 'appid' => $params['appid'], 'userid' => $USER->id
))) {
1239 foreach ($userdevices as $userdevice) {
1240 $userdevice->version
= $params['version']; // Maybe the user upgraded the device.
1241 $userdevice->pushid
= $params['pushid'];
1242 $userdevice->timemodified
= time();
1243 $DB->update_record('user_devices', $userdevice);
1247 $userdevice = new stdclass
;
1248 $userdevice->userid
= $USER->id
;
1249 $userdevice->appid
= $params['appid'];
1250 $userdevice->name
= $params['name'];
1251 $userdevice->model
= $params['model'];
1252 $userdevice->platform
= $params['platform'];
1253 $userdevice->version
= $params['version'];
1254 $userdevice->pushid
= $params['pushid'];
1255 $userdevice->uuid
= $params['uuid'];
1256 $userdevice->timecreated
= time();
1257 $userdevice->timemodified
= $userdevice->timecreated
;
1259 if (!$DB->insert_record('user_devices', $userdevice)) {
1260 throw new moodle_exception("There was a problem saving in the database the device with key: " . $params['pushid']);
1268 * Returns description of method result value.
1270 * @return external_multiple_structure
1273 public static function add_user_device_returns() {
1274 return new external_multiple_structure(
1275 new external_warnings()
1280 * Returns description of method parameters.
1282 * @return external_function_parameters
1285 public static function remove_user_device_parameters() {
1286 return new external_function_parameters(
1288 'uuid' => new external_value(PARAM_RAW
, 'the device UUID'),
1289 'appid' => new external_value(PARAM_NOTAGS
,
1290 'the app id, if empty devices matching the UUID for the user will be removed',
1297 * Remove a user device from the Moodle database (for PUSH notifications usually).
1299 * @param string $uuid The device UUID.
1300 * @param string $appid The app id, opitonal parameter. If empty all the devices fmatching the UUID or the user will be removed.
1301 * @return array List of possible warnings and removal status.
1304 public static function remove_user_device($uuid, $appid = "") {
1306 require_once($CFG->dirroot
. "/user/lib.php");
1308 $params = self
::validate_parameters(self
::remove_user_device_parameters(), array('uuid' => $uuid, 'appid' => $appid));
1310 $context = context_system
::instance();
1311 self
::validate_context($context);
1313 // Warnings array, it can be empty at the end but is mandatory.
1314 $warnings = array();
1316 $removed = user_remove_user_device($params['uuid'], $params['appid']);
1319 $warnings[] = array(
1320 'item' => $params['uuid'],
1321 'warningcode' => 'devicedoesnotexist',
1322 'message' => 'The device doesn\'t exists in the database'
1327 'removed' => $removed,
1328 'warnings' => $warnings
1335 * Returns description of method result value.
1337 * @return external_multiple_structure
1340 public static function remove_user_device_returns() {
1341 return new external_single_structure(
1343 'removed' => new external_value(PARAM_BOOL
, 'True if removed, false if not removed because it doesn\'t exists'),
1344 'warnings' => new external_warnings(),
1350 * Returns description of method parameters
1352 * @return external_function_parameters
1355 public static function view_user_list_parameters() {
1356 return new external_function_parameters(
1358 'courseid' => new external_value(PARAM_INT
, 'id of the course, 0 for site')
1364 * Trigger the user_list_viewed event.
1366 * @param int $courseid id of course
1367 * @return array of warnings and status result
1369 * @throws moodle_exception
1371 public static function view_user_list($courseid) {
1373 require_once($CFG->dirroot
. "/user/lib.php");
1374 require_once($CFG->dirroot
. '/course/lib.php');
1376 $params = self
::validate_parameters(self
::view_user_list_parameters(),
1378 'courseid' => $courseid
1381 $warnings = array();
1383 if (empty($params['courseid'])) {
1384 $params['courseid'] = SITEID
;
1387 $course = get_course($params['courseid']);
1389 if ($course->id
== SITEID
) {
1390 $context = context_system
::instance();
1392 $context = context_course
::instance($course->id
);
1394 self
::validate_context($context);
1396 course_require_view_participants($context);
1398 user_list_view($course, $context);
1401 $result['status'] = true;
1402 $result['warnings'] = $warnings;
1407 * Returns description of method result value
1409 * @return external_description
1412 public static function view_user_list_returns() {
1413 return new external_single_structure(
1415 'status' => new external_value(PARAM_BOOL
, 'status: true if success'),
1416 'warnings' => new external_warnings()
1422 * Returns description of method parameters
1424 * @return external_function_parameters
1427 public static function view_user_profile_parameters() {
1428 return new external_function_parameters(
1430 'userid' => new external_value(PARAM_INT
, 'id of the user, 0 for current user', VALUE_REQUIRED
),
1431 'courseid' => new external_value(PARAM_INT
, 'id of the course, default site course', VALUE_DEFAULT
, 0)
1437 * Trigger the user profile viewed event.
1439 * @param int $userid id of user
1440 * @param int $courseid id of course
1441 * @return array of warnings and status result
1443 * @throws moodle_exception
1445 public static function view_user_profile($userid, $courseid = 0) {
1447 require_once($CFG->dirroot
. "/user/profile/lib.php");
1449 $params = self
::validate_parameters(self
::view_user_profile_parameters(),
1451 'userid' => $userid,
1452 'courseid' => $courseid
1455 $warnings = array();
1457 if (empty($params['userid'])) {
1458 $params['userid'] = $USER->id
;
1461 if (empty($params['courseid'])) {
1462 $params['courseid'] = SITEID
;
1465 $course = get_course($params['courseid']);
1466 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1467 core_user
::require_active_user($user);
1469 if ($course->id
== SITEID
) {
1470 $coursecontext = context_system
::instance();;
1472 $coursecontext = context_course
::instance($course->id
);
1474 self
::validate_context($coursecontext);
1476 $currentuser = $USER->id
== $user->id
;
1477 $usercontext = context_user
::instance($user->id
);
1479 if (!$currentuser and
1480 !has_capability('moodle/user:viewdetails', $coursecontext) and
1481 !has_capability('moodle/user:viewdetails', $usercontext)) {
1482 throw new moodle_exception('cannotviewprofile');
1485 // Case like user/profile.php.
1486 if ($course->id
== SITEID
) {
1487 profile_view($user, $usercontext);
1489 // Case like user/view.php.
1490 if (!$currentuser and !can_access_course($course, $user, '', true)) {
1491 throw new moodle_exception('notenrolledprofile');
1494 profile_view($user, $coursecontext, $course);
1498 $result['status'] = true;
1499 $result['warnings'] = $warnings;
1504 * Returns description of method result value
1506 * @return external_description
1509 public static function view_user_profile_returns() {
1510 return new external_single_structure(
1512 'status' => new external_value(PARAM_BOOL
, 'status: true if success'),
1513 'warnings' => new external_warnings()
1519 * Returns description of method parameters
1521 * @return external_function_parameters
1524 public static function get_user_preferences_parameters() {
1525 return new external_function_parameters(
1527 'name' => new external_value(PARAM_RAW
, 'preference name, empty for all', VALUE_DEFAULT
, ''),
1528 'userid' => new external_value(PARAM_INT
, 'id of the user, default to current user', VALUE_DEFAULT
, 0)
1534 * Return user preferences.
1536 * @param string $name preference name, empty for all
1537 * @param int $userid id of the user, 0 for current user
1538 * @return array of warnings and preferences
1540 * @throws moodle_exception
1542 public static function get_user_preferences($name = '', $userid = 0) {
1545 $params = self
::validate_parameters(self
::get_user_preferences_parameters(),
1550 $preferences = array();
1551 $warnings = array();
1553 $context = context_system
::instance();
1554 self
::validate_context($context);
1556 if (empty($params['name'])) {
1559 if (empty($params['userid'])) {
1562 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1563 core_user
::require_active_user($user);
1564 if ($user->id
!= $USER->id
) {
1565 // Only admins can retrieve other users preferences.
1566 require_capability('moodle/site:config', $context);
1570 $userpreferences = get_user_preferences($name, null, $user);
1571 // Check if we received just one preference.
1572 if (!is_array($userpreferences)) {
1573 $userpreferences = array($name => $userpreferences);
1576 foreach ($userpreferences as $name => $value) {
1577 $preferences[] = array(
1584 $result['preferences'] = $preferences;
1585 $result['warnings'] = $warnings;
1590 * Returns description of method result value
1592 * @return external_description
1595 public static function get_user_preferences_returns() {
1596 return new external_single_structure(
1598 'preferences' => new external_multiple_structure(
1599 new external_single_structure(
1601 'name' => new external_value(PARAM_RAW
, 'The name of the preference'),
1602 'value' => new external_value(PARAM_RAW
, 'The value of the preference'),
1605 'User custom fields (also known as user profile fields)'
1607 'warnings' => new external_warnings()
1613 * Returns description of method parameters
1615 * @return external_function_parameters
1618 public static function update_picture_parameters() {
1619 return new external_function_parameters(
1621 'draftitemid' => new external_value(PARAM_INT
, 'Id of the user draft file to use as image'),
1622 'delete' => new external_value(PARAM_BOOL
, 'If we should delete the user picture', VALUE_DEFAULT
, false),
1623 'userid' => new external_value(PARAM_INT
, 'Id of the user, 0 for current user', VALUE_DEFAULT
, 0)
1629 * Update or delete the user picture in the site
1631 * @param int $draftitemid id of the user draft file to use as image
1632 * @param bool $delete if we should delete the user picture
1633 * @param int $userid id of the user, 0 for current user
1634 * @return array warnings and success status
1636 * @throws moodle_exception
1638 public static function update_picture($draftitemid, $delete = false, $userid = 0) {
1639 global $CFG, $USER, $PAGE;
1641 $params = self
::validate_parameters(
1642 self
::update_picture_parameters(),
1644 'draftitemid' => $draftitemid,
1645 'delete' => $delete,
1650 $context = context_system
::instance();
1651 self
::validate_context($context);
1653 if (!empty($CFG->disableuserimages
)) {
1654 throw new moodle_exception('userimagesdisabled', 'admin');
1657 if (empty($params['userid']) or $params['userid'] == $USER->id
) {
1659 require_capability('moodle/user:editownprofile', $context);
1661 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1662 core_user
::require_active_user($user);
1663 $personalcontext = context_user
::instance($user->id
);
1665 require_capability('moodle/user:editprofile', $personalcontext);
1666 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
1667 throw new moodle_exception('useradmineditadmin');
1671 // Load the appropriate auth plugin.
1672 $userauth = get_auth_plugin($user->auth
);
1673 if (is_mnet_remote_user($user) or !$userauth->can_edit_profile() or $userauth->edit_profile_url()) {
1674 throw new moodle_exception('noprofileedit', 'auth');
1677 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
, 'subdirs' => 0, 'maxfiles' => 1, 'accepted_types' => 'web_image');
1678 $user->deletepicture
= $params['delete'];
1679 $user->imagefile
= $params['draftitemid'];
1680 $success = core_user
::update_picture($user, $filemanageroptions);
1683 'success' => $success,
1684 'warnings' => array(),
1687 $userpicture = new user_picture(core_user
::get_user($user->id
));
1688 $userpicture->size
= 1; // Size f1.
1689 $result['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
1695 * Returns description of method result value
1697 * @return external_description
1700 public static function update_picture_returns() {
1701 return new external_single_structure(
1703 'success' => new external_value(PARAM_BOOL
, 'True if the image was updated, false otherwise.'),
1704 'profileimageurl' => new external_value(PARAM_URL
, 'New profile user image url', VALUE_OPTIONAL
),
1705 'warnings' => new external_warnings()
1711 * Returns description of method parameters
1713 * @return external_function_parameters
1716 public static function set_user_preferences_parameters() {
1717 return new external_function_parameters(
1719 'preferences' => new external_multiple_structure(
1720 new external_single_structure(
1722 'name' => new external_value(PARAM_RAW
, 'The name of the preference'),
1723 'value' => new external_value(PARAM_RAW
, 'The value of the preference'),
1724 'userid' => new external_value(PARAM_INT
, 'Id of the user to set the preference'),
1733 * Set user preferences.
1735 * @param array $preferences list of preferences including name, value and userid
1736 * @return array of warnings and preferences saved
1738 * @throws moodle_exception
1740 public static function set_user_preferences($preferences) {
1743 $params = self
::validate_parameters(self
::set_user_preferences_parameters(), array('preferences' => $preferences));
1744 $warnings = array();
1747 $context = context_system
::instance();
1748 self
::validate_context($context);
1750 $userscache = array();
1751 foreach ($params['preferences'] as $pref) {
1752 // Check to which user set the preference.
1753 if (!empty($userscache[$pref['userid']])) {
1754 $user = $userscache[$pref['userid']];
1757 $user = core_user
::get_user($pref['userid'], '*', MUST_EXIST
);
1758 core_user
::require_active_user($user);
1759 $userscache[$pref['userid']] = $user;
1760 } catch (Exception
$e) {
1761 $warnings[] = array(
1763 'itemid' => $pref['userid'],
1764 'warningcode' => 'invaliduser',
1765 'message' => $e->getMessage()
1772 if (core_user
::can_edit_preference($pref['name'], $user)) {
1773 $value = core_user
::clean_preference($pref['value'], $pref['name']);
1774 set_user_preference($pref['name'], $value, $user->id
);
1776 'name' => $pref['name'],
1777 'userid' => $user->id
,
1780 $warnings[] = array(
1782 'itemid' => $user->id
,
1783 'warningcode' => 'nopermission',
1784 'message' => 'You are not allowed to change the preference '.s($pref['name']).' for user '.$user->id
1787 } catch (Exception
$e) {
1788 $warnings[] = array(
1790 'itemid' => $user->id
,
1791 'warningcode' => 'errorsavingpreference',
1792 'message' => $e->getMessage()
1798 $result['saved'] = $saved;
1799 $result['warnings'] = $warnings;
1804 * Returns description of method result value
1806 * @return external_description
1809 public static function set_user_preferences_returns() {
1810 return new external_single_structure(
1812 'saved' => new external_multiple_structure(
1813 new external_single_structure(
1815 'name' => new external_value(PARAM_RAW
, 'The name of the preference'),
1816 'userid' => new external_value(PARAM_INT
, 'The user the preference was set for'),
1818 ), 'Preferences saved'
1820 'warnings' => new external_warnings()
1826 * Returns description of method parameters.
1828 * @return external_function_parameters
1831 public static function agree_site_policy_parameters() {
1832 return new external_function_parameters(array());
1836 * Agree the site policy for the current user.
1838 * @return array of warnings and status result
1840 * @throws moodle_exception
1842 public static function agree_site_policy() {
1843 global $CFG, $DB, $USER;
1845 $warnings = array();
1847 $context = context_system
::instance();
1849 // We expect an exception here since the user didn't agree the site policy yet.
1850 self
::validate_context($context);
1851 } catch (Exception
$e) {
1852 // We are expecting only a sitepolicynotagreed exception.
1853 if (!($e instanceof moodle_exception
) or $e->errorcode
!= 'sitepolicynotagreed') {
1854 // In case we receive a different exception, throw it.
1859 $manager = new \core_privacy\local\sitepolicy\
manager();
1860 if (!empty($USER->policyagreed
)) {
1862 $warnings[] = array(
1864 'itemid' => $USER->id
,
1865 'warningcode' => 'alreadyagreed',
1866 'message' => 'The user already agreed the site policy.'
1868 } else if (!$manager->is_defined()) {
1870 $warnings[] = array(
1872 'itemid' => $USER->id
,
1873 'warningcode' => 'nositepolicy',
1874 'message' => 'The site does not have a site policy configured.'
1877 $status = $manager->accept();
1881 $result['status'] = $status;
1882 $result['warnings'] = $warnings;
1887 * Returns description of method result value.
1889 * @return external_description
1892 public static function agree_site_policy_returns() {
1893 return new external_single_structure(
1895 'status' => new external_value(PARAM_BOOL
, 'Status: true only if we set the policyagreed to 1 for the user'),
1896 'warnings' => new external_warnings()
1902 * Returns description of method parameters.
1904 * @return external_function_parameters
1907 public static function get_private_files_info_parameters() {
1908 return new external_function_parameters(
1910 'userid' => new external_value(PARAM_INT
, 'Id of the user, default to current user.', VALUE_DEFAULT
, 0)
1916 * Returns general information about files in the user private files area.
1918 * @param int $userid Id of the user, default to current user.
1919 * @return array of warnings and file area information
1921 * @throws moodle_exception
1923 public static function get_private_files_info($userid = 0) {
1925 require_once($CFG->libdir
. '/filelib.php');
1927 $params = self
::validate_parameters(self
::get_private_files_info_parameters(), array('userid' => $userid));
1928 $warnings = array();
1930 $context = context_system
::instance();
1931 self
::validate_context($context);
1933 if (empty($params['userid']) ||
$params['userid'] == $USER->id
) {
1934 $usercontext = context_user
::instance($USER->id
);
1935 require_capability('moodle/user:manageownfiles', $usercontext);
1937 $user = core_user
::get_user($params['userid'], '*', MUST_EXIST
);
1938 core_user
::require_active_user($user);
1939 // Only admins can retrieve other users information.
1940 require_capability('moodle/site:config', $context);
1941 $usercontext = context_user
::instance($user->id
);
1944 $fileareainfo = file_get_file_area_info($usercontext->id
, 'user', 'private');
1947 $result['filecount'] = $fileareainfo['filecount'];
1948 $result['foldercount'] = $fileareainfo['foldercount'];
1949 $result['filesize'] = $fileareainfo['filesize'];
1950 $result['filesizewithoutreferences'] = $fileareainfo['filesize_without_references'];
1951 $result['warnings'] = $warnings;
1956 * Returns description of method result value.
1958 * @return external_description
1961 public static function get_private_files_info_returns() {
1962 return new external_single_structure(
1964 'filecount' => new external_value(PARAM_INT
, 'Number of files in the area.'),
1965 'foldercount' => new external_value(PARAM_INT
, 'Number of folders in the area.'),
1966 'filesize' => new external_value(PARAM_INT
, 'Total size of the files in the area.'),
1967 'filesizewithoutreferences' => new external_value(PARAM_INT
, 'Total size of the area excluding file references'),
1968 'warnings' => new external_warnings()