Merge branch 'MDL-50218-30' of https://github.com/xow/moodle into MOODLE_30_STABLE
[moodle.git] / user / externallib.php
blob8ac34aaf04fd60a28dd1bf152e8683d801a1fae2
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * External user API
20 * @package core_user
21 * @category external
22 * @copyright 2009 Petr Skodak
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("$CFG->libdir/externallib.php");
28 /**
29 * User external functions
31 * @package core_user
32 * @category external
33 * @copyright 2011 Jerome Mouneyrac
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 * @since Moodle 2.2
37 class core_user_external extends external_api {
39 /**
40 * Returns description of method parameters
42 * @return external_function_parameters
43 * @since Moodle 2.2
45 public static function create_users_parameters() {
46 global $CFG;
48 return new external_function_parameters(
49 array(
50 'users' => new external_multiple_structure(
51 new external_single_structure(
52 array(
53 'username' =>
54 new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.'),
55 'password' =>
56 new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL),
57 'createpassword' =>
58 new external_value(PARAM_BOOL, 'True if password should be created and mailed to user.',
59 VALUE_OPTIONAL),
60 'firstname' =>
61 new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
62 'lastname' =>
63 new external_value(PARAM_NOTAGS, 'The family name of the user'),
64 'email' =>
65 new external_value(PARAM_EMAIL, 'A valid and unique email address'),
66 'auth' =>
67 new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT,
68 'manual', NULL_NOT_ALLOWED),
69 'idnumber' =>
70 new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
71 VALUE_DEFAULT, ''),
72 'lang' =>
73 new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_DEFAULT,
74 $CFG->lang, NULL_NOT_ALLOWED),
75 'calendartype' =>
76 new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
77 VALUE_DEFAULT, $CFG->calendartype, VALUE_OPTIONAL),
78 'theme' =>
79 new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
80 VALUE_OPTIONAL),
81 'timezone' =>
82 new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
83 VALUE_OPTIONAL),
84 'mailformat' =>
85 new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
86 VALUE_OPTIONAL),
87 'description' =>
88 new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
89 'city' =>
90 new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
91 'country' =>
92 new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
93 'firstnamephonetic' =>
94 new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
95 'lastnamephonetic' =>
96 new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
97 'middlename' =>
98 new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
99 'alternatename' =>
100 new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
101 'preferences' => new external_multiple_structure(
102 new external_single_structure(
103 array(
104 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
105 'value' => new external_value(PARAM_RAW, 'The value of the preference')
107 ), 'User preferences', VALUE_OPTIONAL),
108 'customfields' => new external_multiple_structure(
109 new external_single_structure(
110 array(
111 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
112 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
114 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL)
123 * Create one or more users.
125 * @throws invalid_parameter_exception
126 * @param array $users An array of users to create.
127 * @return array An array of arrays
128 * @since Moodle 2.2
130 public static function create_users($users) {
131 global $CFG, $DB;
132 require_once($CFG->dirroot."/lib/weblib.php");
133 require_once($CFG->dirroot."/user/lib.php");
134 require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function.
136 // Ensure the current user is allowed to run this function.
137 $context = context_system::instance();
138 self::validate_context($context);
139 require_capability('moodle/user:create', $context);
141 // Do basic automatic PARAM checks on incoming data, using params description.
142 // If any problems are found then exceptions are thrown with helpful error messages.
143 $params = self::validate_parameters(self::create_users_parameters(), array('users' => $users));
145 $availableauths = core_component::get_plugin_list('auth');
146 unset($availableauths['mnet']); // These would need mnethostid too.
147 unset($availableauths['webservice']); // We do not want new webservice users for now.
149 $availablethemes = core_component::get_plugin_list('theme');
150 $availablelangs = get_string_manager()->get_list_of_translations();
152 $transaction = $DB->start_delegated_transaction();
154 $userids = array();
155 $createpassword = false;
156 foreach ($params['users'] as $user) {
157 // Make sure that the username doesn't already exist.
158 if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
159 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
162 // Make sure auth is valid.
163 if (empty($availableauths[$user['auth']])) {
164 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
167 // Make sure lang is valid.
168 if (empty($availablelangs[$user['lang']])) {
169 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
172 // Make sure lang is valid.
173 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { // Theme is VALUE_OPTIONAL,
174 // so no default value
175 // We need to test if the client sent it
176 // => !empty($user['theme']).
177 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
180 // Make sure we have a password or have to create one.
181 if (empty($user['password']) && empty($user['createpassword'])) {
182 throw new invalid_parameter_exception('Invalid password: you must provide a password, or set createpassword.');
185 $user['confirmed'] = true;
186 $user['mnethostid'] = $CFG->mnet_localhost_id;
188 // Start of user info validation.
189 // Make sure we validate current user info as handled by current GUI. See user/editadvanced_form.php func validation().
190 if (!validate_email($user['email'])) {
191 throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
192 } else if (empty($CFG->allowaccountssameemail) &&
193 $DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $user['mnethostid']))) {
194 throw new invalid_parameter_exception('Email address already exists: '.$user['email']);
196 // End of user info validation.
198 $createpassword = !empty($user['createpassword']);
199 unset($user['createpassword']);
200 if ($createpassword) {
201 $user['password'] = '';
202 $updatepassword = false;
203 } else {
204 $updatepassword = true;
207 // Create the user data now!
208 $user['id'] = user_create_user($user, $updatepassword, false);
210 // Custom fields.
211 if (!empty($user['customfields'])) {
212 foreach ($user['customfields'] as $customfield) {
213 // Profile_save_data() saves profile file it's expecting a user with the correct id,
214 // and custom field to be named profile_field_"shortname".
215 $user["profile_field_".$customfield['type']] = $customfield['value'];
217 profile_save_data((object) $user);
220 if ($createpassword) {
221 $userobject = (object)$user;
222 setnew_password_and_mail($userobject);
223 unset_user_preference('create_password', $userobject);
224 set_user_preference('auth_forcepasswordchange', 1, $userobject);
227 // Trigger event.
228 \core\event\user_created::create_from_userid($user['id'])->trigger();
230 // Preferences.
231 if (!empty($user['preferences'])) {
232 foreach ($user['preferences'] as $preference) {
233 set_user_preference($preference['type'], $preference['value'], $user['id']);
237 $userids[] = array('id' => $user['id'], 'username' => $user['username']);
240 $transaction->allow_commit();
242 return $userids;
246 * Returns description of method result value
248 * @return external_description
249 * @since Moodle 2.2
251 public static function create_users_returns() {
252 return new external_multiple_structure(
253 new external_single_structure(
254 array(
255 'id' => new external_value(PARAM_INT, 'user id'),
256 'username' => new external_value(PARAM_USERNAME, 'user name'),
264 * Returns description of method parameters
266 * @return external_function_parameters
267 * @since Moodle 2.2
269 public static function delete_users_parameters() {
270 return new external_function_parameters(
271 array(
272 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
278 * Delete users
280 * @throws moodle_exception
281 * @param array $userids
282 * @return null
283 * @since Moodle 2.2
285 public static function delete_users($userids) {
286 global $CFG, $DB, $USER;
287 require_once($CFG->dirroot."/user/lib.php");
289 // Ensure the current user is allowed to run this function.
290 $context = context_system::instance();
291 require_capability('moodle/user:delete', $context);
292 self::validate_context($context);
294 $params = self::validate_parameters(self::delete_users_parameters(), array('userids' => $userids));
296 $transaction = $DB->start_delegated_transaction();
298 foreach ($params['userids'] as $userid) {
299 $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST);
300 // Must not allow deleting of admins or self!!!
301 if (is_siteadmin($user)) {
302 throw new moodle_exception('useradminodelete', 'error');
304 if ($USER->id == $user->id) {
305 throw new moodle_exception('usernotdeletederror', 'error');
307 user_delete_user($user);
310 $transaction->allow_commit();
312 return null;
316 * Returns description of method result value
318 * @return null
319 * @since Moodle 2.2
321 public static function delete_users_returns() {
322 return null;
327 * Returns description of method parameters
329 * @return external_function_parameters
330 * @since Moodle 2.2
332 public static function update_users_parameters() {
333 return new external_function_parameters(
334 array(
335 'users' => new external_multiple_structure(
336 new external_single_structure(
337 array(
338 'id' =>
339 new external_value(PARAM_INT, 'ID of the user'),
340 'username' =>
341 new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.',
342 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
343 'password' =>
344 new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL,
345 '', NULL_NOT_ALLOWED),
346 'firstname' =>
347 new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',
348 NULL_NOT_ALLOWED),
349 'lastname' =>
350 new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
351 'email' =>
352 new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',
353 NULL_NOT_ALLOWED),
354 'auth' =>
355 new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '',
356 NULL_NOT_ALLOWED),
357 'idnumber' =>
358 new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
359 VALUE_OPTIONAL),
360 'lang' =>
361 new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server',
362 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
363 'calendartype' =>
364 new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
365 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
366 'theme' =>
367 new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
368 VALUE_OPTIONAL),
369 'timezone' =>
370 new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
371 VALUE_OPTIONAL),
372 'mailformat' =>
373 new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
374 VALUE_OPTIONAL),
375 'description' =>
376 new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
377 'city' =>
378 new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
379 'country' =>
380 new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
381 'firstnamephonetic' =>
382 new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
383 'lastnamephonetic' =>
384 new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
385 'middlename' =>
386 new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
387 'alternatename' =>
388 new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
389 'customfields' => new external_multiple_structure(
390 new external_single_structure(
391 array(
392 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
393 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
395 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
396 'preferences' => new external_multiple_structure(
397 new external_single_structure(
398 array(
399 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
400 'value' => new external_value(PARAM_RAW, 'The value of the preference')
402 ), 'User preferences', VALUE_OPTIONAL),
411 * Update users
413 * @param array $users
414 * @return null
415 * @since Moodle 2.2
417 public static function update_users($users) {
418 global $CFG, $DB;
419 require_once($CFG->dirroot."/user/lib.php");
420 require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function.
422 // Ensure the current user is allowed to run this function.
423 $context = context_system::instance();
424 require_capability('moodle/user:update', $context);
425 self::validate_context($context);
427 $params = self::validate_parameters(self::update_users_parameters(), array('users' => $users));
429 $transaction = $DB->start_delegated_transaction();
431 foreach ($params['users'] as $user) {
432 user_update_user($user, true, false);
433 // Update user custom fields.
434 if (!empty($user['customfields'])) {
436 foreach ($user['customfields'] as $customfield) {
437 // Profile_save_data() saves profile file it's expecting a user with the correct id,
438 // and custom field to be named profile_field_"shortname".
439 $user["profile_field_".$customfield['type']] = $customfield['value'];
441 profile_save_data((object) $user);
444 // Trigger event.
445 \core\event\user_updated::create_from_userid($user['id'])->trigger();
447 // Preferences.
448 if (!empty($user['preferences'])) {
449 foreach ($user['preferences'] as $preference) {
450 set_user_preference($preference['type'], $preference['value'], $user['id']);
455 $transaction->allow_commit();
457 return null;
461 * Returns description of method result value
463 * @return null
464 * @since Moodle 2.2
466 public static function update_users_returns() {
467 return null;
471 * Returns description of method parameters
473 * @return external_function_parameters
474 * @since Moodle 2.4
476 public static function get_users_by_field_parameters() {
477 return new external_function_parameters(
478 array(
479 'field' => new external_value(PARAM_ALPHA, 'the search field can be
480 \'id\' or \'idnumber\' or \'username\' or \'email\''),
481 'values' => new external_multiple_structure(
482 new external_value(PARAM_RAW, 'the value to match'))
488 * Get user information for a unique field.
490 * @throws coding_exception
491 * @throws invalid_parameter_exception
492 * @param string $field
493 * @param array $values
494 * @return array An array of arrays containg user profiles.
495 * @since Moodle 2.4
497 public static function get_users_by_field($field, $values) {
498 global $CFG, $USER, $DB;
499 require_once($CFG->dirroot . "/user/lib.php");
501 $params = self::validate_parameters(self::get_users_by_field_parameters(),
502 array('field' => $field, 'values' => $values));
504 // This array will keep all the users that are allowed to be searched,
505 // according to the current user's privileges.
506 $cleanedvalues = array();
508 switch ($field) {
509 case 'id':
510 $paramtype = PARAM_INT;
511 break;
512 case 'idnumber':
513 $paramtype = PARAM_RAW;
514 break;
515 case 'username':
516 $paramtype = PARAM_RAW;
517 break;
518 case 'email':
519 $paramtype = PARAM_EMAIL;
520 break;
521 default:
522 throw new coding_exception('invalid field parameter',
523 'The search field \'' . $field . '\' is not supported, look at the web service documentation');
526 // Clean the values.
527 foreach ($values as $value) {
528 $cleanedvalue = clean_param($value, $paramtype);
529 if ( $value != $cleanedvalue) {
530 throw new invalid_parameter_exception('The field \'' . $field .
531 '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')');
533 $cleanedvalues[] = $cleanedvalue;
536 // Retrieve the users.
537 $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id');
539 // Finally retrieve each users information.
540 $returnedusers = array();
541 foreach ($users as $user) {
542 $userdetails = user_get_user_details_courses($user);
544 // Return the user only if the searched field is returned.
545 // Otherwise it means that the $USER was not allowed to search the returned user.
546 if (!empty($userdetails) and !empty($userdetails[$field])) {
547 $returnedusers[] = $userdetails;
551 return $returnedusers;
555 * Returns description of method result value
557 * @return external_multiple_structure
558 * @since Moodle 2.4
560 public static function get_users_by_field_returns() {
561 return new external_multiple_structure(self::user_description());
566 * Returns description of get_users() parameters.
568 * @return external_function_parameters
569 * @since Moodle 2.5
571 public static function get_users_parameters() {
572 return new external_function_parameters(
573 array(
574 'criteria' => new external_multiple_structure(
575 new external_single_structure(
576 array(
577 'key' => new external_value(PARAM_ALPHA, 'the user column to search, expected keys (value format) are:
578 "id" (int) matching user id,
579 "lastname" (string) user last name (Note: you can use % for searching but it may be considerably slower!),
580 "firstname" (string) user first name (Note: you can use % for searching but it may be considerably slower!),
581 "idnumber" (string) matching user idnumber,
582 "username" (string) matching user username,
583 "email" (string) user email (Note: you can use % for searching but it may be considerably slower!),
584 "auth" (string) matching user auth plugin'),
585 'value' => new external_value(PARAM_RAW, 'the value to search')
587 ), 'the key/value pairs to be considered in user search. Values can not be empty.
588 Specify different keys only once (fullname => \'user1\', auth => \'manual\', ...) -
589 key occurences are forbidden.
590 The search is executed with AND operator on the criterias. Invalid criterias (keys) are ignored,
591 the search is still executed on the valid criterias.
592 You can search without criteria, but the function is not designed for it.
593 It could very slow or timeout. The function is designed to search some specific users.'
600 * Retrieve matching user.
602 * @throws moodle_exception
603 * @param array $criteria the allowed array keys are id/lastname/firstname/idnumber/username/email/auth.
604 * @return array An array of arrays containing user profiles.
605 * @since Moodle 2.5
607 public static function get_users($criteria = array()) {
608 global $CFG, $USER, $DB;
610 require_once($CFG->dirroot . "/user/lib.php");
612 $params = self::validate_parameters(self::get_users_parameters(),
613 array('criteria' => $criteria));
615 // Validate the criteria and retrieve the users.
616 $users = array();
617 $warnings = array();
618 $sqlparams = array();
619 $usedkeys = array();
621 // Do not retrieve deleted users.
622 $sql = ' deleted = 0';
624 foreach ($params['criteria'] as $criteriaindex => $criteria) {
626 // Check that the criteria has never been used.
627 if (array_key_exists($criteria['key'], $usedkeys)) {
628 throw new moodle_exception('keyalreadyset', '', '', null, 'The key ' . $criteria['key'] . ' can only be sent once');
629 } else {
630 $usedkeys[$criteria['key']] = true;
633 $invalidcriteria = false;
634 // Clean the parameters.
635 $paramtype = PARAM_RAW;
636 switch ($criteria['key']) {
637 case 'id':
638 $paramtype = PARAM_INT;
639 break;
640 case 'idnumber':
641 $paramtype = PARAM_RAW;
642 break;
643 case 'username':
644 $paramtype = PARAM_RAW;
645 break;
646 case 'email':
647 // We use PARAM_RAW to allow searches with %.
648 $paramtype = PARAM_RAW;
649 break;
650 case 'auth':
651 $paramtype = PARAM_AUTH;
652 break;
653 case 'lastname':
654 case 'firstname':
655 $paramtype = PARAM_TEXT;
656 break;
657 default:
658 // Send back a warning that this search key is not supported in this version.
659 // This warning will make the function extandable without breaking clients.
660 $warnings[] = array(
661 'item' => $criteria['key'],
662 'warningcode' => 'invalidfieldparameter',
663 'message' =>
664 'The search key \'' . $criteria['key'] . '\' is not supported, look at the web service documentation'
666 // Do not add this invalid criteria to the created SQL request.
667 $invalidcriteria = true;
668 unset($params['criteria'][$criteriaindex]);
669 break;
672 if (!$invalidcriteria) {
673 $cleanedvalue = clean_param($criteria['value'], $paramtype);
675 $sql .= ' AND ';
677 // Create the SQL.
678 switch ($criteria['key']) {
679 case 'id':
680 case 'idnumber':
681 case 'username':
682 case 'auth':
683 $sql .= $criteria['key'] . ' = :' . $criteria['key'];
684 $sqlparams[$criteria['key']] = $cleanedvalue;
685 break;
686 case 'email':
687 case 'lastname':
688 case 'firstname':
689 $sql .= $DB->sql_like($criteria['key'], ':' . $criteria['key'], false);
690 $sqlparams[$criteria['key']] = $cleanedvalue;
691 break;
692 default:
693 break;
698 $users = $DB->get_records_select('user', $sql, $sqlparams, 'id ASC');
700 // Finally retrieve each users information.
701 $returnedusers = array();
702 foreach ($users as $user) {
703 $userdetails = user_get_user_details_courses($user);
705 // Return the user only if all the searched fields are returned.
706 // Otherwise it means that the $USER was not allowed to search the returned user.
707 if (!empty($userdetails)) {
708 $validuser = true;
710 foreach ($params['criteria'] as $criteria) {
711 if (empty($userdetails[$criteria['key']])) {
712 $validuser = false;
716 if ($validuser) {
717 $returnedusers[] = $userdetails;
722 return array('users' => $returnedusers, 'warnings' => $warnings);
726 * Returns description of get_users result value.
728 * @return external_description
729 * @since Moodle 2.5
731 public static function get_users_returns() {
732 return new external_single_structure(
733 array('users' => new external_multiple_structure(
734 self::user_description()
736 'warnings' => new external_warnings('always set to \'key\'', 'faulty key name')
742 * Returns description of method parameters
744 * @return external_function_parameters
745 * @since Moodle 2.2
746 * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
747 * @see core_user_external::get_users_by_field_parameters()
749 public static function get_users_by_id_parameters() {
750 return new external_function_parameters(
751 array(
752 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
758 * Get user information
759 * - This function is matching the permissions of /user/profil.php
760 * - It is also matching some permissions from /user/editadvanced.php for the following fields:
761 * auth, confirmed, idnumber, lang, theme, timezone, mailformat
763 * @param array $userids array of user ids
764 * @return array An array of arrays describing users
765 * @since Moodle 2.2
766 * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
767 * @see core_user_external::get_users_by_field()
769 public static function get_users_by_id($userids) {
770 global $CFG, $USER, $DB;
771 require_once($CFG->dirroot . "/user/lib.php");
773 $params = self::validate_parameters(self::get_users_by_id_parameters(),
774 array('userids' => $userids));
776 list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
777 $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
778 $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
779 $params['contextlevel'] = CONTEXT_USER;
780 $usersql = "SELECT u.* $uselect
781 FROM {user} u $ujoin
782 WHERE u.id $sqluserids";
783 $users = $DB->get_recordset_sql($usersql, $params);
785 $result = array();
786 $hasuserupdatecap = has_capability('moodle/user:update', context_system::instance());
787 foreach ($users as $user) {
788 if (!empty($user->deleted)) {
789 continue;
791 context_helper::preload_from_record($user);
792 $usercontext = context_user::instance($user->id, IGNORE_MISSING);
793 self::validate_context($usercontext);
794 $currentuser = ($user->id == $USER->id);
796 if ($userarray = user_get_user_details($user)) {
797 // Fields matching permissions from /user/editadvanced.php.
798 if ($currentuser or $hasuserupdatecap) {
799 $userarray['auth'] = $user->auth;
800 $userarray['confirmed'] = $user->confirmed;
801 $userarray['idnumber'] = $user->idnumber;
802 $userarray['lang'] = $user->lang;
803 $userarray['theme'] = $user->theme;
804 $userarray['timezone'] = $user->timezone;
805 $userarray['mailformat'] = $user->mailformat;
807 $result[] = $userarray;
810 $users->close();
812 return $result;
816 * Returns description of method result value
818 * @return external_description
819 * @since Moodle 2.2
820 * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
821 * @see core_user_external::get_users_by_field_returns()
823 public static function get_users_by_id_returns() {
824 $additionalfields = array (
825 'enrolledcourses' => new external_multiple_structure(
826 new external_single_structure(
827 array(
828 'id' => new external_value(PARAM_INT, 'Id of the course'),
829 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
830 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
832 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL));
833 return new external_multiple_structure(self::user_description($additionalfields));
837 * Marking the method as deprecated.
839 * @return bool
841 public static function get_users_by_id_is_deprecated() {
842 return true;
846 * Returns description of method parameters
848 * @return external_function_parameters
849 * @since Moodle 2.2
851 public static function get_course_user_profiles_parameters() {
852 return new external_function_parameters(
853 array(
854 'userlist' => new external_multiple_structure(
855 new external_single_structure(
856 array(
857 'userid' => new external_value(PARAM_INT, 'userid'),
858 'courseid' => new external_value(PARAM_INT, 'courseid'),
867 * Get course participant's details
869 * @param array $userlist array of user ids and according course ids
870 * @return array An array of arrays describing course participants
871 * @since Moodle 2.2
873 public static function get_course_user_profiles($userlist) {
874 global $CFG, $USER, $DB;
875 require_once($CFG->dirroot . "/user/lib.php");
876 $params = self::validate_parameters(self::get_course_user_profiles_parameters(), array('userlist' => $userlist));
878 $userids = array();
879 $courseids = array();
880 foreach ($params['userlist'] as $value) {
881 $userids[] = $value['userid'];
882 $courseids[$value['userid']] = $value['courseid'];
885 // Cache all courses.
886 $courses = array();
887 list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids), SQL_PARAMS_NAMED);
888 $cselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
889 $cjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
890 $params['contextlevel'] = CONTEXT_COURSE;
891 $coursesql = "SELECT c.* $cselect
892 FROM {course} c $cjoin
893 WHERE c.id $sqlcourseids";
894 $rs = $DB->get_recordset_sql($coursesql, $params);
895 foreach ($rs as $course) {
896 // Adding course contexts to cache.
897 context_helper::preload_from_record($course);
898 // Cache courses.
899 $courses[$course->id] = $course;
901 $rs->close();
903 list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
904 $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
905 $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
906 $params['contextlevel'] = CONTEXT_USER;
907 $usersql = "SELECT u.* $uselect
908 FROM {user} u $ujoin
909 WHERE u.id $sqluserids";
910 $users = $DB->get_recordset_sql($usersql, $params);
911 $result = array();
912 foreach ($users as $user) {
913 if (!empty($user->deleted)) {
914 continue;
916 context_helper::preload_from_record($user);
917 $course = $courses[$courseids[$user->id]];
918 $context = context_course::instance($courseids[$user->id], IGNORE_MISSING);
919 self::validate_context($context);
920 if ($userarray = user_get_user_details($user, $course)) {
921 $result[] = $userarray;
925 $users->close();
927 return $result;
931 * Returns description of method result value
933 * @return external_description
934 * @since Moodle 2.2
936 public static function get_course_user_profiles_returns() {
937 $additionalfields = array(
938 'groups' => new external_multiple_structure(
939 new external_single_structure(
940 array(
941 'id' => new external_value(PARAM_INT, 'group id'),
942 'name' => new external_value(PARAM_RAW, 'group name'),
943 'description' => new external_value(PARAM_RAW, 'group description'),
944 'descriptionformat' => new external_format_value('description'),
946 ), 'user groups', VALUE_OPTIONAL),
947 'roles' => new external_multiple_structure(
948 new external_single_structure(
949 array(
950 'roleid' => new external_value(PARAM_INT, 'role id'),
951 'name' => new external_value(PARAM_RAW, 'role name'),
952 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
953 'sortorder' => new external_value(PARAM_INT, 'role sortorder')
955 ), 'user roles', VALUE_OPTIONAL),
956 'enrolledcourses' => new external_multiple_structure(
957 new external_single_structure(
958 array(
959 'id' => new external_value(PARAM_INT, 'Id of the course'),
960 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
961 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
963 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
966 return new external_multiple_structure(self::user_description($additionalfields));
970 * Create user return value description.
972 * @param array $additionalfields some additional field
973 * @return single_structure_description
975 public static function user_description($additionalfields = array()) {
976 $userfields = array(
977 'id' => new external_value(PARAM_INT, 'ID of the user'),
978 'username' => new external_value(PARAM_RAW, 'The username', VALUE_OPTIONAL),
979 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
980 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
981 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
982 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
983 'address' => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL),
984 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
985 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
986 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
987 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
988 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
989 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
990 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
991 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
992 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
993 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
994 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
995 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
996 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
997 'auth' => new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL),
998 'confirmed' => new external_value(PARAM_INT, 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL),
999 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL),
1000 'calendartype' => new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL),
1001 'theme' => new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
1002 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
1003 'mailformat' => new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
1004 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
1005 'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
1006 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
1007 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
1008 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
1009 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
1010 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
1011 'customfields' => new external_multiple_structure(
1012 new external_single_structure(
1013 array(
1014 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
1015 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
1016 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
1017 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
1019 ), 'User custom fields (also known as user profile fields)', VALUE_OPTIONAL),
1020 'preferences' => new external_multiple_structure(
1021 new external_single_structure(
1022 array(
1023 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
1024 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
1026 ), 'Users preferences', VALUE_OPTIONAL)
1028 if (!empty($additionalfields)) {
1029 $userfields = array_merge($userfields, $additionalfields);
1031 return new external_single_structure($userfields);
1035 * Returns description of method parameters
1037 * @return external_function_parameters
1038 * @since Moodle 2.6
1040 public static function add_user_private_files_parameters() {
1041 return new external_function_parameters(
1042 array(
1043 'draftid' => new external_value(PARAM_INT, 'draft area id')
1049 * Copy files from a draft area to users private files area.
1051 * @throws invalid_parameter_exception
1052 * @param int $draftid Id of a draft area containing files.
1053 * @return array An array of warnings
1054 * @since Moodle 2.6
1056 public static function add_user_private_files($draftid) {
1057 global $CFG, $USER, $DB;
1059 require_once($CFG->dirroot . "/user/lib.php");
1060 $params = self::validate_parameters(self::add_user_private_files_parameters(), array('draftid' => $draftid));
1062 if (isguestuser()) {
1063 throw new invalid_parameter_exception('Guest users cannot upload files');
1066 $context = context_user::instance($USER->id);
1067 require_capability('moodle/user:manageownfiles', $context);
1069 $maxbytes = $CFG->userquota;
1070 $maxareabytes = $CFG->userquota;
1071 if (has_capability('moodle/user:ignoreuserquota', $context)) {
1072 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
1073 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
1076 $options = array('subdirs' => 1,
1077 'maxbytes' => $maxbytes,
1078 'maxfiles' => -1,
1079 'accepted_types' => '*',
1080 'areamaxbytes' => $maxareabytes);
1082 file_save_draft_area_files($draftid, $context->id, 'user', 'private', 0, $options);
1084 return null;
1088 * Returns description of method result value
1090 * @return external_description
1091 * @since Moodle 2.2
1093 public static function add_user_private_files_returns() {
1094 return null;
1098 * Returns description of method parameters.
1100 * @return external_function_parameters
1101 * @since Moodle 2.6
1103 public static function add_user_device_parameters() {
1104 return new external_function_parameters(
1105 array(
1106 'appid' => new external_value(PARAM_NOTAGS, 'the app id, usually something like com.moodle.moodlemobile'),
1107 'name' => new external_value(PARAM_NOTAGS, 'the device name, \'occam\' or \'iPhone\' etc.'),
1108 'model' => new external_value(PARAM_NOTAGS, 'the device model \'Nexus4\' or \'iPad1,1\' etc.'),
1109 'platform' => new external_value(PARAM_NOTAGS, 'the device platform \'iOS\' or \'Android\' etc.'),
1110 'version' => new external_value(PARAM_NOTAGS, 'the device version \'6.1.2\' or \'4.2.2\' etc.'),
1111 'pushid' => new external_value(PARAM_RAW, 'the device PUSH token/key/identifier/registration id'),
1112 'uuid' => new external_value(PARAM_RAW, 'the device UUID')
1118 * Add a user device in Moodle database (for PUSH notifications usually).
1120 * @throws moodle_exception
1121 * @param string $appid The app id, usually something like com.moodle.moodlemobile.
1122 * @param string $name The device name, occam or iPhone etc.
1123 * @param string $model The device model Nexus4 or iPad1.1 etc.
1124 * @param string $platform The device platform iOs or Android etc.
1125 * @param string $version The device version 6.1.2 or 4.2.2 etc.
1126 * @param string $pushid The device PUSH token/key/identifier/registration id.
1127 * @param string $uuid The device UUID.
1128 * @return array List of possible warnings.
1129 * @since Moodle 2.6
1131 public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) {
1132 global $CFG, $USER, $DB;
1133 require_once($CFG->dirroot . "/user/lib.php");
1135 $params = self::validate_parameters(self::add_user_device_parameters(),
1136 array('appid' => $appid,
1137 'name' => $name,
1138 'model' => $model,
1139 'platform' => $platform,
1140 'version' => $version,
1141 'pushid' => $pushid,
1142 'uuid' => $uuid
1145 $warnings = array();
1147 // Prevent duplicate keys for users.
1148 if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'userid' => $USER->id))) {
1149 $warnings['warning'][] = array(
1150 'item' => $params['pushid'],
1151 'warningcode' => 'existingkeyforthisuser',
1152 'message' => 'This key is already stored for this user'
1154 return $warnings;
1157 // Notice that we can have multiple devices because previously it was allowed to have repeated ones.
1158 // Since we don't have a clear way to decide which one is the more appropiate, we update all.
1159 if ($userdevices = $DB->get_records('user_devices', array('uuid' => $params['uuid'],
1160 'appid' => $params['appid'], 'userid' => $USER->id))) {
1162 foreach ($userdevices as $userdevice) {
1163 $userdevice->version = $params['version']; // Maybe the user upgraded the device.
1164 $userdevice->pushid = $params['pushid'];
1165 $userdevice->timemodified = time();
1166 $DB->update_record('user_devices', $userdevice);
1169 } else {
1170 $userdevice = new stdclass;
1171 $userdevice->userid = $USER->id;
1172 $userdevice->appid = $params['appid'];
1173 $userdevice->name = $params['name'];
1174 $userdevice->model = $params['model'];
1175 $userdevice->platform = $params['platform'];
1176 $userdevice->version = $params['version'];
1177 $userdevice->pushid = $params['pushid'];
1178 $userdevice->uuid = $params['uuid'];
1179 $userdevice->timecreated = time();
1180 $userdevice->timemodified = $userdevice->timecreated;
1182 if (!$DB->insert_record('user_devices', $userdevice)) {
1183 throw new moodle_exception("There was a problem saving in the database the device with key: " . $params['pushid']);
1187 return $warnings;
1191 * Returns description of method result value.
1193 * @return external_multiple_structure
1194 * @since Moodle 2.6
1196 public static function add_user_device_returns() {
1197 return new external_multiple_structure(
1198 new external_warnings()
1203 * Returns description of method parameters.
1205 * @return external_function_parameters
1206 * @since Moodle 2.9
1208 public static function remove_user_device_parameters() {
1209 return new external_function_parameters(
1210 array(
1211 'uuid' => new external_value(PARAM_RAW, 'the device UUID'),
1212 'appid' => new external_value(PARAM_NOTAGS,
1213 'the app id, if empty devices matching the UUID for the user will be removed',
1214 VALUE_DEFAULT, ''),
1220 * Remove a user device from the Moodle database (for PUSH notifications usually).
1222 * @param string $uuid The device UUID.
1223 * @param string $appid The app id, opitonal parameter. If empty all the devices fmatching the UUID or the user will be removed.
1224 * @return array List of possible warnings and removal status.
1225 * @since Moodle 2.9
1227 public static function remove_user_device($uuid, $appid = "") {
1228 global $CFG;
1229 require_once($CFG->dirroot . "/user/lib.php");
1231 $params = self::validate_parameters(self::remove_user_device_parameters(), array('uuid' => $uuid, 'appid' => $appid));
1233 $context = context_system::instance();
1234 self::validate_context($context);
1236 // Warnings array, it can be empty at the end but is mandatory.
1237 $warnings = array();
1239 $removed = user_remove_user_device($params['uuid'], $params['appid']);
1241 if (!$removed) {
1242 $warnings[] = array(
1243 'item' => $params['uuid'],
1244 'warningcode' => 'devicedoesnotexist',
1245 'message' => 'The device doesn\'t exists in the database'
1249 $result = array(
1250 'removed' => $removed,
1251 'warnings' => $warnings
1254 return $result;
1258 * Returns description of method result value.
1260 * @return external_multiple_structure
1261 * @since Moodle 2.9
1263 public static function remove_user_device_returns() {
1264 return new external_single_structure(
1265 array(
1266 'removed' => new external_value(PARAM_BOOL, 'True if removed, false if not removed because it doesn\'t exists'),
1267 'warnings' => new external_warnings(),
1273 * Returns description of method parameters
1275 * @return external_function_parameters
1276 * @since Moodle 2.9
1278 public static function view_user_list_parameters() {
1279 return new external_function_parameters(
1280 array(
1281 'courseid' => new external_value(PARAM_INT, 'id of the course, 0 for site')
1287 * Trigger the user_list_viewed event.
1289 * @param int $courseid id of course
1290 * @return array of warnings and status result
1291 * @since Moodle 2.9
1292 * @throws moodle_exception
1294 public static function view_user_list($courseid) {
1295 global $CFG;
1296 require_once($CFG->dirroot . "/user/lib.php");
1298 $params = self::validate_parameters(self::view_user_list_parameters(),
1299 array(
1300 'courseid' => $courseid
1303 $warnings = array();
1305 if (empty($params['courseid'])) {
1306 $params['courseid'] = SITEID;
1309 $course = get_course($params['courseid']);
1311 if ($course->id == SITEID) {
1312 $context = context_system::instance();
1313 } else {
1314 $context = context_course::instance($course->id);
1316 self::validate_context($context);
1318 if ($course->id == SITEID) {
1319 require_capability('moodle/site:viewparticipants', $context);
1320 } else {
1321 require_capability('moodle/course:viewparticipants', $context);
1324 user_list_view($course, $context);
1326 $result = array();
1327 $result['status'] = true;
1328 $result['warnings'] = $warnings;
1329 return $result;
1333 * Returns description of method result value
1335 * @return external_description
1336 * @since Moodle 2.9
1338 public static function view_user_list_returns() {
1339 return new external_single_structure(
1340 array(
1341 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
1342 'warnings' => new external_warnings()
1348 * Returns description of method parameters
1350 * @return external_function_parameters
1351 * @since Moodle 2.9
1353 public static function view_user_profile_parameters() {
1354 return new external_function_parameters(
1355 array(
1356 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_REQUIRED),
1357 'courseid' => new external_value(PARAM_INT, 'id of the course, default site course', VALUE_DEFAULT, 0)
1363 * Trigger the user profile viewed event.
1365 * @param int $userid id of user
1366 * @param int $courseid id of course
1367 * @return array of warnings and status result
1368 * @since Moodle 2.9
1369 * @throws moodle_exception
1371 public static function view_user_profile($userid, $courseid = 0) {
1372 global $CFG, $USER;
1373 require_once($CFG->dirroot . "/user/profile/lib.php");
1375 $params = self::validate_parameters(self::view_user_profile_parameters(),
1376 array(
1377 'userid' => $userid,
1378 'courseid' => $courseid
1381 $warnings = array();
1383 if (empty($params['userid'])) {
1384 $params['userid'] = $USER->id;
1387 if (empty($params['courseid'])) {
1388 $params['courseid'] = SITEID;
1391 $course = get_course($params['courseid']);
1392 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
1393 core_user::require_active_user($user);
1395 if ($course->id == SITEID) {
1396 $coursecontext = context_system::instance();;
1397 } else {
1398 $coursecontext = context_course::instance($course->id);
1400 self::validate_context($coursecontext);
1402 $currentuser = $USER->id == $user->id;
1403 $usercontext = context_user::instance($user->id);
1405 if (!$currentuser and
1406 !has_capability('moodle/user:viewdetails', $coursecontext) and
1407 !has_capability('moodle/user:viewdetails', $usercontext)) {
1408 throw new moodle_exception('cannotviewprofile');
1411 // Case like user/profile.php.
1412 if ($course->id == SITEID) {
1413 profile_view($user, $usercontext);
1414 } else {
1415 // Case like user/view.php.
1416 if (!$currentuser and !can_access_course($course, $user, '', true)) {
1417 throw new moodle_exception('notenrolledprofile');
1420 profile_view($user, $coursecontext, $course);
1423 $result = array();
1424 $result['status'] = true;
1425 $result['warnings'] = $warnings;
1426 return $result;
1430 * Returns description of method result value
1432 * @return external_description
1433 * @since Moodle 2.9
1435 public static function view_user_profile_returns() {
1436 return new external_single_structure(
1437 array(
1438 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
1439 'warnings' => new external_warnings()
1447 * Deprecated user external functions
1449 * @package core_user
1450 * @copyright 2009 Petr Skodak
1451 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1452 * @since Moodle 2.0
1453 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
1454 * @see core_user_external
1456 class moodle_user_external extends external_api {
1459 * Returns description of method parameters
1461 * @return external_function_parameters
1462 * @since Moodle 2.0
1463 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1464 * @see core_user_external::create_users_parameters()
1466 public static function create_users_parameters() {
1467 return core_user_external::create_users_parameters();
1471 * Create one or more users
1473 * @param array $users An array of users to create.
1474 * @return array An array of arrays
1475 * @since Moodle 2.0
1476 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1477 * @see core_user_external::create_users()
1479 public static function create_users($users) {
1480 return core_user_external::create_users($users);
1484 * Returns description of method result value
1486 * @return external_description
1487 * @since Moodle 2.0
1488 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1489 * @see core_user_external::create_users_returns()
1491 public static function create_users_returns() {
1492 return core_user_external::create_users_returns();
1496 * Marking the method as deprecated.
1498 * @return bool
1500 public static function create_users_is_deprecated() {
1501 return true;
1505 * Returns description of method parameters
1507 * @return external_function_parameters
1508 * @since Moodle 2.0
1509 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1510 * @see core_user_external::delete_users_parameters()
1512 public static function delete_users_parameters() {
1513 return core_user_external::delete_users_parameters();
1517 * Delete users
1519 * @param array $userids
1520 * @return null
1521 * @since Moodle 2.0
1522 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1523 * @see core_user_external::delete_users()
1525 public static function delete_users($userids) {
1526 return core_user_external::delete_users($userids);
1530 * Returns description of method result value
1532 * @return null
1533 * @since Moodle 2.0
1534 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1535 * @see core_user_external::delete_users_returns()
1537 public static function delete_users_returns() {
1538 return core_user_external::delete_users_returns();
1542 * Marking the method as deprecated.
1544 * @return bool
1546 public static function delete_users_is_deprecated() {
1547 return true;
1551 * Returns description of method parameters
1553 * @return external_function_parameters
1554 * @since Moodle 2.0
1555 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1556 * @see core_user_external::update_users_parameters()
1558 public static function update_users_parameters() {
1559 return core_user_external::update_users_parameters();
1563 * Update users
1565 * @param array $users
1566 * @return null
1567 * @since Moodle 2.0
1568 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1569 * @see core_user_external::update_users()
1571 public static function update_users($users) {
1572 return core_user_external::update_users($users);
1576 * Returns description of method result value
1578 * @return null
1579 * @since Moodle 2.0
1580 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1581 * @see core_user_external::update_users_returns()
1583 public static function update_users_returns() {
1584 return core_user_external::update_users_returns();
1588 * Marking the method as deprecated.
1590 * @return bool
1592 public static function update_users_is_deprecated() {
1593 return true;
1597 * Returns description of method parameters
1599 * @return external_function_parameters
1600 * @since Moodle 2.0
1601 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1602 * @see core_user_external::get_users_by_id_parameters()
1604 public static function get_users_by_id_parameters() {
1605 return core_user_external::get_users_by_id_parameters();
1609 * Get user information
1610 * - This function is matching the permissions of /user/profil.php
1611 * - It is also matching some permissions from /user/editadvanced.php for the following fields:
1612 * auth, confirmed, idnumber, lang, theme, timezone, mailformat
1614 * @param array $userids array of user ids
1615 * @return array An array of arrays describing users
1616 * @since Moodle 2.0
1617 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1618 * @see core_user_external::get_users_by_id()
1620 public static function get_users_by_id($userids) {
1621 return core_user_external::get_users_by_id($userids);
1625 * Returns description of method result value
1627 * @return external_description
1628 * @since Moodle 2.0
1629 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1630 * @see core_user_external::get_users_by_id_returns()
1632 public static function get_users_by_id_returns() {
1633 return core_user_external::get_users_by_id_returns();
1637 * Marking the method as deprecated.
1639 * @return bool
1641 public static function get_users_by_id_is_deprecated() {
1642 return true;
1646 * Returns description of method parameters
1648 * @return external_function_parameters
1649 * @since Moodle 2.1
1650 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1651 * @see core_user_external::get_course_user_profiles_parameters()
1653 public static function get_course_participants_by_id_parameters() {
1654 return core_user_external::get_course_user_profiles_parameters();
1658 * Get course participant's details
1660 * @param array $userlist array of user ids and according course ids
1661 * @return array An array of arrays describing course participants
1662 * @since Moodle 2.1
1663 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1664 * @see core_user_external::get_course_user_profiles()
1666 public static function get_course_participants_by_id($userlist) {
1667 return core_user_external::get_course_user_profiles($userlist);
1671 * Returns description of method result value
1673 * @return external_description
1674 * @since Moodle 2.1
1675 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1676 * @see core_user_external::get_course_user_profiles_returns()
1678 public static function get_course_participants_by_id_returns() {
1679 return core_user_external::get_course_user_profiles_returns();
1683 * Marking the method as deprecated.
1685 * @return bool
1687 public static function get_course_participants_by_id_is_deprecated() {
1688 return true;
1692 * Returns description of method parameters
1694 * @return external_function_parameters
1695 * @since Moodle 2.1
1696 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1697 * @see core_enrol_external::get_enrolled_users_parameters()
1699 public static function get_users_by_courseid_parameters() {
1700 global $CFG;
1701 require_once($CFG->dirroot . '/enrol/externallib.php');
1702 return core_enrol_external::get_enrolled_users_parameters();
1706 * Get course participants details
1708 * @param int $courseid course id
1709 * @param array $options options {
1710 * 'name' => option name
1711 * 'value' => option value
1713 * @return array An array of users
1714 * @since Moodle 2.1
1715 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1716 * @see core_enrol_external::get_enrolled_users()
1718 public static function get_users_by_courseid($courseid, $options = array()) {
1719 global $CFG;
1720 require_once($CFG->dirroot . '/enrol/externallib.php');
1721 return core_enrol_external::get_enrolled_users($courseid, $options);
1724 * Returns description of method result value
1726 * @return external_description
1727 * @since Moodle 2.1
1728 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1729 * @see core_enrol_external::get_enrolled_users_returns()
1731 public static function get_users_by_courseid_returns() {
1732 global $CFG;
1733 require_once($CFG->dirroot . '/enrol/externallib.php');
1734 return core_enrol_external::get_enrolled_users_returns();
1738 * Marking the method as deprecated.
1740 * @return bool
1742 public static function get_users_by_courseid_is_deprecated() {
1743 return true;