MDL-22015 fixed more string_manager encapsulation issues - use edit form
[moodle.git] / user / edit_form.php
CommitLineData
8bdc9cac 1<?php
c9c74c47 2
d8734783 3require_once($CFG->dirroot.'/lib/formslib.php');
c9c74c47 4
5class user_edit_form extends moodleform {
6
7 // Define the form
8 function definition () {
b0c90e6e 9 global $CFG, $COURSE;
c9c74c47 10
d8734783 11 $mform =& $this->_form;
8bdc9cac
SH
12 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
13 $editoroptions = $this->_customdata['editoroptions'];
14 } else {
15 $editoroptions = null;
16 }
c8149f0f 17 //Accessibility: "Required" is bad legend text.
18 $strgeneral = get_string('general');
c9c74c47 19 $strrequired = get_string('required');
20
c9c74c47 21 /// Add some extra hidden fields
3c19571f 22 $mform->addElement('hidden', 'id');
d18e0fe6 23 $mform->setType('id', PARAM_INT);
d8734783 24 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 25 $mform->setType('course', PARAM_INT);
acc9c3e0 26
c9c74c47 27 /// Print the required moodle fields first
c8149f0f 28 $mform->addElement('header', 'moodle', $strgeneral);
c9c74c47 29
14a6b7e1 30 /// shared fields
8bdc9cac 31 useredit_shared_definition($mform, $editoroptions);
c9c74c47 32
d8734783 33 /// extra settigs
8bdc9cac 34 $mform->addRule('description_editor', $strrequired, 'required', null, 'client');
d8734783 35 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
36 $mform->removeElement('deletepicture');
37 $mform->removeElement('imagefile');
38 $mform->removeElement('imagealt');
c9c74c47 39 }
acc9c3e0 40
bb6d3d34 41 /// Next the customisable profile fields
42 profile_definition($mform);
43
d8734783 44 $this->add_action_buttons(false, get_string('updatemyprofile'));
45 }
c9c74c47 46
d8734783 47 function definition_after_data() {
f2f085ee 48 global $CFG, $DB, $OUTPUT;
c9c74c47 49
d8734783 50 $mform =& $this->_form;
51 $userid = $mform->getElementValue('id');
acc9c3e0 52
1545d7f0 53 // if language does not exist, use site default lang
54 if ($langsel = $mform->getElementValue('lang')) {
55 $lang = reset($langsel);
eb309bed 56 // check lang exists
38bb62b5 57 if (get_string_manager()->translation_exists($lang, false)) {
1545d7f0 58 $lang_el =& $mform->getElement('lang');
59 $lang_el->setValue($CFG->lang);
60 }
61 }
62
35f0660a 63
5d910388 64 if ($user = $DB->get_record('user', array('id'=>$userid))) {
c9c74c47 65
35f0660a 66 // remove description
6eaf2e37 67 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
8bdc9cac 68 $mform->removeElement('description_editor');
35f0660a 69 }
70
3eff8146 71 // print picture
72 if (!empty($CFG->gdversion)) {
b4c46a8b 73 $image_el =& $mform->getElement('currentpicture');
3eff8146 74 if ($user and $user->picture) {
812dbaf7 75 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID, 'size'=>64)));
3eff8146 76 } else {
77 $image_el->setValue(get_string('none'));
78 }
c9c74c47 79 }
acc9c3e0 80
d8734783 81 /// disable fields that are locked by auth plugins
82 $fields = get_user_fieldnames();
d8734783 83 $authplugin = get_auth_plugin($user->auth);
84 foreach ($fields as $field) {
85 if (!$mform->elementExists($field)) {
86 continue;
87 }
88 $configvariable = 'field_lock_' . $field;
89 if (isset($authplugin->config->{$configvariable})) {
90 if ($authplugin->config->{$configvariable} === 'locked') {
cc444336 91 $mform->hardFreeze($field);
92 $mform->setConstant($field, $user->$field);
d8734783 93 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
cc444336 94 $mform->hardFreeze($field);
95 $mform->setConstant($field, $user->$field);
d8734783 96 }
97 }
68e9d368 98 }
f2f085ee 99
63466095 100 /// Next the customisable profile fields
101 profile_definition_after_data($mform, $user->id);
c9c74c47 102
63466095 103 } else {
104 profile_definition_after_data($mform, 0);
105 }
c9c74c47 106 }
107
a78890d5 108 function validation($usernew, $files) {
5d910388 109 global $CFG, $DB;
c9c74c47 110
a78890d5 111 $errors = parent::validation($usernew, $files);
112
acc9c3e0 113 $usernew = (object)$usernew;
5d910388 114 $user = $DB->get_record('user', array('id'=>$usernew->id));
acc9c3e0 115
d8734783 116 // validate email
d0b2acde 117 if (!isset($usernew->email)) {
118 // mail not confirmed yet
119 } else if (!validate_email($usernew->email)) {
a78890d5 120 $errors['email'] = get_string('invalidemail');
5d910388 121 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
a78890d5 122 $errors['email'] = get_string('emailexists');
c9c74c47 123 }
14a6b7e1 124
d0b2acde 125 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
a78890d5 126 $errors['email'] = get_string('toomanybounces');
c9c74c47 127 }
128
d0b2acde 129 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
bff34a67 130 $errorstr = email_is_not_allowed($usernew->email);
131 if ($errorstr !== false) {
132 $errors['email'] = $errorstr;
133 }
134 }
135
bb6d3d34 136 /// Next the customisable profile fields
a78890d5 137 $errors += profile_validation($usernew, $files);
bb6d3d34 138
a78890d5 139 return $errors;
c9c74c47 140 }
d8734783 141
142 function get_um() {
143 return $this->_upload_manager;
144 }
c9c74c47 145}
146
aa6c1ced 147