MDL-52831 users: Automatically strip whitespace from name fields
[moodle.git] / user / edit_form.php
CommitLineData
8bdc9cac 1<?php
a2ed6e69
SH
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 */
c9c74c47 24
bfebaf64 25if (!defined('MOODLE_INTERNAL')) {
a2ed6e69 26 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
bfebaf64
MD
27}
28
d8734783 29require_once($CFG->dirroot.'/lib/formslib.php');
c9c74c47 30
a2ed6e69
SH
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 */
c9c74c47 37class user_edit_form extends moodleform {
38
a2ed6e69
SH
39 /**
40 * Define the form.
41 */
42 public function definition () {
fc3aa0fb 43 global $CFG, $COURSE, $USER;
c9c74c47 44
a2ed6e69 45 $mform = $this->_form;
fc3aa0fb
RT
46 $editoroptions = null;
47 $filemanageroptions = null;
fc3aa0fb 48
d6e7a63d
PS
49 if (!is_array($this->_customdata)) {
50 throw new coding_exception('invalid custom data for user_edit_form');
4e782b32 51 }
d6e7a63d
PS
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
a2ed6e69 62 // Accessibility: "Required" is bad legend text.
c8149f0f 63 $strgeneral = get_string('general');
c9c74c47 64 $strrequired = get_string('required');
65
a2ed6e69 66 // Add some extra hidden fields.
3c19571f 67 $mform->addElement('hidden', 'id');
d18e0fe6 68 $mform->setType('id', PARAM_INT);
d8734783 69 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 70 $mform->setType('course', PARAM_INT);
acc9c3e0 71
a2ed6e69 72 // Print the required moodle fields first.
c8149f0f 73 $mform->addElement('header', 'moodle', $strgeneral);
c9c74c47 74
a2ed6e69 75 // Shared fields.
d6e7a63d 76 useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
c9c74c47 77
a2ed6e69 78 // Extra settigs.
689096bc 79 if (!empty($CFG->disableuserimages)) {
d8734783 80 $mform->removeElement('deletepicture');
81 $mform->removeElement('imagefile');
82 $mform->removeElement('imagealt');
c9c74c47 83 }
acc9c3e0 84
a2ed6e69 85 // Next the customisable profile fields.
fc3aa0fb 86 profile_definition($mform, $userid);
bb6d3d34 87
d8734783 88 $this->add_action_buttons(false, get_string('updatemyprofile'));
d6e7a63d
PS
89
90 $this->set_data($user);
d8734783 91 }
c9c74c47 92
a2ed6e69
SH
93 /**
94 * Extend the form definition after the data has been parsed.
95 */
96 public function definition_after_data() {
f2f085ee 97 global $CFG, $DB, $OUTPUT;
c9c74c47 98
a2ed6e69 99 $mform = $this->_form;
d8734783 100 $userid = $mform->getElementValue('id');
acc9c3e0 101
e9c27d1e
DM
102 // Trim required name fields.
103 foreach (useredit_get_required_name_fields() as $field) {
104 $mform->applyFilter($field, 'trim');
105 }
106
a2ed6e69 107 if ($user = $DB->get_record('user', array('id' => $userid))) {
35f0660a 108
a2ed6e69
SH
109 // Remove description.
110 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
8bdc9cac 111 $mform->removeElement('description_editor');
35f0660a 112 }
113
a2ed6e69 114 // Print picture.
689096bc
PS
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) {
a2ed6e69 119 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
689096bc
PS
120 } else {
121 $imagevalue = get_string('none');
122 }
123 $imageelement = $mform->getElement('currentpicture');
124 $imageelement->setValue($imagevalue);
4125bdc1 125
689096bc
PS
126 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
127 $mform->removeElement('deletepicture');
c9c74c47 128 }
acc9c3e0 129
a2ed6e69 130 // Disable fields that are locked by auth plugins.
d8734783 131 $fields = get_user_fieldnames();
d8734783 132 $authplugin = get_auth_plugin($user->auth);
b7189068
KW
133 $customfields = $authplugin->get_custom_user_profile_fields();
134 $fields = array_merge($fields, $customfields);
d8734783 135 foreach ($fields as $field) {
5bc88625
AA
136 if ($field === 'description') {
137 // Hard coded hack for description field. See MDL-37704 for details.
138 $formfield = 'description_editor';
139 } else {
140 $formfield = $field;
141 }
142 if (!$mform->elementExists($formfield)) {
d8734783 143 continue;
144 }
956caef6 145 $value = $mform->getElement($formfield)->exportValue($mform->getElementValue($formfield)) ?: '';
d8734783 146 $configvariable = 'field_lock_' . $field;
147 if (isset($authplugin->config->{$configvariable})) {
148 if ($authplugin->config->{$configvariable} === 'locked') {
5bc88625 149 $mform->hardFreeze($formfield);
b7189068
KW
150 $mform->setConstant($formfield, $value);
151 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $value != '') {
5bc88625 152 $mform->hardFreeze($formfield);
b7189068 153 $mform->setConstant($formfield, $value);
d8734783 154 }
155 }
68e9d368 156 }
f2f085ee 157
a2ed6e69 158 // Next the customisable profile fields.
63466095 159 profile_definition_after_data($mform, $user->id);
c9c74c47 160
63466095 161 } else {
162 profile_definition_after_data($mform, 0);
163 }
c9c74c47 164 }
165
a2ed6e69
SH
166 /**
167 * Validate incoming form data.
168 * @param array $usernew
169 * @param array $files
170 * @return array
171 */
172 public function validation($usernew, $files) {
5d910388 173 global $CFG, $DB;
c9c74c47 174
a78890d5 175 $errors = parent::validation($usernew, $files);
176
acc9c3e0 177 $usernew = (object)$usernew;
a2ed6e69 178 $user = $DB->get_record('user', array('id' => $usernew->id));
acc9c3e0 179
a2ed6e69 180 // Validate email.
d0b2acde 181 if (!isset($usernew->email)) {
a2ed6e69 182 // Mail not confirmed yet.
d0b2acde 183 } else if (!validate_email($usernew->email)) {
a78890d5 184 $errors['email'] = get_string('invalidemail');
bc983724 185 } else if (($usernew->email !== $user->email)
95020832 186 and empty($CFG->allowaccountssameemail)
bc983724 187 and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
a78890d5 188 $errors['email'] = get_string('emailexists');
c9c74c47 189 }
14a6b7e1 190
d0b2acde 191 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
a78890d5 192 $errors['email'] = get_string('toomanybounces');
c9c74c47 193 }
194
43731030 195 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
bff34a67 196 $errorstr = email_is_not_allowed($usernew->email);
197 if ($errorstr !== false) {
198 $errors['email'] = $errorstr;
199 }
200 }
201
a2ed6e69 202 // Next the customisable profile fields.
a78890d5 203 $errors += profile_validation($usernew, $files);
bb6d3d34 204
a78890d5 205 return $errors;
c9c74c47 206 }
207}
208
aa6c1ced 209