MDL-33811 Fixed left over debugging code preventing proper setting of YUI gallery...
[moodle.git] / user / editadvanced_form.php
blob58ab2a0d0a06430a75b313b70a8fd3f28fe8a684
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;
17 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
18 $editoroptions = $this->_customdata['editoroptions'];
19 } else {
20 $editoroptions = null;
23 //Accessibility: "Required" is bad legend text.
24 $strgeneral = get_string('general');
25 $strrequired = get_string('required');
27 /// Add some extra hidden fields
28 $mform->addElement('hidden', 'id');
29 $mform->setType('id', PARAM_INT);
30 $mform->addElement('hidden', 'course', $COURSE->id);
31 $mform->setType('course', PARAM_INT);
33 /// Print the required moodle fields first
34 $mform->addElement('header', 'moodle', $strgeneral);
36 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
37 $mform->addRule('username', $strrequired, 'required', null, 'client');
38 $mform->setType('username', PARAM_RAW);
40 $auths = get_plugin_list('auth');
41 $auth_options = array();
42 foreach ($auths as $auth => $unused) {
43 $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
45 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
46 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
48 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
49 $mform->addHelpButton('suspended', 'suspended', 'auth');
51 if (!empty($CFG->passwordpolicy)){
52 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
54 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
55 $mform->addHelpButton('newpassword', 'newpassword');
56 $mform->setType('newpassword', PARAM_RAW);
58 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
59 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
60 /// shared fields
61 useredit_shared_definition($mform, $editoroptions);
63 /// Next the customisable profile fields
64 profile_definition($mform);
66 $this->add_action_buttons(false, get_string('updatemyprofile'));
69 function definition_after_data() {
70 global $USER, $CFG, $DB, $OUTPUT;
72 $mform =& $this->_form;
73 if ($userid = $mform->getElementValue('id')) {
74 $user = $DB->get_record('user', array('id'=>$userid));
75 } else {
76 $user = false;
79 // if language does not exist, use site default lang
80 if ($langsel = $mform->getElementValue('lang')) {
81 $lang = reset($langsel);
82 // check lang exists
83 if (!get_string_manager()->translation_exists($lang, false)) {
84 $lang_el =& $mform->getElement('lang');
85 $lang_el->setValue($CFG->lang);
89 // user can not change own auth method
90 if ($userid == $USER->id) {
91 $mform->hardFreeze('auth');
92 $mform->hardFreeze('preference_auth_forcepasswordchange');
95 // admin must choose some password and supply correct email
96 if (!empty($USER->newadminuser)) {
97 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
98 if ($mform->elementExists('suspended')) {
99 $mform->removeElement('suspended');
103 // require password for new users
104 if ($userid == -1) {
105 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
108 if ($user and is_mnet_remote_user($user)) {
109 // only local accounts can be suspended
110 if ($mform->elementExists('suspended')) {
111 $mform->removeElement('suspended');
114 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
115 // prevent self and admin mess ups
116 if ($mform->elementExists('suspended')) {
117 $mform->hardFreeze('suspended');
121 // print picture
122 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
123 if ($user) {
124 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
125 $fs = get_file_storage();
126 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
127 if (!empty($user->picture) && $hasuploadedpicture) {
128 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
129 } else {
130 $imagevalue = get_string('none');
132 } else {
133 $imagevalue = get_string('none');
135 $imageelement = $mform->getElement('currentpicture');
136 $imageelement->setValue($imagevalue);
138 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
139 $mform->removeElement('deletepicture');
143 /// Next the customisable profile fields
144 profile_definition_after_data($mform, $userid);
147 function validation($usernew, $files) {
148 global $CFG, $DB;
150 $usernew = (object)$usernew;
151 $usernew->username = trim($usernew->username);
153 $user = $DB->get_record('user', array('id'=>$usernew->id));
154 $err = array();
156 if (!empty($usernew->newpassword)) {
157 $errmsg = '';//prevent eclipse warning
158 if (!check_password_policy($usernew->newpassword, $errmsg)) {
159 $err['newpassword'] = $errmsg;
163 if (empty($usernew->username)) {
164 //might be only whitespace
165 $err['username'] = get_string('required');
166 } else if (!$user or $user->username !== $usernew->username) {
167 //check new username does not exist
168 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
169 $err['username'] = get_string('usernameexists');
171 //check allowed characters
172 if ($usernew->username !== textlib::strtolower($usernew->username)) {
173 $err['username'] = get_string('usernamelowercase');
174 } else {
175 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
176 $err['username'] = get_string('invalidusername');
181 if (!$user or $user->email !== $usernew->email) {
182 if (!validate_email($usernew->email)) {
183 $err['email'] = get_string('invalidemail');
184 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
185 $err['email'] = get_string('emailexists');
189 /// Next the customisable profile fields
190 $err += profile_validation($usernew, $files);
192 if (count($err) == 0){
193 return true;
194 } else {
195 return $err;