MDL-4908 Forum: Add help for profile maildigest setting
[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
a327f25e
AG
132 $nameformat = $CFG->fullnamedisplay;
133 if ($nameformat == 'language') {
134 $nameformat = get_string('fullnamedisplay');
baacc970 135 }
136
a327f25e
AG
137 $necessarynames = array('firstname', 'lastname');
138 $enablednames = array_diff(get_all_user_name_fields(), $necessarynames);
139 // Get a list of all of the enabled names.
140 $enabledadditionalusernames = array();
141 foreach ($enablednames as $enabledname) {
142 if (strpos($CFG->fullnamedisplay, $enabledname) !== false) {
143 $enabledadditionalusernames[] = $enabledname;
144 }
145 }
146
147 $combinednames = array_merge($necessarynames, $enabledadditionalusernames);
148 $requirednames = order_in_string($combinednames, $nameformat);
149 foreach ($necessarynames as $necessaryname) {
150 if (!in_array($necessaryname, $requirednames)) {
151 $requirednames = order_in_string($combinednames, get_string('fullnamedisplay'));
152 }
153 }
154 foreach ($requirednames as $fullname) {
155 $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"');
156 $mform->setType($fullname, PARAM_NOTAGS);
157 }
d8734783 158
a327f25e 159 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
d8734783 160 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
a327f25e
AG
161
162 $morenames = array_diff($enabledadditionalusernames, $requirednames);
163 foreach ($morenames as $addname) {
164 $mform->addElement('text', $addname, get_string($addname), 'maxlength="100" size="30"');
165 $mform->setType($addname, PARAM_NOTAGS);
166 }
d8734783 167
d6ace123 168 // Do not show email field if change confirmation is pending
775f811a 169 if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
c6a074f8 170 $notice = get_string('emailchangepending', 'auth', $user);
d6ace123 171 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
c6a074f8 172 . get_string('emailchangecancel', 'auth') . '</a>';
d6ace123 173 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
174 } else {
175 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
176 $mform->addRule('email', $strrequired, 'required', null, 'client');
5df15f25 177 $mform->setType('email', PARAM_EMAIL);
d6ace123 178 }
d8734783 179
180 $choices = array();
181 $choices['0'] = get_string('emaildisplayno');
182 $choices['1'] = get_string('emaildisplayyes');
183 $choices['2'] = get_string('emaildisplaycourse');
184 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
e61f8701 185 $mform->setDefault('maildisplay', 2);
d8734783 186
d8734783 187 $choices = array();
188 $choices['0'] = get_string('textformat');
189 $choices['1'] = get_string('htmlformat');
190 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
c3ed4a5a 191 $mform->setDefault('mailformat', 1);
d8734783 192
193 if (!empty($CFG->allowusermailcharset)) {
ad6226fb 194 $choices = array();
d8734783 195 $charsets = get_list_of_charsets();
196 if (!empty($CFG->sitemailcharset)) {
197 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
198 } else {
9152fc99 199 $choices['0'] = get_string('site').' (UTF-8)';
ad6226fb 200 }
d8734783 201 $choices = array_merge($choices, $charsets);
c3ed4a5a 202 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
d8734783 203 }
204
205 $choices = array();
206 $choices['0'] = get_string('emaildigestoff');
207 $choices['1'] = get_string('emaildigestcomplete');
208 $choices['2'] = get_string('emaildigestsubjects');
209 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
210 $mform->setDefault('maildigest', 0);
3d4cf548 211 $mform->addHelpButton('maildigest', 'emaildigest');
d8734783 212
213 $choices = array();
214 $choices['1'] = get_string('autosubscribeyes');
215 $choices['0'] = get_string('autosubscribeno');
216 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
e61f8701 217 $mform->setDefault('autosubscribe', 1);
d8734783 218
219 if (!empty($CFG->forum_trackreadposts)) {
ad6226fb 220 $choices = array();
d8734783 221 $choices['0'] = get_string('trackforumsno');
222 $choices['1'] = get_string('trackforumsyes');
223 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
224 $mform->setDefault('trackforums', 0);
d8734783 225 }
c3ed4a5a 226
20e5da7d
PS
227 $editors = editors_get_enabled();
228 if (count($editors) > 1) {
ad6226fb 229 $choices = array();
d8734783 230 $choices['0'] = get_string('texteditor');
231 $choices['1'] = get_string('htmleditor');
232 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
233 $mform->setDefault('htmleditor', 1);
20e5da7d
PS
234 } else {
235 $mform->addElement('hidden', 'htmleditor');
236 $mform->setDefault('htmleditor', 1);
237 $mform->setType('htmleditor', PARAM_INT);
d8734783 238 }
c3ed4a5a 239
c5c0d2ff 240 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
071e68f9 241 $mform->setType('city', PARAM_TEXT);
fa7f750c
PS
242 if (!empty($CFG->defaultcity)) {
243 $mform->setDefault('city', $CFG->defaultcity);
244 }
d8734783 245
0aa759b0 246 $choices = get_string_manager()->get_list_of_countries();
d8734783 247 $choices= array(''=>get_string('selectacountry').'...') + $choices;
248 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
d8734783 249 if (!empty($CFG->country)) {
250 $mform->setDefault('country', $CFG->country);
251 }
252
253 $choices = get_list_of_timezones();
254 $choices['99'] = get_string('serverlocaltime');
255 if ($CFG->forcetimezone != 99) {
256 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
257 } else {
258 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
259 $mform->setDefault('timezone', '99');
260 }
261
1f96e907 262 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
ca80f999 263 $mform->setDefault('lang', $CFG->lang);
d8734783 264
265 if (!empty($CFG->allowuserthemes)) {
ad6226fb 266 $choices = array();
d8734783 267 $choices[''] = get_string('default');
36798745
SH
268 $themes = get_list_of_themes();
269 foreach ($themes as $key=>$theme) {
270 if (empty($theme->hidefromselector)) {
d609d962 271 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
36798745
SH
272 }
273 }
c3ed4a5a 274 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 275 }
ad6226fb 276
8bdc9cac
SH
277 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
278 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 279 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 280
689096bc 281 if (empty($USER->newadminuser)) {
757e89d2 282 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 283
4125bdc1 284 if (!empty($CFG->enablegravatar)) {
ed9e0cb6 285 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
4125bdc1
SH
286 }
287
d8734783 288 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 289
d8734783 290 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
edfd6a5e 291 $mform->setDefault('deletepicture', 0);
ad6226fb 292
4e782b32 293 $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
27d1a27c 294 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 295
d8734783 296 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
071e68f9 297 $mform->setType('imagealt', PARAM_TEXT);
ad6226fb 298
d8734783 299 }
ad6226fb 300
a327f25e
AG
301 $alladditionalnames = array_diff(get_all_user_name_fields(), $necessarynames);
302 if (count($enabledadditionalusernames) < count($alladditionalnames)) {
303 $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
304 foreach ($alladditionalnames as $allname) {
305 if (!in_array($allname, $enabledadditionalusernames)) {
306 $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
307 $mform->setType($allname, PARAM_NOTAGS);
308 }
309 }
310
311 }
312
de2d81fa 313 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
1e1c51a3 314 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 315 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 316 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 317 }
d6ace123 318
d8734783 319 /// Moodle optional fields
c3ed4a5a 320 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 321
d8734783 322 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
323 $mform->setType('url', PARAM_URL);
ad6226fb 324
d8734783 325 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
93de0ac2 326 $mform->setType('icq', PARAM_NOTAGS);
ad6226fb 327
d8734783 328 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
93de0ac2 329 $mform->setType('skype', PARAM_NOTAGS);
ad6226fb 330
d8734783 331 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
93de0ac2 332 $mform->setType('aim', PARAM_NOTAGS);
ad6226fb 333
d8734783 334 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
93de0ac2 335 $mform->setType('yahoo', PARAM_NOTAGS);
ad6226fb 336
d8734783 337 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
93de0ac2 338 $mform->setType('msn', PARAM_NOTAGS);
ad6226fb 339
8b9cfac4 340 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 341 $mform->setType('idnumber', PARAM_NOTAGS);
ad6226fb 342
d8734783 343 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
071e68f9 344 $mform->setType('institution', PARAM_TEXT);
ad6226fb 345
d8734783 346 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
071e68f9 347 $mform->setType('department', PARAM_TEXT);
ad6226fb 348
d8734783 349 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
93de0ac2 350 $mform->setType('phone1', PARAM_NOTAGS);
ad6226fb 351
55ac3d6f 352 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 353 $mform->setType('phone2', PARAM_NOTAGS);
ad6226fb 354
d8734783 355 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
071e68f9 356 $mform->setType('address', PARAM_TEXT);
d6ace123 357
358
14a6b7e1 359}
ad6226fb 360
aa6c1ced 361