Merge branch 'MDL-42084-master' of git://github.com/sammarshallou/moodle
[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))) {
58 $cannotchangepass[] = $auth;
59 }
c5772ede
PS
60 if (is_enabled_auth($auth)) {
61 $auth_options[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
62 } else {
63 $auth_options[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
64 }
ad6226fb 65 }
c5772ede 66 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
f728d49b 67 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
ad6226fb 68
4ad72c28
PS
69 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
70 $mform->addHelpButton('suspended', 'suspended', 'auth');
71
483b6804
PS
72 $mform->addElement('checkbox', 'createpassword', get_string('createpassword','auth'));
73
3bf11ca4
DM
74 if (!empty($CFG->passwordpolicy)){
75 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 76 }
54bb33eb 77 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
27d1a27c 78 $mform->addHelpButton('newpassword', 'newpassword');
ad6226fb 79 $mform->setType('newpassword', PARAM_RAW);
483b6804 80 $mform->disabledIf('newpassword', 'createpassword', 'checked');
ad6226fb 81
6513cc03
AA
82 $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
83
fa1d88eb 84 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
27d1a27c 85 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
483b6804
PS
86 $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
87
14a6b7e1 88 /// shared fields
4e782b32 89 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
ad6226fb 90
bb6d3d34 91 /// Next the customisable profile fields
fc3aa0fb 92 profile_definition($mform, $userid);
bb6d3d34 93
607fdfc3
AA
94 if ($userid == -1) {
95 $btnstring = get_string('createuser');
96 } else {
97 $btnstring = get_string('updatemyprofile');
98 }
99
100 $this->add_action_buttons(false, $btnstring);
ad6226fb 101 }
102
103 function definition_after_data() {
f2f085ee 104 global $USER, $CFG, $DB, $OUTPUT;
ad6226fb 105
106 $mform =& $this->_form;
9122137f 107 if ($userid = $mform->getElementValue('id')) {
5d910388 108 $user = $DB->get_record('user', array('id'=>$userid));
9122137f 109 } else {
110 $user = false;
111 }
ad6226fb 112
1545d7f0 113 // if language does not exist, use site default lang
114 if ($langsel = $mform->getElementValue('lang')) {
115 $lang = reset($langsel);
eb309bed 116 // check lang exists
96de38c3 117 if (!get_string_manager()->translation_exists($lang, false)) {
1545d7f0 118 $lang_el =& $mform->getElement('lang');
119 $lang_el->setValue($CFG->lang);
120 }
121 }
122
e8e0bb2d 123 // user can not change own auth method
124 if ($userid == $USER->id) {
125 $mform->hardFreeze('auth');
126 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 127 }
128
129 // admin must choose some password and supply correct email
130 if (!empty($USER->newadminuser)) {
131 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
4ad72c28
PS
132 if ($mform->elementExists('suspended')) {
133 $mform->removeElement('suspended');
134 }
ad6226fb 135 }
136
e8e0bb2d 137 // require password for new users
483b6804
PS
138 if ($userid > 0) {
139 if ($mform->elementExists('createpassword')) {
140 $mform->removeElement('createpassword');
141 }
e8e0bb2d 142 }
143
4ad72c28
PS
144 if ($user and is_mnet_remote_user($user)) {
145 // only local accounts can be suspended
146 if ($mform->elementExists('suspended')) {
147 $mform->removeElement('suspended');
148 }
149 }
150 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
151 // prevent self and admin mess ups
152 if ($mform->elementExists('suspended')) {
153 $mform->hardFreeze('suspended');
154 }
155 }
156
e8e0bb2d 157 // print picture
689096bc 158 if (empty($USER->newadminuser)) {
4125bdc1 159 if ($user) {
43731030 160 $context = context_user::instance($user->id, MUST_EXIST);
4125bdc1 161 $fs = get_file_storage();
ed9e0cb6 162 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
4125bdc1
SH
163 if (!empty($user->picture) && $hasuploadedpicture) {
164 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
165 } else {
166 $imagevalue = get_string('none');
167 }
e8e0bb2d 168 } else {
4125bdc1
SH
169 $imagevalue = get_string('none');
170 }
171 $imageelement = $mform->getElement('currentpicture');
172 $imageelement->setValue($imagevalue);
173
174 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
175 $mform->removeElement('deletepicture');
e8e0bb2d 176 }
177 }
bb6d3d34 178
179 /// Next the customisable profile fields
63466095 180 profile_definition_after_data($mform, $userid);
e8e0bb2d 181 }
182
6b8ad965 183 function validation($usernew, $files) {
5d910388 184 global $CFG, $DB;
e8e0bb2d 185
186 $usernew = (object)$usernew;
f728d49b
PS
187 $usernew->username = trim($usernew->username);
188
189 $user = $DB->get_record('user', array('id'=>$usernew->id));
04f639a0 190 $err = array();
191
483b6804
PS
192 if (!$user and !empty($usernew->createpassword)) {
193 if ($usernew->suspended) {
194 // Show some error because we can not mail suspended users.
195 $err['suspended'] = get_string('error');
196 }
197 } else {
198 if (!empty($usernew->newpassword)) {
199 $errmsg = ''; // Prevent eclipse warning.
200 if (!check_password_policy($usernew->newpassword, $errmsg)) {
201 $err['newpassword'] = $errmsg;
202 }
203 } else if (!$user) {
204 $err['newpassword'] = get_string('required');
83022298 205 }
206 }
207
04f639a0 208 if (empty($usernew->username)) {
209 //might be only whitespace
210 $err['username'] = get_string('required');
211 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 212 //check new username does not exist
5d910388 213 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 214 $err['username'] = get_string('usernameexists');
215 }
f728d49b 216 //check allowed characters
2f1e464a 217 if ($usernew->username !== core_text::strtolower($usernew->username)) {
e8e0bb2d 218 $err['username'] = get_string('usernamelowercase');
6b8ad965
PS
219 } else {
220 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
f4065447 221 $err['username'] = get_string('invalidusername');
e8e0bb2d 222 }
ad6226fb 223 }
224 }
e8e0bb2d 225
226 if (!$user or $user->email !== $usernew->email) {
227 if (!validate_email($usernew->email)) {
228 $err['email'] = get_string('invalidemail');
5d910388 229 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 230 $err['email'] = get_string('emailexists');
231 }
232 }
233
bb6d3d34 234 /// Next the customisable profile fields
c8969c22 235 $err += profile_validation($usernew, $files);
bb6d3d34 236
e8e0bb2d 237 if (count($err) == 0){
238 return true;
239 } else {
240 return $err;
241 }
ad6226fb 242 }
ad6226fb 243}
244
aa6c1ced 245