weekly release 2.7dev
[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 */
4e782b32 45function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
a5d424df 46 global $CFG, $DB;
e88dd876 47 require_once("$CFG->libdir/gdlib.php");
9d85247d 48
43731030 49 $context = context_user::instance($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;
4e782b32
RT
53 // Get file_storage to process files.
54 $fs = get_file_storage();
4125bdc1
SH
55 if (!empty($usernew->deletepicture)) {
56 // The user has chosen to delete the selected users picture
4d254790
PS
57 $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
58 $newpicture = 0;
59
4e782b32
RT
60 } else {
61 // Save newly uploaded file, this will avoid context mismatch for newly created users.
62 file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
63 if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
64 // Get file which was uploaded in draft area
65 foreach ($iconfiles as $file) {
66 if (!$file->is_directory()) {
67 break;
68 }
69 }
70 // Copy file to temporary location and the send it for processing icon
71 if ($iconfile = $file->copy_content_to_temp()) {
72 // There is a new image that has been uploaded
73 // Process the new image and set the user to make use of it.
74 // NOTE: Uploaded images always take over Gravatar
75 $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
76 // Delete temporary file
77 @unlink($iconfile);
78 // Remove uploaded file.
79 $fs->delete_area_files($context->id, 'user', 'newicon');
80 } else {
81 // Something went wrong while creating temp file.
82 // Remove uploaded file.
83 $fs->delete_area_files($context->id, 'user', 'newicon');
84 return false;
85 }
86 }
14a6b7e1 87 }
4125bdc1 88
4d254790
PS
89 if ($newpicture != $user->picture) {
90 $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
4125bdc1 91 return true;
4d254790
PS
92 } else {
93 return false;
4125bdc1 94 }
14a6b7e1 95}
96
97function useredit_update_bounces($user, $usernew) {
98 if (!isset($usernew->email)) {
99 //locked field
100 return;
d8734783 101 }
a9457b54 102 if (!isset($user->email) || $user->email !== $usernew->email) {
14a6b7e1 103 set_bounce_count($usernew,true);
104 set_send_count($usernew,true);
105 }
106}
107
108function useredit_update_trackforums($user, $usernew) {
109 global $CFG;
110 if (!isset($usernew->trackforums)) {
111 //locked field
112 return;
113 }
a9457b54 114 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
14a6b7e1 115 require_once($CFG->dirroot.'/mod/forum/lib.php');
116 forum_tp_delete_read_records($usernew->id);
117 }
118}
119
c060fc6a 120function useredit_update_interests($user, $interests) {
121 tag_set('user', $user->id, $interests);
1e1c51a3 122}
123
4e782b32 124function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) {
d6ace123 125 global $CFG, $USER, $DB;
126
127 $user = $DB->get_record('user', array('id' => $USER->id));
05c38e2b 128 useredit_load_preferences($user, false);
14a6b7e1 129
130 $strrequired = get_string('required');
d8734783 131
1cc3a9ae
AG
132 // Add the necessary names.
133 foreach (useredit_get_required_name_fields() as $fullname) {
a327f25e 134 $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"');
1cc3a9ae 135 $mform->addRule($fullname, $strrequired, 'required', null, 'client');
a327f25e
AG
136 $mform->setType($fullname, PARAM_NOTAGS);
137 }
d8734783 138
1cc3a9ae
AG
139 $enabledusernamefields = useredit_get_enabled_name_fields();
140 // Add the enabled additional name fields.
141 foreach ($enabledusernamefields as $addname) {
a327f25e
AG
142 $mform->addElement('text', $addname, get_string($addname), 'maxlength="100" size="30"');
143 $mform->setType($addname, PARAM_NOTAGS);
144 }
d8734783 145
d6ace123 146 // Do not show email field if change confirmation is pending
775f811a 147 if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
c6a074f8 148 $notice = get_string('emailchangepending', 'auth', $user);
d6ace123 149 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
c6a074f8 150 . get_string('emailchangecancel', 'auth') . '</a>';
d6ace123 151 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
152 } else {
153 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
154 $mform->addRule('email', $strrequired, 'required', null, 'client');
5df15f25 155 $mform->setType('email', PARAM_EMAIL);
d6ace123 156 }
d8734783 157
158 $choices = array();
159 $choices['0'] = get_string('emaildisplayno');
160 $choices['1'] = get_string('emaildisplayyes');
161 $choices['2'] = get_string('emaildisplaycourse');
162 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
e61f8701 163 $mform->setDefault('maildisplay', 2);
d8734783 164
d8734783 165 $choices = array();
166 $choices['0'] = get_string('textformat');
167 $choices['1'] = get_string('htmlformat');
168 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
c3ed4a5a 169 $mform->setDefault('mailformat', 1);
d8734783 170
171 if (!empty($CFG->allowusermailcharset)) {
ad6226fb 172 $choices = array();
d8734783 173 $charsets = get_list_of_charsets();
174 if (!empty($CFG->sitemailcharset)) {
175 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
176 } else {
9152fc99 177 $choices['0'] = get_string('site').' (UTF-8)';
ad6226fb 178 }
d8734783 179 $choices = array_merge($choices, $charsets);
c3ed4a5a 180 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
d8734783 181 }
182
183 $choices = array();
184 $choices['0'] = get_string('emaildigestoff');
185 $choices['1'] = get_string('emaildigestcomplete');
186 $choices['2'] = get_string('emaildigestsubjects');
187 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
188 $mform->setDefault('maildigest', 0);
3d4cf548 189 $mform->addHelpButton('maildigest', 'emaildigest');
d8734783 190
191 $choices = array();
192 $choices['1'] = get_string('autosubscribeyes');
193 $choices['0'] = get_string('autosubscribeno');
194 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
e61f8701 195 $mform->setDefault('autosubscribe', 1);
d8734783 196
197 if (!empty($CFG->forum_trackreadposts)) {
ad6226fb 198 $choices = array();
d8734783 199 $choices['0'] = get_string('trackforumsno');
200 $choices['1'] = get_string('trackforumsyes');
201 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
202 $mform->setDefault('trackforums', 0);
d8734783 203 }
c3ed4a5a 204
20e5da7d
PS
205 $editors = editors_get_enabled();
206 if (count($editors) > 1) {
3d27180e
DW
207 $choices = array('' => get_string('defaulteditor'));
208 $firsteditor = '';
209 foreach (array_keys($editors) as $editor) {
210 if (!$firsteditor) {
211 $firsteditor = $editor;
212 }
213 $choices[$editor] = get_string('pluginname', 'editor_' . $editor);
214 }
215 $mform->addElement('select', 'preference_htmleditor', get_string('textediting'), $choices);
216 $mform->setDefault('preference_htmleditor', '');
20e5da7d 217 } else {
3d27180e
DW
218 // Empty string means use the first chosen text editor.
219 $mform->addElement('hidden', 'preference_htmleditor');
220 $mform->setDefault('preference_htmleditor', '');
221 $mform->setType('preference_htmleditor', PARAM_PLUGIN);
d8734783 222 }
c3ed4a5a 223
c5c0d2ff 224 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
071e68f9 225 $mform->setType('city', PARAM_TEXT);
fa7f750c
PS
226 if (!empty($CFG->defaultcity)) {
227 $mform->setDefault('city', $CFG->defaultcity);
228 }
d8734783 229
0aa759b0 230 $choices = get_string_manager()->get_list_of_countries();
d8734783 231 $choices= array(''=>get_string('selectacountry').'...') + $choices;
232 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
d8734783 233 if (!empty($CFG->country)) {
234 $mform->setDefault('country', $CFG->country);
235 }
236
237 $choices = get_list_of_timezones();
238 $choices['99'] = get_string('serverlocaltime');
239 if ($CFG->forcetimezone != 99) {
240 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
241 } else {
242 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
243 $mform->setDefault('timezone', '99');
244 }
245
1f96e907 246 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
ca80f999 247 $mform->setDefault('lang', $CFG->lang);
d8734783 248
2f00e1b2 249 // Multi-Calendar Support - see MDL-18375.
df5d27d8
MN
250 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
251 // We do not want to show this option unless there is more than one calendar type to display.
252 if (count($calendartypes) > 1) {
253 $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes);
8bf0f207 254 $mform->setDefault('calendartype', $CFG->calendartype);
df5d27d8 255 }
6dd59aab 256
d8734783 257 if (!empty($CFG->allowuserthemes)) {
ad6226fb 258 $choices = array();
d8734783 259 $choices[''] = get_string('default');
36798745
SH
260 $themes = get_list_of_themes();
261 foreach ($themes as $key=>$theme) {
262 if (empty($theme->hidefromselector)) {
d609d962 263 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
36798745
SH
264 }
265 }
c3ed4a5a 266 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 267 }
ad6226fb 268
8bdc9cac
SH
269 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
270 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 271 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 272
689096bc 273 if (empty($USER->newadminuser)) {
757e89d2 274 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 275
4125bdc1 276 if (!empty($CFG->enablegravatar)) {
ed9e0cb6 277 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
4125bdc1
SH
278 }
279
d8734783 280 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 281
d8734783 282 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
edfd6a5e 283 $mform->setDefault('deletepicture', 0);
ad6226fb 284
4e782b32 285 $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
27d1a27c 286 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 287
d8734783 288 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
071e68f9 289 $mform->setType('imagealt', PARAM_TEXT);
ad6226fb 290
d8734783 291 }
ad6226fb 292
1cc3a9ae
AG
293 // Display user name fields that are not currenlty enabled here if there are any.
294 $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields);
b63f542d 295 if (count($disabledusernamefields) > 0) {
a327f25e 296 $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
1cc3a9ae
AG
297 foreach ($disabledusernamefields as $allname) {
298 $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
299 $mform->setType($allname, PARAM_NOTAGS);
a327f25e 300 }
a327f25e
AG
301 }
302
de2d81fa 303 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
1e1c51a3 304 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 305 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 306 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 307 }
d6ace123 308
d8734783 309 /// Moodle optional fields
c3ed4a5a 310 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 311
d8734783 312 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
313 $mform->setType('url', PARAM_URL);
ad6226fb 314
d8734783 315 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
93de0ac2 316 $mform->setType('icq', PARAM_NOTAGS);
ad6226fb 317
d8734783 318 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
93de0ac2 319 $mform->setType('skype', PARAM_NOTAGS);
ad6226fb 320
d8734783 321 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
93de0ac2 322 $mform->setType('aim', PARAM_NOTAGS);
ad6226fb 323
d8734783 324 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
93de0ac2 325 $mform->setType('yahoo', PARAM_NOTAGS);
ad6226fb 326
d8734783 327 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
93de0ac2 328 $mform->setType('msn', PARAM_NOTAGS);
ad6226fb 329
8b9cfac4 330 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 331 $mform->setType('idnumber', PARAM_NOTAGS);
ad6226fb 332
a8fd33b0 333 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
071e68f9 334 $mform->setType('institution', PARAM_TEXT);
ad6226fb 335
a8fd33b0 336 $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
071e68f9 337 $mform->setType('department', PARAM_TEXT);
ad6226fb 338
d8734783 339 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
93de0ac2 340 $mform->setType('phone1', PARAM_NOTAGS);
ad6226fb 341
55ac3d6f 342 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 343 $mform->setType('phone2', PARAM_NOTAGS);
ad6226fb 344
a8fd33b0 345 $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
071e68f9 346 $mform->setType('address', PARAM_TEXT);
d6ace123 347
348
14a6b7e1 349}
ad6226fb 350
1cc3a9ae
AG
351/**
352 * Return required user name fields for forms.
353 *
354 * @return array required user name fields in order according to settings.
355 */
356function useredit_get_required_name_fields() {
357 global $CFG;
358
359 // Get the name display format.
360 $nameformat = $CFG->fullnamedisplay;
361
362 // Names that are required fields on user forms.
363 $necessarynames = array('firstname', 'lastname');
364 $languageformat = get_string('fullnamedisplay');
365
366 // Check that the language string and the $nameformat contain the necessary names.
367 foreach ($necessarynames as $necessaryname) {
368 $pattern = "/$necessaryname\b/";
369 if (!preg_match($pattern, $languageformat)) {
370 // If the language string has been altered then fall back on the below order.
371 $languageformat = 'firstname lastname';
372 }
373 if (!preg_match($pattern, $nameformat)) {
374 // If the nameformat doesn't contain the necessary name fields then use the languageformat.
375 $nameformat = $languageformat;
376 }
377 }
378
379 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
380 $necessarynames = order_in_string($necessarynames, $nameformat);
381 return $necessarynames;
382}
383
384/**
385 * Gets enabled (from fullnameformate setting) user name fields in appropriate order.
386 *
387 * @return array Enabled user name fields.
388 */
389function useredit_get_enabled_name_fields() {
390 global $CFG;
391
392 // Get all of the other name fields which are not ranked as necessary.
393 $additionalusernamefields = array_diff(get_all_user_name_fields(), array('firstname', 'lastname'));
394 // Find out which additional name fields are actually being used from the fullnamedisplay setting.
395 $enabledadditionalusernames = array();
396 foreach ($additionalusernamefields as $enabledname) {
397 if (strpos($CFG->fullnamedisplay, $enabledname) !== false) {
398 $enabledadditionalusernames[] = $enabledname;
399 }
400 }
401
402 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
403 $enabledadditionalusernames = order_in_string($enabledadditionalusernames, $CFG->fullnamedisplay);
404 return $enabledadditionalusernames;
405}
406
407/**
408 * Gets user name fields not enabled from the setting fullnamedisplay.
409 *
410 * @param array $enabledadditionalusernames Current enabled additional user name fields.
411 * @return array Disabled user name fields.
412 */
413function useredit_get_disabled_name_fields($enabledadditionalusernames = null) {
414 // If we don't have enabled additional user name information then go and fetch it (try to avoid).
415 if (!isset($enabledadditionalusernames)) {
416 $enabledadditionalusernames = useredit_get_enabled_name_fields();
417 }
418
419 // These are the additional fields that are not currently enabled.
420 $nonusednamefields = array_diff(get_all_user_name_fields(),
421 array_merge(array('firstname', 'lastname'), $enabledadditionalusernames));
422 return $nonusednamefields;
423}
424
aa6c1ced 425