weekly release 2.2dev
[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)) {
c6a074f8 109 $notice = get_string('emailchangepending', 'auth', $user);
d6ace123 110 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
c6a074f8 111 . get_string('emailchangecancel', 'auth') . '</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');
fa7f750c
PS
197 if (!empty($CFG->defaultcity)) {
198 $mform->setDefault('city', $CFG->defaultcity);
199 }
d8734783 200
0aa759b0 201 $choices = get_string_manager()->get_list_of_countries();
d8734783 202 $choices= array(''=>get_string('selectacountry').'...') + $choices;
203 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
204 $mform->addRule('country', $strrequired, 'required', null, 'client');
205 if (!empty($CFG->country)) {
206 $mform->setDefault('country', $CFG->country);
207 }
208
209 $choices = get_list_of_timezones();
210 $choices['99'] = get_string('serverlocaltime');
211 if ($CFG->forcetimezone != 99) {
212 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
213 } else {
214 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
215 $mform->setDefault('timezone', '99');
216 }
217
1f96e907 218 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
ca80f999 219 $mform->setDefault('lang', $CFG->lang);
d8734783 220
221 if (!empty($CFG->allowuserthemes)) {
ad6226fb 222 $choices = array();
d8734783 223 $choices[''] = get_string('default');
36798745
SH
224 $themes = get_list_of_themes();
225 foreach ($themes as $key=>$theme) {
226 if (empty($theme->hidefromselector)) {
227 $choices[$key] = $theme->name;
228 }
229 }
c3ed4a5a 230 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 231 }
ad6226fb 232
8bdc9cac
SH
233 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
234 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 235 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 236
de2d81fa 237 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
757e89d2 238 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 239
d8734783 240 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 241
d8734783 242 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
edfd6a5e 243 $mform->setDefault('deletepicture', 0);
ad6226fb 244
513aed3c 245 $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
27d1a27c 246 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 247
d8734783 248 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
249 $mform->setType('imagealt', PARAM_MULTILANG);
ad6226fb 250
d8734783 251 }
ad6226fb 252
de2d81fa 253 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
1e1c51a3 254 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 255 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 256 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 257 }
d6ace123 258
d8734783 259 /// Moodle optional fields
c3ed4a5a 260 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 261
d8734783 262 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
263 $mform->setType('url', PARAM_URL);
ad6226fb 264
d8734783 265 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
93de0ac2 266 $mform->setType('icq', PARAM_NOTAGS);
ad6226fb 267
d8734783 268 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
93de0ac2 269 $mform->setType('skype', PARAM_NOTAGS);
ad6226fb 270
d8734783 271 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
93de0ac2 272 $mform->setType('aim', PARAM_NOTAGS);
ad6226fb 273
d8734783 274 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
93de0ac2 275 $mform->setType('yahoo', PARAM_NOTAGS);
ad6226fb 276
d8734783 277 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
93de0ac2 278 $mform->setType('msn', PARAM_NOTAGS);
ad6226fb 279
8b9cfac4 280 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 281 $mform->setType('idnumber', PARAM_NOTAGS);
ad6226fb 282
d8734783 283 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
284 $mform->setType('institution', PARAM_MULTILANG);
ad6226fb 285
d8734783 286 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
287 $mform->setType('department', PARAM_MULTILANG);
ad6226fb 288
d8734783 289 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
93de0ac2 290 $mform->setType('phone1', PARAM_NOTAGS);
ad6226fb 291
55ac3d6f 292 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 293 $mform->setType('phone2', PARAM_NOTAGS);
ad6226fb 294
d8734783 295 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
296 $mform->setType('address', PARAM_MULTILANG);
d6ace123 297
298
14a6b7e1 299}
ad6226fb 300
aa6c1ced 301