Merge branch 'MDL-27143_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / user / editlib.php
blob59228ecbc6c3a0565225037508208dd7d8b078c4
1 <?php
3 function cancel_email_update($userid) {
4 unset_user_preference('newemail', $userid);
5 unset_user_preference('newemailkey', $userid);
6 unset_user_preference('newemailattemptsleft', $userid);
9 function useredit_load_preferences(&$user, $reload=true) {
10 global $USER;
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);
18 if ($preferences = get_user_preferences(null, null, $user->id)) {
19 foreach($preferences as $name=>$value) {
20 $user->{'preference_'.$name} = $value;
26 function 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);
36 /**
37 * Updates the provided users profile picture based upon the expected fields
38 * returned from the edit or edit_advanced forms.
40 * @global moodle_database $DB
41 * @param stdClass $usernew An object that contains some information about the user being updated
42 * @param moodleform $userform The form that was submitted to edit the form
43 * @return bool True if the user was updated, false if it stayed the same.
45 function useredit_update_picture(stdClass $usernew, moodleform $userform) {
46 global $CFG, $DB;
47 require_once("$CFG->libdir/gdlib.php");
49 $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST);
50 // This will hold the value to set to the user's picture field at the end of
51 // this function
52 $picturetouse = null;
53 if (!empty($usernew->deletepicture)) {
54 // The user has chosen to delete the selected users picture
55 $fs = get_file_storage();
56 $fs->delete_area_files($context->id, 'user', 'icon'); // drop all areas
57 $picturetouse = 0;
58 } else if ($iconfile = $userform->save_temp_file('imagefile')) {
59 // There is a new image that has been uploaded
60 // Process the new image and set the user to make use of it.
61 // NOTE: This may be overridden by Gravatar
62 if (process_new_icon($context, 'user', 'icon', 0, $iconfile)) {
63 $picturetouse = 1;
65 // Delete the file that has now been processed
66 @unlink($iconfile);
69 // If we have a picture to set we can now do so. Note this will still be NULL
70 // unless the user has changed their picture or caused a change by selecting
71 // to delete their picture or use gravatar
72 if (!is_null($picturetouse)) {
73 $DB->set_field('user', 'picture', $picturetouse, array('id' => $usernew->id));
74 return true;
77 return false;
80 function useredit_update_bounces($user, $usernew) {
81 if (!isset($usernew->email)) {
82 //locked field
83 return;
85 if (!isset($user->email) || $user->email !== $usernew->email) {
86 set_bounce_count($usernew,true);
87 set_send_count($usernew,true);
91 function useredit_update_trackforums($user, $usernew) {
92 global $CFG;
93 if (!isset($usernew->trackforums)) {
94 //locked field
95 return;
97 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
98 require_once($CFG->dirroot.'/mod/forum/lib.php');
99 forum_tp_delete_read_records($usernew->id);
103 function useredit_update_interests($user, $interests) {
104 tag_set('user', $user->id, $interests);
107 function useredit_shared_definition(&$mform, $editoroptions = null) {
108 global $CFG, $USER, $DB;
110 $user = $DB->get_record('user', array('id' => $USER->id));
111 useredit_load_preferences($user, false);
113 $strrequired = get_string('required');
115 $nameordercheck = new stdClass();
116 $nameordercheck->firstname = 'a';
117 $nameordercheck->lastname = 'b';
118 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
119 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
120 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
121 } else {
122 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
123 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
126 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
127 $mform->setType('firstname', PARAM_NOTAGS);
129 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
130 $mform->setType('lastname', PARAM_NOTAGS);
132 // Do not show email field if change confirmation is pending
133 if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
134 $notice = get_string('emailchangepending', 'auth', $user);
135 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
136 . get_string('emailchangecancel', 'auth') . '</a>';
137 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
138 } else {
139 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
140 $mform->addRule('email', $strrequired, 'required', null, 'client');
143 $choices = array();
144 $choices['0'] = get_string('emaildisplayno');
145 $choices['1'] = get_string('emaildisplayyes');
146 $choices['2'] = get_string('emaildisplaycourse');
147 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
148 $mform->setDefault('maildisplay', 2);
150 $choices = array();
151 $choices['0'] = get_string('textformat');
152 $choices['1'] = get_string('htmlformat');
153 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
154 $mform->setDefault('mailformat', 1);
156 if (!empty($CFG->allowusermailcharset)) {
157 $choices = array();
158 $charsets = get_list_of_charsets();
159 if (!empty($CFG->sitemailcharset)) {
160 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
161 } else {
162 $choices['0'] = get_string('site').' (UTF-8)';
164 $choices = array_merge($choices, $charsets);
165 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
168 $choices = array();
169 $choices['0'] = get_string('emaildigestoff');
170 $choices['1'] = get_string('emaildigestcomplete');
171 $choices['2'] = get_string('emaildigestsubjects');
172 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
173 $mform->setDefault('maildigest', 0);
175 $choices = array();
176 $choices['1'] = get_string('autosubscribeyes');
177 $choices['0'] = get_string('autosubscribeno');
178 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
179 $mform->setDefault('autosubscribe', 1);
181 if (!empty($CFG->forum_trackreadposts)) {
182 $choices = array();
183 $choices['0'] = get_string('trackforumsno');
184 $choices['1'] = get_string('trackforumsyes');
185 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
186 $mform->setDefault('trackforums', 0);
189 $editors = editors_get_enabled();
190 if (count($editors) > 1) {
191 $choices = array();
192 $choices['0'] = get_string('texteditor');
193 $choices['1'] = get_string('htmleditor');
194 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
195 $mform->setDefault('htmleditor', 1);
196 } else {
197 $mform->addElement('hidden', 'htmleditor');
198 $mform->setDefault('htmleditor', 1);
199 $mform->setType('htmleditor', PARAM_INT);
202 if (empty($CFG->enableajax)) {
203 $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
204 } else {
205 $choices = array();
206 $choices['0'] = get_string('ajaxno');
207 $choices['1'] = get_string('ajaxyes');
208 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
209 $mform->setDefault('ajax', 0);
212 $choices = array();
213 $choices['0'] = get_string('screenreaderno');
214 $choices['1'] = get_string('screenreaderyes');
215 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
216 $mform->setDefault('screenreader', 0);
217 $mform->addHelpButton('screenreader', 'screenreaderuse');
219 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
220 $mform->setType('city', PARAM_MULTILANG);
221 $mform->addRule('city', $strrequired, 'required', null, 'client');
222 if (!empty($CFG->defaultcity)) {
223 $mform->setDefault('city', $CFG->defaultcity);
226 $choices = get_string_manager()->get_list_of_countries();
227 $choices= array(''=>get_string('selectacountry').'...') + $choices;
228 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
229 $mform->addRule('country', $strrequired, 'required', null, 'client');
230 if (!empty($CFG->country)) {
231 $mform->setDefault('country', $CFG->country);
234 $choices = get_list_of_timezones();
235 $choices['99'] = get_string('serverlocaltime');
236 if ($CFG->forcetimezone != 99) {
237 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
238 } else {
239 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
240 $mform->setDefault('timezone', '99');
243 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
244 $mform->setDefault('lang', $CFG->lang);
246 if (!empty($CFG->allowuserthemes)) {
247 $choices = array();
248 $choices[''] = get_string('default');
249 $themes = get_list_of_themes();
250 foreach ($themes as $key=>$theme) {
251 if (empty($theme->hidefromselector)) {
252 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
255 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
258 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
259 $mform->setType('description_editor', PARAM_CLEANHTML);
260 $mform->addHelpButton('description_editor', 'userdescription');
262 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
263 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
265 if (!empty($CFG->enablegravatar)) {
266 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
269 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
271 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
272 $mform->setDefault('deletepicture', 0);
274 $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
275 $mform->addHelpButton('imagefile', 'newpicture');
277 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
278 $mform->setType('imagealt', PARAM_MULTILANG);
282 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
283 $mform->addElement('header', 'moodle_interests', get_string('interests'));
284 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
285 $mform->addHelpButton('interests', 'interestslist');
288 /// Moodle optional fields
289 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
291 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
292 $mform->setType('url', PARAM_URL);
294 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
295 $mform->setType('icq', PARAM_NOTAGS);
297 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
298 $mform->setType('skype', PARAM_NOTAGS);
300 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
301 $mform->setType('aim', PARAM_NOTAGS);
303 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
304 $mform->setType('yahoo', PARAM_NOTAGS);
306 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
307 $mform->setType('msn', PARAM_NOTAGS);
309 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
310 $mform->setType('idnumber', PARAM_NOTAGS);
312 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
313 $mform->setType('institution', PARAM_MULTILANG);
315 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
316 $mform->setType('department', PARAM_MULTILANG);
318 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
319 $mform->setType('phone1', PARAM_NOTAGS);
321 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
322 $mform->setType('phone2', PARAM_NOTAGS);
324 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
325 $mform->setType('address', PARAM_MULTILANG);