2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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');
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
{
42 public function definition () {
43 global $CFG, $COURSE, $USER;
45 $mform = $this->_form
;
46 $editoroptions = null;
47 $filemanageroptions = null;
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);
75 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
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'));
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');
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));
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 (!$mform->elementExists($field)) {
139 $configvariable = 'field_lock_' . $field;
140 if (isset($authplugin->config
->{$configvariable})) {
141 if ($authplugin->config
->{$configvariable} === 'locked') {
142 $mform->hardFreeze($field);
143 $mform->setConstant($field, $user->$field);
144 } else if ($authplugin->config
->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
145 $mform->hardFreeze($field);
146 $mform->setConstant($field, $user->$field);
151 // Next the customisable profile fields.
152 profile_definition_after_data($mform, $user->id
);
155 profile_definition_after_data($mform, 0);
160 * Validate incoming form data.
161 * @param array $usernew
162 * @param array $files
165 public function validation($usernew, $files) {
168 $errors = parent
::validation($usernew, $files);
170 $usernew = (object)$usernew;
171 $user = $DB->get_record('user', array('id' => $usernew->id
));
174 if (!isset($usernew->email
)) {
175 // Mail not confirmed yet.
176 } else if (!validate_email($usernew->email
)) {
177 $errors['email'] = get_string('invalidemail');
178 } else if (($usernew->email
!== $user->email
) and $DB->record_exists('user', array('email' => $usernew->email
, 'mnethostid' => $CFG->mnet_localhost_id
))) {
179 $errors['email'] = get_string('emailexists');
182 if (isset($usernew->email
) and $usernew->email
=== $user->email
and over_bounce_threshold($user)) {
183 $errors['email'] = get_string('toomanybounces');
186 if (isset($usernew->email
) and !empty($CFG->verifychangedemail
) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system
::instance())) {
187 $errorstr = email_is_not_allowed($usernew->email
);
188 if ($errorstr !== false) {
189 $errors['email'] = $errorstr;
193 // Next the customisable profile fields.
194 $errors +
= profile_validation($usernew, $files);