[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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. 13 // 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/>. 16 17 /** 18 * External user API 19 * 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 24 */ 25 26 require_once("$CFG->libdir/externallib.php"); 27 28 /** 29 * User external functions 30 * 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 36 */ 37 class core_user_external extends external_api { 38 39 /** 40 * Returns description of method parameters 41 * 42 * @return external_function_parameters 43 * @since Moodle 2.2 44 */ 45 public static function create_users_parameters() { 46 global $CFG; 47 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(core_user::get_property_type('username'), 'Username policy is defined in Moodle security config.'), 55 'password' => 56 new external_value(core_user::get_property_type('password'), 'Plain text password consisting of any characters', VALUE_OPTIONAL), 57 'createpassword' => 58 new external_value(PARAM_BOOL, 'True if password should be created and mailed to user.', 59 VALUE_OPTIONAL), 60 'firstname' => 61 new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user'), 62 'lastname' => 63 new external_value(core_user::get_property_type('lastname'), 'The family name of the user'), 64 'email' => 65 new external_value(core_user::get_property_type('email'), 'A valid and unique email address'), 66 'auth' => 67 new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, imap, etc', VALUE_DEFAULT, 68 'manual', core_user::get_property_null('auth')), 69 'idnumber' => 70 new external_value(core_user::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution', 71 VALUE_DEFAULT, ''), 72 'lang' => 73 new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_DEFAULT, 74 core_user::get_property_default('lang'), core_user::get_property_null('lang')), 75 'calendartype' => 76 new external_value(core_user::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server', 77 VALUE_DEFAULT, $CFG->calendartype, VALUE_OPTIONAL), 78 'theme' => 79 new external_value(core_user::get_property_type('theme'), 'Theme name such as "standard", must exist on server', 80 VALUE_OPTIONAL), 81 'timezone' => 82 new external_value(core_user::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default', 83 VALUE_OPTIONAL), 84 'mailformat' => 85 new external_value(core_user::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc', 86 VALUE_OPTIONAL), 87 'description' => 88 new external_value(core_user::get_property_type('description'), 'User profile description, no HTML', VALUE_OPTIONAL), 89 'city' => 90 new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL), 91 'country' => 92 new external_value(core_user::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 93 'firstnamephonetic' => 94 new external_value(core_user::get_property_type('firstnamephonetic'), 'The first name(s) phonetically of the user', VALUE_OPTIONAL), 95 'lastnamephonetic' => 96 new external_value(core_user::get_property_type('lastnamephonetic'), 'The family name phonetically of the user', VALUE_OPTIONAL), 97 'middlename' => 98 new external_value(core_user::get_property_type('middlename'), 'The middle name of the user', VALUE_OPTIONAL), 99 'alternatename' => 100 new external_value(core_user::get_property_type('alternatename'), 'The alternate name of the user', VALUE_OPTIONAL), 101 'preferences' => new external_multiple_structure( 102 new external_single_structure( 103 array( 104 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'), 105 'value' => new external_value(PARAM_RAW, 'The value of the preference') 106 ) 107 ), 'User preferences', VALUE_OPTIONAL), 108 'customfields' => new external_multiple_structure( 109 new external_single_structure( 110 array( 111 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'), 112 'value' => new external_value(PARAM_RAW, 'The value of the custom field') 113 ) 114 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL) 115 ) 116 ) 117 ) 118 ) 119 ); 120 } 121 122 /** 123 * Create one or more users. 124 * 125 * @throws invalid_parameter_exception 126 * @param array $users An array of users to create. 127 * @return array An array of arrays 128 * @since Moodle 2.2 129 */ 130 public static function create_users($users) { 131 global $CFG, $DB; 132 require_once($CFG->dirroot."/lib/weblib.php"); 133 require_once($CFG->dirroot."/user/lib.php"); 134 require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function. 135 136 // Ensure the current user is allowed to run this function. 137 $context = context_system::instance(); 138 self::validate_context($context); 139 require_capability('moodle/user:create', $context); 140 141 // Do basic automatic PARAM checks on incoming data, using params description. 142 // If any problems are found then exceptions are thrown with helpful error messages. 143 $params = self::validate_parameters(self::create_users_parameters(), array('users' => $users)); 144 145 $availableauths = core_component::get_plugin_list('auth'); 146 unset($availableauths['mnet']); // These would need mnethostid too. 147 unset($availableauths['webservice']); // We do not want new webservice users for now. 148 149 $availablethemes = core_component::get_plugin_list('theme'); 150 $availablelangs = get_string_manager()->get_list_of_translations(); 151 152 $transaction = $DB->start_delegated_transaction(); 153 154 $userids = array(); 155 $createpassword = false; 156 foreach ($params['users'] as $user) { 157 // Make sure that the username doesn't already exist. 158 if ($DB->record_exists('user', array('username' => $user['username'], 'mnethostid' => $CFG->mnet_localhost_id))) { 159 throw new invalid_parameter_exception('Username already exists: '.$user['username']); 160 } 161 162 // Make sure auth is valid. 163 if (empty($availableauths[$user['auth']])) { 164 throw new invalid_parameter_exception('Invalid authentication type: '.$user['auth']); 165 } 166 167 // Make sure lang is valid. 168 if (empty($availablelangs[$user['lang']])) { 169 throw new invalid_parameter_exception('Invalid language code: '.$user['lang']); 170 } 171 172 // Make sure lang is valid. 173 if (!empty($user['theme']) && empty($availablethemes[$user['theme']])) { // Theme is VALUE_OPTIONAL, 174 // so no default value 175 // We need to test if the client sent it 176 // => !empty($user['theme']). 177 throw new invalid_parameter_exception('Invalid theme: '.$user['theme']); 178 } 179 180 // Make sure we have a password or have to create one. 181 if (empty($user['password']) && empty($user['createpassword'])) { 182 throw new invalid_parameter_exception('Invalid password: you must provide a password, or set createpassword.'); 183 } 184 185 $user['confirmed'] = true; 186 $user['mnethostid'] = $CFG->mnet_localhost_id; 187 188 // Start of user info validation. 189 // Make sure we validate current user info as handled by current GUI. See user/editadvanced_form.php func validation(). 190 if (!validate_email($user['email'])) { 191 throw new invalid_parameter_exception('Email address is invalid: '.$user['email']); 192 } else if (empty($CFG->allowaccountssameemail) && 193 $DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $user['mnethostid']))) { 194 throw new invalid_parameter_exception('Email address already exists: '.$user['email']); 195 } 196 // End of user info validation. 197 198 $createpassword = !empty($user['createpassword']); 199 unset($user['createpassword']); 200 if ($createpassword) { 201 $user['password'] = ''; 202 $updatepassword = false; 203 } else { 204 $updatepassword = true; 205 } 206 207 // Create the user data now! 208 $user['id'] = user_create_user($user, $updatepassword, false); 209 210 // Custom fields. 211 if (!empty($user['customfields'])) { 212 foreach ($user['customfields'] as $customfield) { 213 // Profile_save_data() saves profile file it's expecting a user with the correct id, 214 // and custom field to be named profile_field_"shortname". 215 $user["profile_field_".$customfield['type']] = $customfield['value']; 216 } 217 profile_save_data((object) $user); 218 } 219 220 if ($createpassword) { 221 $userobject = (object)$user; 222 setnew_password_and_mail($userobject); 223 unset_user_preference('create_password', $userobject); 224 set_user_preference('auth_forcepasswordchange', 1, $userobject); 225 } 226 227 // Trigger event. 228 \core\event\user_created::create_from_userid($user['id'])->trigger(); 229 230 // Preferences. 231 if (!empty($user['preferences'])) { 232 foreach ($user['preferences'] as $preference) { 233 set_user_preference($preference['type'], $preference['value'], $user['id']); 234 } 235 } 236 237 $userids[] = array('id' => $user['id'], 'username' => $user['username']); 238 } 239 240 $transaction->allow_commit(); 241 242 return $userids; 243 } 244 245 /** 246 * Returns description of method result value 247 * 248 * @return external_description 249 * @since Moodle 2.2 250 */ 251 public static function create_users_returns() { 252 return new external_multiple_structure( 253 new external_single_structure( 254 array( 255 'id' => new external_value(core_user::get_property_type('id'), 'user id'), 256 'username' => new external_value(core_user::get_property_type('username'), 'user name'), 257 ) 258 ) 259 ); 260 } 261 262 263 /** 264 * Returns description of method parameters 265 * 266 * @return external_function_parameters 267 * @since Moodle 2.2 268 */ 269 public static function delete_users_parameters() { 270 return new external_function_parameters( 271 array( 272 'userids' => new external_multiple_structure(new external_value(core_user::get_property_type('id'), 'user ID')), 273 ) 274 ); 275 } 276 277 /** 278 * Delete users 279 * 280 * @throws moodle_exception 281 * @param array $userids 282 * @return null 283 * @since Moodle 2.2 284 */ 285 public static function delete_users($userids) { 286 global $CFG, $DB, $USER; 287 require_once($CFG->dirroot."/user/lib.php"); 288 289 // Ensure the current user is allowed to run this function. 290 $context = context_system::instance(); 291 require_capability('moodle/user:delete', $context); 292 self::validate_context($context); 293 294 $params = self::validate_parameters(self::delete_users_parameters(), array('userids' => $userids)); 295 296 $transaction = $DB->start_delegated_transaction(); 297 298 foreach ($params['userids'] as $userid) { 299 $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST); 300 // Must not allow deleting of admins or self!!! 301 if (is_siteadmin($user)) { 302 throw new moodle_exception('useradminodelete', 'error'); 303 } 304 if ($USER->id == $user->id) { 305 throw new moodle_exception('usernotdeletederror', 'error'); 306 } 307 user_delete_user($user); 308 } 309 310 $transaction->allow_commit(); 311 312 return null; 313 } 314 315 /** 316 * Returns description of method result value 317 * 318 * @return null 319 * @since Moodle 2.2 320 */ 321 public static function delete_users_returns() { 322 return null; 323 } 324 325 326 /** 327 * Returns description of method parameters 328 * 329 * @return external_function_parameters 330 * @since Moodle 2.2 331 */ 332 public static function update_users_parameters() { 333 return new external_function_parameters( 334 array( 335 'users' => new external_multiple_structure( 336 new external_single_structure( 337 array( 338 'id' => 339 new external_value(core_user::get_property_type('id'), 'ID of the user'), 340 'username' => 341 new external_value(core_user::get_property_type('username'), 'Username policy is defined in Moodle security config.', 342 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED), 343 'password' => 344 new external_value(core_user::get_property_type('password'), 'Plain text password consisting of any characters', VALUE_OPTIONAL, 345 '', NULL_NOT_ALLOWED), 346 'firstname' => 347 new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL, '', 348 NULL_NOT_ALLOWED), 349 'lastname' => 350 new external_value(core_user::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL), 351 'email' => 352 new external_value(core_user::get_property_type('email'), 'A valid and unique email address', VALUE_OPTIONAL, '', 353 NULL_NOT_ALLOWED), 354 'auth' => 355 new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL, '', 356 NULL_NOT_ALLOWED), 357 'idnumber' => 358 new external_value(core_user::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution', 359 VALUE_OPTIONAL), 360 'lang' => 361 new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server', 362 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED), 363 'calendartype' => 364 new external_value(core_user::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server', 365 VALUE_OPTIONAL, '', NULL_NOT_ALLOWED), 366 'theme' => 367 new external_value(core_user::get_property_type('theme'), 'Theme name such as "standard", must exist on server', 368 VALUE_OPTIONAL), 369 'timezone' => 370 new external_value(core_user::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default', 371 VALUE_OPTIONAL), 372 'mailformat' => 373 new external_value(core_user::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc', 374 VALUE_OPTIONAL), 375 'description' => 376 new external_value(core_user::get_property_type('description'), 'User profile description, no HTML', VALUE_OPTIONAL), 377 'city' => 378 new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL), 379 'country' => 380 new external_value(core_user::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 381 'firstnamephonetic' => 382 new external_value(core_user::get_property_type('firstnamephonetic'), 'The first name(s) phonetically of the user', VALUE_OPTIONAL), 383 'lastnamephonetic' => 384 new external_value(core_user::get_property_type('lastnamephonetic'), 'The family name phonetically of the user', VALUE_OPTIONAL), 385 'middlename' => 386 new external_value(core_user::get_property_type('middlename'), 'The middle name of the user', VALUE_OPTIONAL), 387 'alternatename' => 388 new external_value(core_user::get_property_type('alternatename'), 'The alternate name of the user', VALUE_OPTIONAL), 389 'userpicture' => 390 new external_value(PARAM_INT, 'The itemid where the new user picture '. 391 'has been uploaded to, 0 to delete', VALUE_OPTIONAL), 392 'customfields' => new external_multiple_structure( 393 new external_single_structure( 394 array( 395 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the custom field'), 396 'value' => new external_value(PARAM_RAW, 'The value of the custom field') 397 ) 398 ), 'User custom fields (also known as user profil fields)', VALUE_OPTIONAL), 399 'preferences' => new external_multiple_structure( 400 new external_single_structure( 401 array( 402 'type' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preference'), 403 'value' => new external_value(PARAM_RAW, 'The value of the preference') 404 ) 405 ), 'User preferences', VALUE_OPTIONAL), 406 ) 407 ) 408 ) 409 ) 410 ); 411 } 412 413 /** 414 * Update users 415 * 416 * @param array $users 417 * @return null 418 * @since Moodle 2.2 419 */ 420 public static function update_users($users) { 421 global $CFG, $DB; 422 require_once($CFG->dirroot."/user/lib.php"); 423 require_once($CFG->dirroot."/user/profile/lib.php"); // Required for customfields related function. 424 425 // Ensure the current user is allowed to run this function. 426 $context = context_system::instance(); 427 require_capability('moodle/user:update', $context); 428 self::validate_context($context); 429 430 $params = self::validate_parameters(self::update_users_parameters(), array('users' => $users)); 431 432 $filemanageroptions = array('maxbytes' => $CFG->maxbytes, 433 'subdirs' => 0, 434 'maxfiles' => 1, 435 'accepted_types' => 'web_image'); 436 437 $transaction = $DB->start_delegated_transaction(); 438 439 foreach ($params['users'] as $user) { 440 user_update_user($user, true, false); 441 442 // Update user picture if it was specified for this user. 443 if (empty($CFG->disableuserimages) && isset($user['userpicture'])) { 444 $userobject = (object)$user; 445 446 $userobject->deletepicture = null; 447 448 if ($user['userpicture'] == 0) { 449 $userobject->deletepicture = true; 450 } else { 451 $userobject->imagefile = $user['userpicture']; 452 } 453 454 core_user::update_picture($userobject, $filemanageroptions); 455 } 456 457 // Update user custom fields. 458 if (!empty($user['customfields'])) { 459 460 foreach ($user['customfields'] as $customfield) { 461 // Profile_save_data() saves profile file it's expecting a user with the correct id, 462 // and custom field to be named profile_field_"shortname". 463 $user["profile_field_".$customfield['type']] = $customfield['value']; 464 } 465 profile_save_data((object) $user); 466 } 467 468 // Trigger event. 469 \core\event\user_updated::create_from_userid($user['id'])->trigger(); 470 471 // Preferences. 472 if (!empty($user['preferences'])) { 473 foreach ($user['preferences'] as $preference) { 474 set_user_preference($preference['type'], $preference['value'], $user['id']); 475 } 476 } 477 } 478 479 $transaction->allow_commit(); 480 481 return null; 482 } 483 484 /** 485 * Returns description of method result value 486 * 487 * @return null 488 * @since Moodle 2.2 489 */ 490 public static function update_users_returns() { 491 return null; 492 } 493 494 /** 495 * Returns description of method parameters 496 * 497 * @return external_function_parameters 498 * @since Moodle 2.4 499 */ 500 public static function get_users_by_field_parameters() { 501 return new external_function_parameters( 502 array( 503 'field' => new external_value(PARAM_ALPHA, 'the search field can be 504 \'id\' or \'idnumber\' or \'username\' or \'email\''), 505 'values' => new external_multiple_structure( 506 new external_value(PARAM_RAW, 'the value to match')) 507 ) 508 ); 509 } 510 511 /** 512 * Get user information for a unique field. 513 * 514 * @throws coding_exception 515 * @throws invalid_parameter_exception 516 * @param string $field 517 * @param array $values 518 * @return array An array of arrays containg user profiles. 519 * @since Moodle 2.4 520 */ 521 public static function get_users_by_field($field, $values) { 522 global $CFG, $USER, $DB; 523 require_once($CFG->dirroot . "/user/lib.php"); 524 525 $params = self::validate_parameters(self::get_users_by_field_parameters(), 526 array('field' => $field, 'values' => $values)); 527 528 // This array will keep all the users that are allowed to be searched, 529 // according to the current user's privileges. 530 $cleanedvalues = array(); 531 532 switch ($field) { 533 case 'id': 534 $paramtype = core_user::get_property_type('id'); 535 break; 536 case 'idnumber': 537 $paramtype = core_user::get_property_type('idnumber'); 538 break; 539 case 'username': 540 $paramtype = core_user::get_property_type('username'); 541 break; 542 case 'email': 543 $paramtype = core_user::get_property_type('email'); 544 break; 545 default: 546 throw new coding_exception('invalid field parameter', 547 'The search field \'' . $field . '\' is not supported, look at the web service documentation'); 548 } 549 550 // Clean the values. 551 foreach ($values as $value) { 552 $cleanedvalue = clean_param($value, $paramtype); 553 if ( $value != $cleanedvalue) { 554 throw new invalid_parameter_exception('The field \'' . $field . 555 '\' value is invalid: ' . $value . '(cleaned value: '.$cleanedvalue.')'); 556 } 557 $cleanedvalues[] = $cleanedvalue; 558 } 559 560 // Retrieve the users. 561 $users = $DB->get_records_list('user', $field, $cleanedvalues, 'id'); 562 563 $context = context_system::instance(); 564 self::validate_context($context); 565 566 // Finally retrieve each users information. 567 $returnedusers = array(); 568 foreach ($users as $user) { 569 $userdetails = user_get_user_details_courses($user); 570 571 // Return the user only if the searched field is returned. 572 // Otherwise it means that the $USER was not allowed to search the returned user. 573 if (!empty($userdetails) and !empty($userdetails[$field])) { 574 $returnedusers[] = $userdetails; 575 } 576 } 577 578 return $returnedusers; 579 } 580 581 /** 582 * Returns description of method result value 583 * 584 * @return external_multiple_structure 585 * @since Moodle 2.4 586 */ 587 public static function get_users_by_field_returns() { 588 return new external_multiple_structure(self::user_description()); 589 } 590 591 592 /** 593 * Returns description of get_users() parameters. 594 * 595 * @return external_function_parameters 596 * @since Moodle 2.5 597 */ 598 public static function get_users_parameters() { 599 return new external_function_parameters( 600 array( 601 'criteria' => new external_multiple_structure( 602 new external_single_structure( 603 array( 604 'key' => new external_value(PARAM_ALPHA, 'the user column to search, expected keys (value format) are: 605 "id" (int) matching user id, 606 "lastname" (string) user last name (Note: you can use % for searching but it may be considerably slower!), 607 "firstname" (string) user first name (Note: you can use % for searching but it may be considerably slower!), 608 "idnumber" (string) matching user idnumber, 609 "username" (string) matching user username, 610 "email" (string) user email (Note: you can use % for searching but it may be considerably slower!), 611 "auth" (string) matching user auth plugin'), 612 'value' => new external_value(PARAM_RAW, 'the value to search') 613 ) 614 ), 'the key/value pairs to be considered in user search. Values can not be empty. 615 Specify different keys only once (fullname => \'user1\', auth => \'manual\', ...) - 616 key occurences are forbidden. 617 The search is executed with AND operator on the criterias. Invalid criterias (keys) are ignored, 618 the search is still executed on the valid criterias. 619 You can search without criteria, but the function is not designed for it. 620 It could very slow or timeout. The function is designed to search some specific users.' 621 ) 622 ) 623 ); 624 } 625 626 /** 627 * Retrieve matching user. 628 * 629 * @throws moodle_exception 630 * @param array $criteria the allowed array keys are id/lastname/firstname/idnumber/username/email/auth. 631 * @return array An array of arrays containing user profiles. 632 * @since Moodle 2.5 633 */ 634 public static function get_users($criteria = array()) { 635 global $CFG, $USER, $DB; 636 637 require_once($CFG->dirroot . "/user/lib.php"); 638 639 $params = self::validate_parameters(self::get_users_parameters(), 640 array('criteria' => $criteria)); 641 642 // Validate the criteria and retrieve the users. 643 $users = array(); 644 $warnings = array(); 645 $sqlparams = array(); 646 $usedkeys = array(); 647 648 // Do not retrieve deleted users. 649 $sql = ' deleted = 0'; 650 651 foreach ($params['criteria'] as $criteriaindex => $criteria) { 652 653 // Check that the criteria has never been used. 654 if (array_key_exists($criteria['key'], $usedkeys)) { 655 throw new moodle_exception('keyalreadyset', '', '', null, 'The key ' . $criteria['key'] . ' can only be sent once'); 656 } else { 657 $usedkeys[$criteria['key']] = true; 658 } 659 660 $invalidcriteria = false; 661 // Clean the parameters. 662 $paramtype = PARAM_RAW; 663 switch ($criteria['key']) { 664 case 'id': 665 $paramtype = core_user::get_property_type('id'); 666 break; 667 case 'idnumber': 668 $paramtype = core_user::get_property_type('idnumber'); 669 break; 670 case 'username': 671 $paramtype = core_user::get_property_type('username'); 672 break; 673 case 'email': 674 // We use PARAM_RAW to allow searches with %. 675 $paramtype = core_user::get_property_type('email'); 676 break; 677 case 'auth': 678 $paramtype = core_user::get_property_type('auth'); 679 break; 680 case 'lastname': 681 case 'firstname': 682 $paramtype = core_user::get_property_type('firstname'); 683 break; 684 default: 685 // Send back a warning that this search key is not supported in this version. 686 // This warning will make the function extandable without breaking clients. 687 $warnings[] = array( 688 'item' => $criteria['key'], 689 'warningcode' => 'invalidfieldparameter', 690 'message' => 691 'The search key \'' . $criteria['key'] . '\' is not supported, look at the web service documentation' 692 ); 693 // Do not add this invalid criteria to the created SQL request. 694 $invalidcriteria = true; 695 unset($params['criteria'][$criteriaindex]); 696 break; 697 } 698 699 if (!$invalidcriteria) { 700 $cleanedvalue = clean_param($criteria['value'], $paramtype); 701 702 $sql .= ' AND '; 703 704 // Create the SQL. 705 switch ($criteria['key']) { 706 case 'id': 707 case 'idnumber': 708 case 'username': 709 case 'auth': 710 $sql .= $criteria['key'] . ' = :' . $criteria['key']; 711 $sqlparams[$criteria['key']] = $cleanedvalue; 712 break; 713 case 'email': 714 case 'lastname': 715 case 'firstname': 716 $sql .= $DB->sql_like($criteria['key'], ':' . $criteria['key'], false); 717 $sqlparams[$criteria['key']] = $cleanedvalue; 718 break; 719 default: 720 break; 721 } 722 } 723 } 724 725 $users = $DB->get_records_select('user', $sql, $sqlparams, 'id ASC'); 726 727 // Finally retrieve each users information. 728 $returnedusers = array(); 729 foreach ($users as $user) { 730 $userdetails = user_get_user_details_courses($user); 731 732 // Return the user only if all the searched fields are returned. 733 // Otherwise it means that the $USER was not allowed to search the returned user. 734 if (!empty($userdetails)) { 735 $validuser = true; 736 737 foreach ($params['criteria'] as $criteria) { 738 if (empty($userdetails[$criteria['key']])) { 739 $validuser = false; 740 } 741 } 742 743 if ($validuser) { 744 $returnedusers[] = $userdetails; 745 } 746 } 747 } 748 749 return array('users' => $returnedusers, 'warnings' => $warnings); 750 } 751 752 /** 753 * Returns description of get_users result value. 754 * 755 * @return external_description 756 * @since Moodle 2.5 757 */ 758 public static function get_users_returns() { 759 return new external_single_structure( 760 array('users' => new external_multiple_structure( 761 self::user_description() 762 ), 763 'warnings' => new external_warnings('always set to \'key\'', 'faulty key name') 764 ) 765 ); 766 } 767 768 /** 769 * Returns description of method parameters 770 * 771 * @return external_function_parameters 772 * @since Moodle 2.2 773 */ 774 public static function get_course_user_profiles_parameters() { 775 return new external_function_parameters( 776 array( 777 'userlist' => new external_multiple_structure( 778 new external_single_structure( 779 array( 780 'userid' => new external_value(core_user::get_property_type('id'), 'userid'), 781 'courseid' => new external_value(PARAM_INT, 'courseid'), 782 ) 783 ) 784 ) 785 ) 786 ); 787 } 788 789 /** 790 * Get course participant's details 791 * 792 * @param array $userlist array of user ids and according course ids 793 * @return array An array of arrays describing course participants 794 * @since Moodle 2.2 795 */ 796 public static function get_course_user_profiles($userlist) { 797 global $CFG, $USER, $DB; 798 require_once($CFG->dirroot . "/user/lib.php"); 799 $params = self::validate_parameters(self::get_course_user_profiles_parameters(), array('userlist' => $userlist)); 800 801 $userids = array(); 802 $courseids = array(); 803 foreach ($params['userlist'] as $value) { 804 $userids[] = $value['userid']; 805 $courseids[$value['userid']] = $value['courseid']; 806 } 807 808 // Cache all courses. 809 $courses = array(); 810 list($sqlcourseids, $params) = $DB->get_in_or_equal(array_unique($courseids), SQL_PARAMS_NAMED); 811 $cselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); 812 $cjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; 813 $params['contextlevel'] = CONTEXT_COURSE; 814 $coursesql = "SELECT c.* $cselect 815 FROM {course} c $cjoin 816 WHERE c.id $sqlcourseids"; 817 $rs = $DB->get_recordset_sql($coursesql, $params); 818 foreach ($rs as $course) { 819 // Adding course contexts to cache. 820 context_helper::preload_from_record($course); 821 // Cache courses. 822 $courses[$course->id] = $course; 823 } 824 $rs->close(); 825 826 list($sqluserids, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED); 827 $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); 828 $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)"; 829 $params['contextlevel'] = CONTEXT_USER; 830 $usersql = "SELECT u.* $uselect 831 FROM {user} u $ujoin 832 WHERE u.id $sqluserids"; 833 $users = $DB->get_recordset_sql($usersql, $params); 834 $result = array(); 835 foreach ($users as $user) { 836 if (!empty($user->deleted)) { 837 continue; 838 } 839 context_helper::preload_from_record($user); 840 $course = $courses[$courseids[$user->id]]; 841 $context = context_course::instance($courseids[$user->id], IGNORE_MISSING); 842 self::validate_context($context); 843 if ($userarray = user_get_user_details($user, $course)) { 844 $result[] = $userarray; 845 } 846 } 847 848 $users->close(); 849 850 return $result; 851 } 852 853 /** 854 * Returns description of method result value 855 * 856 * @return external_description 857 * @since Moodle 2.2 858 */ 859 public static function get_course_user_profiles_returns() { 860 $additionalfields = array( 861 'groups' => new external_multiple_structure( 862 new external_single_structure( 863 array( 864 'id' => new external_value(PARAM_INT, 'group id'), 865 'name' => new external_value(PARAM_RAW, 'group name'), 866 'description' => new external_value(PARAM_RAW, 'group description'), 867 'descriptionformat' => new external_format_value('description'), 868 ) 869 ), 'user groups', VALUE_OPTIONAL), 870 'roles' => new external_multiple_structure( 871 new external_single_structure( 872 array( 873 'roleid' => new external_value(PARAM_INT, 'role id'), 874 'name' => new external_value(PARAM_RAW, 'role name'), 875 'shortname' => new external_value(PARAM_ALPHANUMEXT, 'role shortname'), 876 'sortorder' => new external_value(PARAM_INT, 'role sortorder') 877 ) 878 ), 'user roles', VALUE_OPTIONAL), 879 'enrolledcourses' => new external_multiple_structure( 880 new external_single_structure( 881 array( 882 'id' => new external_value(PARAM_INT, 'Id of the course'), 883 'fullname' => new external_value(PARAM_RAW, 'Fullname of the course'), 884 'shortname' => new external_value(PARAM_RAW, 'Shortname of the course') 885 ) 886 ), 'Courses where the user is enrolled - limited by which courses the user is able to see', VALUE_OPTIONAL) 887 ); 888 889 return new external_multiple_structure(self::user_description($additionalfields)); 890 } 891 892 /** 893 * Create user return value description. 894 * 895 * @param array $additionalfields some additional field 896 * @return single_structure_description 897 */ 898 public static function user_description($additionalfields = array()) { 899 $userfields = array( 900 'id' => new external_value(core_user::get_property_type('id'), 'ID of the user'), 901 'username' => new external_value(core_user::get_property_type('username'), 'The username', VALUE_OPTIONAL), 902 'firstname' => new external_value(core_user::get_property_type('firstname'), 'The first name(s) of the user', VALUE_OPTIONAL), 903 'lastname' => new external_value(core_user::get_property_type('lastname'), 'The family name of the user', VALUE_OPTIONAL), 904 'fullname' => new external_value(core_user::get_property_type('firstname'), 'The fullname of the user'), 905 'email' => new external_value(core_user::get_property_type('email'), 'An email address - allow email as root@localhost', VALUE_OPTIONAL), 906 'address' => new external_value(core_user::get_property_type('address'), 'Postal address', VALUE_OPTIONAL), 907 'phone1' => new external_value(core_user::get_property_type('phone1'), 'Phone 1', VALUE_OPTIONAL), 908 'phone2' => new external_value(core_user::get_property_type('phone2'), 'Phone 2', VALUE_OPTIONAL), 909 'icq' => new external_value(core_user::get_property_type('icq'), 'icq number', VALUE_OPTIONAL), 910 'skype' => new external_value(core_user::get_property_type('skype'), 'skype id', VALUE_OPTIONAL), 911 'yahoo' => new external_value(core_user::get_property_type('yahoo'), 'yahoo id', VALUE_OPTIONAL), 912 'aim' => new external_value(core_user::get_property_type('aim'), 'aim id', VALUE_OPTIONAL), 913 'msn' => new external_value(core_user::get_property_type('msn'), 'msn number', VALUE_OPTIONAL), 914 'department' => new external_value(core_user::get_property_type('department'), 'department', VALUE_OPTIONAL), 915 'institution' => new external_value(core_user::get_property_type('institution'), 'institution', VALUE_OPTIONAL), 916 'idnumber' => new external_value(core_user::get_property_type('idnumber'), 'An arbitrary ID code number perhaps from the institution', VALUE_OPTIONAL), 917 'interests' => new external_value(PARAM_TEXT, 'user interests (separated by commas)', VALUE_OPTIONAL), 918 'firstaccess' => new external_value(core_user::get_property_type('firstaccess'), 'first access to the site (0 if never)', VALUE_OPTIONAL), 919 'lastaccess' => new external_value(core_user::get_property_type('lastaccess'), 'last access to the site (0 if never)', VALUE_OPTIONAL), 920 'auth' => new external_value(core_user::get_property_type('auth'), 'Auth plugins include manual, ldap, imap, etc', VALUE_OPTIONAL), 921 'confirmed' => new external_value(core_user::get_property_type('confirmed'), 'Active user: 1 if confirmed, 0 otherwise', VALUE_OPTIONAL), 922 'lang' => new external_value(core_user::get_property_type('lang'), 'Language code such as "en", must exist on server', VALUE_OPTIONAL), 923 'calendartype' => new external_value(core_user::get_property_type('calendartype'), 'Calendar type such as "gregorian", must exist on server', VALUE_OPTIONAL), 924 'theme' => new external_value(core_user::get_property_type('theme'), 'Theme name such as "standard", must exist on server', VALUE_OPTIONAL), 925 'timezone' => new external_value(core_user::get_property_type('timezone'), 'Timezone code such as Australia/Perth, or 99 for default', VALUE_OPTIONAL), 926 'mailformat' => new external_value(core_user::get_property_type('mailformat'), 'Mail format code is 0 for plain text, 1 for HTML etc', VALUE_OPTIONAL), 927 'description' => new external_value(core_user::get_property_type('description'), 'User profile description', VALUE_OPTIONAL), 928 'descriptionformat' => new external_format_value(core_user::get_property_type('descriptionformat'), VALUE_OPTIONAL), 929 'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user', VALUE_OPTIONAL), 930 'url' => new external_value(core_user::get_property_type('url'), 'URL of the user', VALUE_OPTIONAL), 931 'country' => new external_value(core_user::get_property_type('country'), 'Home country code of the user, such as AU or CZ', VALUE_OPTIONAL), 932 'profileimageurlsmall' => new external_value(PARAM_URL, 'User image profile URL - small version'), 933 'profileimageurl' => new external_value(PARAM_URL, 'User image profile URL - big version'), 934 'customfields' => new external_multiple_structure( 935 new external_single_structure( 936 array( 937 'type' => new external_value(PARAM_ALPHANUMEXT, 'The type of the custom field - text field, checkbox...'), 938 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 939 'name' => new external_value(PARAM_RAW, 'The name of the custom field'), 940 'shortname' => new external_value(PARAM_RAW, 'The shortname of the custom field - to be able to build the field class in the code'), 941 ) 942 ), 'User custom fields (also known as user profile fields)', VALUE_OPTIONAL), 943 'preferences' => new external_multiple_structure( 944 new external_single_structure( 945 array( 946 'name' => new external_value(PARAM_ALPHANUMEXT, 'The name of the preferences'), 947 'value' => new external_value(PARAM_RAW, 'The value of the custom field'), 948 ) 949 ), 'Users preferences', VALUE_OPTIONAL) 950 ); 951 if (!empty($additionalfields)) { 952 $userfields = array_merge($userfields, $additionalfields); 953 } 954 return new external_single_structure($userfields); 955 } 956 957 /** 958 * Returns description of method parameters 959 * 960 * @return external_function_parameters 961 * @since Moodle 2.6 962 */ 963 public static function add_user_private_files_parameters() { 964 return new external_function_parameters( 965 array( 966 'draftid' => new external_value(PARAM_INT, 'draft area id') 967 ) 968 ); 969 } 970 971 /** 972 * Copy files from a draft area to users private files area. 973 * 974 * @throws invalid_parameter_exception 975 * @param int $draftid Id of a draft area containing files. 976 * @return array An array of warnings 977 * @since Moodle 2.6 978 */ 979 public static function add_user_private_files($draftid) { 980 global $CFG, $USER; 981 require_once($CFG->libdir . "/filelib.php"); 982 983 $params = self::validate_parameters(self::add_user_private_files_parameters(), array('draftid' => $draftid)); 984 985 if (isguestuser()) { 986 throw new invalid_parameter_exception('Guest users cannot upload files'); 987 } 988 989 $context = context_user::instance($USER->id); 990 require_capability('moodle/user:manageownfiles', $context); 991 992 $maxbytes = $CFG->userquota; 993 $maxareabytes = $CFG->userquota; 994 if (has_capability('moodle/user:ignoreuserquota', $context)) { 995 $maxbytes = USER_CAN_IGNORE_FILE_SIZE_LIMITS; 996 $maxareabytes = FILE_AREA_MAX_BYTES_UNLIMITED; 997 } 998 999 $options = array('subdirs' => 1, 1000 'maxbytes' => $maxbytes, 1001 'maxfiles' => -1, 1002 'areamaxbytes' => $maxareabytes); 1003 1004 file_merge_files_from_draft_area_into_filearea($draftid, $context->id, 'user', 'private', 0, $options); 1005 1006 return null; 1007 } 1008 1009 /** 1010 * Returns description of method result value 1011 * 1012 * @return external_description 1013 * @since Moodle 2.2 1014 */ 1015 public static function add_user_private_files_returns() { 1016 return null; 1017 } 1018 1019 /** 1020 * Returns description of method parameters. 1021 * 1022 * @return external_function_parameters 1023 * @since Moodle 2.6 1024 */ 1025 public static function add_user_device_parameters() { 1026 return new external_function_parameters( 1027 array( 1028 'appid' => new external_value(PARAM_NOTAGS, 'the app id, usually something like com.moodle.moodlemobile'), 1029 'name' => new external_value(PARAM_NOTAGS, 'the device name, \'occam\' or \'iPhone\' etc.'), 1030 'model' => new external_value(PARAM_NOTAGS, 'the device model \'Nexus4\' or \'iPad1,1\' etc.'), 1031 'platform' => new external_value(PARAM_NOTAGS, 'the device platform \'iOS\' or \'Android\' etc.'), 1032 'version' => new external_value(PARAM_NOTAGS, 'the device version \'6.1.2\' or \'4.2.2\' etc.'), 1033 'pushid' => new external_value(PARAM_RAW, 'the device PUSH token/key/identifier/registration id'), 1034 'uuid' => new external_value(PARAM_RAW, 'the device UUID') 1035 ) 1036 ); 1037 } 1038 1039 /** 1040 * Add a user device in Moodle database (for PUSH notifications usually). 1041 * 1042 * @throws moodle_exception 1043 * @param string $appid The app id, usually something like com.moodle.moodlemobile. 1044 * @param string $name The device name, occam or iPhone etc. 1045 * @param string $model The device model Nexus4 or iPad1.1 etc. 1046 * @param string $platform The device platform iOs or Android etc. 1047 * @param string $version The device version 6.1.2 or 4.2.2 etc. 1048 * @param string $pushid The device PUSH token/key/identifier/registration id. 1049 * @param string $uuid The device UUID. 1050 * @return array List of possible warnings. 1051 * @since Moodle 2.6 1052 */ 1053 public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) { 1054 global $CFG, $USER, $DB; 1055 require_once($CFG->dirroot . "/user/lib.php"); 1056 1057 $params = self::validate_parameters(self::add_user_device_parameters(), 1058 array('appid' => $appid, 1059 'name' => $name, 1060 'model' => $model, 1061 'platform' => $platform, 1062 'version' => $version, 1063 'pushid' => $pushid, 1064 'uuid' => $uuid 1065 )); 1066 1067 $warnings = array(); 1068 1069 // Prevent duplicate keys for users. 1070 if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'userid' => $USER->id))) { 1071 $warnings['warning'][] = array( 1072 'item' => $params['pushid'], 1073 'warningcode' => 'existingkeyforthisuser', 1074 'message' => 'This key is already stored for this user' 1075 ); 1076 return $warnings; 1077 } 1078 1079 // Notice that we can have multiple devices because previously it was allowed to have repeated ones. 1080 // Since we don't have a clear way to decide which one is the more appropiate, we update all. 1081 if ($userdevices = $DB->get_records('user_devices', array('uuid' => $params['uuid'], 1082 'appid' => $params['appid'], 'userid' => $USER->id))) { 1083 1084 foreach ($userdevices as $userdevice) { 1085 $userdevice->version = $params['version']; // Maybe the user upgraded the device. 1086 $userdevice->pushid = $params['pushid']; 1087 $userdevice->timemodified = time(); 1088 $DB->update_record('user_devices', $userdevice); 1089 } 1090 1091 } else { 1092 $userdevice = new stdclass; 1093 $userdevice->userid = $USER->id; 1094 $userdevice->appid = $params['appid']; 1095 $userdevice->name = $params['name']; 1096 $userdevice->model = $params['model']; 1097 $userdevice->platform = $params['platform']; 1098 $userdevice->version = $params['version']; 1099 $userdevice->pushid = $params['pushid']; 1100 $userdevice->uuid = $params['uuid']; 1101 $userdevice->timecreated = time(); 1102 $userdevice->timemodified = $userdevice->timecreated; 1103 1104 if (!$DB->insert_record('user_devices', $userdevice)) { 1105 throw new moodle_exception("There was a problem saving in the database the device with key: " . $params['pushid']); 1106 } 1107 } 1108 1109 return $warnings; 1110 } 1111 1112 /** 1113 * Returns description of method result value. 1114 * 1115 * @return external_multiple_structure 1116 * @since Moodle 2.6 1117 */ 1118 public static function add_user_device_returns() { 1119 return new external_multiple_structure( 1120 new external_warnings() 1121 ); 1122 } 1123 1124 /** 1125 * Returns description of method parameters. 1126 * 1127 * @return external_function_parameters 1128 * @since Moodle 2.9 1129 */ 1130 public static function remove_user_device_parameters() { 1131 return new external_function_parameters( 1132 array( 1133 'uuid' => new external_value(PARAM_RAW, 'the device UUID'), 1134 'appid' => new external_value(PARAM_NOTAGS, 1135 'the app id, if empty devices matching the UUID for the user will be removed', 1136 VALUE_DEFAULT, ''), 1137 ) 1138 ); 1139 } 1140 1141 /** 1142 * Remove a user device from the Moodle database (for PUSH notifications usually). 1143 * 1144 * @param string $uuid The device UUID. 1145 * @param string $appid The app id, opitonal parameter. If empty all the devices fmatching the UUID or the user will be removed. 1146 * @return array List of possible warnings and removal status. 1147 * @since Moodle 2.9 1148 */ 1149 public static function remove_user_device($uuid, $appid = "") { 1150 global $CFG; 1151 require_once($CFG->dirroot . "/user/lib.php"); 1152 1153 $params = self::validate_parameters(self::remove_user_device_parameters(), array('uuid' => $uuid, 'appid' => $appid)); 1154 1155 $context = context_system::instance(); 1156 self::validate_context($context); 1157 1158 // Warnings array, it can be empty at the end but is mandatory. 1159 $warnings = array(); 1160 1161 $removed = user_remove_user_device($params['uuid'], $params['appid']); 1162 1163 if (!$removed) { 1164 $warnings[] = array( 1165 'item' => $params['uuid'], 1166 'warningcode' => 'devicedoesnotexist', 1167 'message' => 'The device doesn\'t exists in the database' 1168 ); 1169 } 1170 1171 $result = array( 1172 'removed' => $removed, 1173 'warnings' => $warnings 1174 ); 1175 1176 return $result; 1177 } 1178 1179 /** 1180 * Returns description of method result value. 1181 * 1182 * @return external_multiple_structure 1183 * @since Moodle 2.9 1184 */ 1185 public static function remove_user_device_returns() { 1186 return new external_single_structure( 1187 array( 1188 'removed' => new external_value(PARAM_BOOL, 'True if removed, false if not removed because it doesn\'t exists'), 1189 'warnings' => new external_warnings(), 1190 ) 1191 ); 1192 } 1193 1194 /** 1195 * Returns description of method parameters 1196 * 1197 * @return external_function_parameters 1198 * @since Moodle 2.9 1199 */ 1200 public static function view_user_list_parameters() { 1201 return new external_function_parameters( 1202 array( 1203 'courseid' => new external_value(PARAM_INT, 'id of the course, 0 for site') 1204 ) 1205 ); 1206 } 1207 1208 /** 1209 * Trigger the user_list_viewed event. 1210 * 1211 * @param int $courseid id of course 1212 * @return array of warnings and status result 1213 * @since Moodle 2.9 1214 * @throws moodle_exception 1215 */ 1216 public static function view_user_list($courseid) { 1217 global $CFG; 1218 require_once($CFG->dirroot . "/user/lib.php"); 1219 1220 $params = self::validate_parameters(self::view_user_list_parameters(), 1221 array( 1222 'courseid' => $courseid 1223 )); 1224 1225 $warnings = array(); 1226 1227 if (empty($params['courseid'])) { 1228 $params['courseid'] = SITEID; 1229 } 1230 1231 $course = get_course($params['courseid']); 1232 1233 if ($course->id == SITEID) { 1234 $context = context_system::instance(); 1235 } else { 1236 $context = context_course::instance($course->id); 1237 } 1238 self::validate_context($context); 1239 1240 if ($course->id == SITEID) { 1241 require_capability('moodle/site:viewparticipants', $context); 1242 } else { 1243 require_capability('moodle/course:viewparticipants', $context); 1244 } 1245 1246 user_list_view($course, $context); 1247 1248 $result = array(); 1249 $result['status'] = true; 1250 $result['warnings'] = $warnings; 1251 return $result; 1252 } 1253 1254 /** 1255 * Returns description of method result value 1256 * 1257 * @return external_description 1258 * @since Moodle 2.9 1259 */ 1260 public static function view_user_list_returns() { 1261 return new external_single_structure( 1262 array( 1263 'status' => new external_value(PARAM_BOOL, 'status: true if success'), 1264 'warnings' => new external_warnings() 1265 ) 1266 ); 1267 } 1268 1269 /** 1270 * Returns description of method parameters 1271 * 1272 * @return external_function_parameters 1273 * @since Moodle 2.9 1274 */ 1275 public static function view_user_profile_parameters() { 1276 return new external_function_parameters( 1277 array( 1278 'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_REQUIRED), 1279 'courseid' => new external_value(PARAM_INT, 'id of the course, default site course', VALUE_DEFAULT, 0) 1280 ) 1281 ); 1282 } 1283 1284 /** 1285 * Trigger the user profile viewed event. 1286 * 1287 * @param int $userid id of user 1288 * @param int $courseid id of course 1289 * @return array of warnings and status result 1290 * @since Moodle 2.9 1291 * @throws moodle_exception 1292 */ 1293 public static function view_user_profile($userid, $courseid = 0) { 1294 global $CFG, $USER; 1295 require_once($CFG->dirroot . "/user/profile/lib.php"); 1296 1297 $params = self::validate_parameters(self::view_user_profile_parameters(), 1298 array( 1299 'userid' => $userid, 1300 'courseid' => $courseid 1301 )); 1302 1303 $warnings = array(); 1304 1305 if (empty($params['userid'])) { 1306 $params['userid'] = $USER->id; 1307 } 1308 1309 if (empty($params['courseid'])) { 1310 $params['courseid'] = SITEID; 1311 } 1312 1313 $course = get_course($params['courseid']); 1314 $user = core_user::get_user($params['userid'], '*', MUST_EXIST); 1315 core_user::require_active_user($user); 1316 1317 if ($course->id == SITEID) { 1318 $coursecontext = context_system::instance();; 1319 } else { 1320 $coursecontext = context_course::instance($course->id); 1321 } 1322 self::validate_context($coursecontext); 1323 1324 $currentuser = $USER->id == $user->id; 1325 $usercontext = context_user::instance($user->id); 1326 1327 if (!$currentuser and 1328 !has_capability('moodle/user:viewdetails', $coursecontext) and 1329 !has_capability('moodle/user:viewdetails', $usercontext)) { 1330 throw new moodle_exception('cannotviewprofile'); 1331 } 1332 1333 // Case like user/profile.php. 1334 if ($course->id == SITEID) { 1335 profile_view($user, $usercontext); 1336 } else { 1337 // Case like user/view.php. 1338 if (!$currentuser and !can_access_course($course, $user, '', true)) { 1339 throw new moodle_exception('notenrolledprofile'); 1340 } 1341 1342 profile_view($user, $coursecontext, $course); 1343 } 1344 1345 $result = array(); 1346 $result['status'] = true; 1347 $result['warnings'] = $warnings; 1348 return $result; 1349 } 1350 1351 /** 1352 * Returns description of method result value 1353 * 1354 * @return external_description 1355 * @since Moodle 2.9 1356 */ 1357 public static function view_user_profile_returns() { 1358 return new external_single_structure( 1359 array( 1360 'status' => new external_value(PARAM_BOOL, 'status: true if success'), 1361 'warnings' => new external_warnings() 1362 ) 1363 ); 1364 } 1365 1366 /** 1367 * Returns description of method parameters 1368 * 1369 * @return external_function_parameters 1370 * @since Moodle 3.2 1371 */ 1372 public static function get_user_preferences_parameters() { 1373 return new external_function_parameters( 1374 array( 1375 'name' => new external_value(PARAM_RAW, 'preference name, empty for all', VALUE_DEFAULT, ''), 1376 'userid' => new external_value(PARAM_INT, 'id of the user, default to current user', VALUE_DEFAULT, 0) 1377 ) 1378 ); 1379 } 1380 1381 /** 1382 * Return user preferences. 1383 * 1384 * @param string $name preference name, empty for all 1385 * @param int $userid id of the user, 0 for current user 1386 * @return array of warnings and preferences 1387 * @since Moodle 3.2 1388 * @throws moodle_exception 1389 */ 1390 public static function get_user_preferences($name = '', $userid = 0) { 1391 global $USER; 1392 1393 $params = self::validate_parameters(self::get_user_preferences_parameters(), 1394 array( 1395 'name' => $name, 1396 'userid' => $userid 1397 )); 1398 $preferences = array(); 1399 $warnings = array(); 1400 1401 $context = context_system::instance(); 1402 self::validate_context($context); 1403 1404 if (empty($params['name'])) { 1405 $name = null; 1406 } 1407 if (empty($params['userid'])) { 1408 $user = null; 1409 } else { 1410 $user = core_user::get_user($params['userid'], '*', MUST_EXIST); 1411 core_user::require_active_user($user); 1412 if ($user->id != $USER->id) { 1413 // Only admins can retrieve other users preferences. 1414 require_capability('moodle/site:config', $context); 1415 } 1416 } 1417 1418 $userpreferences = get_user_preferences($name, null, $user); 1419 // Check if we received just one preference. 1420 if (!is_array($userpreferences)) { 1421 $userpreferences = array($name => $userpreferences); 1422 } 1423 1424 foreach ($userpreferences as $name => $value) { 1425 $preferences[] = array( 1426 'name' => $name, 1427 'value' => $value, 1428 ); 1429 } 1430 1431 $result = array(); 1432 $result['preferences'] = $preferences; 1433 $result['warnings'] = $warnings; 1434 return $result; 1435 } 1436 1437 /** 1438 * Returns description of method result value 1439 * 1440 * @return external_description 1441 * @since Moodle 3.2 1442 */ 1443 public static function get_user_preferences_returns() { 1444 return new external_single_structure( 1445 array( 1446 'preferences' => new external_multiple_structure( 1447 new external_single_structure( 1448 array( 1449 'name' => new external_value(PARAM_RAW, 'The name of the preference'), 1450 'value' => new external_value(PARAM_RAW, 'The value of the preference'), 1451 ) 1452 ), 1453 'User custom fields (also known as user profile fields)' 1454 ), 1455 'warnings' => new external_warnings() 1456 ) 1457 ); 1458 } 1459 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |