MDL-28186 messaging: Fix "Enable messagning setting" infuence on the menus
[moodle.git] / user / edit_form.php
blobd22784857a150a3fdc9285a18f191f0056c0339d
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;
15 $mform =& $this->_form;
16 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
17 $editoroptions = $this->_customdata['editoroptions'];
18 } else {
19 $editoroptions = null;
21 //Accessibility: "Required" is bad legend text.
22 $strgeneral = get_string('general');
23 $strrequired = get_string('required');
25 /// Add some extra hidden fields
26 $mform->addElement('hidden', 'id');
27 $mform->setType('id', PARAM_INT);
28 $mform->addElement('hidden', 'course', $COURSE->id);
29 $mform->setType('course', PARAM_INT);
31 /// Print the required moodle fields first
32 $mform->addElement('header', 'moodle', $strgeneral);
34 /// shared fields
35 useredit_shared_definition($mform, $editoroptions);
37 /// extra settigs
38 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
39 $mform->removeElement('deletepicture');
40 $mform->removeElement('imagefile');
41 $mform->removeElement('imagealt');
44 /// Next the customisable profile fields
45 profile_definition($mform);
47 $this->add_action_buttons(false, get_string('updatemyprofile'));
50 function definition_after_data() {
51 global $CFG, $DB, $OUTPUT;
53 $mform =& $this->_form;
54 $userid = $mform->getElementValue('id');
56 // if language does not exist, use site default lang
57 if ($langsel = $mform->getElementValue('lang')) {
58 $lang = reset($langsel);
59 // check lang exists
60 if (!get_string_manager()->translation_exists($lang, false)) {
61 $lang_el =& $mform->getElement('lang');
62 $lang_el->setValue($CFG->lang);
67 if ($user = $DB->get_record('user', array('id'=>$userid))) {
69 // remove description
70 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
71 $mform->removeElement('description_editor');
74 // print picture
75 if (!empty($CFG->gdversion)) {
76 $image_el =& $mform->getElement('currentpicture');
77 if ($user and $user->picture) {
78 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID, 'size'=>64)));
79 } else {
80 $image_el->setValue(get_string('none'));
84 /// disable fields that are locked by auth plugins
85 $fields = get_user_fieldnames();
86 $authplugin = get_auth_plugin($user->auth);
87 foreach ($fields as $field) {
88 if (!$mform->elementExists($field)) {
89 continue;
91 $configvariable = 'field_lock_' . $field;
92 if (isset($authplugin->config->{$configvariable})) {
93 if ($authplugin->config->{$configvariable} === 'locked') {
94 $mform->hardFreeze($field);
95 $mform->setConstant($field, $user->$field);
96 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
97 $mform->hardFreeze($field);
98 $mform->setConstant($field, $user->$field);
103 /// Next the customisable profile fields
104 profile_definition_after_data($mform, $user->id);
106 } else {
107 profile_definition_after_data($mform, 0);
111 function validation($usernew, $files) {
112 global $CFG, $DB;
114 $errors = parent::validation($usernew, $files);
116 $usernew = (object)$usernew;
117 $user = $DB->get_record('user', array('id'=>$usernew->id));
119 // validate email
120 if (!isset($usernew->email)) {
121 // mail not confirmed yet
122 } else if (!validate_email($usernew->email)) {
123 $errors['email'] = get_string('invalidemail');
124 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
125 $errors['email'] = get_string('emailexists');
128 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
129 $errors['email'] = get_string('toomanybounces');
132 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
133 $errorstr = email_is_not_allowed($usernew->email);
134 if ($errorstr !== false) {
135 $errors['email'] = $errorstr;
139 /// Next the customisable profile fields
140 $errors += profile_validation($usernew, $files);
142 return $errors;