weekly release 2.2.4+
[moodle.git] / user / externallib.php
blob46d251e636165afaacbec09b1416e70e6890d3cb
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * External user API
21 * @package moodlecore
22 * @subpackage webservice
23 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once("$CFG->libdir/externallib.php");
29 /**
30 * User functions
32 class core_user_external extends external_api {
34 /**
35 * Returns description of method parameters
36 * @return external_function_parameters
38 public static function create_users_parameters() {
39 global $CFG;
41 return new external_function_parameters(
42 array(
43 'users' => new external_multiple_structure(
44 new external_single_structure(
45 array(
46 'username' => new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config. Must be lowercase.'),
47 'password' => new external_value(PARAM_RAW, 'Plain text password consisting of any characters'),
48 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
49 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
50 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
51 'auth' => new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT, 'manual', NULL_NOT_ALLOWED),
52 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_DEFAULT, ''),
53 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_DEFAULT, $CFG->lang, NULL_NOT_ALLOWED),
54 'theme' => new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
55 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
56 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
57 'description' => new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
58 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
59 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
60 'preferences' => new external_multiple_structure(
61 new external_single_structure(
62 array(
63 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
64 'value' => new external_value(PARAM_RAW, 'The value of the preference')
66 ), 'User preferences', VALUE_OPTIONAL),
67 'customfields' => new external_multiple_structure(
68 new external_single_structure(
69 array(
70 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
71 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
73 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL)
81 /**
82 * Create one or more users
84 * @param array $users An array of users to create.
85 * @return array An array of arrays
87 public static function create_users($users) {
88 global $CFG, $DB;
89 require_once($CFG->dirroot."/lib/weblib.php");
90 require_once($CFG->dirroot."/user/lib.php");
91 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
92 //TODO: move the functions somewhere else as
93 //they are "user" related
95 // Ensure the current user is allowed to run this function
96 $context = get_context_instance(CONTEXT_SYSTEM);
97 self::validate_context($context);
98 require_capability('moodle/user:create', $context);
100 // Do basic automatic PARAM checks on incoming data, using params description
101 // If any problems are found then exceptions are thrown with helpful error messages
102 $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$users));
104 $availableauths = get_plugin_list('auth');
105 unset($availableauths['mnet']); // these would need mnethostid too
106 unset($availableauths['webservice']); // we do not want new webservice users for now
108 $availablethemes = get_plugin_list('theme');
109 $availablelangs = get_string_manager()->get_list_of_translations();
111 $transaction = $DB->start_delegated_transaction();
113 $userids = array();
114 foreach ($params['users'] as $user) {
115 // Make sure that the username doesn't already exist
116 if ($DB->record_exists('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
117 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
120 // Make sure auth is valid
121 if (empty($availableauths[$user['auth']])) {
122 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
125 // Make sure lang is valid
126 if (empty($availablelangs[$user['lang']])) {
127 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
130 // Make sure lang is valid
131 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { //theme is VALUE_OPTIONAL,
132 // so no default value.
133 // We need to test if the client sent it
134 // => !empty($user['theme'])
135 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
138 $user['confirmed'] = true;
139 $user['mnethostid'] = $CFG->mnet_localhost_id;
141 // Start of user info validation.
142 // Lets make sure we validate current user info as handled by current GUI. see user/editadvanced_form.php function validation()
143 if (!validate_email($user['email'])) {
144 throw new invalid_parameter_exception('Email address is invalid: '.$user['email']);
145 } else if ($DB->record_exists('user', array('email'=>$user['email'], 'mnethostid'=>$user['mnethostid']))) {
146 throw new invalid_parameter_exception('Email address already exists: '.$user['email']);
148 // End of user info validation.
150 // create the user data now!
151 $user['id'] = user_create_user($user);
153 // custom fields
154 if(!empty($user['customfields'])) {
155 foreach($user['customfields'] as $customfield) {
156 $user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
157 //it's expecting a user with the correct id,
158 //and custom field to be named profile_field_"shortname"
160 profile_save_data((object) $user);
163 //preferences
164 if (!empty($user['preferences'])) {
165 foreach($user['preferences'] as $preference) {
166 set_user_preference($preference['type'], $preference['value'],$user['id']);
170 $userids[] = array('id'=>$user['id'], 'username'=>$user['username']);
173 $transaction->allow_commit();
175 return $userids;
179 * Returns description of method result value
180 * @return external_description
182 public static function create_users_returns() {
183 return new external_multiple_structure(
184 new external_single_structure(
185 array(
186 'id' => new external_value(PARAM_INT, 'user id'),
187 'username' => new external_value(PARAM_USERNAME, 'user name'),
195 * Returns description of method parameters
196 * @return external_function_parameters
198 public static function delete_users_parameters() {
199 return new external_function_parameters(
200 array(
201 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
207 * Delete users
208 * @param array $userids
209 * @return null
211 public static function delete_users($userids) {
212 global $CFG, $DB, $USER;
213 require_once($CFG->dirroot."/user/lib.php");
215 // Ensure the current user is allowed to run this function
216 $context = get_context_instance(CONTEXT_SYSTEM);
217 require_capability('moodle/user:delete', $context);
218 self::validate_context($context);
220 $params = self::validate_parameters(self::delete_users_parameters(), array('userids'=>$userids));
222 $transaction = $DB->start_delegated_transaction();
224 foreach ($params['userids'] as $userid) {
225 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
226 // must not allow deleting of admins or self!!!
227 if (is_siteadmin($user)) {
228 throw new moodle_exception('useradminodelete', 'error');
230 if ($USER->id == $user->id) {
231 throw new moodle_exception('usernotdeletederror', 'error');
233 user_delete_user($user);
236 $transaction->allow_commit();
238 return null;
242 * Returns description of method result value
243 * @return external_description
245 public static function delete_users_returns() {
246 return null;
251 * Returns description of method parameters
252 * @return external_function_parameters
254 public static function update_users_parameters() {
255 global $CFG;
256 return new external_function_parameters(
257 array(
258 'users' => new external_multiple_structure(
259 new external_single_structure(
260 array(
261 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
262 'username' => new external_value(PARAM_USERNAME, 'Username policy is defined in Moodle security config. Must be lowercase.', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
263 'password' => new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
264 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
265 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
266 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
267 'auth' => new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
268 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
269 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
270 'theme' => new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
271 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
272 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
273 'description' => new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
274 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
275 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
276 'customfields' => new external_multiple_structure(
277 new external_single_structure(
278 array(
279 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
280 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
282 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
283 'preferences' => new external_multiple_structure(
284 new external_single_structure(
285 array(
286 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
287 'value' => new external_value(PARAM_RAW, 'The value of the preference')
289 ), 'User preferences', VALUE_OPTIONAL),
298 * Update users
299 * @param array $users
300 * @return null
302 public static function update_users($users) {
303 global $CFG, $DB;
304 require_once($CFG->dirroot."/user/lib.php");
305 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
306 //TODO: move the functions somewhere else as
307 //they are "user" related
309 // Ensure the current user is allowed to run this function
310 $context = get_context_instance(CONTEXT_SYSTEM);
311 require_capability('moodle/user:update', $context);
312 self::validate_context($context);
314 $params = self::validate_parameters(self::update_users_parameters(), array('users'=>$users));
316 $transaction = $DB->start_delegated_transaction();
318 foreach ($params['users'] as $user) {
319 user_update_user($user);
320 //update user custom fields
321 if(!empty($user['customfields'])) {
323 foreach($user['customfields'] as $customfield) {
324 $user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
325 //it's expecting a user with the correct id,
326 //and custom field to be named profile_field_"shortname"
328 profile_save_data((object) $user);
331 //preferences
332 if (!empty($user['preferences'])) {
333 foreach($user['preferences'] as $preference) {
334 set_user_preference($preference['type'], $preference['value'],$user['id']);
339 $transaction->allow_commit();
341 return null;
345 * Returns description of method result value
346 * @return external_description
348 public static function update_users_returns() {
349 return null;
353 * Returns description of method parameters
354 * @return external_function_parameters
356 public static function get_users_by_id_parameters() {
357 return new external_function_parameters(
358 array(
359 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
365 * Get user information
366 * - This function is matching the permissions of /user/profil.php
367 * - It is also matching some permissions from /user/editadvanced.php for the following fields:
368 * auth, confirmed, idnumber, lang, theme, timezone, mailformat
369 * @param array $userids array of user ids
370 * @return array An array of arrays describing users
372 public static function get_users_by_id($userids) {
373 global $CFG, $USER, $DB;
374 require_once($CFG->dirroot . "/user/lib.php");
376 $params = self::validate_parameters(self::get_users_by_id_parameters(),
377 array('userids'=>$userids));
379 list($uselect, $ujoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
380 list($sqluserids, $params) = $DB->get_in_or_equal($userids);
381 $usersql = "SELECT u.* $uselect
382 FROM {user} u $ujoin
383 WHERE u.id $sqluserids";
384 $users = $DB->get_recordset_sql($usersql, $params);
386 $result = array();
387 $hasuserupdatecap = has_capability('moodle/user:update', get_system_context());
388 foreach ($users as $user) {
389 if (!empty($user->deleted)) {
390 continue;
392 context_instance_preload($user);
393 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
394 self::validate_context($usercontext);
395 $currentuser = ($user->id == $USER->id);
397 if ($userarray = user_get_user_details($user)) {
398 //fields matching permissions from /user/editadvanced.php
399 if ($currentuser or $hasuserupdatecap) {
400 $userarray['auth'] = $user->auth;
401 $userarray['confirmed'] = $user->confirmed;
402 $userarray['idnumber'] = $user->idnumber;
403 $userarray['lang'] = $user->lang;
404 $userarray['theme'] = $user->theme;
405 $userarray['timezone'] = $user->timezone;
406 $userarray['mailformat'] = $user->mailformat;
408 $result[] = $userarray;
411 $users->close();
413 return $result;
417 * Returns description of method result value
418 * @return external_description
420 public static function get_users_by_id_returns() {
421 return new external_multiple_structure(
422 new external_single_structure(
423 array(
424 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
425 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL),
426 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
427 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
428 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
429 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
430 'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL),
431 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
432 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
433 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
434 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
435 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
436 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
437 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
438 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
439 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
440 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
441 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
442 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
443 'auth' => new external_value(PARAM_PLUGIN, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL),
444 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL),
445 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
446 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL),
447 'theme' => new external_value(PARAM_PLUGIN, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
448 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
449 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
450 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
451 'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL),
452 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
453 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
454 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
455 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
456 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
457 'customfields' => new external_multiple_structure(
458 new external_single_structure(
459 array(
460 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
461 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
462 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
463 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
465 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
466 'preferences' => new external_multiple_structure(
467 new external_single_structure(
468 array(
469 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
470 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
472 ), 'User preferences', VALUE_OPTIONAL),
473 'enrolledcourses' => new external_multiple_structure(
474 new external_single_structure(
475 array(
476 'id' => new external_value(PARAM_INT, 'Id of the course'),
477 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
478 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
480 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
486 * Returns description of method parameters
487 * @return external_function_parameters
489 public static function get_course_user_profiles_parameters() {
490 return new external_function_parameters(
491 array(
492 'userlist' => new external_multiple_structure(
493 new external_single_structure(
494 array(
495 'userid' => new external_value(PARAM_INT, 'userid'),
496 'courseid' => new external_value(PARAM_INT, 'courseid'),
505 * Get course participant's details
506 * @param array $userlist array of user ids and according course ids
507 * @return array An array of arrays describing course participants
509 public static function get_course_user_profiles($userlist) {
510 global $CFG, $USER, $DB;
511 require_once($CFG->dirroot . "/user/lib.php");
512 $params = self::validate_parameters(self::get_course_user_profiles_parameters(), array('userlist'=>$userlist));
514 $userids = array();
515 $courseids = array();
516 foreach ($params['userlist'] as $value) {
517 $userids[] = $value['userid'];
518 $courseids[$value['userid']] = $value['courseid'];
521 // cache all courses
522 $courses = array();
523 list($cselect, $cjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
524 list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids));
525 $coursesql = "SELECT c.* $cselect
526 FROM {course} c $cjoin
527 WHERE c.id $sqlcourseids";
528 $rs = $DB->get_recordset_sql($coursesql, $params);
529 foreach ($rs as $course) {
530 // adding course contexts to cache
531 context_instance_preload($course);
532 // cache courses
533 $courses[$course->id] = $course;
535 $rs->close();
537 list($uselect, $ujoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
538 list($sqluserids, $params) = $DB->get_in_or_equal($userids);
539 $usersql = "SELECT u.* $uselect
540 FROM {user} u $ujoin
541 WHERE u.id $sqluserids";
542 $users = $DB->get_recordset_sql($usersql, $params);
543 $result = array();
544 foreach ($users as $user) {
545 if (!empty($user->deleted)) {
546 continue;
548 context_instance_preload($user);
549 $course = $courses[$courseids[$user->id]];
550 $context = get_context_instance(CONTEXT_COURSE, $courseids[$user->id]);
551 self::validate_context($context);
552 if ($userarray = user_get_user_details($user, $course)) {
553 $result[] = $userarray;
557 $users->close();
559 return $result;
563 * Returns description of method result value
564 * @return external_description
566 public static function get_course_user_profiles_returns() {
567 return new external_multiple_structure(
568 new external_single_structure(
569 array(
570 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
571 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL),
572 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
573 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
574 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
575 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
576 'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL),
577 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
578 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
579 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
580 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
581 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
582 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
583 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
584 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
585 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
586 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
587 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
588 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
589 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
590 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
591 'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL),
592 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
593 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
594 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
595 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
596 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
597 'customfields' => new external_multiple_structure(
598 new external_single_structure(
599 array(
600 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
601 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
602 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
603 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
605 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
606 'groups' => new external_multiple_structure(
607 new external_single_structure(
608 array(
609 'id' => new external_value(PARAM_INT, 'group id'),
610 'name' => new external_value(PARAM_RAW, 'group name'),
611 'description' => new external_value(PARAM_RAW, 'group description'),
613 ), 'user groups', VALUE_OPTIONAL),
614 'roles' => new external_multiple_structure(
615 new external_single_structure(
616 array(
617 'roleid' => new external_value(PARAM_INT, 'role id'),
618 'name' => new external_value(PARAM_RAW, 'role name'),
619 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'),
620 'sortorder' => new external_value(PARAM_INT, 'role sortorder')
622 ), 'user roles', VALUE_OPTIONAL),
623 'preferences' => new external_multiple_structure(
624 new external_single_structure(
625 array(
626 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
627 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
629 ), 'User preferences', VALUE_OPTIONAL),
630 'enrolledcourses' => new external_multiple_structure(
631 new external_single_structure(
632 array(
633 'id' => new external_value(PARAM_INT, 'Id of the course'),
634 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'),
635 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course')
637 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
645 * Deprecated user functions
646 * @deprecated since Moodle 2.2 please use core_user_external instead
648 class moodle_user_external extends external_api {
651 * Returns description of method parameters
652 * @deprecated since Moodle 2.2 please use core_user_external::create_users_parameters instead
653 * @return external_function_parameters
655 public static function create_users_parameters() {
656 return core_user_external::create_users_parameters();
660 * Create one or more users
661 * @deprecated since Moodle 2.2 please use core_user_external::create_users instead
662 * @param array $users An array of users to create.
663 * @return array An array of arrays
665 public static function create_users($users) {
666 return core_user_external::create_users($users);
670 * Returns description of method result value
671 * @deprecated since Moodle 2.2 please use core_user_external::create_users_returns instead
672 * @return external_description
674 public static function create_users_returns() {
675 return core_user_external::create_users_returns();
680 * Returns description of method parameters
681 * @deprecated since Moodle 2.2 please use core_user_external::delete_users_parameters instead
682 * @return external_function_parameters
684 public static function delete_users_parameters() {
685 return core_user_external::delete_users_parameters();
689 * Delete users
690 * @deprecated since Moodle 2.2 please use core_user_external::delete_users instead
691 * @param array $userids
692 * @return null
694 public static function delete_users($userids) {
695 return core_user_external::delete_users($userids);
699 * Returns description of method result value
700 * @deprecated since Moodle 2.2 please use core_user_external::delete_users_returns instead
701 * @return external_description
703 public static function delete_users_returns() {
704 return core_user_external::delete_users_returns();
709 * Returns description of method parameters
710 * @deprecated since Moodle 2.2 please use core_user_external::update_users_parameters instead
711 * @return external_function_parameters
713 public static function update_users_parameters() {
714 return core_user_external::update_users_parameters();
718 * Update users
719 * @deprecated since Moodle 2.2 please use core_user_external::update_users instead
720 * @param array $users
721 * @return null
723 public static function update_users($users) {
724 return core_user_external::update_users($users);
728 * Returns description of method result value
729 * @deprecated since Moodle 2.2 please use core_user_external::update_users_returns instead
730 * @return external_description
732 public static function update_users_returns() {
733 return core_user_external::update_users_returns();
737 * Returns description of method parameters
738 * @deprecated since Moodle 2.2 please use core_user_external::get_users_by_id_parameters instead
739 * @return external_function_parameters
741 public static function get_users_by_id_parameters() {
742 return core_user_external::get_users_by_id_parameters();
746 * Get user information
747 * - This function is matching the permissions of /user/profil.php
748 * - It is also matching some permissions from /user/editadvanced.php for the following fields:
749 * auth, confirmed, idnumber, lang, theme, timezone, mailformat
750 * @deprecated since Moodle 2.2 please use core_user_external::get_users_by_id instead
751 * @param array $userids array of user ids
752 * @return array An array of arrays describing users
754 public static function get_users_by_id($userids) {
755 return core_user_external::get_users_by_id($userids);
759 * Returns description of method result value
760 * @deprecated since Moodle 2.2 please use core_user_external::get_users_by_id_returns instead
761 * @return external_description
763 public static function get_users_by_id_returns() {
764 return core_user_external::get_users_by_id_returns();
767 * Returns description of method parameters
768 * @deprecated since Moodle 2.2 please use core_user_external::get_course_user_profiles_parameters instead
769 * @return external_function_parameters
771 public static function get_course_participants_by_id_parameters() {
772 return core_user_external::get_course_user_profiles_parameters();
776 * Get course participant's details
777 * @deprecated since Moodle 2.2 please use core_user_external::get_course_user_profiles instead
778 * @param array $userlist array of user ids and according course ids
779 * @return array An array of arrays describing course participants
781 public static function get_course_participants_by_id($userlist) {
782 return core_user_external::get_course_user_profiles($userlist);
786 * Returns description of method result value
787 * @deprecated since Moodle 2.2 please use core_user_external::get_course_user_profiles_returns instead
788 * @return external_description
790 public static function get_course_participants_by_id_returns() {
791 return core_user_external::get_course_user_profiles_returns();
795 * Returns description of method parameters
796 * @deprecated since Moodle 2.2 please use core_enrol_external::get_enrolled_users_parameters instead
797 * @return external_function_parameters
799 public static function get_users_by_courseid_parameters() {
800 global $CFG;
801 require_once($CFG->dirroot . '/enrol/externallib.php');
802 return core_enrol_external::get_enrolled_users_parameters();
806 * Get course participants details
807 * @deprecated since Moodle 2.2 please use core_enrol_external::get_enrolled_users instead
808 * @param int $courseid course id
809 * @param array $options options {
810 * 'name' => option name
811 * 'value' => option value
813 * @return array An array of users
815 public static function get_users_by_courseid($courseid, $options) {
816 global $CFG;
817 require_once($CFG->dirroot . '/enrol/externallib.php');
818 return core_enrol_external::get_enrolled_users($courseid, $options);
821 * Returns description of method result value
822 * @deprecated since Moodle 2.2 please use core_enrol_external::get_enrolled_users_returns instead
823 * @return external_description
825 public static function get_users_by_courseid_returns() {
826 global $CFG;
827 require_once($CFG->dirroot . '/enrol/externallib.php');
828 return core_enrol_external::get_enrolled_users_returns();