Merge branch 'MDL-43911-26' of git://github.com/andrewnicols/moodle into MOODLE_26_STABLE
[moodle.git] / user / edit_form.php
blobad4aa3ed570fce46ab842eceeeaaf23741376c7d
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->dirroot.'/lib/formslib.php');
9 class user_edit_form extends moodleform {
11 // Define the form
12 function definition () {
13 global $CFG, $COURSE, $USER;
15 $mform =& $this->_form;
16 $editoroptions = null;
17 $filemanageroptions = null;
18 $userid = $USER->id;
20 if (is_array($this->_customdata)) {
21 if (array_key_exists('editoroptions', $this->_customdata)) {
22 $editoroptions = $this->_customdata['editoroptions'];
24 if (array_key_exists('filemanageroptions', $this->_customdata)) {
25 $filemanageroptions = $this->_customdata['filemanageroptions'];
27 if (array_key_exists('userid', $this->_customdata)) {
28 $userid = $this->_customdata['userid'];
31 //Accessibility: "Required" is bad legend text.
32 $strgeneral = get_string('general');
33 $strrequired = get_string('required');
35 /// Add some extra hidden fields
36 $mform->addElement('hidden', 'id');
37 $mform->setType('id', PARAM_INT);
38 $mform->addElement('hidden', 'course', $COURSE->id);
39 $mform->setType('course', PARAM_INT);
41 /// Print the required moodle fields first
42 $mform->addElement('header', 'moodle', $strgeneral);
44 /// shared fields
45 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
47 /// extra settigs
48 if (!empty($CFG->disableuserimages)) {
49 $mform->removeElement('deletepicture');
50 $mform->removeElement('imagefile');
51 $mform->removeElement('imagealt');
54 /// Next the customisable profile fields
55 profile_definition($mform, $userid);
57 $this->add_action_buttons(false, get_string('updatemyprofile'));
60 function definition_after_data() {
61 global $CFG, $DB, $OUTPUT;
63 $mform =& $this->_form;
64 $userid = $mform->getElementValue('id');
66 // if language does not exist, use site default lang
67 if ($langsel = $mform->getElementValue('lang')) {
68 $lang = reset($langsel);
69 // check lang exists
70 if (!get_string_manager()->translation_exists($lang, false)) {
71 $lang_el =& $mform->getElement('lang');
72 $lang_el->setValue($CFG->lang);
77 if ($user = $DB->get_record('user', array('id'=>$userid))) {
79 // remove description
80 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
81 $mform->removeElement('description_editor');
84 // print picture
85 $context = context_user::instance($user->id, MUST_EXIST);
86 $fs = get_file_storage();
87 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
88 if (!empty($user->picture) && $hasuploadedpicture) {
89 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
90 } else {
91 $imagevalue = get_string('none');
93 $imageelement = $mform->getElement('currentpicture');
94 $imageelement->setValue($imagevalue);
96 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
97 $mform->removeElement('deletepicture');
100 /// disable fields that are locked by auth plugins
101 $fields = get_user_fieldnames();
102 $authplugin = get_auth_plugin($user->auth);
103 foreach ($fields as $field) {
104 if (!$mform->elementExists($field)) {
105 continue;
107 $configvariable = 'field_lock_' . $field;
108 if (isset($authplugin->config->{$configvariable})) {
109 if ($authplugin->config->{$configvariable} === 'locked') {
110 $mform->hardFreeze($field);
111 $mform->setConstant($field, $user->$field);
112 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
113 $mform->hardFreeze($field);
114 $mform->setConstant($field, $user->$field);
119 /// Next the customisable profile fields
120 profile_definition_after_data($mform, $user->id);
122 } else {
123 profile_definition_after_data($mform, 0);
127 function validation($usernew, $files) {
128 global $CFG, $DB;
130 $errors = parent::validation($usernew, $files);
132 $usernew = (object)$usernew;
133 $user = $DB->get_record('user', array('id'=>$usernew->id));
135 // validate email
136 if (!isset($usernew->email)) {
137 // mail not confirmed yet
138 } else if (!validate_email($usernew->email)) {
139 $errors['email'] = get_string('invalidemail');
140 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
141 $errors['email'] = get_string('emailexists');
144 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
145 $errors['email'] = get_string('toomanybounces');
148 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
149 $errorstr = email_is_not_allowed($usernew->email);
150 if ($errorstr !== false) {
151 $errors['email'] = $errorstr;
155 /// Next the customisable profile fields
156 $errors += profile_validation($usernew, $files);
158 return $errors;