MDL-14406
[moodle-linuxchix.git] / user / editadvanced_form.php
blobf26aee5c9c1e1014480b7c623eaedfc767ca70eb
1 <?php //$Id$
3 require_once($CFG->dirroot.'/lib/formslib.php');
5 class user_editadvanced_form extends moodleform {
7 // Define the form
8 function definition() {
9 global $USER, $CFG, $COURSE;
11 $mform =& $this->_form;
12 $this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
13 //Accessibility: "Required" is bad legend text.
14 $strgeneral = get_string('general');
15 $strrequired = get_string('required');
17 /// Add some extra hidden fields
18 $mform->addElement('hidden', 'id');
19 $mform->addElement('hidden', 'course', $COURSE->id);
21 /// Print the required moodle fields first
22 $mform->addElement('header', 'moodle', $strgeneral);
24 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
25 $mform->addRule('username', $strrequired, 'required', null, 'client');
26 $mform->setType('username', PARAM_RAW);
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");
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');
37 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
38 $mform->setHelpButton('newpassword', array(false, get_string('leavetokeep'),
39 false, true, false, get_string('leavetokeep')));
40 $mform->setType('newpassword', PARAM_RAW);
42 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
43 $mform->setHelpButton('preference_auth_forcepasswordchange', array(false, get_string('forcepasswordchangehelp'),
44 false, true, false, get_string('forcepasswordchangehelp')));
46 /// shared fields
47 useredit_shared_definition($mform);
49 /// Next the customisable profile fields
50 profile_definition($mform);
52 $this->add_action_buttons(false, get_string('updatemyprofile'));
55 function definition_after_data() {
56 global $USER, $CFG;
58 $mform =& $this->_form;
59 if ($userid = $mform->getElementValue('id')) {
60 $user = get_record('user', 'id', $userid);
61 } else {
62 $user = false;
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);
75 // user can not change own auth method
76 if ($userid == $USER->id) {
77 $mform->hardFreeze('auth');
78 $mform->hardFreeze('preference_auth_forcepasswordchange');
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');
85 $email_el =& $mform->getElement('email');
86 if ($email_el->getValue() == 'root@localhost') {
87 $email_el->setValue('');
91 // require password for new users
92 if ($userid == -1) {
93 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
96 // print picture
97 if (!empty($CFG->gdversion)) {
98 $image_el =& $mform->getElement('currentpicture');
99 if ($user and $user->picture) {
100 $image_el->setValue(print_user_picture($user, SITEID, $user->picture, 64, true, false, '', true));
101 } else {
102 $image_el->setValue(get_string('none'));
106 /// Next the customisable profile fields
107 profile_definition_after_data($mform);
110 function validation($usernew, $files) {
111 global $CFG;
113 $usernew = (object)$usernew;
114 $usernew->username = trim($usernew->username);
116 $user = get_record('user', 'id', $usernew->id);
117 $err = array();
119 if (!empty($usernew->newpassword)) {
120 $errmsg = '';//prevent eclipse warning
121 if (!check_password_policy($usernew->newpassword, $errmsg)) {
122 $err['newpassword'] = $errmsg;
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) {
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');
134 //check allowed characters
135 if ($usernew->username !== moodle_strtolower($usernew->username)) {
136 $err['username'] = get_string('usernamelowercase');
137 } else {
138 if (empty($CFG->extendedusernamechars)) {
139 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
140 if ($usernew->username !== $string) {
141 $err['username'] = get_string('alphanumerical');
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');
155 /// Next the customisable profile fields
156 $err += profile_validation($usernew, $files);
158 if (count($err) == 0){
159 return true;
160 } else {
161 return $err;
165 function get_um() {
166 return $this->_upload_manager;