Creating course grade_category and course grade_item when not yet created and overvie...
[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"');
1c54b2bb 36 $mform->setHelpButton('newpassword', array(false, get_string('leavetokeep'),
37 false, true, false, get_string('leavetokeep')));
ad6226fb 38 $mform->setType('newpassword', PARAM_RAW);
ad6226fb 39
fa1d88eb 40 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
1c54b2bb 41 $mform->setHelpButton('preference_auth_forcepasswordchange', array(false, get_string('forcepasswordchangehelp'),
42 false, true, false, get_string('forcepasswordchangehelp')));
ad6226fb 43
14a6b7e1 44 /// shared fields
45 useredit_shared_definition($mform);
ad6226fb 46
bb6d3d34 47 /// Next the customisable profile fields
48 profile_definition($mform);
49
ad6226fb 50 $this->add_action_buttons(false, get_string('updatemyprofile'));
51 }
52
53 function definition_after_data() {
54 global $USER, $CFG;
55
56 $mform =& $this->_form;
e8e0bb2d 57 $userid = $mform->getElementValue('id');
58 $user = get_record('user', 'id', $userid);
ad6226fb 59
1545d7f0 60 // if language does not exist, use site default lang
61 if ($langsel = $mform->getElementValue('lang')) {
62 $lang = reset($langsel);
63 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
64 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
65 $lang_el =& $mform->getElement('lang');
66 $lang_el->setValue($CFG->lang);
67 }
68 }
69
e8e0bb2d 70 // user can not change own auth method
71 if ($userid == $USER->id) {
72 $mform->hardFreeze('auth');
73 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 74 }
75
76 // admin must choose some password and supply correct email
77 if (!empty($USER->newadminuser)) {
78 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
79
b4c46a8b 80 $email_el =& $mform->getElement('email');
e8e0bb2d 81 if ($email_el->getValue() == 'root@localhost') {
82 $email_el->setValue('');
ad6226fb 83 }
84 }
85
e8e0bb2d 86 // require password for new users
87 if ($userid == -1) {
88 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
89 }
90
91 // print picture
ad6226fb 92 if (!empty($CFG->gdversion)) {
b4c46a8b 93 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 94 if ($user and $user->picture) {
95 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
96 } else {
97 $image_el->setValue(get_string('none'));
98 }
99 }
bb6d3d34 100
101 /// Next the customisable profile fields
102 profile_definition_after_data($mform);
e8e0bb2d 103 }
104
bb6d3d34 105 function validation($usernew) {
e8e0bb2d 106 global $CFG;
107
108 $usernew = (object)$usernew;
04f639a0 109 $usernew->username = trim($usernew->username);
e8e0bb2d 110
04f639a0 111 $user = get_record('user', 'id', $usernew->id);
112 $err = array();
113
83022298 114 if (!empty($usernew->newpassword)) {
115 $errmsg = '';//prevent eclipse warning
116 if (!check_password_policy($usernew->newpassword, $errmsg)) {
117 $err['newpassword'] = $errmsg;
118 }
119 }
120
04f639a0 121 if (empty($usernew->username)) {
122 //might be only whitespace
123 $err['username'] = get_string('required');
124 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 125 //check new username does not exist
126 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
127 $err['username'] = get_string('usernameexists');
128 }
129 //check allowed characters
130 if ($usernew->username !== moodle_strtolower($usernew->username)) {
131 $err['username'] = get_string('usernamelowercase');
ad6226fb 132 } else {
e8e0bb2d 133 if (empty($CFG->extendedusernamechars)) {
134 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
135 if ($usernew->username !== $string) {
136 $err['username'] = get_string('alphanumerical');
137 }
138 }
ad6226fb 139 }
140 }
e8e0bb2d 141
142 if (!$user or $user->email !== $usernew->email) {
143 if (!validate_email($usernew->email)) {
144 $err['email'] = get_string('invalidemail');
145 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
146 $err['email'] = get_string('emailexists');
147 }
148 }
149
bb6d3d34 150 /// Next the customisable profile fields
151 $err += profile_validation($usernew);
152
e8e0bb2d 153 if (count($err) == 0){
154 return true;
155 } else {
156 return $err;
157 }
ad6226fb 158 }
159
160 function get_um() {
161 return $this->_upload_manager;
162 }
163}
164
165?>