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