on-demand release 2.1beta
[moodle.git] / user / externallib.php
CommitLineData
ef22c1b6 1<?php
2
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.
14//
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/>.
17
18/**
19 * External user API
20 *
21 * @package moodlecore
22 * @subpackage webservice
551f4420 23 * @copyright 2009 Moodle Pty Ltd (http://moodle.com)
ef22c1b6 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once("$CFG->libdir/externallib.php");
28
29class moodle_user_external extends external_api {
30
7b472b32
PS
31 /**
32 * Returns description of method parameters
33 * @return external_function_parameters
34 */
d4e13355 35 public static function create_users_parameters() {
667b496a
PS
36 global $CFG;
37
35b9a80a 38 return new external_function_parameters(
39 array(
40 'users' => new external_multiple_structure(
41 new external_single_structure(
42 array(
7b472b32 43 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config'),
667b496a 44 'password' => new external_value(PARAM_RAW, 'Plain text password consisting of any characters'),
7b472b32
PS
45 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user'),
46 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user'),
47 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address'),
fb79269b 48 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT, 'manual', NULL_NOT_ALLOWED),
610a447e 49 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_DEFAULT, ''),
3a915b06 50 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_DEFAULT, $CFG->lang, NULL_NOT_ALLOWED),
fb79269b 51 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
ccc77f91 52 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
fb79269b 53 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
d9ad0103 54 'description' => new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
fb79269b 55 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
56 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
35b9a80a 57 'preferences' => new external_multiple_structure(
58 new external_single_structure(
59 array(
7b472b32 60 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
35b9a80a 61 'value' => new external_value(PARAM_RAW, 'The value of the preference')
62 )
fb79269b 63 ), 'User preferences', VALUE_OPTIONAL),
35b9a80a 64 'customfields' => new external_multiple_structure(
65 new external_single_structure(
66 array(
7b472b32 67 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
35b9a80a 68 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
69 )
6bb31e40 70 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL)
35b9a80a 71 )
72 )
73 )
74 )
75 );
625f0a24 76 }
77
d4e13355 78 /**
5de592b1 79 * Create one or more users
80 *
71864f15
PS
81 * @param array $users An array of users to create.
82 * @return array An array of arrays
5de592b1 83 */
7b472b32 84 public static function create_users($users) {
ef22c1b6 85 global $CFG, $DB;
fb79269b 86 require_once($CFG->dirroot."/user/lib.php");
30a4fb1b 87 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
88 //TODO: move the functions somewhere else as
89 //they are "user" related
109b453b 90
5de592b1 91 // Ensure the current user is allowed to run this function
ef22c1b6 92 $context = get_context_instance(CONTEXT_SYSTEM);
ef22c1b6 93 self::validate_context($context);
fb79269b 94 require_capability('moodle/user:create', $context);
d9ad0103 95
5de592b1 96 // Do basic automatic PARAM checks on incoming data, using params description
5de592b1 97 // If any problems are found then exceptions are thrown with helpful error messages
7b472b32 98 $params = self::validate_parameters(self::create_users_parameters(), array('users'=>$users));
109b453b 99
667b496a
PS
100 $availableauths = get_plugin_list('auth');
101 unset($availableauths['mnet']); // these would need mnethostid too
102 unset($availableauths['webservice']); // we do not want new webservice users for now
103
104 $availablethemes = get_plugin_list('theme');
1f96e907 105 $availablelangs = get_string_manager()->get_list_of_translations();
5de592b1 106
38b76f3c 107 $transaction = $DB->start_delegated_transaction();
5de592b1 108
fb79269b 109 $userids = array();
7b472b32 110 foreach ($params['users'] as $user) {
667b496a
PS
111 // Make sure that the username doesn't already exist
112 if ($DB->record_exists('user', array('username'=>$user['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
113 throw new invalid_parameter_exception('Username already exists: '.$user['username']);
ef22c1b6 114 }
ef22c1b6 115
667b496a
PS
116 // Make sure auth is valid
117 if (empty($availableauths[$user['auth']])) {
118 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']);
ef22c1b6 119 }
120
667b496a
PS
121 // Make sure lang is valid
122 if (empty($availablelangs[$user['lang']])) {
123 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']);
ef22c1b6 124 }
125
667b496a 126 // Make sure lang is valid
fb79269b 127 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { //theme is VALUE_OPTIONAL,
128 // so no default value.
129 // We need to test if the client sent it
130 // => !empty($user['theme'])
667b496a 131 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']);
ef22c1b6 132 }
5de592b1 133
38b76f3c
PS
134 // make sure there is no data loss during truncation
135 $truncated = truncate_userinfo($user);
136 foreach ($truncated as $key=>$value) {
610a447e 137 if ($truncated[$key] !== $user[$key]) {
138 throw new invalid_parameter_exception('Property: '.$key.' is too long: '.$user[$key]);
139 }
38b76f3c 140 }
5de592b1 141
fb79269b 142 $user['confirmed'] = true;
a1988186 143 $user['mnethostid'] = $CFG->mnet_localhost_id;
30a4fb1b 144 $user['id'] = user_create_user($user);
145
146 // custom fields
147 if(!empty($user['customfields'])) {
148 foreach($user['customfields'] as $customfield) {
149 $user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
150 //it's expecting a user with the correct id,
151 //and custom field to be named profile_field_"shortname"
152 }
153 profile_save_data((object) $user);
154 }
667b496a 155
d9ad0103 156 //preferences
157 if (!empty($user['preferences'])) {
158 foreach($user['preferences'] as $preference) {
159 set_user_preference($preference['type'], $preference['value'],$user['id']);
160 }
161 }
d4e13355 162
c4c352dd 163 $userids[] = array('id'=>$user['id'], 'username'=>$user['username']);
ef22c1b6 164 }
165
38b76f3c 166 $transaction->allow_commit();
667b496a 167
fb79269b 168 return $userids;
ef22c1b6 169 }
170
7b472b32
PS
171 /**
172 * Returns description of method result value
173 * @return external_description
174 */
175 public static function create_users_returns() {
176 return new external_multiple_structure(
177 new external_single_structure(
178 array(
179 'id' => new external_value(PARAM_INT, 'user id'),
180 'username' => new external_value(PARAM_RAW, 'user name'),
181 )
182 )
183 );
d4e13355 184 }
185
186
930680cb
PS
187 /**
188 * Returns description of method parameters
189 * @return external_function_parameters
190 */
d4e13355 191 public static function delete_users_parameters() {
930680cb
PS
192 return new external_function_parameters(
193 array(
194 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
195 )
196 );
d4e13355 197 }
930680cb 198
38b76f3c 199 public static function delete_users($userids) {
b73a28be 200 global $CFG, $DB, $USER;
fb79269b 201 require_once($CFG->dirroot."/user/lib.php");
38b76f3c
PS
202
203 // Ensure the current user is allowed to run this function
204 $context = get_context_instance(CONTEXT_SYSTEM);
205 require_capability('moodle/user:delete', $context);
206 self::validate_context($context);
207
fb79269b 208 $params = self::validate_parameters(self::delete_users_parameters(), array('userids'=>$userids));
38b76f3c
PS
209
210 $transaction = $DB->start_delegated_transaction();
38b76f3c
PS
211
212 foreach ($params['userids'] as $userid) {
213 $user = $DB->get_record('user', array('id'=>$userid, 'deleted'=>0), '*', MUST_EXIST);
b73a28be 214 // must not allow deleting of admins or self!!!
4f622c38
PS
215 if (is_siteadmin($user)) {
216 throw new moodle_exception('useradminodelete', 'error');
217 }
218 if ($USER->id == $user->id) {
219 throw new moodle_exception('usernotdeletederror', 'error');
b73a28be 220 }
fb79269b 221 user_delete_user($user);
38b76f3c
PS
222 }
223
224 $transaction->allow_commit();
225
226 return null;
ef22c1b6 227 }
930680cb
PS
228
229 /**
230 * Returns description of method result value
231 * @return external_description
232 */
d4e13355 233 public static function delete_users_returns() {
930680cb 234 return null;
d4e13355 235 }
ef22c1b6 236
237
930680cb
PS
238 /**
239 * Returns description of method parameters
240 * @return external_function_parameters
241 */
d4e13355 242 public static function update_users_parameters() {
fb79269b 243 global $CFG;
244 return new external_function_parameters(
245 array(
246 'users' => new external_multiple_structure(
247 new external_single_structure(
248 array(
249 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
250 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
251 'password' => new external_value(PARAM_RAW, 'Plain text password consisting of any characters', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
252 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
253 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
254 'email' => new external_value(PARAM_EMAIL, 'A valid and unique email address', VALUE_OPTIONAL, '',NULL_NOT_ALLOWED),
255 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
256 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
3a915b06 257 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
fb79269b 258 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
ccc77f91 259 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
fb79269b 260 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
d9ad0103 261 'description' => new external_value(PARAM_TEXT, 'User profile description, no HTML', VALUE_OPTIONAL),
fb79269b 262 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
263 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
fb79269b 264 'customfields' => new external_multiple_structure(
265 new external_single_structure(
266 array(
267 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'),
268 'value' => new external_value(PARAM_RAW, 'The value of the custom field')
269 )
6bb31e40 270 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
d9ad0103 271 'preferences' => new external_multiple_structure(
272 new external_single_structure(
273 array(
274 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'),
275 'value' => new external_value(PARAM_RAW, 'The value of the preference')
276 )
277 ), 'User preferences', VALUE_OPTIONAL),
fb79269b 278 )
279 )
280 )
281 )
282 );
d4e13355 283 }
38b76f3c
PS
284
285 public static function update_users($users) {
286 global $CFG, $DB;
fb79269b 287 require_once($CFG->dirroot."/user/lib.php");
9baf3a7b 288 require_once($CFG->dirroot."/user/profile/lib.php"); //required for customfields related function
289 //TODO: move the functions somewhere else as
290 //they are "user" related
38b76f3c
PS
291
292 // Ensure the current user is allowed to run this function
293 $context = get_context_instance(CONTEXT_SYSTEM);
294 require_capability('moodle/user:update', $context);
295 self::validate_context($context);
296
297 $params = self::validate_parameters(self::update_users_parameters(), array('users'=>$users));
298
299 $transaction = $DB->start_delegated_transaction();
300
301 foreach ($params['users'] as $user) {
fb79269b 302 user_update_user($user);
9baf3a7b 303 //update user custom fields
304 if(!empty($user['customfields'])) {
305
306 foreach($user['customfields'] as $customfield) {
307 $user["profile_field_".$customfield['type']] = $customfield['value']; //profile_save_data() saves profile file
308 //it's expecting a user with the correct id,
309 //and custom field to be named profile_field_"shortname"
310 }
311 profile_save_data((object) $user);
312 }
d9ad0103 313
314 //preferences
315 if (!empty($user['preferences'])) {
316 foreach($user['preferences'] as $preference) {
317 set_user_preference($preference['type'], $preference['value'],$user['id']);
318 }
319 }
38b76f3c
PS
320 }
321
322 $transaction->allow_commit();
323
324 return null;
ef22c1b6 325 }
930680cb
PS
326
327 /**
328 * Returns description of method result value
329 * @return external_description
330 */
d4e13355 331 public static function update_users_returns() {
930680cb 332 return null;
d4e13355 333 }
334
7b472b32
PS
335 /**
336 * Returns description of method parameters
337 * @return external_function_parameters
338 */
fb79269b 339 public static function get_users_by_id_parameters() {
71864f15 340 return new external_function_parameters(
109b453b 341 array(
342 'userids' => new external_multiple_structure(new external_value(PARAM_INT, 'user ID')),
343 )
71864f15 344 );
d4e13355 345 }
7b472b32 346
71864f15
PS
347 /**
348 * Get user information
b4c74367
JM
349 * - This function is matching the permissions of /user/profil.php
350 * - It is also matching some permissions from /user/editadvanced.php for the following fields:
351 * auth, confirmed, idnumber, lang, theme, timezone, mailformat
71864f15
PS
352 * @param array $userids array of user ids
353 * @return array An array of arrays describing users
354 */
fb79269b 355 public static function get_users_by_id($userids) {
b4c74367 356 global $CFG, $USER, $DB;
109b453b 357 require_once($CFG->dirroot . "/user/lib.php");
b4c74367 358 require_once($CFG->dirroot . "/user/profile/lib.php"); //custom field library
7a5f3a3c 359 require_once($CFG->dirroot . "/lib/filelib.php"); // file handling on description and friends
b4c74367
JM
360
361 $isadmin = is_siteadmin($USER);
fb79269b 362
109b453b 363 $params = self::validate_parameters(self::get_users_by_id_parameters(),
364 array('userids'=>$userids));
5de592b1 365
b4c74367 366 $userscontexts = get_context_instance(CONTEXT_USER, $params['userids']);
d4e13355 367
fb79269b 368 $users = user_get_users_by_id($params['userids']);
109b453b 369 $result = array();
d4e13355 370 foreach ($users as $user) {
109b453b 371
b4c74367
JM
372 $context = $userscontexts[$user->id];
373 $hasviewdetailscap = has_capability('moodle/user:viewdetails', $context);
374 $hasuserupdatecap = has_capability('moodle/user:update', get_system_context());
375
6d153e67 376 self::validate_context($context);
377
b4c74367
JM
378 $currentuser = ($user->id == $USER->id);
379
fb79269b 380 if (empty($user->deleted)) {
381
b4c74367
JM
382 if (!$currentuser && !$hasviewdetailscap && !has_coursecontact_role($user->id)) {
383 throw new moodle_exception('usernotavailable', 'error');
384 }
385
109b453b 386 $userarray = array();
b4c74367
JM
387
388 //basic fields
109b453b 389 $userarray['id'] = $user->id;
b4c74367
JM
390 if ($isadmin) {
391 $userarray['username'] = $user->username;
392 }
393 if ($isadmin or has_capability('moodle/site:viewfullnames', $context)) {
394 $userarray['firstname'] = $user->firstname;
395 $userarray['lastname'] = $user->lastname;
396 }
397 $userarray['fullname'] = fullname($user);
398
399 //fields matching permissions from /user/editadvanced.php
400 if ($currentuser or $hasuserupdatecap) {
401 $userarray['auth'] = $user->auth;
402 $userarray['confirmed'] = $user->confirmed;
403 $userarray['idnumber'] = $user->idnumber;
404 $userarray['lang'] = $user->lang;
405 $userarray['theme'] = $user->theme;
406 $userarray['timezone'] = $user->timezone;
407 $userarray['mailformat'] = $user->mailformat;
408 }
409
410 //Custom fields (matching /user/profil/lib.php - profile_display_fields code logic)
0f31850a 411 $userarray['customfields'] = array();
b4c74367
JM
412 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
413 foreach ($categories as $category) {
414 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
415 foreach ($fields as $field) {
416 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
417 $newfield = 'profile_field_'.$field->datatype;
418 $formfield = new $newfield($field->id, $user->id);
419 if ($formfield->is_visible() and !$formfield->is_empty()) {
420 $userarray['customfields'][] =
421 array('name' => $formfield->field->name, 'value' => $formfield->data,
422 'type' => $field->datatype, 'shortname' => $formfield->field->shortname);
423 }
424 }
425 }
426 }
427 }
428
429 //image profiles urls (public, no permission required in fact)
430 $profileimageurl = moodle_url::make_pluginfile_url($context->id, 'user', 'icon', NULL, '/', 'f1');
431 $userarray['profileimageurl'] = $profileimageurl->out(false);
432 $profileimageurlsmall = moodle_url::make_pluginfile_url($context->id, 'user', 'icon', NULL, '/', 'f2');
433 $userarray['profileimageurlsmall'] = $profileimageurlsmall->out(false);
434
435 //hidden user field
436 if (has_capability('moodle/user:viewhiddendetails', $context)) {
437 $hiddenfields = array();
438 } else {
439 $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
fb79269b 440 }
441
b4c74367
JM
442 if (isset($user->description) && (!isset($hiddenfields['description']) or $isadmin)) {
443 if (empty($CFG->profilesforenrolledusersonly) || $currentuser) {
444 $user->description = file_rewrite_pluginfile_urls($user->description, 'pluginfile.php', $context->id, 'user', 'profile', null);
445 $userarray['description'] = $user->description;
446 $userarray['descriptionformat'] = $user->descriptionformat;
447 }
448 }
449
450 if ((! isset($hiddenfields['country']) or $isadmin) && $user->country) {
451 $userarray['country'] = $user->country;
452 }
453
454 if ((! isset($hiddenfields['city']) or $isadmin) && $user->city) {
455 $userarray['city'] = $user->city;
456 }
457
458 if (has_capability('moodle/user:viewhiddendetails', $context)) {
459 if ($user->address) {
460 $userarray['address'] = $user->address;
461 }
462 if ($user->phone1) {
463 $userarray['phone1'] = $user->phone1;
464 }
465 if ($user->phone2) {
466 $userarray['phone2'] = $user->phone2;
467 }
468 }
469
470 if ($currentuser
471 or $user->maildisplay == 1
472 or has_capability('moodle/course:useremail', $context)
473 or ($user->maildisplay == 2 and enrol_sharing_course($user, $USER))) {
474 $userarray['email'] = $user->email;;
475 }
476
477 if ($user->url && (!isset($hiddenfields['webpage']) or $isadmin)) {
478 $url = $user->url;
479 if (strpos($user->url, '://') === false) {
480 $url = 'http://'. $url;
481 }
482 $user->url = clean_param($user->url, PARAM_URL);
483 $userarray['url'] = $user->url;
484 }
485
486 if ($user->icq && (!isset($hiddenfields['icqnumber']) or $isadmin)) {
487 $userarray['icq'] = $user->icq;
488 }
489
490 if ($user->skype && (!isset($hiddenfields['skypeid']) or $isadmin)) {
491 $userarray['skype'] = $user->skype;
492 }
493 if ($user->yahoo && (!isset($hiddenfields['yahooid']) or $isadmin)) {
494 $userarray['yahoo'] = $user->yahoo;
495 }
496 if ($user->aim && (!isset($hiddenfields['aimid']) or $isadmin)) {
497 $userarray['aim'] = $user->aim;
498 }
499 if ($user->msn && (!isset($hiddenfields['msnid']) or $isadmin)) {
500 $userarray['msn'] = $user->msn;
501 }
502
503 if ((!isset($hiddenfields['firstaccess'])) or $isadmin) {
504 if ($user->firstaccess) {
505 $userarray['firstaccess'] = $user->firstaccess;
506 } else {
507 $userarray['firstaccess'] = 0;
508 }
509 }
510 if ((!isset($hiddenfields['lastaccess'])) or $isadmin) {
511 if ($user->lastaccess) {
512 $userarray['lastaccess'] = $user->lastaccess;
513 } else {
514 $userarray['lastaccess'] = 0;
515 }
516 }
517 /// Printing tagged interests
518 if (!empty($CFG->usetags)) {
519 require_once($CFG->dirroot . '/tag/lib.php');
520 if ($interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT) ) {
521 $userarray['interests'] = $interests;
522 }
523 }
524
525 //Departement/Institution are not displayed on any profile, however you can get them from editing profile.
526 if ($isadmin or $currentuser) {
527 if ($user->institution) {
528 $userarray['institution'] = $user->institution;
529 }
530 if (isset($user->department)) { //isset because it's ok to have department 0
531 $userarray['department'] = $user->department;
532 }
533 }
534
535 //list of courses where the user is enrolled
536 $enrolledcourses = array();
537 if (!isset($hiddenfields['mycourses'])) {
538 if ($mycourses = enrol_get_users_courses($user->id, true, NULL, 'visible DESC,sortorder ASC')) {
539 $courselisting = '';
540 foreach ($mycourses as $mycourse) {
541 if ($mycourse->category) {
542 if ($mycourse->visible == 0) {
543 $ccontext = get_context_instance(CONTEXT_COURSE, $mycourse->id);
544 if (!has_capability('moodle/course:viewhiddencourses', $ccontext)) {
545 continue;
546 }
547 }
548 $enrolledcourse = array();
549 $enrolledcourse['id'] = $mycourse->id;
550 $enrolledcourse['fullname'] = $mycourse->fullname;
551 $enrolledcourses[] = $enrolledcourse;
552 }
553 }
554 $userarray['enrolledcourses'] = $enrolledcourses;
555 }
556 }
557
558 //user preferences
559 if ($currentuser) {
560 $preferences = array();
561 $userpreferences = get_user_preferences();
562 foreach($userpreferences as $prefname => $prefvalue) {
563 $preferences[] = array('name' => $prefname, 'value' => $prefvalue);
564 }
565 $userarray['preferences'] = $preferences;
566 }
fb79269b 567 }
b4c74367
JM
568
569 $result[] = $userarray;
fb79269b 570 }
71864f15
PS
571
572 return $result;
d4e13355 573 }
7b472b32 574
109b453b 575 /**
7b472b32
PS
576 * Returns description of method result value
577 * @return external_description
578 */
fb79269b 579 public static function get_users_by_id_returns() {
71864f15 580 return new external_multiple_structure(
109b453b 581 new external_single_structure(
582 array(
fb79269b 583 'id' => new external_value(PARAM_NUMBER, 'ID of the user'),
b4c74367
JM
584 'username' => new external_value(PARAM_RAW, 'Username policy is defined in Moodle security config', VALUE_OPTIONAL),
585 'firstname' => new external_value(PARAM_NOTAGS, 'The first name(s) of the user', VALUE_OPTIONAL),
586 'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
587 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
588 'email' => new external_value(PARAM_TEXT, 'An email address - allow email as root@localhost', VALUE_OPTIONAL),
589 'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL),
590 'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
591 'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
592 'icq' => new external_value(PARAM_NOTAGS, 'icq number', VALUE_OPTIONAL),
593 'skype' => new external_value(PARAM_NOTAGS, 'skype id', VALUE_OPTIONAL),
594 'yahoo' => new external_value(PARAM_NOTAGS, 'yahoo id', VALUE_OPTIONAL),
595 'aim' => new external_value(PARAM_NOTAGS, 'aim id', VALUE_OPTIONAL),
596 'msn' => new external_value(PARAM_NOTAGS, 'msn number', VALUE_OPTIONAL),
597 'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
598 'institution' => new external_value(PARAM_TEXT, 'institution', VALUE_OPTIONAL),
599 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL),
600 'firstaccess' => new external_value(PARAM_INT, 'first access to the site (0 if never)', VALUE_OPTIONAL),
601 'lastaccess' => new external_value(PARAM_INT, 'last access to the site (0 if never)', VALUE_OPTIONAL),
602 'auth' => new external_value(PARAM_SAFEDIR, 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL),
603 'confirmed' => new external_value(PARAM_NUMBER, 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL),
604 'idnumber' => new external_value(PARAM_RAW, 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL),
605 'lang' => new external_value(PARAM_SAFEDIR, 'Language code such as "en", must exist on server', VALUE_OPTIONAL),
606 'theme' => new external_value(PARAM_SAFEDIR, 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL),
b4e29077 607 'timezone' => new external_value(PARAM_TIMEZONE, 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL),
b4c74367
JM
608 'mailformat' => new external_value(PARAM_INTEGER, 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL),
609 'description' => new external_value(PARAM_RAW, 'User profile description', VALUE_OPTIONAL),
610 'descriptionformat' => new external_value(PARAM_INT, 'User profile description format', VALUE_OPTIONAL),
611 'city' => new external_value(PARAM_NOTAGS, 'Home city of the user', VALUE_OPTIONAL),
612 'url' => new external_value(PARAM_URL, 'URL of the user', VALUE_OPTIONAL),
613 'country' => new external_value(PARAM_ALPHA, 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL),
614 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'),
615 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'),
71864f15 616 'customfields' => new external_multiple_structure(
109b453b 617 new external_single_structure(
618 array(
b4c74367
JM
619 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'),
620 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
621 'name' => new external_value(PARAM_RAW, 'The name of the custom field'),
622 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'),
109b453b 623 )
b4c74367
JM
624 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL),
625 'preferences' => new external_multiple_structure(
626 new external_single_structure(
627 array(
628 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'),
629 'value' => new external_value(PARAM_RAW, 'The value of the custom field'),
630 )
631 ), 'User preferences', VALUE_OPTIONAL),
632 'enrolledcourses' => new external_multiple_structure(
633 new external_single_structure(
634 array(
635 'id' => new external_value(PARAM_INT, 'Id of the course'),
636 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course')
637 )
638 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL)
109b453b 639 )
71864f15 640 )
71864f15 641 );
5de592b1 642 }
5de592b1 643}