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;
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'];
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);
76 useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
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);
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 // Trim required name fields.
103 foreach (useredit_get_required_name_fields() as $field) {
104 $mform->applyFilter($field, 'trim');
107 if ($user = $DB->get_record('user', array('id' => $userid))) {
109 // Remove description.
110 if (empty($user->description
) && !empty($CFG->profilesforenrolledusersonly
) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
111 $mform->removeElement('description_editor');
115 $context = context_user
::instance($user->id
, MUST_EXIST
);
116 $fs = get_file_storage();
117 $hasuploadedpicture = ($fs->file_exists($context->id
, 'user', 'icon', 0, '/', 'f2.png') ||
$fs->file_exists($context->id
, 'user', 'icon', 0, '/', 'f2.jpg'));
118 if (!empty($user->picture
) && $hasuploadedpicture) {
119 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID
, 'size' => 64));
121 $imagevalue = get_string('none');
123 $imageelement = $mform->getElement('currentpicture');
124 $imageelement->setValue($imagevalue);
126 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
127 $mform->removeElement('deletepicture');
130 // Disable fields that are locked by auth plugins.
131 $fields = get_user_fieldnames();
132 $authplugin = get_auth_plugin($user->auth
);
133 $customfields = $authplugin->get_custom_user_profile_fields();
134 $customfieldsdata = profile_user_record($userid, false);
135 $fields = array_merge($fields, $customfields);
136 foreach ($fields as $field) {
137 if ($field === 'description') {
138 // Hard coded hack for description field. See MDL-37704 for details.
139 $formfield = 'description_editor';
143 if (!$mform->elementExists($formfield)) {
147 // Get the original value for the field.
148 if (in_array($field, $customfields)) {
149 $key = str_replace('profile_field_', '', $field);
150 $value = isset($customfieldsdata->{$key}) ?
$customfieldsdata->{$key} : '';
152 $value = $user->{$field};
155 $configvariable = 'field_lock_' . $field;
156 if (isset($authplugin->config
->{$configvariable})) {
157 if ($authplugin->config
->{$configvariable} === 'locked') {
158 $mform->hardFreeze($formfield);
159 $mform->setConstant($formfield, $value);
160 } else if ($authplugin->config
->{$configvariable} === 'unlockedifempty' and $value != '') {
161 $mform->hardFreeze($formfield);
162 $mform->setConstant($formfield, $value);
167 // Next the customisable profile fields.
168 profile_definition_after_data($mform, $user->id
);
171 profile_definition_after_data($mform, 0);
176 * Validate incoming form data.
177 * @param array $usernew
178 * @param array $files
181 public function validation($usernew, $files) {
184 $errors = parent
::validation($usernew, $files);
186 $usernew = (object)$usernew;
187 $user = $DB->get_record('user', array('id' => $usernew->id
));
190 if (!isset($usernew->email
)) {
191 // Mail not confirmed yet.
192 } else if (!validate_email($usernew->email
)) {
193 $errors['email'] = get_string('invalidemail');
194 } else if (($usernew->email
!== $user->email
)
195 and empty($CFG->allowaccountssameemail
)
196 and $DB->record_exists('user', array('email' => $usernew->email
, 'mnethostid' => $CFG->mnet_localhost_id
))) {
197 $errors['email'] = get_string('emailexists');
200 if (isset($usernew->email
) and $usernew->email
=== $user->email
and over_bounce_threshold($user)) {
201 $errors['email'] = get_string('toomanybounces');
204 if (isset($usernew->email
) and !empty($CFG->verifychangedemail
) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system
::instance())) {
205 $errorstr = email_is_not_allowed($usernew->email
);
206 if ($errorstr !== false) {
207 $errors['email'] = $errorstr;
211 // Next the customisable profile fields.
212 $errors +
= profile_validation($usernew, $files);