Merge branch 'install_21_STABLE' of git://github.com/amosbot/moodle into MOODLE_21_STABLE
[moodle.git] / user / editadvanced_form.php
blob10f6b456bfb72228d0770601ea5723430e82caf4
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->dirroot.'/lib/formslib.php');
9 class user_editadvanced_form extends moodleform {
11 // Define the form
12 function definition() {
13 global $USER, $CFG, $COURSE;
15 $mform =& $this->_form;
17 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
18 $editoroptions = $this->_customdata['editoroptions'];
19 } else {
20 $editoroptions = null;
23 //Accessibility: "Required" is bad legend text.
24 $strgeneral = get_string('general');
25 $strrequired = get_string('required');
27 /// Add some extra hidden fields
28 $mform->addElement('hidden', 'id');
29 $mform->setType('id', PARAM_INT);
30 $mform->addElement('hidden', 'course', $COURSE->id);
31 $mform->setType('course', PARAM_INT);
33 /// Print the required moodle fields first
34 $mform->addElement('header', 'moodle', $strgeneral);
36 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
37 $mform->addRule('username', $strrequired, 'required', null, 'client');
38 $mform->setType('username', PARAM_RAW);
40 $auths = get_plugin_list('auth');
41 $auth_options = array();
42 foreach ($auths as $auth => $unused) {
43 $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
45 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
46 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
48 if (!empty($CFG->passwordpolicy)){
49 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
51 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
52 $mform->addHelpButton('newpassword', 'newpassword');
53 $mform->setType('newpassword', PARAM_RAW);
55 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
56 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
57 /// shared fields
58 useredit_shared_definition($mform, $editoroptions);
60 /// Next the customisable profile fields
61 profile_definition($mform);
63 $this->add_action_buttons(false, get_string('updatemyprofile'));
66 function definition_after_data() {
67 global $USER, $CFG, $DB, $OUTPUT;
69 $mform =& $this->_form;
70 if ($userid = $mform->getElementValue('id')) {
71 $user = $DB->get_record('user', array('id'=>$userid));
72 } else {
73 $user = false;
76 // if language does not exist, use site default lang
77 if ($langsel = $mform->getElementValue('lang')) {
78 $lang = reset($langsel);
79 // check lang exists
80 if (!get_string_manager()->translation_exists($lang, false)) {
81 $lang_el =& $mform->getElement('lang');
82 $lang_el->setValue($CFG->lang);
86 // user can not change own auth method
87 if ($userid == $USER->id) {
88 $mform->hardFreeze('auth');
89 $mform->hardFreeze('preference_auth_forcepasswordchange');
92 // admin must choose some password and supply correct email
93 if (!empty($USER->newadminuser)) {
94 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
97 // require password for new users
98 if ($userid == -1) {
99 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
102 // print picture
103 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
104 $image_el =& $mform->getElement('currentpicture');
105 if ($user and $user->picture) {
106 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID)));
107 } else {
108 $image_el->setValue(get_string('none'));
112 /// Next the customisable profile fields
113 profile_definition_after_data($mform, $userid);
116 function validation($usernew, $files) {
117 global $CFG, $DB;
119 $usernew = (object)$usernew;
120 $usernew->username = trim($usernew->username);
122 $user = $DB->get_record('user', array('id'=>$usernew->id));
123 $err = array();
125 if (!empty($usernew->newpassword)) {
126 $errmsg = '';//prevent eclipse warning
127 if (!check_password_policy($usernew->newpassword, $errmsg)) {
128 $err['newpassword'] = $errmsg;
132 if (empty($usernew->username)) {
133 //might be only whitespace
134 $err['username'] = get_string('required');
135 } else if (!$user or $user->username !== $usernew->username) {
136 //check new username does not exist
137 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
138 $err['username'] = get_string('usernameexists');
140 //check allowed characters
141 if ($usernew->username !== moodle_strtolower($usernew->username)) {
142 $err['username'] = get_string('usernamelowercase');
143 } else {
144 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
145 $err['username'] = get_string('invalidusername');
150 if (!$user or $user->email !== $usernew->email) {
151 if (!validate_email($usernew->email)) {
152 $err['email'] = get_string('invalidemail');
153 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
154 $err['email'] = get_string('emailexists');
158 /// Next the customisable profile fields
159 $err += profile_validation($usernew, $files);
161 if (count($err) == 0){
162 return true;
163 } else {
164 return $err;