MDL-23184 PARAM_CLEANHTML used in messaging for now, adding some hints how to impleme...
[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
172dd12c 36function useredit_update_picture(&$usernew, $userform) {
a5d424df 37 global $CFG, $DB;
e88dd876 38 require_once("$CFG->libdir/gdlib.php");
9d85247d 39
edfd6a5e
PS
40 $fs = get_file_storage();
41 $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST);
42
14a6b7e1 43 if (isset($usernew->deletepicture) and $usernew->deletepicture) {
edfd6a5e 44 $fs->delete_area_files($context->id, 'user', 'icon'); // drop all areas
a5d424df 45 $DB->set_field('user', 'picture', 0, array('id'=>$usernew->id));
172dd12c 46
edfd6a5e
PS
47 } else if ($iconfile = $userform->save_temp_file('imagefile')) {
48 if (process_new_icon($context, 'user', 'icon', 0, $iconfile)) {
49 $DB->set_field('user', 'picture', 1, array('id'=>$usernew->id));
50 }
51 @unlink($iconfile);
14a6b7e1 52 }
53}
54
55function useredit_update_bounces($user, $usernew) {
56 if (!isset($usernew->email)) {
57 //locked field
58 return;
d8734783 59 }
a9457b54 60 if (!isset($user->email) || $user->email !== $usernew->email) {
14a6b7e1 61 set_bounce_count($usernew,true);
62 set_send_count($usernew,true);
63 }
64}
65
66function useredit_update_trackforums($user, $usernew) {
67 global $CFG;
68 if (!isset($usernew->trackforums)) {
69 //locked field
70 return;
71 }
a9457b54 72 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
14a6b7e1 73 require_once($CFG->dirroot.'/mod/forum/lib.php');
74 forum_tp_delete_read_records($usernew->id);
75 }
76}
77
c060fc6a 78function useredit_update_interests($user, $interests) {
79 tag_set('user', $user->id, $interests);
1e1c51a3 80}
81
8bdc9cac 82function useredit_shared_definition(&$mform, $editoroptions = null) {
d6ace123 83 global $CFG, $USER, $DB;
84
85 $user = $DB->get_record('user', array('id' => $USER->id));
05c38e2b 86 useredit_load_preferences($user, false);
14a6b7e1 87
88 $strrequired = get_string('required');
d8734783 89
c1658cc5 90 $nameordercheck = new object();
baacc970 91 $nameordercheck->firstname = 'a';
92 $nameordercheck->lastname = 'b';
93 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
94 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
95 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
96 } else {
97 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
98 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
99 }
100
d8734783 101 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
102 $mform->setType('firstname', PARAM_NOTAGS);
103
d8734783 104 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
105 $mform->setType('lastname', PARAM_NOTAGS);
106
d6ace123 107 // Do not show email field if change confirmation is pending
775f811a 108 if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
2b06294b 109 $notice = get_string('auth_emailchangepending', 'auth_email', $user);
d6ace123 110 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
2b06294b 111 . get_string('auth_emailchangecancel', 'auth_email') . '</a>';
d6ace123 112 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
113 } else {
114 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
115 $mform->addRule('email', $strrequired, 'required', null, 'client');
116 }
d8734783 117
118 $choices = array();
119 $choices['0'] = get_string('emaildisplayno');
120 $choices['1'] = get_string('emaildisplayyes');
121 $choices['2'] = get_string('emaildisplaycourse');
122 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
e61f8701 123 $mform->setDefault('maildisplay', 2);
d8734783 124
125 $choices = array();
126 $choices['0'] = get_string('emailenable');
127 $choices['1'] = get_string('emaildisable');
128 $mform->addElement('select', 'emailstop', get_string('emailactive'), $choices);
ecdf8d2e 129 $mform->setDefault('emailstop', 0);
d8734783 130
131 $choices = array();
132 $choices['0'] = get_string('textformat');
133 $choices['1'] = get_string('htmlformat');
134 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
c3ed4a5a 135 $mform->setDefault('mailformat', 1);
d8734783 136
137 if (!empty($CFG->allowusermailcharset)) {
ad6226fb 138 $choices = array();
d8734783 139 $charsets = get_list_of_charsets();
140 if (!empty($CFG->sitemailcharset)) {
141 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
142 } else {
9152fc99 143 $choices['0'] = get_string('site').' (UTF-8)';
ad6226fb 144 }
d8734783 145 $choices = array_merge($choices, $charsets);
c3ed4a5a 146 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
d8734783 147 }
148
149 $choices = array();
150 $choices['0'] = get_string('emaildigestoff');
151 $choices['1'] = get_string('emaildigestcomplete');
152 $choices['2'] = get_string('emaildigestsubjects');
153 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
154 $mform->setDefault('maildigest', 0);
d8734783 155
156 $choices = array();
157 $choices['1'] = get_string('autosubscribeyes');
158 $choices['0'] = get_string('autosubscribeno');
159 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
e61f8701 160 $mform->setDefault('autosubscribe', 1);
d8734783 161
162 if (!empty($CFG->forum_trackreadposts)) {
ad6226fb 163 $choices = array();
d8734783 164 $choices['0'] = get_string('trackforumsno');
165 $choices['1'] = get_string('trackforumsyes');
166 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
167 $mform->setDefault('trackforums', 0);
d8734783 168 }
c3ed4a5a 169
20e5da7d
PS
170 $editors = editors_get_enabled();
171 if (count($editors) > 1) {
ad6226fb 172 $choices = array();
d8734783 173 $choices['0'] = get_string('texteditor');
174 $choices['1'] = get_string('htmleditor');
175 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
176 $mform->setDefault('htmleditor', 1);
20e5da7d
PS
177 } else {
178 $mform->addElement('hidden', 'htmleditor');
179 $mform->setDefault('htmleditor', 1);
180 $mform->setType('htmleditor', PARAM_INT);
d8734783 181 }
c3ed4a5a 182
d8734783 183 if (empty($CFG->enableajax)) {
184 $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
d8734783 185 } else {
ad6226fb 186 $choices = array();
d8734783 187 $choices['0'] = get_string('ajaxno');
188 $choices['1'] = get_string('ajaxyes');
189 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
190 $mform->setDefault('ajax', 0);
d8734783 191 }
192
193 $choices = array();
194 $choices['0'] = get_string('screenreaderno');
195 $choices['1'] = get_string('screenreaderyes');
196 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
197 $mform->setDefault('screenreader', 0);
7851bd35 198 $mform->addHelpButton('screenreader', 'screenreaderuse');
d8734783 199
c5c0d2ff 200 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
d8734783 201 $mform->setType('city', PARAM_MULTILANG);
202 $mform->addRule('city', $strrequired, 'required', null, 'client');
203
204
0aa759b0 205 $choices = get_string_manager()->get_list_of_countries();
d8734783 206 $choices= array(''=>get_string('selectacountry').'...') + $choices;
207 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
208 $mform->addRule('country', $strrequired, 'required', null, 'client');
209 if (!empty($CFG->country)) {
210 $mform->setDefault('country', $CFG->country);
211 }
212
213 $choices = get_list_of_timezones();
214 $choices['99'] = get_string('serverlocaltime');
215 if ($CFG->forcetimezone != 99) {
216 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
217 } else {
218 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
219 $mform->setDefault('timezone', '99');
220 }
221
1f96e907 222 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
ca80f999 223 $mform->setDefault('lang', $CFG->lang);
d8734783 224
225 if (!empty($CFG->allowuserthemes)) {
ad6226fb 226 $choices = array();
d8734783 227 $choices[''] = get_string('default');
36798745
SH
228 $themes = get_list_of_themes();
229 foreach ($themes as $key=>$theme) {
230 if (empty($theme->hidefromselector)) {
231 $choices[$key] = $theme->name;
232 }
233 }
c3ed4a5a 234 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 235 }
ad6226fb 236
8bdc9cac
SH
237 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
238 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 239 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 240
d8734783 241 if (!empty($CFG->gdversion)) {
757e89d2 242 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 243
d8734783 244 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 245
d8734783 246 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
edfd6a5e 247 $mform->setDefault('deletepicture', 0);
ad6226fb 248
92b143f1 249 $mform->addElement('filepicker', 'imagefile', get_string('newpicture'));
27d1a27c 250 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 251
d8734783 252 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
253 $mform->setType('imagealt', PARAM_MULTILANG);
ad6226fb 254
d8734783 255 }
ad6226fb 256
c060fc6a 257 if (!empty($CFG->usetags)) {
1e1c51a3 258 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 259 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 260 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 261 }
d6ace123 262
d8734783 263 /// Moodle optional fields
c3ed4a5a 264 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 265
d8734783 266 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
267 $mform->setType('url', PARAM_URL);
ad6226fb 268
d8734783 269 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
270 $mform->setType('icq', PARAM_CLEAN);
ad6226fb 271
d8734783 272 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
273 $mform->setType('skype', PARAM_CLEAN);
ad6226fb 274
d8734783 275 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
276 $mform->setType('aim', PARAM_CLEAN);
ad6226fb 277
d8734783 278 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
279 $mform->setType('yahoo', PARAM_CLEAN);
ad6226fb 280
d8734783 281 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
282 $mform->setType('msn', PARAM_CLEAN);
ad6226fb 283
8b9cfac4 284 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
9e7cd813 285 $mform->setType('idnumber', PARAM_CLEAN);
ad6226fb 286
d8734783 287 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
288 $mform->setType('institution', PARAM_MULTILANG);
ad6226fb 289
d8734783 290 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
291 $mform->setType('department', PARAM_MULTILANG);
ad6226fb 292
d8734783 293 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
294 $mform->setType('phone1', PARAM_CLEAN);
ad6226fb 295
55ac3d6f 296 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
d8734783 297 $mform->setType('phone2', PARAM_CLEAN);
ad6226fb 298
d8734783 299 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
300 $mform->setType('address', PARAM_MULTILANG);
d6ace123 301
302
14a6b7e1 303}
ad6226fb 304
aa6c1ced 305