MDL-14679 towards /admin conversion
[moodle.git] / user / editadvanced_form.php
CommitLineData
ad6226fb 1<?php //$Id$
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;
ad6226fb 12 $this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
c8149f0f 13 //Accessibility: "Required" is bad legend text.
14 $strgeneral = get_string('general');
ad6226fb 15 $strrequired = get_string('required');
16
17 /// Add some extra hidden fields
18 $mform->addElement('hidden', 'id');
d8734783 19 $mform->addElement('hidden', 'course', $COURSE->id);
ad6226fb 20
21 /// Print the required moodle fields first
c8149f0f 22 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 23
24 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
25 $mform->addRule('username', $strrequired, 'required', null, 'client');
26 $mform->setType('username', PARAM_RAW);
27
28 $modules = get_list_of_plugins('auth');
29 $auth_options = array();
30 foreach ($modules as $module) {
31 $auth_options[$module] = get_string("auth_$module"."title", "auth");
32 }
33 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
34 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
35 $mform->setAdvanced('auth');
36
54bb33eb 37 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
1c54b2bb 38 $mform->setHelpButton('newpassword', array(false, get_string('leavetokeep'),
39 false, true, false, get_string('leavetokeep')));
ad6226fb 40 $mform->setType('newpassword', PARAM_RAW);
ad6226fb 41
fa1d88eb 42 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
1c54b2bb 43 $mform->setHelpButton('preference_auth_forcepasswordchange', array(false, get_string('forcepasswordchangehelp'),
44 false, true, false, get_string('forcepasswordchangehelp')));
ad6226fb 45
14a6b7e1 46 /// shared fields
47 useredit_shared_definition($mform);
ad6226fb 48
bb6d3d34 49 /// Next the customisable profile fields
50 profile_definition($mform);
51
ad6226fb 52 $this->add_action_buttons(false, get_string('updatemyprofile'));
53 }
54
55 function definition_after_data() {
56 global $USER, $CFG;
57
58 $mform =& $this->_form;
9122137f 59 if ($userid = $mform->getElementValue('id')) {
60 $user = get_record('user', 'id', $userid);
61 } else {
62 $user = false;
63 }
ad6226fb 64
1545d7f0 65 // if language does not exist, use site default lang
66 if ($langsel = $mform->getElementValue('lang')) {
67 $lang = reset($langsel);
68 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
69 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
70 $lang_el =& $mform->getElement('lang');
71 $lang_el->setValue($CFG->lang);
72 }
73 }
74
e8e0bb2d 75 // user can not change own auth method
76 if ($userid == $USER->id) {
77 $mform->hardFreeze('auth');
78 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 79 }
80
81 // admin must choose some password and supply correct email
82 if (!empty($USER->newadminuser)) {
83 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
84
b4c46a8b 85 $email_el =& $mform->getElement('email');
e8e0bb2d 86 if ($email_el->getValue() == 'root@localhost') {
87 $email_el->setValue('');
ad6226fb 88 }
89 }
90
e8e0bb2d 91 // require password for new users
92 if ($userid == -1) {
93 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
94 }
95
96 // print picture
ad6226fb 97 if (!empty($CFG->gdversion)) {
b4c46a8b 98 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 99 if ($user and $user->picture) {
65bcf17b 100 $image_el->setValue(print_user_picture($user, SITEID, $user->picture, 64, true, false, '', true));
e8e0bb2d 101 } else {
102 $image_el->setValue(get_string('none'));
103 }
104 }
bb6d3d34 105
106 /// Next the customisable profile fields
107 profile_definition_after_data($mform);
e8e0bb2d 108 }
109
c8969c22 110 function validation($usernew, $files) {
e8e0bb2d 111 global $CFG;
112
113 $usernew = (object)$usernew;
04f639a0 114 $usernew->username = trim($usernew->username);
e8e0bb2d 115
04f639a0 116 $user = get_record('user', 'id', $usernew->id);
117 $err = array();
118
83022298 119 if (!empty($usernew->newpassword)) {
120 $errmsg = '';//prevent eclipse warning
121 if (!check_password_policy($usernew->newpassword, $errmsg)) {
122 $err['newpassword'] = $errmsg;
123 }
124 }
125
04f639a0 126 if (empty($usernew->username)) {
127 //might be only whitespace
128 $err['username'] = get_string('required');
129 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 130 //check new username does not exist
131 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
132 $err['username'] = get_string('usernameexists');
133 }
134 //check allowed characters
135 if ($usernew->username !== moodle_strtolower($usernew->username)) {
136 $err['username'] = get_string('usernamelowercase');
ad6226fb 137 } else {
e8e0bb2d 138 if (empty($CFG->extendedusernamechars)) {
139 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
140 if ($usernew->username !== $string) {
141 $err['username'] = get_string('alphanumerical');
142 }
143 }
ad6226fb 144 }
145 }
e8e0bb2d 146
147 if (!$user or $user->email !== $usernew->email) {
148 if (!validate_email($usernew->email)) {
149 $err['email'] = get_string('invalidemail');
150 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
151 $err['email'] = get_string('emailexists');
152 }
153 }
154
bb6d3d34 155 /// Next the customisable profile fields
c8969c22 156 $err += profile_validation($usernew, $files);
bb6d3d34 157
e8e0bb2d 158 if (count($err) == 0){
159 return true;
160 } else {
161 return $err;
162 }
ad6226fb 163 }
164
165 function get_um() {
166 return $this->_upload_manager;
167 }
168}
169
170?>