MDL-21676 user: minor gravatar fixes
[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)) {
4125bdc1
SH
104 if ($user) {
105 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
106 $fs = get_file_storage();
ed9e0cb6 107 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
4125bdc1
SH
108 if (!empty($user->picture) && $hasuploadedpicture) {
109 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
110 } else {
111 $imagevalue = get_string('none');
112 }
e8e0bb2d 113 } else {
4125bdc1
SH
114 $imagevalue = get_string('none');
115 }
116 $imageelement = $mform->getElement('currentpicture');
117 $imageelement->setValue($imagevalue);
118
119 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
120 $mform->removeElement('deletepicture');
e8e0bb2d 121 }
122 }
bb6d3d34 123
124 /// Next the customisable profile fields
63466095 125 profile_definition_after_data($mform, $userid);
e8e0bb2d 126 }
127
6b8ad965 128 function validation($usernew, $files) {
5d910388 129 global $CFG, $DB;
e8e0bb2d 130
131 $usernew = (object)$usernew;
f728d49b
PS
132 $usernew->username = trim($usernew->username);
133
134 $user = $DB->get_record('user', array('id'=>$usernew->id));
04f639a0 135 $err = array();
136
83022298 137 if (!empty($usernew->newpassword)) {
138 $errmsg = '';//prevent eclipse warning
139 if (!check_password_policy($usernew->newpassword, $errmsg)) {
140 $err['newpassword'] = $errmsg;
141 }
142 }
143
04f639a0 144 if (empty($usernew->username)) {
145 //might be only whitespace
146 $err['username'] = get_string('required');
147 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 148 //check new username does not exist
5d910388 149 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 150 $err['username'] = get_string('usernameexists');
151 }
f728d49b 152 //check allowed characters
e8e0bb2d 153 if ($usernew->username !== moodle_strtolower($usernew->username)) {
154 $err['username'] = get_string('usernamelowercase');
6b8ad965
PS
155 } else {
156 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
f4065447 157 $err['username'] = get_string('invalidusername');
e8e0bb2d 158 }
ad6226fb 159 }
160 }
e8e0bb2d 161
162 if (!$user or $user->email !== $usernew->email) {
163 if (!validate_email($usernew->email)) {
164 $err['email'] = get_string('invalidemail');
5d910388 165 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 166 $err['email'] = get_string('emailexists');
167 }
168 }
169
bb6d3d34 170 /// Next the customisable profile fields
c8969c22 171 $err += profile_validation($usernew, $files);
bb6d3d34 172
e8e0bb2d 173 if (count($err) == 0){
174 return true;
175 } else {
176 return $err;
177 }
ad6226fb 178 }
ad6226fb 179}
180
aa6c1ced 181