Merge branch 'MDL-42338' of git://github.com/rwijaya/moodle
[moodle.git] / user / editadvanced_form.php
blobae94f687f79eca1f325a1be0acb012c4acc5f0fd
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->dirroot.'/lib/formslib.php');
9 class user_editadvanced_form extends moodleform {
11 // Define the form
12 function definition() {
13 global $USER, $CFG, $COURSE;
15 $mform =& $this->_form;
16 $editoroptions = null;
17 $filemanageroptions = null;
18 $userid = $USER->id;
20 if (is_array($this->_customdata)) {
21 if (array_key_exists('editoroptions', $this->_customdata)) {
22 $editoroptions = $this->_customdata['editoroptions'];
24 if (array_key_exists('filemanageroptions', $this->_customdata)) {
25 $filemanageroptions = $this->_customdata['filemanageroptions'];
27 if (array_key_exists('userid', $this->_customdata)) {
28 $userid = $this->_customdata['userid'];
32 //Accessibility: "Required" is bad legend text.
33 $strgeneral = get_string('general');
34 $strrequired = get_string('required');
36 /// Add some extra hidden fields
37 $mform->addElement('hidden', 'id');
38 $mform->setType('id', PARAM_INT);
39 $mform->addElement('hidden', 'course', $COURSE->id);
40 $mform->setType('course', PARAM_INT);
42 /// Print the required moodle fields first
43 $mform->addElement('header', 'moodle', $strgeneral);
45 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
46 $mform->addRule('username', $strrequired, 'required', null, 'client');
47 $mform->setType('username', PARAM_RAW);
49 $auths = core_component::get_plugin_list('auth');
50 $enabled = get_string('pluginenabled', 'core_plugin');
51 $disabled = get_string('plugindisabled', 'core_plugin');
52 $auth_options = array($enabled=>array(), $disabled=>array());
53 foreach ($auths as $auth => $unused) {
54 if (is_enabled_auth($auth)) {
55 $auth_options[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
56 } else {
57 $auth_options[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
60 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
61 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
63 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
64 $mform->addHelpButton('suspended', 'suspended', 'auth');
66 $mform->addElement('checkbox', 'createpassword', get_string('createpassword','auth'));
68 if (!empty($CFG->passwordpolicy)){
69 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
71 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
72 $mform->addHelpButton('newpassword', 'newpassword');
73 $mform->setType('newpassword', PARAM_RAW);
74 $mform->disabledIf('newpassword', 'createpassword', 'checked');
76 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
77 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
78 $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
80 /// shared fields
81 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
83 /// Next the customisable profile fields
84 profile_definition($mform, $userid);
86 if ($userid == -1) {
87 $btnstring = get_string('createuser');
88 } else {
89 $btnstring = get_string('updatemyprofile');
92 $this->add_action_buttons(false, $btnstring);
95 function definition_after_data() {
96 global $USER, $CFG, $DB, $OUTPUT;
98 $mform =& $this->_form;
99 if ($userid = $mform->getElementValue('id')) {
100 $user = $DB->get_record('user', array('id'=>$userid));
101 } else {
102 $user = false;
105 // if language does not exist, use site default lang
106 if ($langsel = $mform->getElementValue('lang')) {
107 $lang = reset($langsel);
108 // check lang exists
109 if (!get_string_manager()->translation_exists($lang, false)) {
110 $lang_el =& $mform->getElement('lang');
111 $lang_el->setValue($CFG->lang);
115 // user can not change own auth method
116 if ($userid == $USER->id) {
117 $mform->hardFreeze('auth');
118 $mform->hardFreeze('preference_auth_forcepasswordchange');
121 // admin must choose some password and supply correct email
122 if (!empty($USER->newadminuser)) {
123 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
124 if ($mform->elementExists('suspended')) {
125 $mform->removeElement('suspended');
129 // require password for new users
130 if ($userid > 0) {
131 if ($mform->elementExists('createpassword')) {
132 $mform->removeElement('createpassword');
136 if ($user and is_mnet_remote_user($user)) {
137 // only local accounts can be suspended
138 if ($mform->elementExists('suspended')) {
139 $mform->removeElement('suspended');
142 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
143 // prevent self and admin mess ups
144 if ($mform->elementExists('suspended')) {
145 $mform->hardFreeze('suspended');
149 // print picture
150 if (empty($USER->newadminuser)) {
151 if ($user) {
152 $context = context_user::instance($user->id, MUST_EXIST);
153 $fs = get_file_storage();
154 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
155 if (!empty($user->picture) && $hasuploadedpicture) {
156 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
157 } else {
158 $imagevalue = get_string('none');
160 } else {
161 $imagevalue = get_string('none');
163 $imageelement = $mform->getElement('currentpicture');
164 $imageelement->setValue($imagevalue);
166 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
167 $mform->removeElement('deletepicture');
171 /// Next the customisable profile fields
172 profile_definition_after_data($mform, $userid);
175 function validation($usernew, $files) {
176 global $CFG, $DB;
178 $usernew = (object)$usernew;
179 $usernew->username = trim($usernew->username);
181 $user = $DB->get_record('user', array('id'=>$usernew->id));
182 $err = array();
184 if (!$user and !empty($usernew->createpassword)) {
185 if ($usernew->suspended) {
186 // Show some error because we can not mail suspended users.
187 $err['suspended'] = get_string('error');
189 } else {
190 if (!empty($usernew->newpassword)) {
191 $errmsg = ''; // Prevent eclipse warning.
192 if (!check_password_policy($usernew->newpassword, $errmsg)) {
193 $err['newpassword'] = $errmsg;
195 } else if (!$user) {
196 $err['newpassword'] = get_string('required');
200 if (empty($usernew->username)) {
201 //might be only whitespace
202 $err['username'] = get_string('required');
203 } else if (!$user or $user->username !== $usernew->username) {
204 //check new username does not exist
205 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
206 $err['username'] = get_string('usernameexists');
208 //check allowed characters
209 if ($usernew->username !== core_text::strtolower($usernew->username)) {
210 $err['username'] = get_string('usernamelowercase');
211 } else {
212 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
213 $err['username'] = get_string('invalidusername');
218 if (!$user or $user->email !== $usernew->email) {
219 if (!validate_email($usernew->email)) {
220 $err['email'] = get_string('invalidemail');
221 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
222 $err['email'] = get_string('emailexists');
226 /// Next the customisable profile fields
227 $err += profile_validation($usernew, $files);
229 if (count($err) == 0){
230 return true;
231 } else {
232 return $err;