Merge branch 'MDL-52835-m29' of git://github.com/deraadt/moodle into MOODLE_29_STABLE
[moodle.git] / user / edit_form.php
blob3b8d7f4348072711eaf7311dbf4cddcc6c8e9368
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.
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/>.
17 /**
18 * Form to edit a users profile
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
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
29 require_once($CFG->dirroot.'/lib/formslib.php');
31 /**
32 * Class user_edit_form.
34 * @copyright 1999 Martin Dougiamas http://dougiamas.com
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class user_edit_form extends moodleform {
39 /**
40 * Define the form.
42 public function definition () {
43 global $CFG, $COURSE, $USER;
45 $mform = $this->_form;
46 $editoroptions = null;
47 $filemanageroptions = null;
49 if (!is_array($this->_customdata)) {
50 throw new coding_exception('invalid custom data for user_edit_form');
52 $editoroptions = $this->_customdata['editoroptions'];
53 $filemanageroptions = $this->_customdata['filemanageroptions'];
54 $user = $this->_customdata['user'];
55 $userid = $user->id;
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);
62 // Accessibility: "Required" is bad legend text.
63 $strgeneral = get_string('general');
64 $strrequired = get_string('required');
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);
72 // Print the required moodle fields first.
73 $mform->addElement('header', 'moodle', $strgeneral);
75 // Shared fields.
76 useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
78 // Extra settigs.
79 if (!empty($CFG->disableuserimages)) {
80 $mform->removeElement('deletepicture');
81 $mform->removeElement('imagefile');
82 $mform->removeElement('imagealt');
85 // Next the customisable profile fields.
86 profile_definition($mform, $userid);
88 $this->add_action_buttons(false, get_string('updatemyprofile'));
90 $this->set_data($user);
93 /**
94 * Extend the form definition after the data has been parsed.
96 public function definition_after_data() {
97 global $CFG, $DB, $OUTPUT;
99 $mform = $this->_form;
100 $userid = $mform->getElementValue('id');
102 if ($user = $DB->get_record('user', array('id' => $userid))) {
104 // Remove description.
105 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
106 $mform->removeElement('description_editor');
109 // Print picture.
110 $context = context_user::instance($user->id, MUST_EXIST);
111 $fs = get_file_storage();
112 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
113 if (!empty($user->picture) && $hasuploadedpicture) {
114 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
115 } else {
116 $imagevalue = get_string('none');
118 $imageelement = $mform->getElement('currentpicture');
119 $imageelement->setValue($imagevalue);
121 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
122 $mform->removeElement('deletepicture');
125 // Disable fields that are locked by auth plugins.
126 $fields = get_user_fieldnames();
127 $authplugin = get_auth_plugin($user->auth);
128 $customfields = $authplugin->get_custom_user_profile_fields();
129 $fields = array_merge($fields, $customfields);
130 foreach ($fields as $field) {
131 if ($field === 'description') {
132 // Hard coded hack for description field. See MDL-37704 for details.
133 $formfield = 'description_editor';
134 } else {
135 $formfield = $field;
137 if (!$mform->elementExists($formfield)) {
138 continue;
140 $value = $mform->getElement($formfield)->exportValue($mform->getElementValue($formfield)) ?: '';
141 $configvariable = 'field_lock_' . $field;
142 if (isset($authplugin->config->{$configvariable})) {
143 if ($authplugin->config->{$configvariable} === 'locked') {
144 $mform->hardFreeze($formfield);
145 $mform->setConstant($formfield, $value);
146 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $value != '') {
147 $mform->hardFreeze($formfield);
148 $mform->setConstant($formfield, $value);
153 // Next the customisable profile fields.
154 profile_definition_after_data($mform, $user->id);
156 } else {
157 profile_definition_after_data($mform, 0);
162 * Validate incoming form data.
163 * @param array $usernew
164 * @param array $files
165 * @return array
167 public function validation($usernew, $files) {
168 global $CFG, $DB;
170 $errors = parent::validation($usernew, $files);
172 $usernew = (object)$usernew;
173 $user = $DB->get_record('user', array('id' => $usernew->id));
175 // Validate email.
176 if (!isset($usernew->email)) {
177 // Mail not confirmed yet.
178 } else if (!validate_email($usernew->email)) {
179 $errors['email'] = get_string('invalidemail');
180 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
181 $errors['email'] = get_string('emailexists');
184 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
185 $errors['email'] = get_string('toomanybounces');
188 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
189 $errorstr = email_is_not_allowed($usernew->email);
190 if ($errorstr !== false) {
191 $errors['email'] = $errorstr;
195 // Next the customisable profile fields.
196 $errors += profile_validation($usernew, $files);
198 return $errors;