files MDL-20635 Added support for cleaning up files associated with a context when...
[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;
c8149f0f 12 //Accessibility: "Required" is bad legend text.
13 $strgeneral = get_string('general');
ad6226fb 14 $strrequired = get_string('required');
15
16 /// Add some extra hidden fields
17 $mform->addElement('hidden', 'id');
d18e0fe6 18 $mform->setType('id', PARAM_INT);
d8734783 19 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 20 $mform->setType('course', PARAM_INT);
ad6226fb 21
22 /// Print the required moodle fields first
c8149f0f 23 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 24
25 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
26 $mform->addRule('username', $strrequired, 'required', null, 'client');
27 $mform->setType('username', PARAM_RAW);
28
17da2e6f 29 $auths = get_plugin_list('auth');
ad6226fb 30 $auth_options = array();
17da2e6f 31 foreach ($auths as $auth => $unused) {
32 $auth_options[$auth] = auth_get_plugin_title($auth);
ad6226fb 33 }
34 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
35 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
36 $mform->setAdvanced('auth');
37
54bb33eb 38 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
ab50ec12 39 $mform->setHelpButton('newpassword',array('newpassword', get_string('leavetokeep')));
ad6226fb 40 $mform->setType('newpassword', PARAM_RAW);
ad6226fb 41
fa1d88eb 42 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
ab50ec12 43 $mform->setHelpButton('preference_auth_forcepasswordchange',array('forcepasswordchange', get_string('forcepasswordchange')));
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() {
f2f085ee 54 global $USER, $CFG, $DB, $OUTPUT;
ad6226fb 55
56 $mform =& $this->_form;
9122137f 57 if ($userid = $mform->getElementValue('id')) {
5d910388 58 $user = $DB->get_record('user', array('id'=>$userid));
9122137f 59 } else {
60 $user = false;
61 }
ad6226fb 62
1545d7f0 63 // if language does not exist, use site default lang
64 if ($langsel = $mform->getElementValue('lang')) {
65 $lang = reset($langsel);
eb309bed 66 // missing _utf8 in language, add it before further processing. MDL-11829 MDL-16845
67 if (strpos($lang, '_utf8') === false) {
68 $lang = $lang . '_utf8';
69 $lang_el =& $mform->getElement('lang');
70 $lang_el->setValue($lang);
71 }
72 // check lang exists
73 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
1545d7f0 74 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
75 $lang_el =& $mform->getElement('lang');
76 $lang_el->setValue($CFG->lang);
77 }
78 }
79
e8e0bb2d 80 // user can not change own auth method
81 if ($userid == $USER->id) {
82 $mform->hardFreeze('auth');
83 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 84 }
85
86 // admin must choose some password and supply correct email
87 if (!empty($USER->newadminuser)) {
88 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
ad6226fb 89 }
90
e8e0bb2d 91 // require password for new users
92 if ($userid == -1) {
93 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
94 }
95
96 // print picture
ad6226fb 97 if (!empty($CFG->gdversion)) {
b4c46a8b 98 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 99 if ($user and $user->picture) {
f2f085ee 100 $userpic = moodle_user_picture::make($user, SITEID);
101 $userpic->alttext = true;
102 $image_el->setValue($OUTPUT->user_picture($userpic));
e8e0bb2d 103 } else {
104 $image_el->setValue(get_string('none'));
105 }
106 }
bb6d3d34 107
108 /// Next the customisable profile fields
63466095 109 profile_definition_after_data($mform, $userid);
e8e0bb2d 110 }
111
c8969c22 112 function validation($usernew, $files) {
5d910388 113 global $CFG, $DB;
e8e0bb2d 114
115 $usernew = (object)$usernew;
04f639a0 116 $usernew->username = trim($usernew->username);
e8e0bb2d 117
5d910388 118 $user = $DB->get_record('user', array('id'=>$usernew->id));
04f639a0 119 $err = array();
120
83022298 121 if (!empty($usernew->newpassword)) {
122 $errmsg = '';//prevent eclipse warning
123 if (!check_password_policy($usernew->newpassword, $errmsg)) {
124 $err['newpassword'] = $errmsg;
125 }
126 }
127
04f639a0 128 if (empty($usernew->username)) {
129 //might be only whitespace
130 $err['username'] = get_string('required');
131 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 132 //check new username does not exist
5d910388 133 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 134 $err['username'] = get_string('usernameexists');
135 }
136 //check allowed characters
137 if ($usernew->username !== moodle_strtolower($usernew->username)) {
138 $err['username'] = get_string('usernamelowercase');
ad6226fb 139 } else {
e8e0bb2d 140 if (empty($CFG->extendedusernamechars)) {
6dbcacee 141 $string = preg_replace("/[^(-\.[:alnum:])]/i", '', $usernew->username);
e8e0bb2d 142 if ($usernew->username !== $string) {
143 $err['username'] = get_string('alphanumerical');
144 }
145 }
ad6226fb 146 }
147 }
e8e0bb2d 148
149 if (!$user or $user->email !== $usernew->email) {
150 if (!validate_email($usernew->email)) {
151 $err['email'] = get_string('invalidemail');
5d910388 152 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 153 $err['email'] = get_string('emailexists');
154 }
155 }
156
bb6d3d34 157 /// Next the customisable profile fields
c8969c22 158 $err += profile_validation($usernew, $files);
bb6d3d34 159
e8e0bb2d 160 if (count($err) == 0){
161 return true;
162 } else {
163 return $err;
164 }
ad6226fb 165 }
166
167 function get_um() {
168 return $this->_upload_manager;
169 }
170}
171
172?>