[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/user/ -> edit_form.php (source)

   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 to edit 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  
  31  /**
  32   * Class user_edit_form.
  33   *
  34   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  35   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class user_edit_form extends moodleform {
  38  
  39      /**
  40       * Define the form.
  41       */
  42      public function definition () {
  43          global $CFG, $COURSE, $USER;
  44  
  45          $mform = $this->_form;
  46          $editoroptions = null;
  47          $filemanageroptions = null;
  48  
  49          if (!is_array($this->_customdata)) {
  50              throw new coding_exception('invalid custom data for user_edit_form');
  51          }
  52          $editoroptions = $this->_customdata['editoroptions'];
  53          $filemanageroptions = $this->_customdata['filemanageroptions'];
  54          $user = $this->_customdata['user'];
  55          $userid = $user->id;
  56  
  57          if (empty($user->country)) {
  58              // We must unset the value here so $CFG->country can be used as default one.
  59              unset($user->country);
  60          }
  61  
  62          // Accessibility: "Required" is bad legend text.
  63          $strgeneral  = get_string('general');
  64          $strrequired = get_string('required');
  65  
  66          // Add some extra hidden fields.
  67          $mform->addElement('hidden', 'id');
  68          $mform->setType('id', PARAM_INT);
  69          $mform->addElement('hidden', 'course', $COURSE->id);
  70          $mform->setType('course', PARAM_INT);
  71  
  72          // Print the required moodle fields first.
  73          $mform->addElement('header', 'moodle', $strgeneral);
  74  
  75          // Shared fields.
  76          useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
  77  
  78          // Extra settigs.
  79          if (!empty($CFG->disableuserimages)) {
  80              $mform->removeElement('deletepicture');
  81              $mform->removeElement('imagefile');
  82              $mform->removeElement('imagealt');
  83          }
  84  
  85          // Next the customisable profile fields.
  86          profile_definition($mform, $userid);
  87  
  88          $this->add_action_buttons(false, get_string('updatemyprofile'));
  89  
  90          $this->set_data($user);
  91      }
  92  
  93      /**
  94       * Extend the form definition after the data has been parsed.
  95       */
  96      public function definition_after_data() {
  97          global $CFG, $DB, $OUTPUT;
  98  
  99          $mform = $this->_form;
 100          $userid = $mform->getElementValue('id');
 101  
 102          // Trim required name fields.
 103          foreach (useredit_get_required_name_fields() as $field) {
 104              $mform->applyFilter($field, 'trim');
 105          }
 106  
 107          if ($user = $DB->get_record('user', array('id' => $userid))) {
 108  
 109              // Remove description.
 110              if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
 111                  $mform->removeElement('description_editor');
 112              }
 113  
 114              // Print picture.
 115              $context = context_user::instance($user->id, MUST_EXIST);
 116              $fs = get_file_storage();
 117              $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
 118              if (!empty($user->picture) && $hasuploadedpicture) {
 119                  $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
 120              } else {
 121                  $imagevalue = get_string('none');
 122              }
 123              $imageelement = $mform->getElement('currentpicture');
 124              $imageelement->setValue($imagevalue);
 125  
 126              if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
 127                  $mform->removeElement('deletepicture');
 128              }
 129  
 130              // Disable fields that are locked by auth plugins.
 131              $fields = get_user_fieldnames();
 132              $authplugin = get_auth_plugin($user->auth);
 133              $customfields = $authplugin->get_custom_user_profile_fields();
 134              $customfieldsdata = profile_user_record($userid, false);
 135              $fields = array_merge($fields, $customfields);
 136              foreach ($fields as $field) {
 137                  if ($field === 'description') {
 138                      // Hard coded hack for description field. See MDL-37704 for details.
 139                      $formfield = 'description_editor';
 140                  } else {
 141                      $formfield = $field;
 142                  }
 143                  if (!$mform->elementExists($formfield)) {
 144                      continue;
 145                  }
 146  
 147                  // Get the original value for the field.
 148                  if (in_array($field, $customfields)) {
 149                      $key = str_replace('profile_field_', '', $field);
 150                      $value = isset($customfieldsdata->{$key}) ? $customfieldsdata->{$key} : '';
 151                  } else {
 152                      $value = $user->{$field};
 153                  }
 154  
 155                  $configvariable = 'field_lock_' . $field;
 156                  if (isset($authplugin->config->{$configvariable})) {
 157                      if ($authplugin->config->{$configvariable} === 'locked') {
 158                          $mform->hardFreeze($formfield);
 159                          $mform->setConstant($formfield, $value);
 160                      } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $value != '') {
 161                          $mform->hardFreeze($formfield);
 162                          $mform->setConstant($formfield, $value);
 163                      }
 164                  }
 165              }
 166  
 167              // Next the customisable profile fields.
 168              profile_definition_after_data($mform, $user->id);
 169  
 170          } else {
 171              profile_definition_after_data($mform, 0);
 172          }
 173      }
 174  
 175      /**
 176       * Validate incoming form data.
 177       * @param array $usernew
 178       * @param array $files
 179       * @return array
 180       */
 181      public function validation($usernew, $files) {
 182          global $CFG, $DB;
 183  
 184          $errors = parent::validation($usernew, $files);
 185  
 186          $usernew = (object)$usernew;
 187          $user    = $DB->get_record('user', array('id' => $usernew->id));
 188  
 189          // Validate email.
 190          if (!isset($usernew->email)) {
 191              // Mail not confirmed yet.
 192          } else if (!validate_email($usernew->email)) {
 193              $errors['email'] = get_string('invalidemail');
 194          } else if (($usernew->email !== $user->email)
 195                  and empty($CFG->allowaccountssameemail)
 196                  and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
 197              $errors['email'] = get_string('emailexists');
 198          }
 199  
 200          if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
 201              $errors['email'] = get_string('toomanybounces');
 202          }
 203  
 204          if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
 205              $errorstr = email_is_not_allowed($usernew->email);
 206              if ($errorstr !== false) {
 207                  $errors['email'] = $errorstr;
 208              }
 209          }
 210  
 211          // Next the customisable profile fields.
 212          $errors += profile_validation($usernew, $files);
 213  
 214          return $errors;
 215      }
 216  }
 217  
 218  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1