MDL-9147
[moodle-pu.git] / user / editadvanced_form.php
blobd23aa8b6e8987ecf1a9035eeff9670d2d144cafa
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 $strrequired = get_string('required');
15 /// Add some extra hidden fields
16 $mform->addElement('hidden', 'id');
17 $mform->addElement('hidden', 'course', $COURSE->id);
19 /// Print the required moodle fields first
20 $mform->addElement('header', 'moodle', $strrequired);
22 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
23 $mform->addRule('username', $strrequired, 'required', null, 'client');
24 $mform->setType('username', PARAM_RAW);
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");
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');
35 $mform->addElement('text', 'newpassword', get_string('newpassword'), 'size="20"');
36 $mform->setType('newpassword', PARAM_RAW);
37 //TODO: add missing help - empty means no change
39 $mform->addElement('checkbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
40 //TODO: add missing help - user will be forced to change password
42 /// shared fields
43 useredit_shared_definition($mform);
45 /// Next the customisable profile fields
46 profile_definition($mform);
48 $this->add_action_buttons(false, get_string('updatemyprofile'));
51 function definition_after_data() {
52 global $USER, $CFG;
54 $mform =& $this->_form;
55 $userid = $mform->getElementValue('id');
56 $user = get_record('user', 'id', $userid);
58 // user can not change own auth method
59 if ($userid == $USER->id) {
60 $mform->hardFreeze('auth');
61 $mform->hardFreeze('preference_auth_forcepasswordchange');
64 // admin must choose some password and supply correct email
65 if (!empty($USER->newadminuser)) {
66 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
68 $email_el =& $mform->getElement('email');
69 if ($email_el->getValue() == 'root@localhost') {
70 $email_el->setValue('');
74 // require password for new users
75 if ($userid == -1) {
76 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
79 // print picture
80 if (!empty($CFG->gdversion)) {
81 $image_el =& $mform->getElement('currentpicture');
82 if ($user and $user->picture) {
83 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
84 } else {
85 $image_el->setValue(get_string('none'));
89 /// Next the customisable profile fields
90 profile_definition_after_data($mform);
93 function validation($usernew) {
94 global $CFG;
96 $usernew = (object)$usernew;
97 $usernew->username = trim($usernew->username);
99 $user = get_record('user', 'id', $usernew->id);
100 $err = array();
102 if (empty($usernew->username)) {
103 //might be only whitespace
104 $err['username'] = get_string('required');
105 } else if (!$user or $user->username !== $usernew->username) {
106 //check new username does not exist
107 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
108 $err['username'] = get_string('usernameexists');
110 //check allowed characters
111 if ($usernew->username !== moodle_strtolower($usernew->username)) {
112 $err['username'] = get_string('usernamelowercase');
113 } else {
114 if (empty($CFG->extendedusernamechars)) {
115 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
116 if ($usernew->username !== $string) {
117 $err['username'] = get_string('alphanumerical');
123 if (!$user or $user->email !== $usernew->email) {
124 if (!validate_email($usernew->email)) {
125 $err['email'] = get_string('invalidemail');
126 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
127 $err['email'] = get_string('emailexists');
131 /// Next the customisable profile fields
132 $err += profile_validation($usernew);
134 if (count($err) == 0){
135 return true;
136 } else {
137 return $err;
141 function get_um() {
142 return $this->_upload_manager;