on-demand release 2.3dev
[moodle.git] / user / editlib.php
CommitLineData
8bdc9cac 1<?php
ad6226fb 2
05c38e2b 3function cancel_email_update($userid) {
4 unset_user_preference('newemail', $userid);
5 unset_user_preference('newemailkey', $userid);
6 unset_user_preference('newemailattemptsleft', $userid);
7}
8
9function useredit_load_preferences(&$user, $reload=true) {
10 global $USER;
14a6b7e1 11
05c38e2b 12 if (!empty($user->id)) {
13 if ($reload and $USER->id == $user->id) {
14 // reload preferences in case it was changed in other session
15 unset($USER->preference);
16 }
aa6c1ced 17
05c38e2b 18 if ($preferences = get_user_preferences(null, null, $user->id)) {
19 foreach($preferences as $name=>$value) {
20 $user->{'preference_'.$name} = $value;
21 }
14a6b7e1 22 }
23 }
24}
25
26function useredit_update_user_preference($usernew) {
27 $ua = (array)$usernew;
28 foreach($ua as $key=>$value) {
29 if (strpos($key, 'preference_') === 0) {
30 $name = substr($key, strlen('preference_'));
31 set_user_preference($name, $value, $usernew->id);
32 }
33 }
34}
35
4125bdc1
SH
36/**
37 * Updates the provided users profile picture based upon the expected fields
38 * returned from the edit or edit_advanced forms.
39 *
40 * @global moodle_database $DB
41 * @param stdClass $usernew An object that contains some information about the user being updated
42 * @param moodleform $userform The form that was submitted to edit the form
43 * @return bool True if the user was updated, false if it stayed the same.
44 */
45function useredit_update_picture(stdClass $usernew, moodleform $userform) {
a5d424df 46 global $CFG, $DB;
e88dd876 47 require_once("$CFG->libdir/gdlib.php");
9d85247d 48
edfd6a5e 49 $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST);
4d254790
PS
50 $user = $DB->get_record('user', array('id'=>$usernew->id), 'id, picture', MUST_EXIST);
51
52 $newpicture = $user->picture;
53
4125bdc1
SH
54 if (!empty($usernew->deletepicture)) {
55 // The user has chosen to delete the selected users picture
56 $fs = get_file_storage();
4d254790
PS
57 $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
58 $newpicture = 0;
59
edfd6a5e 60 } else if ($iconfile = $userform->save_temp_file('imagefile')) {
4125bdc1
SH
61 // There is a new image that has been uploaded
62 // Process the new image and set the user to make use of it.
4d254790
PS
63 // NOTE: Uploaded images always take over Gravatar
64 $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
4125bdc1 65 // Delete the file that has now been processed
edfd6a5e 66 @unlink($iconfile);
14a6b7e1 67 }
4125bdc1 68
4d254790
PS
69 if ($newpicture != $user->picture) {
70 $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
4125bdc1 71 return true;
4d254790
PS
72 } else {
73 return false;
4125bdc1 74 }
14a6b7e1 75}
76
77function useredit_update_bounces($user, $usernew) {
78 if (!isset($usernew->email)) {
79 //locked field
80 return;
d8734783 81 }
a9457b54 82 if (!isset($user->email) || $user->email !== $usernew->email) {
14a6b7e1 83 set_bounce_count($usernew,true);
84 set_send_count($usernew,true);
85 }
86}
87
88function useredit_update_trackforums($user, $usernew) {
89 global $CFG;
90 if (!isset($usernew->trackforums)) {
91 //locked field
92 return;
93 }
a9457b54 94 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
14a6b7e1 95 require_once($CFG->dirroot.'/mod/forum/lib.php');
96 forum_tp_delete_read_records($usernew->id);
97 }
98}
99
c060fc6a 100function useredit_update_interests($user, $interests) {
101 tag_set('user', $user->id, $interests);
1e1c51a3 102}
103
8bdc9cac 104function useredit_shared_definition(&$mform, $editoroptions = null) {
d6ace123 105 global $CFG, $USER, $DB;
106
107 $user = $DB->get_record('user', array('id' => $USER->id));
05c38e2b 108 useredit_load_preferences($user, false);
14a6b7e1 109
110 $strrequired = get_string('required');
d8734783 111
bf718f50 112 $nameordercheck = new stdClass();
baacc970 113 $nameordercheck->firstname = 'a';
114 $nameordercheck->lastname = 'b';
115 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
116 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
117 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
118 } else {
119 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
120 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
121 }
122
d8734783 123 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
124 $mform->setType('firstname', PARAM_NOTAGS);
125
d8734783 126 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
127 $mform->setType('lastname', PARAM_NOTAGS);
128
d6ace123 129 // Do not show email field if change confirmation is pending
775f811a 130 if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
c6a074f8 131 $notice = get_string('emailchangepending', 'auth', $user);
d6ace123 132 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
c6a074f8 133 . get_string('emailchangecancel', 'auth') . '</a>';
d6ace123 134 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
135 } else {
136 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
137 $mform->addRule('email', $strrequired, 'required', null, 'client');
138 }
d8734783 139
140 $choices = array();
141 $choices['0'] = get_string('emaildisplayno');
142 $choices['1'] = get_string('emaildisplayyes');
143 $choices['2'] = get_string('emaildisplaycourse');
144 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
e61f8701 145 $mform->setDefault('maildisplay', 2);
d8734783 146
d8734783 147 $choices = array();
148 $choices['0'] = get_string('textformat');
149 $choices['1'] = get_string('htmlformat');
150 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
c3ed4a5a 151 $mform->setDefault('mailformat', 1);
d8734783 152
153 if (!empty($CFG->allowusermailcharset)) {
ad6226fb 154 $choices = array();
d8734783 155 $charsets = get_list_of_charsets();
156 if (!empty($CFG->sitemailcharset)) {
157 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
158 } else {
9152fc99 159 $choices['0'] = get_string('site').' (UTF-8)';
ad6226fb 160 }
d8734783 161 $choices = array_merge($choices, $charsets);
c3ed4a5a 162 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
d8734783 163 }
164
165 $choices = array();
166 $choices['0'] = get_string('emaildigestoff');
167 $choices['1'] = get_string('emaildigestcomplete');
168 $choices['2'] = get_string('emaildigestsubjects');
169 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
170 $mform->setDefault('maildigest', 0);
d8734783 171
172 $choices = array();
173 $choices['1'] = get_string('autosubscribeyes');
174 $choices['0'] = get_string('autosubscribeno');
175 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
e61f8701 176 $mform->setDefault('autosubscribe', 1);
d8734783 177
178 if (!empty($CFG->forum_trackreadposts)) {
ad6226fb 179 $choices = array();
d8734783 180 $choices['0'] = get_string('trackforumsno');
181 $choices['1'] = get_string('trackforumsyes');
182 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
183 $mform->setDefault('trackforums', 0);
d8734783 184 }
c3ed4a5a 185
20e5da7d
PS
186 $editors = editors_get_enabled();
187 if (count($editors) > 1) {
ad6226fb 188 $choices = array();
d8734783 189 $choices['0'] = get_string('texteditor');
190 $choices['1'] = get_string('htmleditor');
191 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
192 $mform->setDefault('htmleditor', 1);
20e5da7d
PS
193 } else {
194 $mform->addElement('hidden', 'htmleditor');
195 $mform->setDefault('htmleditor', 1);
196 $mform->setType('htmleditor', PARAM_INT);
d8734783 197 }
c3ed4a5a 198
d8734783 199 if (empty($CFG->enableajax)) {
200 $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
d8734783 201 } else {
ad6226fb 202 $choices = array();
d8734783 203 $choices['0'] = get_string('ajaxno');
204 $choices['1'] = get_string('ajaxyes');
205 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
789df20f 206 $mform->setDefault('ajax', 1);
d8734783 207 }
208
209 $choices = array();
210 $choices['0'] = get_string('screenreaderno');
211 $choices['1'] = get_string('screenreaderyes');
212 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
213 $mform->setDefault('screenreader', 0);
7851bd35 214 $mform->addHelpButton('screenreader', 'screenreaderuse');
d8734783 215
c5c0d2ff 216 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
d8734783 217 $mform->setType('city', PARAM_MULTILANG);
218 $mform->addRule('city', $strrequired, 'required', null, 'client');
fa7f750c
PS
219 if (!empty($CFG->defaultcity)) {
220 $mform->setDefault('city', $CFG->defaultcity);
221 }
d8734783 222
0aa759b0 223 $choices = get_string_manager()->get_list_of_countries();
d8734783 224 $choices= array(''=>get_string('selectacountry').'...') + $choices;
225 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
226 $mform->addRule('country', $strrequired, 'required', null, 'client');
227 if (!empty($CFG->country)) {
228 $mform->setDefault('country', $CFG->country);
229 }
230
231 $choices = get_list_of_timezones();
232 $choices['99'] = get_string('serverlocaltime');
233 if ($CFG->forcetimezone != 99) {
234 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
235 } else {
236 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
237 $mform->setDefault('timezone', '99');
238 }
239
1f96e907 240 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
ca80f999 241 $mform->setDefault('lang', $CFG->lang);
d8734783 242
243 if (!empty($CFG->allowuserthemes)) {
ad6226fb 244 $choices = array();
d8734783 245 $choices[''] = get_string('default');
36798745
SH
246 $themes = get_list_of_themes();
247 foreach ($themes as $key=>$theme) {
248 if (empty($theme->hidefromselector)) {
d609d962 249 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
36798745
SH
250 }
251 }
c3ed4a5a 252 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 253 }
ad6226fb 254
8bdc9cac
SH
255 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
256 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 257 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 258
de2d81fa 259 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
757e89d2 260 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 261
4125bdc1 262 if (!empty($CFG->enablegravatar)) {
ed9e0cb6 263 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
4125bdc1
SH
264 }
265
d8734783 266 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 267
d8734783 268 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
edfd6a5e 269 $mform->setDefault('deletepicture', 0);
ad6226fb 270
513aed3c 271 $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
27d1a27c 272 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 273
d8734783 274 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
275 $mform->setType('imagealt', PARAM_MULTILANG);
ad6226fb 276
d8734783 277 }
ad6226fb 278
de2d81fa 279 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
1e1c51a3 280 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 281 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 282 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 283 }
d6ace123 284
d8734783 285 /// Moodle optional fields
c3ed4a5a 286 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 287
d8734783 288 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
289 $mform->setType('url', PARAM_URL);
ad6226fb 290
d8734783 291 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
93de0ac2 292 $mform->setType('icq', PARAM_NOTAGS);
ad6226fb 293
d8734783 294 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
93de0ac2 295 $mform->setType('skype', PARAM_NOTAGS);
ad6226fb 296
d8734783 297 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
93de0ac2 298 $mform->setType('aim', PARAM_NOTAGS);
ad6226fb 299
d8734783 300 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
93de0ac2 301 $mform->setType('yahoo', PARAM_NOTAGS);
ad6226fb 302
d8734783 303 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
93de0ac2 304 $mform->setType('msn', PARAM_NOTAGS);
ad6226fb 305
8b9cfac4 306 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 307 $mform->setType('idnumber', PARAM_NOTAGS);
ad6226fb 308
d8734783 309 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
310 $mform->setType('institution', PARAM_MULTILANG);
ad6226fb 311
d8734783 312 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
313 $mform->setType('department', PARAM_MULTILANG);
ad6226fb 314
d8734783 315 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
93de0ac2 316 $mform->setType('phone1', PARAM_NOTAGS);
ad6226fb 317
55ac3d6f 318 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 319 $mform->setType('phone2', PARAM_NOTAGS);
ad6226fb 320
d8734783 321 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
322 $mform->setType('address', PARAM_MULTILANG);
d6ace123 323
324
14a6b7e1 325}
ad6226fb 326
aa6c1ced 327