MDL-30315 grades: Unit tests for grade import.
[moodle.git] / user / externallib.php
blobcb617b47189f1f43daa6e31f3655d46d1adeb9f0
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'),
57 'firstname' =>
58 new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
59 'lastname' =>
60 new external_value(PARAM_NOTAGS, 'The family name of the user'),
61 'email' =>
62 new external_value(PARAM_EMAIL, 'A valid and unique email address'),
63 'auth' =>
64 new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT,
65 'manual', NULL_NOT_ALLOWED),
66 'idnumber' =>
67 new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
68 VALUE_DEFAULT, ''),
69 'lang' =>
70 new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_DEFAULT,
71 $CFG->lang, NULL_NOT_ALLOWED),
72 'calendartype' =>
73 new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
74 VALUE_DEFAULT, $CFG->calendartype, VALUE_OPTIONAL),
75 'theme' =>
76 new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
77 VALUE_OPTIONAL),
78 'timezone' =>
79 new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
80 VALUE_OPTIONAL),
81 'mailformat' =>
82 new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
83 VALUE_OPTIONAL),
84 'description' =>
85 new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
86 'city' =>
87 new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
88 'country' =>
89 new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
90 'firstnamephonetic' =>
91 new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
92 'lastnamephonetic' =>
93 new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
94 'middlename' =>
95 new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
96 'alternatename' =>
97 new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
98 'preferences' => new external_multiple_structure(
99 new external_single_structure(
100 array(
101 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
102 'value' => new external_value(PARAM_RAW, 'The value of the preference')
104 ), 'User preferences', VALUE_OPTIONAL),
105 'customfields' => new external_multiple_structure(
106 new external_single_structure(
107 array(
108 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
109 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
111 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL)
120 * Create one or more users.
122 * @throws invalid_parameter_exception
123 * @param array $users An array of users to create.
124 * @return array An array of arrays
125 * @since Moodle 2.2
127 public static function create_users($users) {
128 global $CFG, $DB;
129 require_once($CFG->dirroot."/lib/weblib.php");
130 require_once($CFG->dirroot."/user/lib.php");
131 require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function.
133 // Ensure the current user is allowed to run this function.
134 $context = context_system::instance();
135 self::validate_context($context);
136 require_capability('moodle/user:create', $context);
138 // Do basic automatic PARAM checks on incoming data, using params description.
139 // If any problems are found then exceptions are thrown with helpful error messages.
140 $params = self::validate_parameters(self::create_users_parameters(), array('users' => $users));
142 $availableauths = core_component::get_plugin_list('auth');
143 unset($availableauths['mnet']); // These would need mnethostid too.
144 unset($availableauths['webservice']); // We do not want new webservice users for now.
146 $availablethemes = core_component::get_plugin_list('theme');
147 $availablelangs = get_string_manager()->get_list_of_translations();
149 $transaction = $DB->start_delegated_transaction();
151 $userids = array();
152 foreach ($params['users'] as $user) {
153 // Make sure that the username doesn't already exist.
154 if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) {
155 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
158 // Make sure auth is valid.
159 if (empty($availableauths[$user['auth']])) {
160 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
163 // Make sure lang is valid.
164 if (empty($availablelangs[$user['lang']])) {
165 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
168 // Make sure lang is valid.
169 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { // Theme is VALUE_OPTIONAL,
170 // so no default value
171 // We need to test if the client sent it
172 // => !empty($user['theme']).
173 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
176 $user['confirmed'] = true;
177 $user['mnethostid'] = $CFG->mnet_localhost_id;
179 // Start of user info validation.
180 // Make sure we validate current user info as handled by current GUI. See user/editadvanced_form.php func validation().
181 if (!validate_email($user['email'])) {
182 throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
183 } else if ($DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $user['mnethostid']))) {
184 throw new invalid_parameter_exception('Email address already exists: '.$user['email']);
186 // End of user info validation.
188 // Create the user data now!
189 $user['id'] = user_create_user($user, true, false);
191 // Custom fields.
192 if (!empty($user['customfields'])) {
193 foreach ($user['customfields'] as $customfield) {
194 // Profile_save_data() saves profile file it's expecting a user with the correct id,
195 // and custom field to be named profile_field_"shortname".
196 $user["profile_field_".$customfield['type']] = $customfield['value'];
198 profile_save_data((object) $user);
201 // Trigger event.
202 \core\event\user_created::create_from_userid($user['id'])->trigger();
204 // Preferences.
205 if (!empty($user['preferences'])) {
206 foreach ($user['preferences'] as $preference) {
207 set_user_preference($preference['type'], $preference['value'], $user['id']);
211 $userids[] = array('id' => $user['id'], 'username' => $user['username']);
214 $transaction->allow_commit();
216 return $userids;
220 * Returns description of method result value
222 * @return external_description
223 * @since Moodle 2.2
225 public static function create_users_returns() {
226 return new external_multiple_structure(
227 new external_single_structure(
228 array(
229 'id' => new external_value(PARAM_INT, 'user id'),
230 'username' => new external_value(PARAM_USERNAME, 'user name'),
238 * Returns description of method parameters
240 * @return external_function_parameters
241 * @since Moodle 2.2
243 public static function delete_users_parameters() {
244 return new external_function_parameters(
245 array(
246 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
252 * Delete users
254 * @throws moodle_exception
255 * @param array $userids
256 * @return null
257 * @since Moodle 2.2
259 public static function delete_users($userids) {
260 global $CFG, $DB, $USER;
261 require_once($CFG->dirroot."/user/lib.php");
263 // Ensure the current user is allowed to run this function.
264 $context = context_system::instance();
265 require_capability('moodle/user:delete', $context);
266 self::validate_context($context);
268 $params = self::validate_parameters(self::delete_users_parameters(), array('userids' => $userids));
270 $transaction = $DB->start_delegated_transaction();
272 foreach ($params['userids'] as $userid) {
273 $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST);
274 // Must not allow deleting of admins or self!!!
275 if (is_siteadmin($user)) {
276 throw new moodle_exception('useradminodelete', 'error');
278 if ($USER->id == $user->id) {
279 throw new moodle_exception('usernotdeletederror', 'error');
281 user_delete_user($user);
284 $transaction->allow_commit();
286 return null;
290 * Returns description of method result value
292 * @return null
293 * @since Moodle 2.2
295 public static function delete_users_returns() {
296 return null;
301 * Returns description of method parameters
303 * @return external_function_parameters
304 * @since Moodle 2.2
306 public static function update_users_parameters() {
307 return new external_function_parameters(
308 array(
309 'users' => new external_multiple_structure(
310 new external_single_structure(
311 array(
312 'id' =>
313 new external_value(PARAM_INT, 'ID of the user'),
314 'username' =>
315 new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config.',
316 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
317 'password' =>
318 new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL,
319 '', NULL_NOT_ALLOWED),
320 'firstname' =>
321 new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',
322 NULL_NOT_ALLOWED),
323 'lastname' =>
324 new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
325 'email' =>
326 new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',
327 NULL_NOT_ALLOWED),
328 'auth' =>
329 new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '',
330 NULL_NOT_ALLOWED),
331 'idnumber' =>
332 new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution',
333 VALUE_OPTIONAL),
334 'lang' =>
335 new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server',
336 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
337 'calendartype' =>
338 new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server',
339 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
340 'theme' =>
341 new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server',
342 VALUE_OPTIONAL),
343 'timezone' =>
344 new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default',
345 VALUE_OPTIONAL),
346 'mailformat' =>
347 new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc',
348 VALUE_OPTIONAL),
349 'description' =>
350 new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
351 'city' =>
352 new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
353 'country' =>
354 new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
355 'firstnamephonetic' =>
356 new external_value(PARAM_NOTAGS, 'The first name(s) phonetically of the user', VALUE_OPTIONAL),
357 'lastnamephonetic' =>
358 new external_value(PARAM_NOTAGS, 'The family name phonetically of the user', VALUE_OPTIONAL),
359 'middlename' =>
360 new external_value(PARAM_NOTAGS, 'The middle name of the user', VALUE_OPTIONAL),
361 'alternatename' =>
362 new external_value(PARAM_NOTAGS, 'The alternate name of the user', VALUE_OPTIONAL),
363 'customfields' => new external_multiple_structure(
364 new external_single_structure(
365 array(
366 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
367 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
369 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
370 'preferences' => new external_multiple_structure(
371 new external_single_structure(
372 array(
373 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
374 'value' => new external_value(PARAM_RAW, 'The value of the preference')
376 ), 'User preferences', VALUE_OPTIONAL),
385 * Update users
387 * @param array $users
388 * @return null
389 * @since Moodle 2.2
391 public static function update_users($users) {
392 global $CFG, $DB;
393 require_once($CFG->dirroot."/user/lib.php");
394 require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function.
396 // Ensure the current user is allowed to run this function.
397 $context = context_system::instance();
398 require_capability('moodle/user:update', $context);
399 self::validate_context($context);
401 $params = self::validate_parameters(self::update_users_parameters(), array('users' => $users));
403 $transaction = $DB->start_delegated_transaction();
405 foreach ($params['users'] as $user) {
406 user_update_user($user, true, false);
407 // Update user custom fields.
408 if (!empty($user['customfields'])) {
410 foreach ($user['customfields'] as $customfield) {
411 // Profile_save_data() saves profile file it's expecting a user with the correct id,
412 // and custom field to be named profile_field_"shortname".
413 $user["profile_field_".$customfield['type']] = $customfield['value'];
415 profile_save_data((object) $user);
418 // Trigger event.
419 \core\event\user_updated::create_from_userid($user['id'])->trigger();
421 // Preferences.
422 if (!empty($user['preferences'])) {
423 foreach ($user['preferences'] as $preference) {
424 set_user_preference($preference['type'], $preference['value'], $user['id']);
429 $transaction->allow_commit();
431 return null;
435 * Returns description of method result value
437 * @return null
438 * @since Moodle 2.2
440 public static function update_users_returns() {
441 return null;
445 * Returns description of method parameters
447 * @return external_function_parameters
448 * @since Moodle 2.4
450 public static function get_users_by_field_parameters() {
451 return new external_function_parameters(
452 array(
453 'field' => new external_value(PARAM_ALPHA, 'the search field can be
454 \'id\' or \'idnumber\' or \'username\' or \'email\''),
455 'values' => new external_multiple_structure(
456 new external_value(PARAM_RAW, 'the value to match'))
462 * Get user information for a unique field.
464 * @throws coding_exception
465 * @throws invalid_parameter_exception
466 * @param string $field
467 * @param array $values
468 * @return array An array of arrays containg user profiles.
469 * @since Moodle 2.4
471 public static function get_users_by_field($field, $values) {
472 global $CFG, $USER, $DB;
473 require_once($CFG->dirroot . "/user/lib.php");
475 $params = self::validate_parameters(self::get_users_by_field_parameters(),
476 array('field' => $field, 'values' => $values));
478 // This array will keep all the users that are allowed to be searched,
479 // according to the current user's privileges.
480 $cleanedvalues = array();
482 switch ($field) {
483 case 'id':
484 $paramtype = PARAM_INT;
485 break;
486 case 'idnumber':
487 $paramtype = PARAM_RAW;
488 break;
489 case 'username':
490 $paramtype = PARAM_RAW;
491 break;
492 case 'email':
493 $paramtype = PARAM_EMAIL;
494 break;
495 default:
496 throw new coding_exception('invalid field parameter',
497 'The search field \'' . $field . '\' is not supported, look at the web service documentation');
500 // Clean the values.
501 foreach ($values as $value) {
502 $cleanedvalue = clean_param($value, $paramtype);
503 if ( $value != $cleanedvalue) {
504 throw new invalid_parameter_exception('The field \'' . $field .
505 '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')');
507 $cleanedvalues[] = $cleanedvalue;
510 // Retrieve the users.
511 $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id');
513 // Finally retrieve each users information.
514 $returnedusers = array();
515 foreach ($users as $user) {
516 $userdetails = user_get_user_details_courses($user);
518 // Return the user only if the searched field is returned.
519 // Otherwise it means that the $USER was not allowed to search the returned user.
520 if (!empty($userdetails) and !empty($userdetails[$field])) {
521 $returnedusers[] = $userdetails;
525 return $returnedusers;
529 * Returns description of method result value
531 * @return external_multiple_structure
532 * @since Moodle 2.4
534 public static function get_users_by_field_returns() {
535 return new external_multiple_structure(self::user_description());
540 * Returns description of get_users() parameters.
542 * @return external_function_parameters
543 * @since Moodle 2.5
545 public static function get_users_parameters() {
546 return new external_function_parameters(
547 array(
548 'criteria' => new external_multiple_structure(
549 new external_single_structure(
550 array(
551 'key' => new external_value(PARAM_ALPHA, 'the user column to search, expected keys (value format) are:
552 "id" (int) matching user id,
553 "lastname" (string) user last name (Note: you can use % for searching but it may be considerably slower!),
554 "firstname" (string) user first name (Note: you can use % for searching but it may be considerably slower!),
555 "idnumber" (string) matching user idnumber,
556 "username" (string) matching user username,
557 "email" (string) user email (Note: you can use % for searching but it may be considerably slower!),
558 "auth" (string) matching user auth plugin'),
559 'value' => new external_value(PARAM_RAW, 'the value to search')
561 ), 'the key/value pairs to be considered in user search. Values can not be empty.
562 Specify different keys only once (fullname => \'user1\', auth => \'manual\', ...) -
563 key occurences are forbidden.
564 The search is executed with AND operator on the criterias. Invalid criterias (keys) are ignored,
565 the search is still executed on the valid criterias.
566 You can search without criteria, but the function is not designed for it.
567 It could very slow or timeout. The function is designed to search some specific users.'
574 * Retrieve matching user.
576 * @throws moodle_exception
577 * @param array $criteria the allowed array keys are id/lastname/firstname/idnumber/username/email/auth.
578 * @return array An array of arrays containing user profiles.
579 * @since Moodle 2.5
581 public static function get_users($criteria = array()) {
582 global $CFG, $USER, $DB;
584 require_once($CFG->dirroot . "/user/lib.php");
586 $params = self::validate_parameters(self::get_users_parameters(),
587 array('criteria' => $criteria));
589 // Validate the criteria and retrieve the users.
590 $users = array();
591 $warnings = array();
592 $sqlparams = array();
593 $usedkeys = array();
595 // Do not retrieve deleted users.
596 $sql = ' deleted = 0';
598 foreach ($params['criteria'] as $criteriaindex => $criteria) {
600 // Check that the criteria has never been used.
601 if (array_key_exists($criteria['key'], $usedkeys)) {
602 throw new moodle_exception('keyalreadyset', '', '', null, 'The key ' . $criteria['key'] . ' can only be sent once');
603 } else {
604 $usedkeys[$criteria['key']] = true;
607 $invalidcriteria = false;
608 // Clean the parameters.
609 $paramtype = PARAM_RAW;
610 switch ($criteria['key']) {
611 case 'id':
612 $paramtype = PARAM_INT;
613 break;
614 case 'idnumber':
615 $paramtype = PARAM_RAW;
616 break;
617 case 'username':
618 $paramtype = PARAM_RAW;
619 break;
620 case 'email':
621 // We use PARAM_RAW to allow searches with %.
622 $paramtype = PARAM_RAW;
623 break;
624 case 'auth':
625 $paramtype = PARAM_AUTH;
626 break;
627 case 'lastname':
628 case 'firstname':
629 $paramtype = PARAM_TEXT;
630 break;
631 default:
632 // Send back a warning that this search key is not supported in this version.
633 // This warning will make the function extandable without breaking clients.
634 $warnings[] = array(
635 'item' => $criteria['key'],
636 'warningcode' => 'invalidfieldparameter',
637 'message' =>
638 'The search key \'' . $criteria['key'] . '\' is not supported, look at the web service documentation'
640 // Do not add this invalid criteria to the created SQL request.
641 $invalidcriteria = true;
642 unset($params['criteria'][$criteriaindex]);
643 break;
646 if (!$invalidcriteria) {
647 $cleanedvalue = clean_param($criteria['value'], $paramtype);
649 $sql .= ' AND ';
651 // Create the SQL.
652 switch ($criteria['key']) {
653 case 'id':
654 case 'idnumber':
655 case 'username':
656 case 'auth':
657 $sql .= $criteria['key'] . ' = :' . $criteria['key'];
658 $sqlparams[$criteria['key']] = $cleanedvalue;
659 break;
660 case 'email':
661 case 'lastname':
662 case 'firstname':
663 $sql .= $DB->sql_like($criteria['key'], ':' . $criteria['key'], false);
664 $sqlparams[$criteria['key']] = $cleanedvalue;
665 break;
666 default:
667 break;
672 $users = $DB->get_records_select('user', $sql, $sqlparams, 'id ASC');
674 // Finally retrieve each users information.
675 $returnedusers = array();
676 foreach ($users as $user) {
677 $userdetails = user_get_user_details_courses($user);
679 // Return the user only if all the searched fields are returned.
680 // Otherwise it means that the $USER was not allowed to search the returned user.
681 if (!empty($userdetails)) {
682 $validuser = true;
684 foreach ($params['criteria'] as $criteria) {
685 if (empty($userdetails[$criteria['key']])) {
686 $validuser = false;
690 if ($validuser) {
691 $returnedusers[] = $userdetails;
696 return array('users' => $returnedusers, 'warnings' => $warnings);
700 * Returns description of get_users result value.
702 * @return external_description
703 * @since Moodle 2.5
705 public static function get_users_returns() {
706 return new external_single_structure(
707 array('users' => new external_multiple_structure(
708 self::user_description()
710 'warnings' => new external_warnings('always set to \'key\'', 'faulty key name')
716 * Returns description of method parameters
718 * @return external_function_parameters
719 * @since Moodle 2.2
720 * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
721 * @see core_user_external::get_users_by_field_parameters()
723 public static function get_users_by_id_parameters() {
724 return new external_function_parameters(
725 array(
726 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
732 * Get user information
733 * - This function is matching the permissions of /user/profil.php
734 * - It is also matching some permissions from /user/editadvanced.php for the following fields:
735 * auth, confirmed, idnumber, lang, theme, timezone, mailformat
737 * @param array $userids array of user ids
738 * @return array An array of arrays describing users
739 * @since Moodle 2.2
740 * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
741 * @see core_user_external::get_users_by_field()
743 public static function get_users_by_id($userids) {
744 global $CFG, $USER, $DB;
745 require_once($CFG->dirroot . "/user/lib.php");
747 $params = self::validate_parameters(self::get_users_by_id_parameters(),
748 array('userids' => $userids));
750 list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
751 $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
752 $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
753 $params['contextlevel'] = CONTEXT_USER;
754 $usersql = "SELECT u.* $uselect
755 FROM {user} u $ujoin
756 WHERE u.id $sqluserids";
757 $users = $DB->get_recordset_sql($usersql, $params);
759 $result = array();
760 $hasuserupdatecap = has_capability('moodle/user:update', context_system::instance());
761 foreach ($users as $user) {
762 if (!empty($user->deleted)) {
763 continue;
765 context_helper::preload_from_record($user);
766 $usercontext = context_user::instance($user->id, IGNORE_MISSING);
767 self::validate_context($usercontext);
768 $currentuser = ($user->id == $USER->id);
770 if ($userarray = user_get_user_details($user)) {
771 // Fields matching permissions from /user/editadvanced.php.
772 if ($currentuser or $hasuserupdatecap) {
773 $userarray['auth'] = $user->auth;
774 $userarray['confirmed'] = $user->confirmed;
775 $userarray['idnumber'] = $user->idnumber;
776 $userarray['lang'] = $user->lang;
777 $userarray['theme'] = $user->theme;
778 $userarray['timezone'] = $user->timezone;
779 $userarray['mailformat'] = $user->mailformat;
781 $result[] = $userarray;
784 $users->close();
786 return $result;
790 * Returns description of method result value
792 * @return external_description
793 * @since Moodle 2.2
794 * @deprecated Moodle 2.5 MDL-38030 - Please do not call this function any more.
795 * @see core_user_external::get_users_by_field_returns()
797 public static function get_users_by_id_returns() {
798 $additionalfields = array (
799 'enrolledcourses' => new external_multiple_structure(
800 new external_single_structure(
801 array(
802 'id' => new external_value(PARAM_INT, 'Id of the course'),
803 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
804 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
806 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL));
807 return new external_multiple_structure(self::user_description($additionalfields));
811 * Marking the method as deprecated.
813 * @return bool
815 public static function get_users_by_id_is_deprecated() {
816 return true;
820 * Returns description of method parameters
822 * @return external_function_parameters
823 * @since Moodle 2.2
825 public static function get_course_user_profiles_parameters() {
826 return new external_function_parameters(
827 array(
828 'userlist' => new external_multiple_structure(
829 new external_single_structure(
830 array(
831 'userid' => new external_value(PARAM_INT, 'userid'),
832 'courseid' => new external_value(PARAM_INT, 'courseid'),
841 * Get course participant's details
843 * @param array $userlist array of user ids and according course ids
844 * @return array An array of arrays describing course participants
845 * @since Moodle 2.2
847 public static function get_course_user_profiles($userlist) {
848 global $CFG, $USER, $DB;
849 require_once($CFG->dirroot . "/user/lib.php");
850 $params = self::validate_parameters(self::get_course_user_profiles_parameters(), array('userlist' => $userlist));
852 $userids = array();
853 $courseids = array();
854 foreach ($params['userlist'] as $value) {
855 $userids[] = $value['userid'];
856 $courseids[$value['userid']] = $value['courseid'];
859 // Cache all courses.
860 $courses = array();
861 list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids), SQL_PARAMS_NAMED);
862 $cselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
863 $cjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
864 $params['contextlevel'] = CONTEXT_COURSE;
865 $coursesql = "SELECT c.* $cselect
866 FROM {course} c $cjoin
867 WHERE c.id $sqlcourseids";
868 $rs = $DB->get_recordset_sql($coursesql, $params);
869 foreach ($rs as $course) {
870 // Adding course contexts to cache.
871 context_helper::preload_from_record($course);
872 // Cache courses.
873 $courses[$course->id] = $course;
875 $rs->close();
877 list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
878 $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
879 $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
880 $params['contextlevel'] = CONTEXT_USER;
881 $usersql = "SELECT u.* $uselect
882 FROM {user} u $ujoin
883 WHERE u.id $sqluserids";
884 $users = $DB->get_recordset_sql($usersql, $params);
885 $result = array();
886 foreach ($users as $user) {
887 if (!empty($user->deleted)) {
888 continue;
890 context_helper::preload_from_record($user);
891 $course = $courses[$courseids[$user->id]];
892 $context = context_course::instance($courseids[$user->id], IGNORE_MISSING);
893 self::validate_context($context);
894 if ($userarray = user_get_user_details($user, $course)) {
895 $result[] = $userarray;
899 $users->close();
901 return $result;
905 * Returns description of method result value
907 * @return external_description
908 * @since Moodle 2.2
910 public static function get_course_user_profiles_returns() {
911 $additionalfields = array(
912 'groups' => new external_multiple_structure(
913 new external_single_structure(
914 array(
915 'id' => new external_value(PARAM_INT, 'group id'),
916 'name' => new external_value(PARAM_RAW, 'group name'),
917 'description' => new external_value(PARAM_RAW, 'group description'),
918 'descriptionformat' => new external_format_value('description'),
920 ), 'user groups', VALUE_OPTIONAL),
921 'roles' => new external_multiple_structure(
922 new external_single_structure(
923 array(
924 'roleid' => new external_value(PARAM_INT, 'role id'),
925 'name' => new external_value(PARAM_RAW, 'role name'),
926 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
927 'sortorder' => new external_value(PARAM_INT, 'role sortorder')
929 ), 'user roles', VALUE_OPTIONAL),
930 'enrolledcourses' => new external_multiple_structure(
931 new external_single_structure(
932 array(
933 'id' => new external_value(PARAM_INT, 'Id of the course'),
934 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
935 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
937 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
940 return new external_multiple_structure(self::user_description($additionalfields));
944 * Create user return value description.
946 * @param array $additionalfields some additional field
947 * @return single_structure_description
949 public static function user_description($additionalfields = array()) {
950 $userfields = array(
951 'id' => new external_value(PARAM_INT, 'ID of the user'),
952 'username' => new external_value(PARAM_RAW, 'The username', VALUE_OPTIONAL),
953 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
954 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
955 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
956 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
957 'address' => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL),
958 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
959 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
960 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
961 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
962 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
963 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
964 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
965 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
966 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
967 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
968 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
969 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
970 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
971 'auth' => new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL),
972 'confirmed' => new external_value(PARAM_INT, 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL),
973 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL),
974 'calendartype' => new external_value(PARAM_PLUGIN, 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL),
975 'theme' => new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
976 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
977 'mailformat' => new external_value(PARAM_INT, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
978 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
979 'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
980 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
981 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
982 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
983 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
984 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
985 'customfields' => new external_multiple_structure(
986 new external_single_structure(
987 array(
988 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
989 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
990 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
991 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
993 ), 'User custom fields (also known as user profile fields)', VALUE_OPTIONAL),
994 'preferences' => new external_multiple_structure(
995 new external_single_structure(
996 array(
997 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
998 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
1000 ), 'Users preferences', VALUE_OPTIONAL)
1002 if (!empty($additionalfields)) {
1003 $userfields = array_merge($userfields, $additionalfields);
1005 return new external_single_structure($userfields);
1009 * Returns description of method parameters
1011 * @return external_function_parameters
1012 * @since Moodle 2.6
1014 public static function add_user_private_files_parameters() {
1015 return new external_function_parameters(
1016 array(
1017 'draftid' => new external_value(PARAM_INT, 'draft area id')
1023 * Copy files from a draft area to users private files area.
1025 * @throws invalid_parameter_exception
1026 * @param int $draftid Id of a draft area containing files.
1027 * @return array An array of warnings
1028 * @since Moodle 2.6
1030 public static function add_user_private_files($draftid) {
1031 global $CFG, $USER, $DB;
1033 require_once($CFG->dirroot . "/user/lib.php");
1034 $params = self::validate_parameters(self::add_user_private_files_parameters(), array('draftid' => $draftid));
1036 if (isguestuser()) {
1037 throw new invalid_parameter_exception('Guest users cannot upload files');
1040 $context = context_user::instance($USER->id);
1041 require_capability('moodle/user:manageownfiles', $context);
1043 $maxbytes = $CFG->userquota;
1044 $maxareabytes = $CFG->userquota;
1045 if (has_capability('moodle/user:ignoreuserquota', $context)) {
1046 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS;
1047 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED;
1050 $options = array('subdirs' => 1,
1051 'maxbytes' => $maxbytes,
1052 'maxfiles' => -1,
1053 'accepted_types' => '*',
1054 'areamaxbytes' => $maxareabytes);
1056 file_save_draft_area_files($draftid, $context->id, 'user', 'private', 0, $options);
1058 return null;
1062 * Returns description of method result value
1064 * @return external_description
1065 * @since Moodle 2.2
1067 public static function add_user_private_files_returns() {
1068 return null;
1072 * Returns description of method parameters.
1074 * @return external_function_parameters
1075 * @since Moodle 2.6
1077 public static function add_user_device_parameters() {
1078 return new external_function_parameters(
1079 array(
1080 'appid' => new external_value(PARAM_NOTAGS, 'the app id, usually something like com.moodle.moodlemobile'),
1081 'name' => new external_value(PARAM_NOTAGS, 'the device name, \'occam\' or \'iPhone\' etc.'),
1082 'model' => new external_value(PARAM_NOTAGS, 'the device model \'Nexus4\' or \'iPad1,1\' etc.'),
1083 'platform' => new external_value(PARAM_NOTAGS, 'the device platform \'iOS\' or \'Android\' etc.'),
1084 'version' => new external_value(PARAM_NOTAGS, 'the device version \'6.1.2\' or \'4.2.2\' etc.'),
1085 'pushid' => new external_value(PARAM_RAW, 'the device PUSH token/key/identifier/registration id'),
1086 'uuid' => new external_value(PARAM_RAW, 'the device UUID')
1092 * Add a user device in Moodle database (for PUSH notifications usually).
1094 * @throws moodle_exception
1095 * @param string $appid The app id, usually something like com.moodle.moodlemobile.
1096 * @param string $name The device name, occam or iPhone etc.
1097 * @param string $model The device model Nexus4 or iPad1.1 etc.
1098 * @param string $platform The device platform iOs or Android etc.
1099 * @param string $version The device version 6.1.2 or 4.2.2 etc.
1100 * @param string $pushid The device PUSH token/key/identifier/registration id.
1101 * @param string $uuid The device UUID.
1102 * @return array List of possible warnings.
1103 * @since Moodle 2.6
1105 public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) {
1106 global $CFG, $USER, $DB;
1107 require_once($CFG->dirroot . "/user/lib.php");
1109 $params = self::validate_parameters(self::add_user_device_parameters(),
1110 array('appid' => $appid,
1111 'name' => $name,
1112 'model' => $model,
1113 'platform' => $platform,
1114 'version' => $version,
1115 'pushid' => $pushid,
1116 'uuid' => $uuid
1119 $warnings = array();
1121 // Prevent duplicate keys for users.
1122 if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'userid' => $USER->id))) {
1123 $warnings['warning'][] = array(
1124 'item' => $params['pushid'],
1125 'warningcode' => 'existingkeyforthisuser',
1126 'message' => 'This key is already stored for this user'
1128 return $warnings;
1131 // Notice that we can have multiple devices because previously it was allowed to have repeated ones.
1132 // Since we don't have a clear way to decide which one is the more appropiate, we update all.
1133 if ($userdevices = $DB->get_records('user_devices', array('uuid' => $params['uuid'],
1134 'appid' => $params['appid'], 'userid' => $USER->id))) {
1136 foreach ($userdevices as $userdevice) {
1137 $userdevice->version = $params['version']; // Maybe the user upgraded the device.
1138 $userdevice->pushid = $params['pushid'];
1139 $userdevice->timemodified = time();
1140 $DB->update_record('user_devices', $userdevice);
1143 } else {
1144 $userdevice = new stdclass;
1145 $userdevice->userid = $USER->id;
1146 $userdevice->appid = $params['appid'];
1147 $userdevice->name = $params['name'];
1148 $userdevice->model = $params['model'];
1149 $userdevice->platform = $params['platform'];
1150 $userdevice->version = $params['version'];
1151 $userdevice->pushid = $params['pushid'];
1152 $userdevice->uuid = $params['uuid'];
1153 $userdevice->timecreated = time();
1154 $userdevice->timemodified = $userdevice->timecreated;
1156 if (!$DB->insert_record('user_devices', $userdevice)) {
1157 throw new moodle_exception("There was a problem saving in the database the device with key: " . $params['pushid']);
1161 return $warnings;
1165 * Returns description of method result value.
1167 * @return external_multiple_structure
1168 * @since Moodle 2.6
1170 public static function add_user_device_returns() {
1171 return new external_multiple_structure(
1172 new external_warnings()
1177 * Returns description of method parameters.
1179 * @return external_function_parameters
1180 * @since Moodle 2.9
1182 public static function remove_user_device_parameters() {
1183 return new external_function_parameters(
1184 array(
1185 'uuid' => new external_value(PARAM_RAW, 'the device UUID'),
1186 'appid' => new external_value(PARAM_NOTAGS,
1187 'the app id, if empty devices matching the UUID for the user will be removed',
1188 VALUE_DEFAULT, ''),
1194 * Remove a user device from the Moodle database (for PUSH notifications usually).
1196 * @param string $uuid The device UUID.
1197 * @param string $appid The app id, opitonal parameter. If empty all the devices fmatching the UUID or the user will be removed.
1198 * @return array List of possible warnings and removal status.
1199 * @since Moodle 2.9
1201 public static function remove_user_device($uuid, $appid = "") {
1202 global $CFG;
1203 require_once($CFG->dirroot . "/user/lib.php");
1205 $params = self::validate_parameters(self::remove_user_device_parameters(), array('uuid' => $uuid, 'appid' => $appid));
1207 $context = context_system::instance();
1208 self::validate_context($context);
1210 // Warnings array, it can be empty at the end but is mandatory.
1211 $warnings = array();
1213 $removed = user_remove_user_device($params['uuid'], $params['appid']);
1215 if (!$removed) {
1216 $warnings[] = array(
1217 'item' => $params['uuid'],
1218 'warningcode' => 'devicedoesnotexist',
1219 'message' => 'The device doesn\'t exists in the database'
1223 $result = array(
1224 'removed' => $removed,
1225 'warnings' => $warnings
1228 return $result;
1232 * Returns description of method result value.
1234 * @return external_multiple_structure
1235 * @since Moodle 2.9
1237 public static function remove_user_device_returns() {
1238 return new external_single_structure(
1239 array(
1240 'removed' => new external_value(PARAM_BOOL, 'True if removed, false if not removed because it doesn\'t exists'),
1241 'warnings' => new external_warnings(),
1247 * Returns description of method parameters
1249 * @return external_function_parameters
1250 * @since Moodle 2.9
1252 public static function view_user_list_parameters() {
1253 return new external_function_parameters(
1254 array(
1255 'courseid' => new external_value(PARAM_INT, 'id of the course, 0 for site')
1261 * Trigger the user_list_viewed event.
1263 * @param int $courseid id of course
1264 * @return array of warnings and status result
1265 * @since Moodle 2.9
1266 * @throws moodle_exception
1268 public static function view_user_list($courseid) {
1269 global $CFG;
1270 require_once($CFG->dirroot . "/user/lib.php");
1272 $params = self::validate_parameters(self::view_user_list_parameters(),
1273 array(
1274 'courseid' => $courseid
1277 $warnings = array();
1279 if (empty($params['courseid'])) {
1280 $params['courseid'] = SITEID;
1283 $course = get_course($params['courseid']);
1285 if ($course->id == SITEID) {
1286 $context = context_system::instance();
1287 } else {
1288 $context = context_course::instance($course->id);
1290 self::validate_context($context);
1292 if ($course->id == SITEID) {
1293 require_capability('moodle/site:viewparticipants', $context);
1294 } else {
1295 require_capability('moodle/course:viewparticipants', $context);
1298 user_list_view($course, $context);
1300 $result = array();
1301 $result['status'] = true;
1302 $result['warnings'] = $warnings;
1303 return $result;
1307 * Returns description of method result value
1309 * @return external_description
1310 * @since Moodle 2.9
1312 public static function view_user_list_returns() {
1313 return new external_single_structure(
1314 array(
1315 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
1316 'warnings' => new external_warnings()
1322 * Returns description of method parameters
1324 * @return external_function_parameters
1325 * @since Moodle 2.9
1327 public static function view_user_profile_parameters() {
1328 return new external_function_parameters(
1329 array(
1330 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_REQUIRED),
1331 'courseid' => new external_value(PARAM_INT, 'id of the course, default site course', VALUE_DEFAULT, 0)
1337 * Trigger the user profile viewed event.
1339 * @param int $userid id of user
1340 * @param int $courseid id of course
1341 * @return array of warnings and status result
1342 * @since Moodle 2.9
1343 * @throws moodle_exception
1345 public static function view_user_profile($userid, $courseid = 0) {
1346 global $CFG, $USER;
1347 require_once($CFG->dirroot . "/user/profile/lib.php");
1349 $params = self::validate_parameters(self::view_user_profile_parameters(),
1350 array(
1351 'userid' => $userid,
1352 'courseid' => $courseid
1355 $warnings = array();
1357 if (empty($params['userid'])) {
1358 $params['userid'] = $USER->id;
1361 if (empty($params['courseid'])) {
1362 $params['courseid'] = SITEID;
1365 $course = get_course($params['courseid']);
1366 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
1368 if ($user->deleted) {
1369 throw new moodle_exception('userdeleted');
1371 if (isguestuser($user)) {
1372 // Can not view profile of guest - thre is nothing to see there.
1373 throw new moodle_exception('invaliduserid');
1376 if ($course->id == SITEID) {
1377 $coursecontext = context_system::instance();;
1378 } else {
1379 $coursecontext = context_course::instance($course->id);
1381 self::validate_context($coursecontext);
1383 $currentuser = $USER->id == $user->id;
1384 $usercontext = context_user::instance($user->id);
1386 if (!$currentuser and
1387 !has_capability('moodle/user:viewdetails', $coursecontext) and
1388 !has_capability('moodle/user:viewdetails', $usercontext)) {
1389 throw new moodle_exception('cannotviewprofile');
1392 // Case like user/profile.php.
1393 if ($course->id == SITEID) {
1394 profile_view($user, $usercontext);
1395 } else {
1396 // Case like user/view.php.
1397 if (!$currentuser and !is_enrolled($coursecontext, $user->id)) {
1398 throw new moodle_exception('notenrolledprofile');
1401 profile_view($user, $coursecontext, $course);
1404 $result = array();
1405 $result['status'] = true;
1406 $result['warnings'] = $warnings;
1407 return $result;
1411 * Returns description of method result value
1413 * @return external_description
1414 * @since Moodle 2.9
1416 public static function view_user_profile_returns() {
1417 return new external_single_structure(
1418 array(
1419 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
1420 'warnings' => new external_warnings()
1428 * Deprecated user external functions
1430 * @package core_user
1431 * @copyright 2009 Petr Skodak
1432 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1433 * @since Moodle 2.0
1434 * @deprecated Moodle 2.2 MDL-29106 - Please do not use this class any more.
1435 * @see core_user_external
1437 class moodle_user_external extends external_api {
1440 * Returns description of method parameters
1442 * @return external_function_parameters
1443 * @since Moodle 2.0
1444 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1445 * @see core_user_external::create_users_parameters()
1447 public static function create_users_parameters() {
1448 return core_user_external::create_users_parameters();
1452 * Create one or more users
1454 * @param array $users An array of users to create.
1455 * @return array An array of arrays
1456 * @since Moodle 2.0
1457 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1458 * @see core_user_external::create_users()
1460 public static function create_users($users) {
1461 return core_user_external::create_users($users);
1465 * Returns description of method result value
1467 * @return external_description
1468 * @since Moodle 2.0
1469 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1470 * @see core_user_external::create_users_returns()
1472 public static function create_users_returns() {
1473 return core_user_external::create_users_returns();
1477 * Marking the method as deprecated.
1479 * @return bool
1481 public static function create_users_is_deprecated() {
1482 return true;
1486 * Returns description of method parameters
1488 * @return external_function_parameters
1489 * @since Moodle 2.0
1490 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1491 * @see core_user_external::delete_users_parameters()
1493 public static function delete_users_parameters() {
1494 return core_user_external::delete_users_parameters();
1498 * Delete users
1500 * @param array $userids
1501 * @return null
1502 * @since Moodle 2.0
1503 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1504 * @see core_user_external::delete_users()
1506 public static function delete_users($userids) {
1507 return core_user_external::delete_users($userids);
1511 * Returns description of method result value
1513 * @return null
1514 * @since Moodle 2.0
1515 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1516 * @see core_user_external::delete_users_returns()
1518 public static function delete_users_returns() {
1519 return core_user_external::delete_users_returns();
1523 * Marking the method as deprecated.
1525 * @return bool
1527 public static function delete_users_is_deprecated() {
1528 return true;
1532 * Returns description of method parameters
1534 * @return external_function_parameters
1535 * @since Moodle 2.0
1536 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1537 * @see core_user_external::update_users_parameters()
1539 public static function update_users_parameters() {
1540 return core_user_external::update_users_parameters();
1544 * Update users
1546 * @param array $users
1547 * @return null
1548 * @since Moodle 2.0
1549 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1550 * @see core_user_external::update_users()
1552 public static function update_users($users) {
1553 return core_user_external::update_users($users);
1557 * Returns description of method result value
1559 * @return null
1560 * @since Moodle 2.0
1561 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1562 * @see core_user_external::update_users_returns()
1564 public static function update_users_returns() {
1565 return core_user_external::update_users_returns();
1569 * Marking the method as deprecated.
1571 * @return bool
1573 public static function update_users_is_deprecated() {
1574 return true;
1578 * Returns description of method parameters
1580 * @return external_function_parameters
1581 * @since Moodle 2.0
1582 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1583 * @see core_user_external::get_users_by_id_parameters()
1585 public static function get_users_by_id_parameters() {
1586 return core_user_external::get_users_by_id_parameters();
1590 * Get user information
1591 * - This function is matching the permissions of /user/profil.php
1592 * - It is also matching some permissions from /user/editadvanced.php for the following fields:
1593 * auth, confirmed, idnumber, lang, theme, timezone, mailformat
1595 * @param array $userids array of user ids
1596 * @return array An array of arrays describing users
1597 * @since Moodle 2.0
1598 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1599 * @see core_user_external::get_users_by_id()
1601 public static function get_users_by_id($userids) {
1602 return core_user_external::get_users_by_id($userids);
1606 * Returns description of method result value
1608 * @return external_description
1609 * @since Moodle 2.0
1610 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1611 * @see core_user_external::get_users_by_id_returns()
1613 public static function get_users_by_id_returns() {
1614 return core_user_external::get_users_by_id_returns();
1618 * Marking the method as deprecated.
1620 * @return bool
1622 public static function get_users_by_id_is_deprecated() {
1623 return true;
1627 * Returns description of method parameters
1629 * @return external_function_parameters
1630 * @since Moodle 2.1
1631 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1632 * @see core_user_external::get_course_user_profiles_parameters()
1634 public static function get_course_participants_by_id_parameters() {
1635 return core_user_external::get_course_user_profiles_parameters();
1639 * Get course participant's details
1641 * @param array $userlist array of user ids and according course ids
1642 * @return array An array of arrays describing course participants
1643 * @since Moodle 2.1
1644 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1645 * @see core_user_external::get_course_user_profiles()
1647 public static function get_course_participants_by_id($userlist) {
1648 return core_user_external::get_course_user_profiles($userlist);
1652 * Returns description of method result value
1654 * @return external_description
1655 * @since Moodle 2.1
1656 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1657 * @see core_user_external::get_course_user_profiles_returns()
1659 public static function get_course_participants_by_id_returns() {
1660 return core_user_external::get_course_user_profiles_returns();
1664 * Marking the method as deprecated.
1666 * @return bool
1668 public static function get_course_participants_by_id_is_deprecated() {
1669 return true;
1673 * Returns description of method parameters
1675 * @return external_function_parameters
1676 * @since Moodle 2.1
1677 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1678 * @see core_enrol_external::get_enrolled_users_parameters()
1680 public static function get_users_by_courseid_parameters() {
1681 global $CFG;
1682 require_once($CFG->dirroot . '/enrol/externallib.php');
1683 return core_enrol_external::get_enrolled_users_parameters();
1687 * Get course participants details
1689 * @param int $courseid course id
1690 * @param array $options options {
1691 * 'name' => option name
1692 * 'value' => option value
1694 * @return array An array of users
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()
1699 public static function get_users_by_courseid($courseid, $options = array()) {
1700 global $CFG;
1701 require_once($CFG->dirroot . '/enrol/externallib.php');
1702 return core_enrol_external::get_enrolled_users($courseid, $options);
1705 * Returns description of method result value
1707 * @return external_description
1708 * @since Moodle 2.1
1709 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
1710 * @see core_enrol_external::get_enrolled_users_returns()
1712 public static function get_users_by_courseid_returns() {
1713 global $CFG;
1714 require_once($CFG->dirroot . '/enrol/externallib.php');
1715 return core_enrol_external::get_enrolled_users_returns();
1719 * Marking the method as deprecated.
1721 * @return bool
1723 public static function get_users_by_courseid_is_deprecated() {
1724 return true;