MDL-33811 Fixed left over debugging code preventing proper setting of YUI gallery...
[moodle.git] / user / editlib.php
blob8110f61a89818cabd425e24db2ab4764b644eebf
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 $user = $DB->get_record('user', array('id'=>$usernew->id), 'id, picture', MUST_EXIST);
52 $newpicture = $user->picture;
54 if (!empty($usernew->deletepicture)) {
55 // The user has chosen to delete the selected users picture
56 $fs = get_file_storage();
57 $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
58 $newpicture = 0;
60 } else if ($iconfile = $userform->save_temp_file('imagefile')) {
61 // There is a new image that has been uploaded
62 // Process the new image and set the user to make use of it.
63 // NOTE: Uploaded images always take over Gravatar
64 $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
65 // Delete the file that has now been processed
66 @unlink($iconfile);
69 if ($newpicture != $user->picture) {
70 $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
71 return true;
72 } else {
73 return false;
77 function useredit_update_bounces($user, $usernew) {
78 if (!isset($usernew->email)) {
79 //locked field
80 return;
82 if (!isset($user->email) || $user->email !== $usernew->email) {
83 set_bounce_count($usernew,true);
84 set_send_count($usernew,true);
88 function useredit_update_trackforums($user, $usernew) {
89 global $CFG;
90 if (!isset($usernew->trackforums)) {
91 //locked field
92 return;
94 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
95 require_once($CFG->dirroot.'/mod/forum/lib.php');
96 forum_tp_delete_read_records($usernew->id);
100 function useredit_update_interests($user, $interests) {
101 tag_set('user', $user->id, $interests);
104 function useredit_shared_definition(&$mform, $editoroptions = null) {
105 global $CFG, $USER, $DB;
107 $user = $DB->get_record('user', array('id' => $USER->id));
108 useredit_load_preferences($user, false);
110 $strrequired = get_string('required');
112 $nameordercheck = new stdClass();
113 $nameordercheck->firstname = 'a';
114 $nameordercheck->lastname = 'b';
115 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
116 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
117 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
118 } else {
119 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
120 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
123 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
124 $mform->setType('firstname', PARAM_NOTAGS);
126 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
127 $mform->setType('lastname', PARAM_NOTAGS);
129 // Do not show email field if change confirmation is pending
130 if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
131 $notice = get_string('emailchangepending', 'auth', $user);
132 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
133 . get_string('emailchangecancel', 'auth') . '</a>';
134 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
135 } else {
136 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
137 $mform->addRule('email', $strrequired, 'required', null, 'client');
140 $choices = array();
141 $choices['0'] = get_string('emaildisplayno');
142 $choices['1'] = get_string('emaildisplayyes');
143 $choices['2'] = get_string('emaildisplaycourse');
144 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
145 $mform->setDefault('maildisplay', 2);
147 $choices = array();
148 $choices['0'] = get_string('textformat');
149 $choices['1'] = get_string('htmlformat');
150 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
151 $mform->setDefault('mailformat', 1);
153 if (!empty($CFG->allowusermailcharset)) {
154 $choices = array();
155 $charsets = get_list_of_charsets();
156 if (!empty($CFG->sitemailcharset)) {
157 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
158 } else {
159 $choices['0'] = get_string('site').' (UTF-8)';
161 $choices = array_merge($choices, $charsets);
162 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
165 $choices = array();
166 $choices['0'] = get_string('emaildigestoff');
167 $choices['1'] = get_string('emaildigestcomplete');
168 $choices['2'] = get_string('emaildigestsubjects');
169 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
170 $mform->setDefault('maildigest', 0);
172 $choices = array();
173 $choices['1'] = get_string('autosubscribeyes');
174 $choices['0'] = get_string('autosubscribeno');
175 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
176 $mform->setDefault('autosubscribe', 1);
178 if (!empty($CFG->forum_trackreadposts)) {
179 $choices = array();
180 $choices['0'] = get_string('trackforumsno');
181 $choices['1'] = get_string('trackforumsyes');
182 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
183 $mform->setDefault('trackforums', 0);
186 $editors = editors_get_enabled();
187 if (count($editors) > 1) {
188 $choices = array();
189 $choices['0'] = get_string('texteditor');
190 $choices['1'] = get_string('htmleditor');
191 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
192 $mform->setDefault('htmleditor', 1);
193 } else {
194 $mform->addElement('hidden', 'htmleditor');
195 $mform->setDefault('htmleditor', 1);
196 $mform->setType('htmleditor', PARAM_INT);
199 if (empty($CFG->enableajax)) {
200 $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
201 } else {
202 $choices = array();
203 $choices['0'] = get_string('ajaxno');
204 $choices['1'] = get_string('ajaxyes');
205 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
206 $mform->setDefault('ajax', 1);
209 $choices = array();
210 $choices['0'] = get_string('screenreaderno');
211 $choices['1'] = get_string('screenreaderyes');
212 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
213 $mform->setDefault('screenreader', 0);
214 $mform->addHelpButton('screenreader', 'screenreaderuse');
216 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
217 $mform->setType('city', PARAM_MULTILANG);
218 $mform->addRule('city', $strrequired, 'required', null, 'client');
219 if (!empty($CFG->defaultcity)) {
220 $mform->setDefault('city', $CFG->defaultcity);
223 $choices = get_string_manager()->get_list_of_countries();
224 $choices= array(''=>get_string('selectacountry').'...') + $choices;
225 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
226 $mform->addRule('country', $strrequired, 'required', null, 'client');
227 if (!empty($CFG->country)) {
228 $mform->setDefault('country', $CFG->country);
231 $choices = get_list_of_timezones();
232 $choices['99'] = get_string('serverlocaltime');
233 if ($CFG->forcetimezone != 99) {
234 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
235 } else {
236 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
237 $mform->setDefault('timezone', '99');
240 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
241 $mform->setDefault('lang', $CFG->lang);
243 if (!empty($CFG->allowuserthemes)) {
244 $choices = array();
245 $choices[''] = get_string('default');
246 $themes = get_list_of_themes();
247 foreach ($themes as $key=>$theme) {
248 if (empty($theme->hidefromselector)) {
249 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
252 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
255 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
256 $mform->setType('description_editor', PARAM_CLEANHTML);
257 $mform->addHelpButton('description_editor', 'userdescription');
259 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
260 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
262 if (!empty($CFG->enablegravatar)) {
263 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
266 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
268 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
269 $mform->setDefault('deletepicture', 0);
271 $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
272 $mform->addHelpButton('imagefile', 'newpicture');
274 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
275 $mform->setType('imagealt', PARAM_MULTILANG);
279 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
280 $mform->addElement('header', 'moodle_interests', get_string('interests'));
281 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
282 $mform->addHelpButton('interests', 'interestslist');
285 /// Moodle optional fields
286 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
288 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
289 $mform->setType('url', PARAM_URL);
291 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
292 $mform->setType('icq', PARAM_NOTAGS);
294 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
295 $mform->setType('skype', PARAM_NOTAGS);
297 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
298 $mform->setType('aim', PARAM_NOTAGS);
300 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
301 $mform->setType('yahoo', PARAM_NOTAGS);
303 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
304 $mform->setType('msn', PARAM_NOTAGS);
306 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
307 $mform->setType('idnumber', PARAM_NOTAGS);
309 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
310 $mform->setType('institution', PARAM_MULTILANG);
312 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
313 $mform->setType('department', PARAM_MULTILANG);
315 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
316 $mform->setType('phone1', PARAM_NOTAGS);
318 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
319 $mform->setType('phone2', PARAM_NOTAGS);
321 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
322 $mform->setType('address', PARAM_MULTILANG);