Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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   * Form for editing a users profile
  19   *
  20   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core_user
  23   */
  24  
  25  if (!defined('MOODLE_INTERNAL')) {
  26      die('Direct access to this script is forbidden.');    //  It must be included from a Moodle page.
  27  }
  28  
  29  require_once($CFG->dirroot.'/lib/formslib.php');
  30  require_once($CFG->dirroot.'/user/lib.php');
  31  
  32  /**
  33   * Class user_editadvanced_form.
  34   *
  35   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  36   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class user_editadvanced_form extends moodleform {
  39  
  40      /**
  41       * Define the form.
  42       */
  43      public function definition() {
  44          global $USER, $CFG, $COURSE;
  45  
  46          $mform = $this->_form;
  47          $editoroptions = null;
  48          $filemanageroptions = null;
  49  
  50          if (!is_array($this->_customdata)) {
  51              throw new coding_exception('invalid custom data for user_edit_form');
  52          }
  53          $editoroptions = $this->_customdata['editoroptions'];
  54          $filemanageroptions = $this->_customdata['filemanageroptions'];
  55          $user = $this->_customdata['user'];
  56          $userid = $user->id;
  57  
  58          // Accessibility: "Required" is bad legend text.
  59          $strgeneral  = get_string('general');
  60          $strrequired = get_string('required');
  61  
  62          // Add some extra hidden fields.
  63          $mform->addElement('hidden', 'id');
  64          $mform->setType('id', core_user::get_property_type('id'));
  65          $mform->addElement('hidden', 'course', $COURSE->id);
  66          $mform->setType('course', PARAM_INT);
  67  
  68          // Print the required moodle fields first.
  69          $mform->addElement('header', 'moodle', $strgeneral);
  70  
  71          $auths = core_component::get_plugin_list('auth');
  72          $enabled = get_string('pluginenabled', 'core_plugin');
  73          $disabled = get_string('plugindisabled', 'core_plugin');
  74          $authoptions = array($enabled => array(), $disabled => array());
  75          $cannotchangepass = array();
  76          $cannotchangeusername = array();
  77          foreach ($auths as $auth => $unused) {
  78              $authinst = get_auth_plugin($auth);
  79  
  80              if (!$authinst->is_internal()) {
  81                  $cannotchangeusername[] = $auth;
  82              }
  83  
  84              $passwordurl = $authinst->change_password_url();
  85              if (!($authinst->can_change_password() && empty($passwordurl))) {
  86                  if ($userid < 1 and $authinst->is_internal()) {
  87                      // This is unlikely but we can not create account without password
  88                      // when plugin uses passwords, we need to set it initially at least.
  89                  } else {
  90                      $cannotchangepass[] = $auth;
  91                  }
  92              }
  93              if (is_enabled_auth($auth)) {
  94                  $authoptions[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
  95              } else {
  96                  $authoptions[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
  97              }
  98          }
  99  
 100          $purpose = user_edit_map_field_purpose($userid, 'username');
 101          $mform->addElement('text', 'username', get_string('username'), 'size="20"' . $purpose);
 102          $mform->addHelpButton('username', 'username', 'auth');
 103          $mform->setType('username', PARAM_RAW);
 104  
 105          if ($userid !== -1) {
 106              $mform->disabledIf('username', 'auth', 'in', $cannotchangeusername);
 107          }
 108  
 109          $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod', 'auth'), $authoptions);
 110          $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
 111  
 112          $mform->addElement('advcheckbox', 'suspended', get_string('suspended', 'auth'));
 113          $mform->addHelpButton('suspended', 'suspended', 'auth');
 114  
 115          $mform->addElement('checkbox', 'createpassword', get_string('createpassword', 'auth'));
 116          $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
 117  
 118          if (!empty($CFG->passwordpolicy)) {
 119              $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
 120          }
 121  
 122          $purpose = user_edit_map_field_purpose($userid, 'password');
 123          $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'),
 124              'maxlength="'.MAX_PASSWORD_CHARACTERS.'" size="20"' . $purpose);
 125          $mform->addRule('newpassword', get_string('maximumchars', '', MAX_PASSWORD_CHARACTERS),
 126              'maxlength', MAX_PASSWORD_CHARACTERS, 'client');
 127          $mform->addHelpButton('newpassword', 'newpassword');
 128          $mform->setType('newpassword', core_user::get_property_type('password'));
 129          $mform->disabledIf('newpassword', 'createpassword', 'checked');
 130  
 131          $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
 132  
 133          // Check if the user has active external tokens.
 134          if ($userid and empty($CFG->passwordchangetokendeletion)) {
 135              if ($tokens = webservice::get_active_tokens($userid)) {
 136                  $services = '';
 137                  foreach ($tokens as $token) {
 138                      $services .= format_string($token->servicename) . ',';
 139                  }
 140                  $services = get_string('userservices', 'webservice', rtrim($services, ','));
 141                  $mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'), $services);
 142                  $mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
 143                  $mform->disabledIf('signoutofotherservices', 'newpassword', 'eq', '');
 144                  $mform->setDefault('signoutofotherservices', 1);
 145              }
 146          }
 147  
 148          $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
 149          $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
 150          $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
 151  
 152          // Shared fields.
 153          useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
 154  
 155          // Next the customisable profile fields.
 156          profile_definition($mform, $userid);
 157  
 158          if ($userid == -1) {
 159              $btnstring = get_string('createuser');
 160          } else {
 161              $btnstring = get_string('updatemyprofile');
 162          }
 163  
 164          $this->add_action_buttons(true, $btnstring);
 165  
 166          $this->set_data($user);
 167      }
 168  
 169      /**
 170       * Extend the form definition after data has been parsed.
 171       */
 172      public function definition_after_data() {
 173          global $USER, $CFG, $DB, $OUTPUT;
 174  
 175          $mform = $this->_form;
 176  
 177          // Trim required name fields.
 178          foreach (useredit_get_required_name_fields() as $field) {
 179              $mform->applyFilter($field, 'trim');
 180          }
 181  
 182          if ($userid = $mform->getElementValue('id')) {
 183              $user = $DB->get_record('user', array('id' => $userid));
 184          } else {
 185              $user = false;
 186          }
 187  
 188          // User can not change own auth method.
 189          if ($userid == $USER->id) {
 190              $mform->hardFreeze('auth');
 191              $mform->hardFreeze('preference_auth_forcepasswordchange');
 192          }
 193  
 194          // Admin must choose some password and supply correct email.
 195          if (!empty($USER->newadminuser)) {
 196              $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
 197              if ($mform->elementExists('suspended')) {
 198                  $mform->removeElement('suspended');
 199              }
 200          }
 201  
 202          // Require password for new users.
 203          if ($userid > 0) {
 204              if ($mform->elementExists('createpassword')) {
 205                  $mform->removeElement('createpassword');
 206              }
 207          }
 208  
 209          if ($user and is_mnet_remote_user($user)) {
 210              // Only local accounts can be suspended.
 211              if ($mform->elementExists('suspended')) {
 212                  $mform->removeElement('suspended');
 213              }
 214          }
 215          if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
 216              // Prevent self and admin mess ups.
 217              if ($mform->elementExists('suspended')) {
 218                  $mform->hardFreeze('suspended');
 219              }
 220          }
 221  
 222          // Print picture.
 223          if (empty($USER->newadminuser)) {
 224              if ($user) {
 225                  $context = context_user::instance($user->id, MUST_EXIST);
 226                  $fs = get_file_storage();
 227                  $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
 228                  if (!empty($user->picture) && $hasuploadedpicture) {
 229                      $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
 230                  } else {
 231                      $imagevalue = get_string('none');
 232                  }
 233              } else {
 234                  $imagevalue = get_string('none');
 235              }
 236              $imageelement = $mform->getElement('currentpicture');
 237              $imageelement->setValue($imagevalue);
 238  
 239              if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
 240                  $mform->removeElement('deletepicture');
 241              }
 242          }
 243  
 244          // Next the customisable profile fields.
 245          profile_definition_after_data($mform, $userid);
 246      }
 247  
 248      /**
 249       * Validate the form data.
 250       * @param array $usernew
 251       * @param array $files
 252       * @return array|bool
 253       */
 254      public function validation($usernew, $files) {
 255          global $CFG, $DB;
 256  
 257          $usernew = (object)$usernew;
 258          $usernew->username = trim($usernew->username);
 259  
 260          $user = $DB->get_record('user', array('id' => $usernew->id));
 261          $err = array();
 262  
 263          if (!$user and !empty($usernew->createpassword)) {
 264              if ($usernew->suspended) {
 265                  // Show some error because we can not mail suspended users.
 266                  $err['suspended'] = get_string('error');
 267              }
 268          } else {
 269              if (!empty($usernew->newpassword)) {
 270                  $errmsg = ''; // Prevent eclipse warning.
 271                  if (!check_password_policy($usernew->newpassword, $errmsg, $usernew)) {
 272                      $err['newpassword'] = $errmsg;
 273                  }
 274              } else if (!$user) {
 275                  $auth = get_auth_plugin($usernew->auth);
 276                  if ($auth->is_internal()) {
 277                      // Internal accounts require password!
 278                      $err['newpassword'] = get_string('required');
 279                  }
 280              }
 281          }
 282  
 283          if (empty($usernew->username)) {
 284              // Might be only whitespace.
 285              $err['username'] = get_string('required');
 286          } else if (!$user or $user->username !== $usernew->username) {
 287              // Check new username does not exist.
 288              if ($DB->record_exists('user', array('username' => $usernew->username, 'mnethostid' => $CFG->mnet_localhost_id))) {
 289                  $err['username'] = get_string('usernameexists');
 290              }
 291              // Check allowed characters.
 292              if ($usernew->username !== core_text::strtolower($usernew->username)) {
 293                  $err['username'] = get_string('usernamelowercase');
 294              } else {
 295                  if ($usernew->username !== core_user::clean_field($usernew->username, 'username')) {
 296                      $err['username'] = get_string('invalidusername');
 297                  }
 298              }
 299          }
 300  
 301          if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
 302              if (!validate_email($usernew->email)) {
 303                  $err['email'] = get_string('invalidemail');
 304              } else if (empty($CFG->allowaccountssameemail)) {
 305                  // Make a case-insensitive query for the given email address.
 306                  $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
 307                  $params = array(
 308                      'email' => $usernew->email,
 309                      'mnethostid' => $CFG->mnet_localhost_id,
 310                      'userid' => $usernew->id
 311                  );
 312                  // If there are other user(s) that already have the same email, show an error.
 313                  if ($DB->record_exists_select('user', $select, $params)) {
 314                      $err['email'] = get_string('emailexists');
 315                  }
 316              }
 317          }
 318  
 319          // Next the customisable profile fields.
 320          $err += profile_validation($usernew, $files);
 321  
 322          if (count($err) == 0) {
 323              return true;
 324          } else {
 325              return $err;
 326          }
 327      }
 328  }
 329  
 330