Updated the HEAD build version to 20101203
[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
bf718f50 90 $nameordercheck = new stdClass();
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
d8734783 125 $choices = array();
126 $choices['0'] = get_string('textformat');
127 $choices['1'] = get_string('htmlformat');
128 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
c3ed4a5a 129 $mform->setDefault('mailformat', 1);
d8734783 130
131 if (!empty($CFG->allowusermailcharset)) {
ad6226fb 132 $choices = array();
d8734783 133 $charsets = get_list_of_charsets();
134 if (!empty($CFG->sitemailcharset)) {
135 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
136 } else {
9152fc99 137 $choices['0'] = get_string('site').' (UTF-8)';
ad6226fb 138 }
d8734783 139 $choices = array_merge($choices, $charsets);
c3ed4a5a 140 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
d8734783 141 }
142
143 $choices = array();
144 $choices['0'] = get_string('emaildigestoff');
145 $choices['1'] = get_string('emaildigestcomplete');
146 $choices['2'] = get_string('emaildigestsubjects');
147 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
148 $mform->setDefault('maildigest', 0);
d8734783 149
150 $choices = array();
151 $choices['1'] = get_string('autosubscribeyes');
152 $choices['0'] = get_string('autosubscribeno');
153 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
e61f8701 154 $mform->setDefault('autosubscribe', 1);
d8734783 155
156 if (!empty($CFG->forum_trackreadposts)) {
ad6226fb 157 $choices = array();
d8734783 158 $choices['0'] = get_string('trackforumsno');
159 $choices['1'] = get_string('trackforumsyes');
160 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
161 $mform->setDefault('trackforums', 0);
d8734783 162 }
c3ed4a5a 163
20e5da7d
PS
164 $editors = editors_get_enabled();
165 if (count($editors) > 1) {
ad6226fb 166 $choices = array();
d8734783 167 $choices['0'] = get_string('texteditor');
168 $choices['1'] = get_string('htmleditor');
169 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
170 $mform->setDefault('htmleditor', 1);
20e5da7d
PS
171 } else {
172 $mform->addElement('hidden', 'htmleditor');
173 $mform->setDefault('htmleditor', 1);
174 $mform->setType('htmleditor', PARAM_INT);
d8734783 175 }
c3ed4a5a 176
d8734783 177 if (empty($CFG->enableajax)) {
178 $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
d8734783 179 } else {
ad6226fb 180 $choices = array();
d8734783 181 $choices['0'] = get_string('ajaxno');
182 $choices['1'] = get_string('ajaxyes');
183 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
184 $mform->setDefault('ajax', 0);
d8734783 185 }
186
187 $choices = array();
188 $choices['0'] = get_string('screenreaderno');
189 $choices['1'] = get_string('screenreaderyes');
190 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
191 $mform->setDefault('screenreader', 0);
7851bd35 192 $mform->addHelpButton('screenreader', 'screenreaderuse');
d8734783 193
c5c0d2ff 194 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
d8734783 195 $mform->setType('city', PARAM_MULTILANG);
196 $mform->addRule('city', $strrequired, 'required', null, 'client');
197
198
0aa759b0 199 $choices = get_string_manager()->get_list_of_countries();
d8734783 200 $choices= array(''=>get_string('selectacountry').'...') + $choices;
201 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
202 $mform->addRule('country', $strrequired, 'required', null, 'client');
203 if (!empty($CFG->country)) {
204 $mform->setDefault('country', $CFG->country);
205 }
206
207 $choices = get_list_of_timezones();
208 $choices['99'] = get_string('serverlocaltime');
209 if ($CFG->forcetimezone != 99) {
210 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
211 } else {
212 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
213 $mform->setDefault('timezone', '99');
214 }
215
1f96e907 216 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
ca80f999 217 $mform->setDefault('lang', $CFG->lang);
d8734783 218
219 if (!empty($CFG->allowuserthemes)) {
ad6226fb 220 $choices = array();
d8734783 221 $choices[''] = get_string('default');
36798745
SH
222 $themes = get_list_of_themes();
223 foreach ($themes as $key=>$theme) {
224 if (empty($theme->hidefromselector)) {
225 $choices[$key] = $theme->name;
226 }
227 }
c3ed4a5a 228 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 229 }
ad6226fb 230
8bdc9cac
SH
231 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
232 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 233 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 234
d8734783 235 if (!empty($CFG->gdversion)) {
757e89d2 236 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 237
d8734783 238 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 239
d8734783 240 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
edfd6a5e 241 $mform->setDefault('deletepicture', 0);
ad6226fb 242
513aed3c 243 $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
27d1a27c 244 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 245
d8734783 246 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
247 $mform->setType('imagealt', PARAM_MULTILANG);
ad6226fb 248
d8734783 249 }
ad6226fb 250
c060fc6a 251 if (!empty($CFG->usetags)) {
1e1c51a3 252 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 253 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 254 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 255 }
d6ace123 256
d8734783 257 /// Moodle optional fields
c3ed4a5a 258 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 259
d8734783 260 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
261 $mform->setType('url', PARAM_URL);
ad6226fb 262
d8734783 263 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
93de0ac2 264 $mform->setType('icq', PARAM_NOTAGS);
ad6226fb 265
d8734783 266 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
93de0ac2 267 $mform->setType('skype', PARAM_NOTAGS);
ad6226fb 268
d8734783 269 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
93de0ac2 270 $mform->setType('aim', PARAM_NOTAGS);
ad6226fb 271
d8734783 272 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
93de0ac2 273 $mform->setType('yahoo', PARAM_NOTAGS);
ad6226fb 274
d8734783 275 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
93de0ac2 276 $mform->setType('msn', PARAM_NOTAGS);
ad6226fb 277
8b9cfac4 278 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 279 $mform->setType('idnumber', PARAM_NOTAGS);
ad6226fb 280
d8734783 281 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
282 $mform->setType('institution', PARAM_MULTILANG);
ad6226fb 283
d8734783 284 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
285 $mform->setType('department', PARAM_MULTILANG);
ad6226fb 286
d8734783 287 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
93de0ac2 288 $mform->setType('phone1', PARAM_NOTAGS);
ad6226fb 289
55ac3d6f 290 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 291 $mform->setType('phone2', PARAM_NOTAGS);
ad6226fb 292
d8734783 293 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
294 $mform->setType('address', PARAM_MULTILANG);
d6ace123 295
296
14a6b7e1 297}
ad6226fb 298
aa6c1ced 299