Merge branch 'MDL-49075-27' of https://github.com/lucisgit/moodle into MOODLE_27_STABLE
[moodle.git] / user / edit_form.php
blob5456c38a28095e114a723c8461af9b4838e34a88
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;
48 $userid = $USER->id;
50 if (is_array($this->_customdata)) {
51 if (array_key_exists('editoroptions', $this->_customdata)) {
52 $editoroptions = $this->_customdata['editoroptions'];
54 if (array_key_exists('filemanageroptions', $this->_customdata)) {
55 $filemanageroptions = $this->_customdata['filemanageroptions'];
57 if (array_key_exists('userid', $this->_customdata)) {
58 $userid = $this->_customdata['userid'];
61 // Accessibility: "Required" is bad legend text.
62 $strgeneral = get_string('general');
63 $strrequired = get_string('required');
65 // Add some extra hidden fields.
66 $mform->addElement('hidden', 'id');
67 $mform->setType('id', PARAM_INT);
68 $mform->addElement('hidden', 'course', $COURSE->id);
69 $mform->setType('course', PARAM_INT);
71 // Print the required moodle fields first.
72 $mform->addElement('header', 'moodle', $strgeneral);
74 // Shared fields.
75 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
77 // Extra settigs.
78 if (!empty($CFG->disableuserimages)) {
79 $mform->removeElement('deletepicture');
80 $mform->removeElement('imagefile');
81 $mform->removeElement('imagealt');
84 // Next the customisable profile fields.
85 profile_definition($mform, $userid);
87 $this->add_action_buttons(false, get_string('updatemyprofile'));
90 /**
91 * Extend the form definition after the data has been parsed.
93 public function definition_after_data() {
94 global $CFG, $DB, $OUTPUT;
96 $mform = $this->_form;
97 $userid = $mform->getElementValue('id');
99 // If language does not exist, use site default lang.
100 if ($langsel = $mform->getElementValue('lang')) {
101 $lang = reset($langsel);
102 // Check lang exists.
103 if (!get_string_manager()->translation_exists($lang, false)) {
104 $langel =& $mform->getElement('lang');
105 $langel->setValue($CFG->lang);
109 if ($user = $DB->get_record('user', array('id' => $userid))) {
111 // Remove description.
112 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
113 $mform->removeElement('description_editor');
116 // Print picture.
117 $context = context_user::instance($user->id, MUST_EXIST);
118 $fs = get_file_storage();
119 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
120 if (!empty($user->picture) && $hasuploadedpicture) {
121 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
122 } else {
123 $imagevalue = get_string('none');
125 $imageelement = $mform->getElement('currentpicture');
126 $imageelement->setValue($imagevalue);
128 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
129 $mform->removeElement('deletepicture');
132 // Disable fields that are locked by auth plugins.
133 $fields = get_user_fieldnames();
134 $authplugin = get_auth_plugin($user->auth);
135 foreach ($fields as $field) {
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;
142 if (!$mform->elementExists($formfield)) {
143 continue;
145 $configvariable = 'field_lock_' . $field;
146 if (isset($authplugin->config->{$configvariable})) {
147 if ($authplugin->config->{$configvariable} === 'locked') {
148 $mform->hardFreeze($formfield);
149 $mform->setConstant($formfield, $user->$field);
150 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
151 $mform->hardFreeze($formfield);
152 $mform->setConstant($formfield, $user->$field);
157 // Next the customisable profile fields.
158 profile_definition_after_data($mform, $user->id);
160 } else {
161 profile_definition_after_data($mform, 0);
166 * Validate incoming form data.
167 * @param array $usernew
168 * @param array $files
169 * @return array
171 public function validation($usernew, $files) {
172 global $CFG, $DB;
174 $errors = parent::validation($usernew, $files);
176 $usernew = (object)$usernew;
177 $user = $DB->get_record('user', array('id' => $usernew->id));
179 // Validate email.
180 if (!isset($usernew->email)) {
181 // Mail not confirmed yet.
182 } else if (!validate_email($usernew->email)) {
183 $errors['email'] = get_string('invalidemail');
184 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
185 $errors['email'] = get_string('emailexists');
188 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
189 $errors['email'] = get_string('toomanybounces');
192 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
193 $errorstr = email_is_not_allowed($usernew->email);
194 if ($errorstr !== false) {
195 $errors['email'] = $errorstr;
199 // Next the customisable profile fields.
200 $errors += profile_validation($usernew, $files);
202 return $errors;