MDL-13811 reworked unsetting of preferences and minor tweaks
[moodle.git] / user / editlib.php
CommitLineData
ad6226fb 1<?php //$Id$
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 }
17
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
36function useredit_update_picture(&$usernew, &$userform) {
a5d424df 37 global $CFG, $DB;
9d85247d 38
14a6b7e1 39 if (isset($usernew->deletepicture) and $usernew->deletepicture) {
56a1a882 40 $location = make_user_directory($usernew->id, true);
9d85247d 41 @remove_dir($location);
a5d424df 42 $DB->set_field('user', 'picture', 0, array('id'=>$usernew->id));
56a1a882 43 } else if ($usernew->picture = save_profile_image($usernew->id, $userform->get_um(), 'user')) {
a5d424df 44 $DB->set_field('user', 'picture', 1, array('id'=>$usernew->id));
14a6b7e1 45 }
46}
47
48function useredit_update_bounces($user, $usernew) {
49 if (!isset($usernew->email)) {
50 //locked field
51 return;
d8734783 52 }
a9457b54 53 if (!isset($user->email) || $user->email !== $usernew->email) {
14a6b7e1 54 set_bounce_count($usernew,true);
55 set_send_count($usernew,true);
56 }
57}
58
59function useredit_update_trackforums($user, $usernew) {
60 global $CFG;
61 if (!isset($usernew->trackforums)) {
62 //locked field
63 return;
64 }
a9457b54 65 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
14a6b7e1 66 require_once($CFG->dirroot.'/mod/forum/lib.php');
67 forum_tp_delete_read_records($usernew->id);
68 }
69}
70
3d535996 71function useredit_update_interests($user, $csv_tag_names) {
72 tag_set('user', $user->id, explode(',', $csv_tag_names));
1e1c51a3 73}
74
14a6b7e1 75function useredit_shared_definition(&$mform) {
d6ace123 76 global $CFG, $USER, $DB;
77
78 $user = $DB->get_record('user', array('id' => $USER->id));
05c38e2b 79 useredit_load_preferences($user, false);
14a6b7e1 80
81 $strrequired = get_string('required');
d8734783 82
c1658cc5 83 $nameordercheck = new object();
baacc970 84 $nameordercheck->firstname = 'a';
85 $nameordercheck->lastname = 'b';
86 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
87 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
88 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
89 } else {
90 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
91 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
92 }
93
d8734783 94 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
95 $mform->setType('firstname', PARAM_NOTAGS);
96
d8734783 97 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
98 $mform->setType('lastname', PARAM_NOTAGS);
99
d6ace123 100 // Do not show email field if change confirmation is pending
101 if ($CFG->emailchangeconfirmation && !empty($user->preference_newemail)) {
102 $notice = get_string('auth_emailchangepending', 'auth', $user);
103 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
104 . get_string('auth_emailchangecancel', 'auth') . '</a>';
105 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
106 } else {
107 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
108 $mform->addRule('email', $strrequired, 'required', null, 'client');
109 }
d8734783 110
111 $choices = array();
112 $choices['0'] = get_string('emaildisplayno');
113 $choices['1'] = get_string('emaildisplayyes');
114 $choices['2'] = get_string('emaildisplaycourse');
115 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
e61f8701 116 $mform->setDefault('maildisplay', 2);
d8734783 117
118 $choices = array();
119 $choices['0'] = get_string('emailenable');
120 $choices['1'] = get_string('emaildisable');
121 $mform->addElement('select', 'emailstop', get_string('emailactive'), $choices);
ecdf8d2e 122 $mform->setDefault('emailstop', 0);
d8734783 123
124 $choices = array();
125 $choices['0'] = get_string('textformat');
126 $choices['1'] = get_string('htmlformat');
127 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
128 $mform->setDefault('mailformat', 1);
129 $mform->setAdvanced('mailformat');
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);
140 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
141 $mform->setAdvanced('preference_mailcharset');
142 }
143
144 $choices = array();
145 $choices['0'] = get_string('emaildigestoff');
146 $choices['1'] = get_string('emaildigestcomplete');
147 $choices['2'] = get_string('emaildigestsubjects');
148 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
149 $mform->setDefault('maildigest', 0);
150 $mform->setAdvanced('maildigest');
151
152 $choices = array();
153 $choices['1'] = get_string('autosubscribeyes');
154 $choices['0'] = get_string('autosubscribeno');
155 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
e61f8701 156 $mform->setDefault('autosubscribe', 1);
d8734783 157 $mform->setAdvanced('autosubscribe');
158
159 if (!empty($CFG->forum_trackreadposts)) {
ad6226fb 160 $choices = array();
d8734783 161 $choices['0'] = get_string('trackforumsno');
162 $choices['1'] = get_string('trackforumsyes');
163 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
164 $mform->setDefault('trackforums', 0);
165 $mform->setAdvanced('trackforums');
166 }
167
168 if ($CFG->htmleditor) {
ad6226fb 169 $choices = array();
d8734783 170 $choices['0'] = get_string('texteditor');
171 $choices['1'] = get_string('htmleditor');
172 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
173 $mform->setDefault('htmleditor', 1);
174 $mform->setAdvanced('htmleditor');
175 }
176
177 if (empty($CFG->enableajax)) {
178 $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
179 $mform->setAdvanced('ajaxdisabled');
180 } else {
ad6226fb 181 $choices = array();
d8734783 182 $choices['0'] = get_string('ajaxno');
183 $choices['1'] = get_string('ajaxyes');
184 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
185 $mform->setDefault('ajax', 0);
186 $mform->setAdvanced('ajax');
187 }
188
189 $choices = array();
190 $choices['0'] = get_string('screenreaderno');
191 $choices['1'] = get_string('screenreaderyes');
192 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
193 $mform->setDefault('screenreader', 0);
194 $mform->setAdvanced('screenreader');
195
a1df2e98 196 $mform->addElement('text', 'city', get_string('city'), 'maxlength="20" size="21"');
d8734783 197 $mform->setType('city', PARAM_MULTILANG);
198 $mform->addRule('city', $strrequired, 'required', null, 'client');
199
200
201 $choices = get_list_of_countries();
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
ca80f999 218 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_list_of_languages());
219 $mform->setDefault('lang', $CFG->lang);
d8734783 220
221 if (!empty($CFG->allowuserthemes)) {
ad6226fb 222 $choices = array();
d8734783 223 $choices[''] = get_string('default');
224 $choices += get_list_of_themes();
225 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
226 $mform->setAdvanced('theme');
227 }
ad6226fb 228
d8734783 229 $mform->addElement('htmleditor', 'description', get_string('userdescription'));
230 $mform->setType('description', PARAM_CLEAN);
231 $mform->setHelpButton('description', array('text', get_string('helptext')));
ad6226fb 232
d8734783 233 if (!empty($CFG->gdversion)) {
234 $mform->addElement('header', 'moodle_picture', get_string('pictureof'));//TODO: Accessibility fix fieldset legend
ad6226fb 235
d8734783 236 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 237
d8734783 238 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
239 $mform->setDefault('deletepicture',false);
ad6226fb 240
d8734783 241 $mform->addElement('file', 'imagefile', get_string('newpicture'));
242 $mform->setHelpButton('imagefile', array('picture', get_string('helppicture')));
ad6226fb 243
d8734783 244 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
245 $mform->setType('imagealt', PARAM_MULTILANG);
ad6226fb 246
d8734783 247 }
ad6226fb 248
f5488402 249 if ( !empty($CFG->usetags) && has_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM)) ) {
1e1c51a3 250 $mform->addElement('header', 'moodle_interests', get_string('interests'));
1c54b2bb 251 $mform->addElement('textarea', 'interests', get_string('interestslist'), 'cols="45" rows="3"');
ab50ec12 252 $mform->setHelpButton('interests', array('interestslist', get_string('helpinterestslist'),
d6ace123 253 false, true, false));
1e1c51a3 254 }
d6ace123 255
d8734783 256 /// Moodle optional fields
257 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
258 $mform->setAdvanced('moodle_optional');
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"');
264 $mform->setType('icq', PARAM_CLEAN);
ad6226fb 265
d8734783 266 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
267 $mform->setType('skype', PARAM_CLEAN);
ad6226fb 268
d8734783 269 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
270 $mform->setType('aim', PARAM_CLEAN);
ad6226fb 271
d8734783 272 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
273 $mform->setType('yahoo', PARAM_CLEAN);
ad6226fb 274
d8734783 275 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
276 $mform->setType('msn', PARAM_CLEAN);
ad6226fb 277
8b9cfac4 278 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
9e7cd813 279 $mform->setType('idnumber', PARAM_CLEAN);
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"');
288 $mform->setType('phone1', PARAM_CLEAN);
ad6226fb 289
55ac3d6f 290 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
d8734783 291 $mform->setType('phone2', PARAM_CLEAN);
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
baacc970 299?>