MDL-11275 added (submissions). Also fixed :
[moodle-pu.git] / user / editadvanced_form.php
blob1bcd86a42a82f899d934e484541232e9bbb3a8e4
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('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
36 $mform->setHelpButton('newpassword', array(false, get_string('leavetokeep'),
37 false, true, false, get_string('leavetokeep')));
38 $mform->setType('newpassword', PARAM_RAW);
40 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
41 $mform->setHelpButton('preference_auth_forcepasswordchange', array(false, get_string('forcepasswordchangehelp'),
42 false, true, false, get_string('forcepasswordchangehelp')));
44 /// shared fields
45 useredit_shared_definition($mform);
47 /// Next the customisable profile fields
48 profile_definition($mform);
50 $this->add_action_buttons(false, get_string('updatemyprofile'));
53 function definition_after_data() {
54 global $USER, $CFG;
56 $mform =& $this->_form;
57 $userid = $mform->getElementValue('id');
58 $user = get_record('user', 'id', $userid);
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);
70 // user can not change own auth method
71 if ($userid == $USER->id) {
72 $mform->hardFreeze('auth');
73 $mform->hardFreeze('preference_auth_forcepasswordchange');
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');
80 $email_el =& $mform->getElement('email');
81 if ($email_el->getValue() == 'root@localhost') {
82 $email_el->setValue('');
86 // require password for new users
87 if ($userid == -1) {
88 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
91 // print picture
92 if (!empty($CFG->gdversion)) {
93 $image_el =& $mform->getElement('currentpicture');
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'));
101 /// Next the customisable profile fields
102 profile_definition_after_data($mform);
105 function validation($usernew) {
106 global $CFG;
108 $usernew = (object)$usernew;
109 $usernew->username = trim($usernew->username);
111 $user = get_record('user', 'id', $usernew->id);
112 $err = array();
114 if (!empty($usernew->newpassword)) {
115 $errmsg = '';//prevent eclipse warning
116 if (!check_password_policy($usernew->newpassword, $errmsg)) {
117 $err['newpassword'] = $errmsg;
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) {
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');
129 //check allowed characters
130 if ($usernew->username !== moodle_strtolower($usernew->username)) {
131 $err['username'] = get_string('usernamelowercase');
132 } else {
133 if (empty($CFG->extendedusernamechars)) {
134 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
135 if ($usernew->username !== $string) {
136 $err['username'] = get_string('alphanumerical');
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');
150 /// Next the customisable profile fields
151 $err += profile_validation($usernew);
153 if (count($err) == 0){
154 return true;
155 } else {
156 return $err;
160 function get_um() {
161 return $this->_upload_manager;