FIxed an annoying little artifact showing up on the second line of the last tab ...
[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
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
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
38
39 $mform->addElement('checkbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
40 //TODO: add missing help - user will be forced to change password
41
42 require('edit_form_common.php');
43
44 $this->add_action_buttons(false, get_string('updatemyprofile'));
45 }
46
47 function definition_after_data() {
48 global $USER, $CFG;
49
50 $mform =& $this->_form;
e8e0bb2d 51 $userid = $mform->getElementValue('id');
52 $user = get_record('user', 'id', $userid);
ad6226fb 53
e8e0bb2d 54 // user can not change own auth method
55 if ($userid == $USER->id) {
56 $mform->hardFreeze('auth');
57 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 58 }
59
60 // admin must choose some password and supply correct email
61 if (!empty($USER->newadminuser)) {
62 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
63
e8e0bb2d 64 $email_el = $mform->getElement('email');
65 if ($email_el->getValue() == 'root@localhost') {
66 $email_el->setValue('');
ad6226fb 67 }
68 }
69
e8e0bb2d 70 // require password for new users
71 if ($userid == -1) {
72 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
73 }
74
75 // print picture
ad6226fb 76 if (!empty($CFG->gdversion)) {
e8e0bb2d 77 $image_el = $mform->getElement('currentpicture');
78 if ($user and $user->picture) {
79 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
80 } else {
81 $image_el->setValue(get_string('none'));
82 }
83 }
84 }
85
86 function validation ($usernew) {
87 global $CFG;
88
89 $usernew = (object)$usernew;
90 $user = get_record('user', 'id', $usernew->id);
91 $err = array();
92
93 if (!$user or $user->username !== $usernew->username) {
94 //check new username does not exist
95 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
96 $err['username'] = get_string('usernameexists');
97 }
98 //check allowed characters
99 if ($usernew->username !== moodle_strtolower($usernew->username)) {
100 $err['username'] = get_string('usernamelowercase');
ad6226fb 101 } else {
e8e0bb2d 102 if (empty($CFG->extendedusernamechars)) {
103 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
104 if ($usernew->username !== $string) {
105 $err['username'] = get_string('alphanumerical');
106 }
107 }
ad6226fb 108 }
109 }
e8e0bb2d 110
111 if (!$user or $user->email !== $usernew->email) {
112 if (!validate_email($usernew->email)) {
113 $err['email'] = get_string('invalidemail');
114 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
115 $err['email'] = get_string('emailexists');
116 }
117 }
118
119 if (count($err) == 0){
120 return true;
121 } else {
122 return $err;
123 }
ad6226fb 124 }
125
126 function get_um() {
127 return $this->_upload_manager;
128 }
129}
130
131?>