Merge branch 'wip-mdl-29079' of git://github.com/rajeshtaneja/moodle
[moodle.git] / user / editadvanced_form.php
CommitLineData
aa6c1ced 1<?php
ad6226fb 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
ad6226fb 7require_once($CFG->dirroot.'/lib/formslib.php');
ad6226fb 8
9class user_editadvanced_form extends moodleform {
10
11 // Define the form
bb6d3d34 12 function definition() {
d8734783 13 global $USER, $CFG, $COURSE;
ad6226fb 14
15 $mform =& $this->_form;
8bdc9cac
SH
16
17 if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
18 $editoroptions = $this->_customdata['editoroptions'];
19 } else {
20 $editoroptions = null;
21 }
22
c8149f0f 23 //Accessibility: "Required" is bad legend text.
24 $strgeneral = get_string('general');
ad6226fb 25 $strrequired = get_string('required');
26
27 /// Add some extra hidden fields
28 $mform->addElement('hidden', 'id');
d18e0fe6 29 $mform->setType('id', PARAM_INT);
d8734783 30 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 31 $mform->setType('course', PARAM_INT);
ad6226fb 32
33 /// Print the required moodle fields first
c8149f0f 34 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 35
36 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
37 $mform->addRule('username', $strrequired, 'required', null, 'client');
38 $mform->setType('username', PARAM_RAW);
39
17da2e6f 40 $auths = get_plugin_list('auth');
ad6226fb 41 $auth_options = array();
17da2e6f 42 foreach ($auths as $auth => $unused) {
370f10b7 43 $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
ad6226fb 44 }
45 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
f728d49b 46 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
ad6226fb 47
3bf11ca4
DM
48 if (!empty($CFG->passwordpolicy)){
49 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 50 }
54bb33eb 51 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
27d1a27c 52 $mform->addHelpButton('newpassword', 'newpassword');
ad6226fb 53 $mform->setType('newpassword', PARAM_RAW);
ad6226fb 54
fa1d88eb 55 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
27d1a27c 56 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
14a6b7e1 57 /// shared fields
8bdc9cac 58 useredit_shared_definition($mform, $editoroptions);
ad6226fb 59
bb6d3d34 60 /// Next the customisable profile fields
61 profile_definition($mform);
62
ad6226fb 63 $this->add_action_buttons(false, get_string('updatemyprofile'));
64 }
65
66 function definition_after_data() {
f2f085ee 67 global $USER, $CFG, $DB, $OUTPUT;
ad6226fb 68
69 $mform =& $this->_form;
9122137f 70 if ($userid = $mform->getElementValue('id')) {
5d910388 71 $user = $DB->get_record('user', array('id'=>$userid));
9122137f 72 } else {
73 $user = false;
74 }
ad6226fb 75
1545d7f0 76 // if language does not exist, use site default lang
77 if ($langsel = $mform->getElementValue('lang')) {
78 $lang = reset($langsel);
eb309bed 79 // check lang exists
96de38c3 80 if (!get_string_manager()->translation_exists($lang, false)) {
1545d7f0 81 $lang_el =& $mform->getElement('lang');
82 $lang_el->setValue($CFG->lang);
83 }
84 }
85
e8e0bb2d 86 // user can not change own auth method
87 if ($userid == $USER->id) {
88 $mform->hardFreeze('auth');
89 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 90 }
91
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');
ad6226fb 95 }
96
e8e0bb2d 97 // require password for new users
98 if ($userid == -1) {
99 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
100 }
101
102 // print picture
de2d81fa 103 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
b4c46a8b 104 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 105 if ($user and $user->picture) {
812dbaf7 106 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID)));
e8e0bb2d 107 } else {
108 $image_el->setValue(get_string('none'));
109 }
110 }
bb6d3d34 111
112 /// Next the customisable profile fields
63466095 113 profile_definition_after_data($mform, $userid);
e8e0bb2d 114 }
115
6b8ad965 116 function validation($usernew, $files) {
5d910388 117 global $CFG, $DB;
e8e0bb2d 118
119 $usernew = (object)$usernew;
f728d49b
PS
120 $usernew->username = trim($usernew->username);
121
122 $user = $DB->get_record('user', array('id'=>$usernew->id));
04f639a0 123 $err = array();
124
83022298 125 if (!empty($usernew->newpassword)) {
126 $errmsg = '';//prevent eclipse warning
127 if (!check_password_policy($usernew->newpassword, $errmsg)) {
128 $err['newpassword'] = $errmsg;
129 }
130 }
131
04f639a0 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) {
e8e0bb2d 136 //check new username does not exist
5d910388 137 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 138 $err['username'] = get_string('usernameexists');
139 }
f728d49b 140 //check allowed characters
e8e0bb2d 141 if ($usernew->username !== moodle_strtolower($usernew->username)) {
142 $err['username'] = get_string('usernamelowercase');
6b8ad965
PS
143 } else {
144 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
f4065447 145 $err['username'] = get_string('invalidusername');
e8e0bb2d 146 }
ad6226fb 147 }
148 }
e8e0bb2d 149
150 if (!$user or $user->email !== $usernew->email) {
151 if (!validate_email($usernew->email)) {
152 $err['email'] = get_string('invalidemail');
5d910388 153 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 154 $err['email'] = get_string('emailexists');
155 }
156 }
157
bb6d3d34 158 /// Next the customisable profile fields
c8969c22 159 $err += profile_validation($usernew, $files);
bb6d3d34 160
e8e0bb2d 161 if (count($err) == 0){
162 return true;
163 } else {
164 return $err;
165 }
ad6226fb 166 }
ad6226fb 167}
168
aa6c1ced 169