Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 * Profile field API library file. 19 * 20 * @package core_user 21 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 /** 26 * Visible to anyone who has the moodle/site:viewuseridentity permission. 27 * Editable by the profile owner if they have the moodle/user:editownprofile capability 28 * or any user with the moodle/user:update capability. 29 */ 30 define('PROFILE_VISIBLE_TEACHERS', '3'); 31 32 /** 33 * Visible to anyone who can view the user. 34 * Editable by the profile owner if they have the moodle/user:editownprofile capability 35 * or any user with the moodle/user:update capability. 36 */ 37 define('PROFILE_VISIBLE_ALL', '2'); 38 /** 39 * Visible to the profile owner or anyone with the moodle/user:viewalldetails capability. 40 * Editable by the profile owner if they have the moodle/user:editownprofile capability 41 * or any user with moodle/user:viewalldetails and moodle/user:update capabilities. 42 */ 43 define('PROFILE_VISIBLE_PRIVATE', '1'); 44 /** 45 * Only visible to users with the moodle/user:viewalldetails capability. 46 * Only editable by users with the moodle/user:viewalldetails and moodle/user:update capabilities. 47 */ 48 define('PROFILE_VISIBLE_NONE', '0'); 49 50 /** 51 * Base class for the customisable profile fields. 52 * 53 * @package core_user 54 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com} 55 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 56 */ 57 class profile_field_base { 58 59 // These 2 variables are really what we're interested in. 60 // Everything else can be extracted from them. 61 62 /** @var int */ 63 public $fieldid; 64 65 /** @var int */ 66 public $userid; 67 68 /** @var stdClass */ 69 public $field; 70 71 /** @var string */ 72 public $inputname; 73 74 /** @var mixed */ 75 public $data; 76 77 /** @var string */ 78 public $dataformat; 79 80 /** @var string name of the user profile category */ 81 protected $categoryname; 82 83 /** 84 * Constructor method. 85 * @param int $fieldid id of the profile from the user_info_field table 86 * @param int $userid id of the user for whom we are displaying data 87 * @param stdClass $fielddata optional data for the field object plus additional fields 'hasuserdata', 'data' and 'dataformat' 88 * with user data. (If $fielddata->hasuserdata is empty, user data is not available and we should use default data). 89 * If this parameter is passed, constructor will not call load_data() at all. 90 */ 91 public function __construct($fieldid=0, $userid=0, $fielddata=null) { 92 global $CFG; 93 94 if ($CFG->debugdeveloper) { 95 // In Moodle 3.4 the new argument $fielddata was added to the constructor. Make sure that 96 // plugin constructor properly passes this argument. 97 $backtrace = debug_backtrace(); 98 if (isset($backtrace[1]['class']) && $backtrace[1]['function'] === '__construct' && 99 in_array(self::class, class_parents($backtrace[1]['class']))) { 100 // If this constructor is called from the constructor of the plugin make sure that the third argument was passed through. 101 if (count($backtrace[1]['args']) >= 3 && count($backtrace[0]['args']) < 3) { 102 debugging($backtrace[1]['class'].'::__construct() must support $fielddata as the third argument ' . 103 'and pass it to the parent constructor', DEBUG_DEVELOPER); 104 } 105 } 106 } 107 108 $this->set_fieldid($fieldid); 109 $this->set_userid($userid); 110 if ($fielddata) { 111 $this->set_field($fielddata); 112 if ($userid > 0 && !empty($fielddata->hasuserdata)) { 113 $this->set_user_data($fielddata->data, $fielddata->dataformat); 114 } 115 } else { 116 $this->load_data(); 117 } 118 } 119 120 /** 121 * Old syntax of class constructor. Deprecated in PHP7. 122 * 123 * @deprecated since Moodle 3.1 124 */ 125 public function profile_field_base($fieldid=0, $userid=0) { 126 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); 127 self::__construct($fieldid, $userid); 128 } 129 130 /** 131 * Abstract method: Adds the profile field to the moodle form class 132 * @abstract The following methods must be overwritten by child classes 133 * @param MoodleQuickForm $mform instance of the moodleform class 134 */ 135 public function edit_field_add($mform) { 136 print_error('mustbeoveride', 'debug', '', 'edit_field_add'); 137 } 138 139 /** 140 * Display the data for this field 141 * @return string 142 */ 143 public function display_data() { 144 $options = new stdClass(); 145 $options->para = false; 146 return format_text($this->data, FORMAT_MOODLE, $options); 147 } 148 149 /** 150 * Print out the form field in the edit profile page 151 * @param MoodleQuickForm $mform instance of the moodleform class 152 * @return bool 153 */ 154 public function edit_field($mform) { 155 if (!$this->is_editable()) { 156 return false; 157 } 158 159 $this->edit_field_add($mform); 160 $this->edit_field_set_default($mform); 161 $this->edit_field_set_required($mform); 162 return true; 163 } 164 165 /** 166 * Tweaks the edit form 167 * @param MoodleQuickForm $mform instance of the moodleform class 168 * @return bool 169 */ 170 public function edit_after_data($mform) { 171 if (!$this->is_editable()) { 172 return false; 173 } 174 175 $this->edit_field_set_locked($mform); 176 return true; 177 } 178 179 /** 180 * Saves the data coming from form 181 * @param stdClass $usernew data coming from the form 182 */ 183 public function edit_save_data($usernew) { 184 global $DB; 185 186 if (!isset($usernew->{$this->inputname})) { 187 // Field not present in form, probably locked and invisible - skip it. 188 return; 189 } 190 191 $data = new stdClass(); 192 193 $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname}, $data); 194 if (!isset($usernew->{$this->inputname})) { 195 // Field cannot be set to null, set the default value. 196 $usernew->{$this->inputname} = $this->field->defaultdata; 197 } 198 199 $data->userid = $usernew->id; 200 $data->fieldid = $this->field->id; 201 $data->data = $usernew->{$this->inputname}; 202 203 if ($dataid = $DB->get_field('user_info_data', 'id', array('userid' => $data->userid, 'fieldid' => $data->fieldid))) { 204 $data->id = $dataid; 205 $DB->update_record('user_info_data', $data); 206 } else { 207 $DB->insert_record('user_info_data', $data); 208 } 209 } 210 211 /** 212 * Validate the form field from profile page 213 * 214 * @param stdClass $usernew 215 * @return array error messages for the form validation 216 */ 217 public function edit_validate_field($usernew) { 218 global $DB; 219 220 $errors = array(); 221 // Get input value. 222 if (isset($usernew->{$this->inputname})) { 223 if (is_array($usernew->{$this->inputname}) && isset($usernew->{$this->inputname}['text'])) { 224 $value = $usernew->{$this->inputname}['text']; 225 } else { 226 $value = $usernew->{$this->inputname}; 227 } 228 } else { 229 $value = ''; 230 } 231 232 // Check for uniqueness of data if required. 233 if ($this->is_unique() && (($value !== '') || $this->is_required())) { 234 $data = $DB->get_records_sql(' 235 SELECT id, userid 236 FROM {user_info_data} 237 WHERE fieldid = ? 238 AND ' . $DB->sql_compare_text('data', 255) . ' = ' . $DB->sql_compare_text('?', 255), 239 array($this->field->id, $value)); 240 if ($data) { 241 $existing = false; 242 foreach ($data as $v) { 243 if ($v->userid == $usernew->id) { 244 $existing = true; 245 break; 246 } 247 } 248 if (!$existing) { 249 $errors[$this->inputname] = get_string('valuealreadyused'); 250 } 251 } 252 } 253 return $errors; 254 } 255 256 /** 257 * Sets the default data for the field in the form object 258 * @param MoodleQuickForm $mform instance of the moodleform class 259 */ 260 public function edit_field_set_default($mform) { 261 if (!empty($this->field->defaultdata)) { 262 $mform->setDefault($this->inputname, $this->field->defaultdata); 263 } 264 } 265 266 /** 267 * Sets the required flag for the field in the form object 268 * 269 * @param MoodleQuickForm $mform instance of the moodleform class 270 */ 271 public function edit_field_set_required($mform) { 272 global $USER; 273 if ($this->is_required() && ($this->userid == $USER->id || isguestuser())) { 274 $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client'); 275 } 276 } 277 278 /** 279 * HardFreeze the field if locked. 280 * @param MoodleQuickForm $mform instance of the moodleform class 281 */ 282 public function edit_field_set_locked($mform) { 283 if (!$mform->elementExists($this->inputname)) { 284 return; 285 } 286 if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) { 287 $mform->hardFreeze($this->inputname); 288 $mform->setConstant($this->inputname, $this->data); 289 } 290 } 291 292 /** 293 * Hook for child classess to process the data before it gets saved in database 294 * @param stdClass $data 295 * @param stdClass $datarecord The object that will be used to save the record 296 * @return mixed 297 */ 298 public function edit_save_data_preprocess($data, $datarecord) { 299 return $data; 300 } 301 302 /** 303 * Loads a user object with data for this field ready for the edit profile 304 * form 305 * @param stdClass $user a user object 306 */ 307 public function edit_load_user_data($user) { 308 if ($this->data !== null) { 309 $user->{$this->inputname} = $this->data; 310 } 311 } 312 313 /** 314 * Check if the field data should be loaded into the user object 315 * By default it is, but for field types where the data may be potentially 316 * large, the child class should override this and return false 317 * @return bool 318 */ 319 public function is_user_object_data() { 320 return true; 321 } 322 323 /** 324 * Accessor method: set the userid for this instance 325 * @internal This method should not generally be overwritten by child classes. 326 * @param integer $userid id from the user table 327 */ 328 public function set_userid($userid) { 329 $this->userid = $userid; 330 } 331 332 /** 333 * Accessor method: set the fieldid for this instance 334 * @internal This method should not generally be overwritten by child classes. 335 * @param integer $fieldid id from the user_info_field table 336 */ 337 public function set_fieldid($fieldid) { 338 $this->fieldid = $fieldid; 339 } 340 341 /** 342 * Sets the field object and default data and format into $this->data and $this->dataformat 343 * 344 * This method should be called before {@link self::set_user_data} 345 * 346 * @param stdClass $field 347 * @throws coding_exception 348 */ 349 public function set_field($field) { 350 global $CFG; 351 if ($CFG->debugdeveloper) { 352 $properties = ['id', 'shortname', 'name', 'datatype', 'description', 'descriptionformat', 'categoryid', 'sortorder', 353 'required', 'locked', 'visible', 'forceunique', 'signup', 'defaultdata', 'defaultdataformat', 'param1', 'param2', 354 'param3', 'param4', 'param5']; 355 foreach ($properties as $property) { 356 if (!property_exists($field, $property)) { 357 debugging('The \'' . $property . '\' property must be set.', DEBUG_DEVELOPER); 358 } 359 } 360 } 361 if ($this->fieldid && $this->fieldid != $field->id) { 362 throw new coding_exception('Can not set field object after a different field id was set'); 363 } 364 $this->fieldid = $field->id; 365 $this->field = $field; 366 $this->inputname = 'profile_field_' . $this->field->shortname; 367 $this->data = $this->field->defaultdata; 368 $this->dataformat = FORMAT_HTML; 369 } 370 371 /** 372 * Sets user id and user data for the field 373 * 374 * @param mixed $data 375 * @param int $dataformat 376 */ 377 public function set_user_data($data, $dataformat) { 378 $this->data = $data; 379 $this->dataformat = $dataformat; 380 } 381 382 /** 383 * Set the name for the profile category where this field is 384 * 385 * @param string $categoryname 386 */ 387 public function set_category_name($categoryname) { 388 $this->categoryname = $categoryname; 389 } 390 391 /** 392 * Return field short name 393 * 394 * @return string 395 */ 396 public function get_shortname(): string { 397 return $this->field->shortname; 398 } 399 400 /** 401 * Returns the name of the profile category where this field is 402 * 403 * @return string 404 */ 405 public function get_category_name() { 406 global $DB; 407 if ($this->categoryname === null) { 408 $this->categoryname = $DB->get_field('user_info_category', 'name', ['id' => $this->field->categoryid]); 409 } 410 return $this->categoryname; 411 } 412 413 /** 414 * Accessor method: Load the field record and user data associated with the 415 * object's fieldid and userid 416 * 417 * @internal This method should not generally be overwritten by child classes. 418 */ 419 public function load_data() { 420 global $DB; 421 422 // Load the field object. 423 if (($this->fieldid == 0) or (!($field = $DB->get_record('user_info_field', array('id' => $this->fieldid))))) { 424 $this->field = null; 425 $this->inputname = ''; 426 } else { 427 $this->set_field($field); 428 } 429 430 if (!empty($this->field) && $this->userid > 0) { 431 $params = array('userid' => $this->userid, 'fieldid' => $this->fieldid); 432 if ($data = $DB->get_record('user_info_data', $params, 'data, dataformat')) { 433 $this->set_user_data($data->data, $data->dataformat); 434 } 435 } else { 436 $this->data = null; 437 } 438 } 439 440 /** 441 * Check if the field data is visible to the current user 442 * @internal This method should not generally be overwritten by child classes. 443 * @return bool 444 */ 445 public function is_visible() { 446 global $USER, $COURSE; 447 448 $context = ($this->userid > 0) ? context_user::instance($this->userid) : context_system::instance(); 449 450 switch ($this->field->visible) { 451 case PROFILE_VISIBLE_TEACHERS: 452 if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) { 453 return true; 454 } else if ($this->userid == $USER->id) { 455 return true; 456 } else { 457 $coursecontext = context_course::instance($COURSE->id); 458 return has_capability('moodle/site:viewuseridentity', $coursecontext); 459 } 460 case PROFILE_VISIBLE_ALL: 461 return true; 462 case PROFILE_VISIBLE_PRIVATE: 463 if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) { 464 return true; 465 } else if ($this->userid == $USER->id) { 466 return true; 467 } else { 468 return has_capability('moodle/user:viewalldetails', $context); 469 } 470 default: 471 return has_capability('moodle/user:viewalldetails', $context); 472 } 473 } 474 475 /** 476 * Check if the field data is editable for the current user 477 * This method should not generally be overwritten by child classes. 478 * @return bool 479 */ 480 public function is_editable() { 481 global $USER; 482 483 if (!$this->is_visible()) { 484 return false; 485 } 486 487 if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) { 488 // Allow editing the field on the signup page. 489 return true; 490 } 491 492 $systemcontext = context_system::instance(); 493 494 if ($this->userid == $USER->id && has_capability('moodle/user:editownprofile', $systemcontext)) { 495 return true; 496 } 497 498 if (has_capability('moodle/user:update', $systemcontext)) { 499 return true; 500 } 501 502 return false; 503 } 504 505 /** 506 * Check if the field data is considered empty 507 * @internal This method should not generally be overwritten by child classes. 508 * @return boolean 509 */ 510 public function is_empty() { 511 return ( ($this->data != '0') and empty($this->data)); 512 } 513 514 /** 515 * Check if the field is required on the edit profile page 516 * @internal This method should not generally be overwritten by child classes. 517 * @return bool 518 */ 519 public function is_required() { 520 return (boolean)$this->field->required; 521 } 522 523 /** 524 * Check if the field is locked on the edit profile page 525 * @internal This method should not generally be overwritten by child classes. 526 * @return bool 527 */ 528 public function is_locked() { 529 return (boolean)$this->field->locked; 530 } 531 532 /** 533 * Check if the field data should be unique 534 * @internal This method should not generally be overwritten by child classes. 535 * @return bool 536 */ 537 public function is_unique() { 538 return (boolean)$this->field->forceunique; 539 } 540 541 /** 542 * Check if the field should appear on the signup page 543 * @internal This method should not generally be overwritten by child classes. 544 * @return bool 545 */ 546 public function is_signup_field() { 547 return (boolean)$this->field->signup; 548 } 549 550 /** 551 * Return the field settings suitable to be exported via an external function. 552 * By default it return all the field settings. 553 * 554 * @return array all the settings 555 * @since Moodle 3.2 556 */ 557 public function get_field_config_for_external() { 558 return (array) $this->field; 559 } 560 561 /** 562 * Return the field type and null properties. 563 * This will be used for validating the data submitted by a user. 564 * 565 * @return array the param type and null property 566 * @since Moodle 3.2 567 */ 568 public function get_field_properties() { 569 return array(PARAM_RAW, NULL_NOT_ALLOWED); 570 } 571 } 572 573 /** 574 * Returns an array of all custom field records with any defined data (or empty data), for the specified user id. 575 * @param int $userid 576 * @return profile_field_base[] 577 */ 578 function profile_get_user_fields_with_data(int $userid): array { 579 global $DB, $CFG; 580 581 // Join any user info data present with each user info field for the user object. 582 $sql = 'SELECT uif.*, uic.name AS categoryname '; 583 if ($userid > 0) { 584 $sql .= ', uind.id AS hasuserdata, uind.data, uind.dataformat '; 585 } 586 $sql .= 'FROM {user_info_field} uif '; 587 $sql .= 'LEFT JOIN {user_info_category} uic ON uif.categoryid = uic.id '; 588 if ($userid > 0) { 589 $sql .= 'LEFT JOIN {user_info_data} uind ON uif.id = uind.fieldid AND uind.userid = :userid '; 590 } 591 $sql .= 'ORDER BY uic.sortorder ASC, uif.sortorder ASC '; 592 $fields = $DB->get_records_sql($sql, ['userid' => $userid]); 593 $data = []; 594 foreach ($fields as $field) { 595 require_once($CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php'); 596 $classname = 'profile_field_' . $field->datatype; 597 $field->hasuserdata = !empty($field->hasuserdata); 598 /** @var profile_field_base $fieldobject */ 599 $fieldobject = new $classname($field->id, $userid, $field); 600 $fieldobject->set_category_name($field->categoryname); 601 unset($field->categoryname); 602 $data[] = $fieldobject; 603 } 604 return $data; 605 } 606 607 /** 608 * Returns an array of all custom field records with any defined data (or empty data), for the specified user id, by category. 609 * @param int $userid 610 * @return profile_field_base[][] 611 */ 612 function profile_get_user_fields_with_data_by_category(int $userid): array { 613 $fields = profile_get_user_fields_with_data($userid); 614 $data = []; 615 foreach ($fields as $field) { 616 $data[$field->field->categoryid][] = $field; 617 } 618 return $data; 619 } 620 621 /** 622 * Loads user profile field data into the user object. 623 * @param stdClass $user 624 */ 625 function profile_load_data(stdClass $user): void { 626 $fields = profile_get_user_fields_with_data($user->id); 627 foreach ($fields as $formfield) { 628 $formfield->edit_load_user_data($user); 629 } 630 } 631 632 /** 633 * Print out the customisable categories and fields for a users profile 634 * 635 * @param MoodleQuickForm $mform instance of the moodleform class 636 * @param int $userid id of user whose profile is being edited or 0 for the new user 637 */ 638 function profile_definition(MoodleQuickForm $mform, int $userid = 0): void { 639 $categories = profile_get_user_fields_with_data_by_category($userid); 640 foreach ($categories as $categoryid => $fields) { 641 // Check first if *any* fields will be displayed. 642 $fieldstodisplay = []; 643 644 foreach ($fields as $formfield) { 645 if ($formfield->is_editable()) { 646 $fieldstodisplay[] = $formfield; 647 } 648 } 649 650 if (empty($fieldstodisplay)) { 651 continue; 652 } 653 654 // Display the header and the fields. 655 $mform->addElement('header', 'category_'.$categoryid, format_string($fields[0]->get_category_name())); 656 foreach ($fieldstodisplay as $formfield) { 657 $formfield->edit_field($mform); 658 } 659 } 660 } 661 662 /** 663 * Adds profile fields to user edit forms. 664 * @param MoodleQuickForm $mform 665 * @param int $userid 666 */ 667 function profile_definition_after_data(MoodleQuickForm $mform, int $userid): void { 668 $userid = ($userid < 0) ? 0 : (int)$userid; 669 670 $fields = profile_get_user_fields_with_data($userid); 671 foreach ($fields as $formfield) { 672 $formfield->edit_after_data($mform); 673 } 674 } 675 676 /** 677 * Validates profile data. 678 * @param stdClass $usernew 679 * @param array $files 680 * @return array array of errors, same as in {@see moodleform::validation()} 681 */ 682 function profile_validation(stdClass $usernew, array $files): array { 683 $err = array(); 684 $fields = profile_get_user_fields_with_data($usernew->id); 685 foreach ($fields as $formfield) { 686 $err += $formfield->edit_validate_field($usernew, $files); 687 } 688 return $err; 689 } 690 691 /** 692 * Saves profile data for a user. 693 * @param stdClass $usernew 694 */ 695 function profile_save_data(stdClass $usernew): void { 696 global $CFG; 697 698 $fields = profile_get_user_fields_with_data($usernew->id); 699 foreach ($fields as $formfield) { 700 $formfield->edit_save_data($usernew); 701 } 702 } 703 704 /** 705 * Display profile fields. 706 * 707 * @deprecated since Moodle 3.11 MDL-71051 - please do not use this function any more. 708 * @todo MDL-71413 This will be deleted in Moodle 4.3. 709 * 710 * @param int $userid 711 */ 712 function profile_display_fields($userid) { 713 debugging('Function profile_display_fields() is deprecated because it is no longer used and will be '. 714 'removed in future versions of Moodle', DEBUG_DEVELOPER); 715 716 $categories = profile_get_user_fields_with_data_by_category($userid); 717 foreach ($categories as $categoryid => $fields) { 718 foreach ($fields as $formfield) { 719 if ($formfield->is_visible() and !$formfield->is_empty()) { 720 echo html_writer::tag('dt', format_string($formfield->field->name)); 721 echo html_writer::tag('dd', $formfield->display_data()); 722 } 723 } 724 } 725 } 726 727 /** 728 * Retrieves a list of profile fields that must be displayed in the sign-up form. 729 * 730 * @return array list of profile fields info 731 * @since Moodle 3.2 732 */ 733 function profile_get_signup_fields(): array { 734 $profilefields = array(); 735 $fieldobjects = profile_get_user_fields_with_data(0); 736 foreach ($fieldobjects as $fieldobject) { 737 $field = (object)$fieldobject->get_field_config_for_external(); 738 if ($fieldobject->get_category_name() !== null && $fieldobject->is_signup_field() && $field->visible <> 0) { 739 $profilefields[] = (object) array( 740 'categoryid' => $field->categoryid, 741 'categoryname' => $fieldobject->get_category_name(), 742 'fieldid' => $field->id, 743 'datatype' => $field->datatype, 744 'object' => $fieldobject 745 ); 746 } 747 } 748 return $profilefields; 749 } 750 751 /** 752 * Adds code snippet to a moodle form object for custom profile fields that 753 * should appear on the signup page 754 * @param MoodleQuickForm $mform moodle form object 755 */ 756 function profile_signup_fields(MoodleQuickForm $mform): void { 757 758 if ($fields = profile_get_signup_fields()) { 759 foreach ($fields as $field) { 760 // Check if we change the categories. 761 if (!isset($currentcat) || $currentcat != $field->categoryid) { 762 $currentcat = $field->categoryid; 763 $mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname)); 764 }; 765 $field->object->edit_field($mform); 766 } 767 } 768 } 769 770 /** 771 * Returns an object with the custom profile fields set for the given user 772 * @param int $userid 773 * @param bool $onlyinuserobject True if you only want the ones in $USER. 774 * @return stdClass object where properties names are shortnames of custom profile fields 775 */ 776 function profile_user_record(int $userid, bool $onlyinuserobject = true): stdClass { 777 $usercustomfields = new stdClass(); 778 779 $fields = profile_get_user_fields_with_data($userid); 780 foreach ($fields as $formfield) { 781 if (!$onlyinuserobject || $formfield->is_user_object_data()) { 782 $usercustomfields->{$formfield->field->shortname} = $formfield->data; 783 } 784 } 785 786 return $usercustomfields; 787 } 788 789 /** 790 * Obtains a list of all available custom profile fields, indexed by id. 791 * 792 * Some profile fields are not included in the user object data (see 793 * profile_user_record function above). Optionally, you can obtain only those 794 * fields that are included in the user object. 795 * 796 * To be clear, this function returns the available fields, and does not 797 * return the field values for a particular user. 798 * 799 * @param bool $onlyinuserobject True if you only want the ones in $USER 800 * @return array Array of field objects from database (indexed by id) 801 * @since Moodle 2.7.1 802 */ 803 function profile_get_custom_fields(bool $onlyinuserobject = false): array { 804 $fieldobjects = profile_get_user_fields_with_data(0); 805 $fields = []; 806 foreach ($fieldobjects as $fieldobject) { 807 if (!$onlyinuserobject || $fieldobject->is_user_object_data()) { 808 $fields[$fieldobject->fieldid] = (object)$fieldobject->get_field_config_for_external(); 809 } 810 } 811 ksort($fields); 812 return $fields; 813 } 814 815 /** 816 * Load custom profile fields into user object 817 * 818 * @param stdClass $user user object 819 */ 820 function profile_load_custom_fields($user) { 821 $user->profile = (array)profile_user_record($user->id); 822 } 823 824 /** 825 * Save custom profile fields for a user. 826 * 827 * @param int $userid The user id 828 * @param array $profilefields The fields to save 829 */ 830 function profile_save_custom_fields($userid, $profilefields) { 831 global $DB; 832 833 $fields = profile_get_user_fields_with_data(0); 834 if ($fields) { 835 foreach ($fields as $fieldobject) { 836 $field = (object)$fieldobject->get_field_config_for_external(); 837 if (isset($profilefields[$field->shortname])) { 838 $conditions = array('fieldid' => $field->id, 'userid' => $userid); 839 $id = $DB->get_field('user_info_data', 'id', $conditions); 840 $data = $profilefields[$field->shortname]; 841 if ($id) { 842 $DB->set_field('user_info_data', 'data', $data, array('id' => $id)); 843 } else { 844 $record = array('fieldid' => $field->id, 'userid' => $userid, 'data' => $data); 845 $DB->insert_record('user_info_data', $record); 846 } 847 } 848 } 849 } 850 } 851 852 /** 853 * Gets basic data about custom profile fields. This is minimal data that is cached within the 854 * current request for all fields so that it can be used quickly. 855 * 856 * @param string $shortname Shortname of custom profile field 857 * @param bool $casesensitive Whether to perform case-sensitive matching of shortname. Note current limitations of custom profile 858 * fields allow the same shortname to exist differing only by it's case 859 * @return stdClass|null Object with properties id, shortname, name, visible, datatype, categoryid, etc 860 */ 861 function profile_get_custom_field_data_by_shortname(string $shortname, bool $casesensitive = true): ?stdClass { 862 $cache = \cache::make_from_params(cache_store::MODE_REQUEST, 'core_profile', 'customfields', 863 [], ['simplekeys' => true, 'simpledata' => true]); 864 $data = $cache->get($shortname); 865 if ($data === false) { 866 // If we don't have data, we get and cache it for all fields to avoid multiple DB requests. 867 $fields = profile_get_custom_fields(); 868 $data = null; 869 foreach ($fields as $field) { 870 $cache->set($field->shortname, $field); 871 872 // Perform comparison according to case sensitivity parameter. 873 $shortnamematch = $casesensitive 874 ? strcmp($field->shortname, $shortname) === 0 875 : strcasecmp($field->shortname, $shortname) === 0; 876 877 if ($shortnamematch) { 878 $data = $field; 879 } 880 } 881 } 882 883 return $data; 884 } 885 886 /** 887 * Trigger a user profile viewed event. 888 * 889 * @param stdClass $user user object 890 * @param stdClass $context context object (course or user) 891 * @param stdClass $course course object 892 * @since Moodle 2.9 893 */ 894 function profile_view($user, $context, $course = null) { 895 896 $eventdata = array( 897 'objectid' => $user->id, 898 'relateduserid' => $user->id, 899 'context' => $context 900 ); 901 902 if (!empty($course)) { 903 $eventdata['courseid'] = $course->id; 904 $eventdata['other'] = array( 905 'courseid' => $course->id, 906 'courseshortname' => $course->shortname, 907 'coursefullname' => $course->fullname 908 ); 909 } 910 911 $event = \core\event\user_profile_viewed::create($eventdata); 912 $event->add_record_snapshot('user', $user); 913 $event->trigger(); 914 } 915 916 /** 917 * Does the user have all required custom fields set? 918 * 919 * Internal, to be exclusively used by {@link user_not_fully_set_up()} only. 920 * 921 * Note that if users have no way to fill a required field via editing their 922 * profiles (e.g. the field is not visible or it is locked), we still return true. 923 * So this is actually checking if we should redirect the user to edit their 924 * profile, rather than whether there is a value in the database. 925 * 926 * @param int $userid 927 * @return bool 928 */ 929 function profile_has_required_custom_fields_set($userid) { 930 $profilefields = profile_get_user_fields_with_data($userid); 931 foreach ($profilefields as $profilefield) { 932 if ($profilefield->is_required() && !$profilefield->is_locked() && 933 $profilefield->is_empty() && $profilefield->get_field_config_for_external()['visible']) { 934 return false; 935 } 936 } 937 938 return true; 939 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body