gradebook MDL-22931 made some of the setting labels more specific and made blocks...
[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);
d8734783 188
a1df2e98 189 $mform->addElement('text', 'city', get_string('city'), 'maxlength="20" size="21"');
d8734783 190 $mform->setType('city', PARAM_MULTILANG);
191 $mform->addRule('city', $strrequired, 'required', null, 'client');
192
193
0aa759b0 194 $choices = get_string_manager()->get_list_of_countries();
d8734783 195 $choices= array(''=>get_string('selectacountry').'...') + $choices;
196 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
197 $mform->addRule('country', $strrequired, 'required', null, 'client');
198 if (!empty($CFG->country)) {
199 $mform->setDefault('country', $CFG->country);
200 }
201
202 $choices = get_list_of_timezones();
203 $choices['99'] = get_string('serverlocaltime');
204 if ($CFG->forcetimezone != 99) {
205 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
206 } else {
207 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
208 $mform->setDefault('timezone', '99');
209 }
210
1f96e907 211 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
ca80f999 212 $mform->setDefault('lang', $CFG->lang);
d8734783 213
214 if (!empty($CFG->allowuserthemes)) {
ad6226fb 215 $choices = array();
d8734783 216 $choices[''] = get_string('default');
36798745
SH
217 $themes = get_list_of_themes();
218 foreach ($themes as $key=>$theme) {
219 if (empty($theme->hidefromselector)) {
220 $choices[$key] = $theme->name;
221 }
222 }
c2a2b08f 223 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 224 }
ad6226fb 225
8bdc9cac
SH
226 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
227 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 228 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 229
d8734783 230 if (!empty($CFG->gdversion)) {
757e89d2 231 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 232
d8734783 233 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 234
d8734783 235 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
236 $mform->setDefault('deletepicture',false);
ad6226fb 237
92b143f1 238 $mform->addElement('filepicker', 'imagefile', get_string('newpicture'));
27d1a27c 239 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 240
d8734783 241 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
242 $mform->setType('imagealt', PARAM_MULTILANG);
ad6226fb 243
d8734783 244 }
ad6226fb 245
c060fc6a 246 if (!empty($CFG->usetags)) {
1e1c51a3 247 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 248 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 249 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 250 }
d6ace123 251
d8734783 252 /// Moodle optional fields
c2a2b08f 253 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 254
d8734783 255 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
256 $mform->setType('url', PARAM_URL);
ad6226fb 257
d8734783 258 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
259 $mform->setType('icq', PARAM_CLEAN);
ad6226fb 260
d8734783 261 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
262 $mform->setType('skype', PARAM_CLEAN);
ad6226fb 263
d8734783 264 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
265 $mform->setType('aim', PARAM_CLEAN);
ad6226fb 266
d8734783 267 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
268 $mform->setType('yahoo', PARAM_CLEAN);
ad6226fb 269
d8734783 270 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
271 $mform->setType('msn', PARAM_CLEAN);
ad6226fb 272
8b9cfac4 273 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
9e7cd813 274 $mform->setType('idnumber', PARAM_CLEAN);
ad6226fb 275
d8734783 276 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
277 $mform->setType('institution', PARAM_MULTILANG);
ad6226fb 278
d8734783 279 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
280 $mform->setType('department', PARAM_MULTILANG);
ad6226fb 281
d8734783 282 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
283 $mform->setType('phone1', PARAM_CLEAN);
ad6226fb 284
55ac3d6f 285 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
d8734783 286 $mform->setType('phone2', PARAM_CLEAN);
ad6226fb 287
d8734783 288 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
289 $mform->setType('address', PARAM_MULTILANG);
d6ace123 290
291
14a6b7e1 292}
ad6226fb 293
aa6c1ced 294