MDL-22015 fixed more string_manager encapsulation issues - use edit form
[moodle.git] / user / editadvanced_form.php
CommitLineData
aa6c1ced 1<?php
ad6226fb 2
3require_once($CFG->dirroot.'/lib/formslib.php');
ad6226fb 4
5class user_editadvanced_form extends moodleform {
6
7 // Define the form
bb6d3d34 8 function definition() {
d8734783 9 global $USER, $CFG, $COURSE;
ad6226fb 10
11 $mform =& $this->_form;
8bdc9cac
SH
12
13 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
14 $editoroptions = $this->_customdata['editoroptions'];
15 } else {
16 $editoroptions = null;
17 }
18
c8149f0f 19 //Accessibility: "Required" is bad legend text.
20 $strgeneral = get_string('general');
ad6226fb 21 $strrequired = get_string('required');
22
23 /// Add some extra hidden fields
24 $mform->addElement('hidden', 'id');
d18e0fe6 25 $mform->setType('id', PARAM_INT);
d8734783 26 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 27 $mform->setType('course', PARAM_INT);
ad6226fb 28
29 /// Print the required moodle fields first
c8149f0f 30 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 31
32 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
33 $mform->addRule('username', $strrequired, 'required', null, 'client');
34 $mform->setType('username', PARAM_RAW);
35
17da2e6f 36 $auths = get_plugin_list('auth');
ad6226fb 37 $auth_options = array();
17da2e6f 38 foreach ($auths as $auth => $unused) {
370f10b7 39 $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
ad6226fb 40 }
41 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
42 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
43 $mform->setAdvanced('auth');
44
3bf11ca4
DM
45 if (!empty($CFG->passwordpolicy)){
46 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 47 }
54bb33eb 48 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
ab50ec12 49 $mform->setHelpButton('newpassword',array('newpassword', get_string('leavetokeep')));
ad6226fb 50 $mform->setType('newpassword', PARAM_RAW);
ad6226fb 51
fa1d88eb 52 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
ab50ec12 53 $mform->setHelpButton('preference_auth_forcepasswordchange',array('forcepasswordchange', get_string('forcepasswordchange')));
14a6b7e1 54 /// shared fields
8bdc9cac 55 useredit_shared_definition($mform, $editoroptions);
ad6226fb 56
bb6d3d34 57 /// Next the customisable profile fields
58 profile_definition($mform);
59
ad6226fb 60 $this->add_action_buttons(false, get_string('updatemyprofile'));
61 }
62
63 function definition_after_data() {
f2f085ee 64 global $USER, $CFG, $DB, $OUTPUT;
ad6226fb 65
66 $mform =& $this->_form;
9122137f 67 if ($userid = $mform->getElementValue('id')) {
5d910388 68 $user = $DB->get_record('user', array('id'=>$userid));
9122137f 69 } else {
70 $user = false;
71 }
ad6226fb 72
1545d7f0 73 // if language does not exist, use site default lang
74 if ($langsel = $mform->getElementValue('lang')) {
75 $lang = reset($langsel);
eb309bed 76 // check lang exists
38bb62b5 77 if (get_string_manager()->translation_exists($lang, false)) {
1545d7f0 78 $lang_el =& $mform->getElement('lang');
79 $lang_el->setValue($CFG->lang);
80 }
81 }
82
e8e0bb2d 83 // user can not change own auth method
84 if ($userid == $USER->id) {
85 $mform->hardFreeze('auth');
86 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 87 }
88
89 // admin must choose some password and supply correct email
90 if (!empty($USER->newadminuser)) {
91 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
ad6226fb 92 }
93
e8e0bb2d 94 // require password for new users
95 if ($userid == -1) {
96 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
97 }
98
99 // print picture
ad6226fb 100 if (!empty($CFG->gdversion)) {
b4c46a8b 101 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 102 if ($user and $user->picture) {
812dbaf7 103 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID)));
e8e0bb2d 104 } else {
105 $image_el->setValue(get_string('none'));
106 }
107 }
bb6d3d34 108
109 /// Next the customisable profile fields
63466095 110 profile_definition_after_data($mform, $userid);
e8e0bb2d 111 }
112
07ed083e 113 function validation($usernew, $files) {
5d910388 114 global $CFG, $DB;
e8e0bb2d 115
116 $usernew = (object)$usernew;
04f639a0 117 $usernew->username = trim($usernew->username);
e8e0bb2d 118
5d910388 119 $user = $DB->get_record('user', array('id'=>$usernew->id));
04f639a0 120 $err = array();
121
83022298 122 if (!empty($usernew->newpassword)) {
123 $errmsg = '';//prevent eclipse warning
124 if (!check_password_policy($usernew->newpassword, $errmsg)) {
125 $err['newpassword'] = $errmsg;
126 }
127 }
128
04f639a0 129 if (empty($usernew->username)) {
130 //might be only whitespace
131 $err['username'] = get_string('required');
132 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 133 //check new username does not exist
5d910388 134 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 135 $err['username'] = get_string('usernameexists');
136 }
137 //check allowed characters
138 if ($usernew->username !== moodle_strtolower($usernew->username)) {
139 $err['username'] = get_string('usernamelowercase');
f4065447
RW
140 } else {
141 $string = clean_param($usernew->username, PARAM_USERNAME);
142 if ($usernew->username !== $string) {
143 $err['username'] = get_string('invalidusername');
e8e0bb2d 144 }
ad6226fb 145 }
146 }
e8e0bb2d 147
148 if (!$user or $user->email !== $usernew->email) {
149 if (!validate_email($usernew->email)) {
150 $err['email'] = get_string('invalidemail');
5d910388 151 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 152 $err['email'] = get_string('emailexists');
153 }
154 }
155
bb6d3d34 156 /// Next the customisable profile fields
c8969c22 157 $err += profile_validation($usernew, $files);
bb6d3d34 158
e8e0bb2d 159 if (count($err) == 0){
160 return true;
161 } else {
162 return $err;
163 }
ad6226fb 164 }
165
166 function get_um() {
167 return $this->_upload_manager;
168 }
169}
170
aa6c1ced 171