fixed incorrect coding style {}
[moodle.git] / user / editadvanced_form.php
CommitLineData
ad6226fb 1<?php //$Id$
2
3require_once($CFG->dirroot.'/lib/formslib.php');
ad6226fb 4
5class user_editadvanced_form extends moodleform {
6
7 // Define the form
bb6d3d34 8 function definition() {
d8734783 9 global $USER, $CFG, $COURSE;
ad6226fb 10
11 $mform =& $this->_form;
ad6226fb 12 $this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
13 $strrequired = get_string('required');
14
15 /// Add some extra hidden fields
16 $mform->addElement('hidden', 'id');
d8734783 17 $mform->addElement('hidden', 'course', $COURSE->id);
ad6226fb 18
19 /// Print the required moodle fields first
20 $mform->addElement('header', 'moodle', $strrequired);
21
22 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
23 $mform->addRule('username', $strrequired, 'required', null, 'client');
24 $mform->setType('username', PARAM_RAW);
25
26 $modules = get_list_of_plugins('auth');
27 $auth_options = array();
28 foreach ($modules as $module) {
29 $auth_options[$module] = get_string("auth_$module"."title", "auth");
30 }
31 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
32 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
33 $mform->setAdvanced('auth');
34
54bb33eb 35 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
ad6226fb 36 $mform->setType('newpassword', PARAM_RAW);
37 //TODO: add missing help - empty means no change
38
fa1d88eb 39 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
ad6226fb 40 //TODO: add missing help - user will be forced to change password
41
14a6b7e1 42 /// shared fields
43 useredit_shared_definition($mform);
ad6226fb 44
bb6d3d34 45 /// Next the customisable profile fields
46 profile_definition($mform);
47
ad6226fb 48 $this->add_action_buttons(false, get_string('updatemyprofile'));
49 }
50
51 function definition_after_data() {
52 global $USER, $CFG;
53
54 $mform =& $this->_form;
e8e0bb2d 55 $userid = $mform->getElementValue('id');
56 $user = get_record('user', 'id', $userid);
ad6226fb 57
1545d7f0 58 // if language does not exist, use site default lang
59 if ($langsel = $mform->getElementValue('lang')) {
60 $lang = reset($langsel);
61 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
62 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
63 $lang_el =& $mform->getElement('lang');
64 $lang_el->setValue($CFG->lang);
65 }
66 }
67
e8e0bb2d 68 // user can not change own auth method
69 if ($userid == $USER->id) {
70 $mform->hardFreeze('auth');
71 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 72 }
73
74 // admin must choose some password and supply correct email
75 if (!empty($USER->newadminuser)) {
76 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
77
b4c46a8b 78 $email_el =& $mform->getElement('email');
e8e0bb2d 79 if ($email_el->getValue() == 'root@localhost') {
80 $email_el->setValue('');
ad6226fb 81 }
82 }
83
e8e0bb2d 84 // require password for new users
85 if ($userid == -1) {
86 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
87 }
88
89 // print picture
ad6226fb 90 if (!empty($CFG->gdversion)) {
b4c46a8b 91 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 92 if ($user and $user->picture) {
93 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
94 } else {
95 $image_el->setValue(get_string('none'));
96 }
97 }
bb6d3d34 98
99 /// Next the customisable profile fields
100 profile_definition_after_data($mform);
e8e0bb2d 101 }
102
bb6d3d34 103 function validation($usernew) {
e8e0bb2d 104 global $CFG;
105
106 $usernew = (object)$usernew;
04f639a0 107 $usernew->username = trim($usernew->username);
e8e0bb2d 108
04f639a0 109 $user = get_record('user', 'id', $usernew->id);
110 $err = array();
111
83022298 112 if (!empty($usernew->newpassword)) {
113 $errmsg = '';//prevent eclipse warning
114 if (!check_password_policy($usernew->newpassword, $errmsg)) {
115 $err['newpassword'] = $errmsg;
116 }
117 }
118
04f639a0 119 if (empty($usernew->username)) {
120 //might be only whitespace
121 $err['username'] = get_string('required');
122 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 123 //check new username does not exist
124 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
125 $err['username'] = get_string('usernameexists');
126 }
127 //check allowed characters
128 if ($usernew->username !== moodle_strtolower($usernew->username)) {
129 $err['username'] = get_string('usernamelowercase');
ad6226fb 130 } else {
e8e0bb2d 131 if (empty($CFG->extendedusernamechars)) {
132 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
133 if ($usernew->username !== $string) {
134 $err['username'] = get_string('alphanumerical');
135 }
136 }
ad6226fb 137 }
138 }
e8e0bb2d 139
140 if (!$user or $user->email !== $usernew->email) {
141 if (!validate_email($usernew->email)) {
142 $err['email'] = get_string('invalidemail');
143 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
144 $err['email'] = get_string('emailexists');
145 }
146 }
147
bb6d3d34 148 /// Next the customisable profile fields
149 $err += profile_validation($usernew);
150
e8e0bb2d 151 if (count($err) == 0){
152 return true;
153 } else {
154 return $err;
155 }
ad6226fb 156 }
157
158 function get_um() {
159 return $this->_upload_manager;
160 }
161}
162
163?>