Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / user / edit_form.php
blob0c0e2a6e03fa48bc3a0679b7d321c22b62032c0d
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 foreach ($fields as $field) {
129 if ($field === 'description') {
130 // Hard coded hack for description field. See MDL-37704 for details.
131 $formfield = 'description_editor';
132 } else {
133 $formfield = $field;
135 if (!$mform->elementExists($formfield)) {
136 continue;
138 $configvariable = 'field_lock_' . $field;
139 if (isset($authplugin->config->{$configvariable})) {
140 if ($authplugin->config->{$configvariable} === 'locked') {
141 $mform->hardFreeze($formfield);
142 $mform->setConstant($formfield, $user->$field);
143 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
144 $mform->hardFreeze($formfield);
145 $mform->setConstant($formfield, $user->$field);
150 // Next the customisable profile fields.
151 profile_definition_after_data($mform, $user->id);
153 } else {
154 profile_definition_after_data($mform, 0);
159 * Validate incoming form data.
160 * @param array $usernew
161 * @param array $files
162 * @return array
164 public function validation($usernew, $files) {
165 global $CFG, $DB;
167 $errors = parent::validation($usernew, $files);
169 $usernew = (object)$usernew;
170 $user = $DB->get_record('user', array('id' => $usernew->id));
172 // Validate email.
173 if (!isset($usernew->email)) {
174 // Mail not confirmed yet.
175 } else if (!validate_email($usernew->email)) {
176 $errors['email'] = get_string('invalidemail');
177 } else if (($usernew->email !== $user->email)
178 and empty($CFG->allowaccountssameemail)
179 and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
180 $errors['email'] = get_string('emailexists');
183 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
184 $errors['email'] = get_string('toomanybounces');
187 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
188 $errorstr = email_is_not_allowed($usernew->email);
189 if ($errorstr !== false) {
190 $errors['email'] = $errorstr;
194 // Next the customisable profile fields.
195 $errors += profile_validation($usernew, $files);
197 return $errors;