weekly release 2.7dev
[moodle.git] / user / editadvanced_form.php
CommitLineData
aa6c1ced 1<?php
ad6226fb 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
ad6226fb 7require_once($CFG->dirroot.'/lib/formslib.php');
ad6226fb 8
9class user_editadvanced_form extends moodleform {
10
11 // Define the form
bb6d3d34 12 function definition() {
d8734783 13 global $USER, $CFG, $COURSE;
ad6226fb 14
15 $mform =& $this->_form;
fc3aa0fb
RT
16 $editoroptions = null;
17 $filemanageroptions = null;
18 $userid = $USER->id;
19
20 if (is_array($this->_customdata)) {
21 if (array_key_exists('editoroptions', $this->_customdata)) {
22 $editoroptions = $this->_customdata['editoroptions'];
23 }
24 if (array_key_exists('filemanageroptions', $this->_customdata)) {
25 $filemanageroptions = $this->_customdata['filemanageroptions'];
26 }
27 if (array_key_exists('userid', $this->_customdata)) {
28 $userid = $this->_customdata['userid'];
29 }
4e782b32 30 }
8bdc9cac 31
c8149f0f 32 //Accessibility: "Required" is bad legend text.
33 $strgeneral = get_string('general');
ad6226fb 34 $strrequired = get_string('required');
35
36 /// Add some extra hidden fields
37 $mform->addElement('hidden', 'id');
d18e0fe6 38 $mform->setType('id', PARAM_INT);
d8734783 39 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 40 $mform->setType('course', PARAM_INT);
ad6226fb 41
42 /// Print the required moodle fields first
c8149f0f 43 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 44
45 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
46 $mform->addRule('username', $strrequired, 'required', null, 'client');
47 $mform->setType('username', PARAM_RAW);
48
bd3b3bba 49 $auths = core_component::get_plugin_list('auth');
c5772ede
PS
50 $enabled = get_string('pluginenabled', 'core_plugin');
51 $disabled = get_string('plugindisabled', 'core_plugin');
52 $auth_options = array($enabled=>array(), $disabled=>array());
6513cc03 53 $cannotchangepass = array();
17da2e6f 54 foreach ($auths as $auth => $unused) {
6513cc03
AA
55 $authinst = get_auth_plugin($auth);
56 $passwordurl = $authinst->change_password_url();
57 if (!($authinst->can_change_password() && empty($passwordurl))) {
0b3bd8cb
PS
58 if (!$userid and $authinst->is_internal()) {
59 // This is unlikely but we can not create account without password
60 // when plugin uses passwords, we need to set it initially at least.
61 } else {
62 $cannotchangepass[] = $auth;
63 }
6513cc03 64 }
c5772ede
PS
65 if (is_enabled_auth($auth)) {
66 $auth_options[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
67 } else {
68 $auth_options[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
69 }
ad6226fb 70 }
c5772ede 71 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
f728d49b 72 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
ad6226fb 73
4ad72c28
PS
74 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
75 $mform->addHelpButton('suspended', 'suspended', 'auth');
76
483b6804 77 $mform->addElement('checkbox', 'createpassword', get_string('createpassword','auth'));
0b3bd8cb 78 $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
483b6804 79
3bf11ca4
DM
80 if (!empty($CFG->passwordpolicy)){
81 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 82 }
54bb33eb 83 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
27d1a27c 84 $mform->addHelpButton('newpassword', 'newpassword');
ad6226fb 85 $mform->setType('newpassword', PARAM_RAW);
483b6804 86 $mform->disabledIf('newpassword', 'createpassword', 'checked');
ad6226fb 87
6513cc03
AA
88 $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
89
fa1d88eb 90 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
27d1a27c 91 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
483b6804
PS
92 $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
93
14a6b7e1 94 /// shared fields
4e782b32 95 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
ad6226fb 96
bb6d3d34 97 /// Next the customisable profile fields
fc3aa0fb 98 profile_definition($mform, $userid);
bb6d3d34 99
607fdfc3
AA
100 if ($userid == -1) {
101 $btnstring = get_string('createuser');
102 } else {
103 $btnstring = get_string('updatemyprofile');
104 }
105
106 $this->add_action_buttons(false, $btnstring);
ad6226fb 107 }
108
109 function definition_after_data() {
f2f085ee 110 global $USER, $CFG, $DB, $OUTPUT;
ad6226fb 111
112 $mform =& $this->_form;
9122137f 113 if ($userid = $mform->getElementValue('id')) {
5d910388 114 $user = $DB->get_record('user', array('id'=>$userid));
9122137f 115 } else {
116 $user = false;
117 }
ad6226fb 118
1545d7f0 119 // if language does not exist, use site default lang
120 if ($langsel = $mform->getElementValue('lang')) {
121 $lang = reset($langsel);
eb309bed 122 // check lang exists
96de38c3 123 if (!get_string_manager()->translation_exists($lang, false)) {
1545d7f0 124 $lang_el =& $mform->getElement('lang');
125 $lang_el->setValue($CFG->lang);
126 }
127 }
128
e8e0bb2d 129 // user can not change own auth method
130 if ($userid == $USER->id) {
131 $mform->hardFreeze('auth');
132 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 133 }
134
135 // admin must choose some password and supply correct email
136 if (!empty($USER->newadminuser)) {
137 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
4ad72c28
PS
138 if ($mform->elementExists('suspended')) {
139 $mform->removeElement('suspended');
140 }
ad6226fb 141 }
142
e8e0bb2d 143 // require password for new users
483b6804
PS
144 if ($userid > 0) {
145 if ($mform->elementExists('createpassword')) {
146 $mform->removeElement('createpassword');
147 }
e8e0bb2d 148 }
149
4ad72c28
PS
150 if ($user and is_mnet_remote_user($user)) {
151 // only local accounts can be suspended
152 if ($mform->elementExists('suspended')) {
153 $mform->removeElement('suspended');
154 }
155 }
156 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
157 // prevent self and admin mess ups
158 if ($mform->elementExists('suspended')) {
159 $mform->hardFreeze('suspended');
160 }
161 }
162
e8e0bb2d 163 // print picture
689096bc 164 if (empty($USER->newadminuser)) {
4125bdc1 165 if ($user) {
43731030 166 $context = context_user::instance($user->id, MUST_EXIST);
4125bdc1 167 $fs = get_file_storage();
ed9e0cb6 168 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
4125bdc1
SH
169 if (!empty($user->picture) && $hasuploadedpicture) {
170 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
171 } else {
172 $imagevalue = get_string('none');
173 }
e8e0bb2d 174 } else {
4125bdc1
SH
175 $imagevalue = get_string('none');
176 }
177 $imageelement = $mform->getElement('currentpicture');
178 $imageelement->setValue($imagevalue);
179
180 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
181 $mform->removeElement('deletepicture');
e8e0bb2d 182 }
183 }
bb6d3d34 184
185 /// Next the customisable profile fields
63466095 186 profile_definition_after_data($mform, $userid);
e8e0bb2d 187 }
188
6b8ad965 189 function validation($usernew, $files) {
5d910388 190 global $CFG, $DB;
e8e0bb2d 191
192 $usernew = (object)$usernew;
f728d49b
PS
193 $usernew->username = trim($usernew->username);
194
195 $user = $DB->get_record('user', array('id'=>$usernew->id));
04f639a0 196 $err = array();
197
483b6804
PS
198 if (!$user and !empty($usernew->createpassword)) {
199 if ($usernew->suspended) {
200 // Show some error because we can not mail suspended users.
201 $err['suspended'] = get_string('error');
202 }
203 } else {
204 if (!empty($usernew->newpassword)) {
205 $errmsg = ''; // Prevent eclipse warning.
206 if (!check_password_policy($usernew->newpassword, $errmsg)) {
207 $err['newpassword'] = $errmsg;
208 }
209 } else if (!$user) {
0b3bd8cb
PS
210 $auth = get_auth_plugin($usernew->auth);
211 if ($auth->is_internal()) {
212 // Internal accounts require password!
213 $err['newpassword'] = get_string('required');
214 }
83022298 215 }
216 }
217
04f639a0 218 if (empty($usernew->username)) {
219 //might be only whitespace
220 $err['username'] = get_string('required');
221 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 222 //check new username does not exist
5d910388 223 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 224 $err['username'] = get_string('usernameexists');
225 }
f728d49b 226 //check allowed characters
2f1e464a 227 if ($usernew->username !== core_text::strtolower($usernew->username)) {
e8e0bb2d 228 $err['username'] = get_string('usernamelowercase');
6b8ad965
PS
229 } else {
230 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
f4065447 231 $err['username'] = get_string('invalidusername');
e8e0bb2d 232 }
ad6226fb 233 }
234 }
e8e0bb2d 235
236 if (!$user or $user->email !== $usernew->email) {
237 if (!validate_email($usernew->email)) {
238 $err['email'] = get_string('invalidemail');
5d910388 239 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 240 $err['email'] = get_string('emailexists');
241 }
242 }
243
bb6d3d34 244 /// Next the customisable profile fields
c8969c22 245 $err += profile_validation($usernew, $files);
bb6d3d34 246
e8e0bb2d 247 if (count($err) == 0){
248 return true;
249 } else {
250 return $err;
251 }
ad6226fb 252 }
ad6226fb 253}
254
aa6c1ced 255