MDL-29320 user: Make email query case-insensitive
[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;
c86500a4 48 $usernotfullysetup = user_not_fully_set_up($USER);
fc3aa0fb 49
d6e7a63d
PS
50 if (!is_array($this->_customdata)) {
51 throw new coding_exception('invalid custom data for user_edit_form');
4e782b32 52 }
d6e7a63d
PS
53 $editoroptions = $this->_customdata['editoroptions'];
54 $filemanageroptions = $this->_customdata['filemanageroptions'];
55 $user = $this->_customdata['user'];
56 $userid = $user->id;
57
58 if (empty($user->country)) {
59 // We must unset the value here so $CFG->country can be used as default one.
60 unset($user->country);
61 }
62
a2ed6e69 63 // Accessibility: "Required" is bad legend text.
c8149f0f 64 $strgeneral = get_string('general');
c9c74c47 65 $strrequired = get_string('required');
66
a2ed6e69 67 // Add some extra hidden fields.
3c19571f 68 $mform->addElement('hidden', 'id');
d18e0fe6 69 $mform->setType('id', PARAM_INT);
d8734783 70 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 71 $mform->setType('course', PARAM_INT);
acc9c3e0 72
a2ed6e69 73 // Print the required moodle fields first.
c8149f0f 74 $mform->addElement('header', 'moodle', $strgeneral);
c9c74c47 75
a2ed6e69 76 // Shared fields.
d6e7a63d 77 useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
c9c74c47 78
a2ed6e69 79 // Extra settigs.
c86500a4 80 if (!empty($CFG->disableuserimages) || $usernotfullysetup) {
d8734783 81 $mform->removeElement('deletepicture');
82 $mform->removeElement('imagefile');
83 $mform->removeElement('imagealt');
c9c74c47 84 }
acc9c3e0 85
c86500a4
CB
86 // If the user isn't fully set up, let them know that they will be able to change
87 // their profile picture once their profile is complete.
88 if ($usernotfullysetup) {
89 $userpicturewarning = $mform->createElement('warning', 'userpicturewarning', 'notifymessage', get_string('newpictureusernotsetup'));
90 $enabledusernamefields = useredit_get_enabled_name_fields();
91 if ($mform->elementExists('moodle_additional_names')) {
92 $mform->insertElementBefore($userpicturewarning, 'moodle_additional_names');
93 } else if ($mform->elementExists('moodle_interests')) {
94 $mform->insertElementBefore($userpicturewarning, 'moodle_interests');
95 } else {
96 $mform->insertElementBefore($userpicturewarning, 'moodle_optional');
97 }
98
99 // This is expected to exist when the form is submitted.
100 $imagefile = $mform->createElement('hidden', 'imagefile');
101 $mform->insertElementBefore($imagefile, 'userpicturewarning');
102 }
103
a2ed6e69 104 // Next the customisable profile fields.
fc3aa0fb 105 profile_definition($mform, $userid);
bb6d3d34 106
7ce538be 107 $this->add_action_buttons(true, get_string('updatemyprofile'));
d6e7a63d
PS
108
109 $this->set_data($user);
d8734783 110 }
c9c74c47 111
a2ed6e69
SH
112 /**
113 * Extend the form definition after the data has been parsed.
114 */
115 public function definition_after_data() {
f2f085ee 116 global $CFG, $DB, $OUTPUT;
c9c74c47 117
a2ed6e69 118 $mform = $this->_form;
d8734783 119 $userid = $mform->getElementValue('id');
acc9c3e0 120
e9c27d1e
DM
121 // Trim required name fields.
122 foreach (useredit_get_required_name_fields() as $field) {
123 $mform->applyFilter($field, 'trim');
124 }
125
a2ed6e69 126 if ($user = $DB->get_record('user', array('id' => $userid))) {
35f0660a 127
a2ed6e69
SH
128 // Remove description.
129 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
8bdc9cac 130 $mform->removeElement('description_editor');
35f0660a 131 }
132
a2ed6e69 133 // Print picture.
689096bc
PS
134 $context = context_user::instance($user->id, MUST_EXIST);
135 $fs = get_file_storage();
136 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
137 if (!empty($user->picture) && $hasuploadedpicture) {
a2ed6e69 138 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
689096bc
PS
139 } else {
140 $imagevalue = get_string('none');
141 }
142 $imageelement = $mform->getElement('currentpicture');
143 $imageelement->setValue($imagevalue);
4125bdc1 144
689096bc
PS
145 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
146 $mform->removeElement('deletepicture');
c9c74c47 147 }
acc9c3e0 148
a2ed6e69 149 // Disable fields that are locked by auth plugins.
d8734783 150 $fields = get_user_fieldnames();
d8734783 151 $authplugin = get_auth_plugin($user->auth);
b7189068 152 $customfields = $authplugin->get_custom_user_profile_fields();
da1296dd 153 $customfieldsdata = profile_user_record($userid, false);
b7189068 154 $fields = array_merge($fields, $customfields);
d8734783 155 foreach ($fields as $field) {
5bc88625
AA
156 if ($field === 'description') {
157 // Hard coded hack for description field. See MDL-37704 for details.
158 $formfield = 'description_editor';
159 } else {
160 $formfield = $field;
161 }
162 if (!$mform->elementExists($formfield)) {
d8734783 163 continue;
164 }
da1296dd
FM
165
166 // Get the original value for the field.
167 if (in_array($field, $customfields)) {
168 $key = str_replace('profile_field_', '', $field);
169 $value = isset($customfieldsdata->{$key}) ? $customfieldsdata->{$key} : '';
170 } else {
171 $value = $user->{$field};
172 }
173
d8734783 174 $configvariable = 'field_lock_' . $field;
175 if (isset($authplugin->config->{$configvariable})) {
176 if ($authplugin->config->{$configvariable} === 'locked') {
5bc88625 177 $mform->hardFreeze($formfield);
b7189068
KW
178 $mform->setConstant($formfield, $value);
179 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $value != '') {
5bc88625 180 $mform->hardFreeze($formfield);
b7189068 181 $mform->setConstant($formfield, $value);
d8734783 182 }
183 }
68e9d368 184 }
f2f085ee 185
a2ed6e69 186 // Next the customisable profile fields.
63466095 187 profile_definition_after_data($mform, $user->id);
c9c74c47 188
63466095 189 } else {
190 profile_definition_after_data($mform, 0);
191 }
c9c74c47 192 }
193
a2ed6e69
SH
194 /**
195 * Validate incoming form data.
196 * @param array $usernew
197 * @param array $files
198 * @return array
199 */
200 public function validation($usernew, $files) {
5d910388 201 global $CFG, $DB;
c9c74c47 202
a78890d5 203 $errors = parent::validation($usernew, $files);
204
acc9c3e0 205 $usernew = (object)$usernew;
a2ed6e69 206 $user = $DB->get_record('user', array('id' => $usernew->id));
acc9c3e0 207
a2ed6e69 208 // Validate email.
d0b2acde 209 if (!isset($usernew->email)) {
a2ed6e69 210 // Mail not confirmed yet.
d0b2acde 211 } else if (!validate_email($usernew->email)) {
a78890d5 212 $errors['email'] = get_string('invalidemail');
1b3a6e77
JP
213 } else if (($usernew->email !== $user->email) && empty($CFG->allowaccountssameemail)) {
214 // Make a case-insensitive query for the given email address.
215 $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
216 $params = array(
217 'email' => $usernew->email,
218 'mnethostid' => $CFG->mnet_localhost_id,
219 'userid' => $usernew->id
220 );
221 // If there are other user(s) that already have the same email, show an error.
222 if ($DB->record_exists_select('user', $select, $params)) {
223 $errors['email'] = get_string('emailexists');
224 }
c9c74c47 225 }
14a6b7e1 226
d0b2acde 227 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
a78890d5 228 $errors['email'] = get_string('toomanybounces');
c9c74c47 229 }
230
43731030 231 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
bff34a67 232 $errorstr = email_is_not_allowed($usernew->email);
233 if ($errorstr !== false) {
234 $errors['email'] = $errorstr;
235 }
236 }
237
a2ed6e69 238 // Next the customisable profile fields.
a78890d5 239 $errors += profile_validation($usernew, $files);
bb6d3d34 240
a78890d5 241 return $errors;
c9c74c47 242 }
243}
244
aa6c1ced 245