MDL-32003 xmldb: Use action error for filemodified check instead of structure one...
[moodle.git] / user / editlib.php
blobf229a04509ac1585da6fbd0cd2b35a5360c3f09c
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, $filemanageroptions = array()) {
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;
53 // Get file_storage to process files.
54 $fs = get_file_storage();
55 if (!empty($usernew->deletepicture)) {
56 // The user has chosen to delete the selected users picture
57 $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
58 $newpicture = 0;
60 } else {
61 // Save newly uploaded file, this will avoid context mismatch for newly created users.
62 file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
63 if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
64 // Get file which was uploaded in draft area
65 foreach ($iconfiles as $file) {
66 if (!$file->is_directory()) {
67 break;
70 // Copy file to temporary location and the send it for processing icon
71 if ($iconfile = $file->copy_content_to_temp()) {
72 // There is a new image that has been uploaded
73 // Process the new image and set the user to make use of it.
74 // NOTE: Uploaded images always take over Gravatar
75 $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
76 // Delete temporary file
77 @unlink($iconfile);
78 // Remove uploaded file.
79 $fs->delete_area_files($context->id, 'user', 'newicon');
80 } else {
81 // Something went wrong while creating temp file.
82 // Remove uploaded file.
83 $fs->delete_area_files($context->id, 'user', 'newicon');
84 return false;
89 if ($newpicture != $user->picture) {
90 $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
91 return true;
92 } else {
93 return false;
97 function useredit_update_bounces($user, $usernew) {
98 if (!isset($usernew->email)) {
99 //locked field
100 return;
102 if (!isset($user->email) || $user->email !== $usernew->email) {
103 set_bounce_count($usernew,true);
104 set_send_count($usernew,true);
108 function useredit_update_trackforums($user, $usernew) {
109 global $CFG;
110 if (!isset($usernew->trackforums)) {
111 //locked field
112 return;
114 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
115 require_once($CFG->dirroot.'/mod/forum/lib.php');
116 forum_tp_delete_read_records($usernew->id);
120 function useredit_update_interests($user, $interests) {
121 tag_set('user', $user->id, $interests);
124 function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) {
125 global $CFG, $USER, $DB;
127 $user = $DB->get_record('user', array('id' => $USER->id));
128 useredit_load_preferences($user, false);
130 $strrequired = get_string('required');
132 $nameordercheck = new stdClass();
133 $nameordercheck->firstname = 'a';
134 $nameordercheck->lastname = 'b';
135 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
136 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
137 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
138 } else {
139 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
140 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
143 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
144 $mform->setType('firstname', PARAM_NOTAGS);
146 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
147 $mform->setType('lastname', PARAM_NOTAGS);
149 // Do not show email field if change confirmation is pending
150 if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
151 $notice = get_string('emailchangepending', 'auth', $user);
152 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
153 . get_string('emailchangecancel', 'auth') . '</a>';
154 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
155 } else {
156 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
157 $mform->addRule('email', $strrequired, 'required', null, 'client');
160 $choices = array();
161 $choices['0'] = get_string('emaildisplayno');
162 $choices['1'] = get_string('emaildisplayyes');
163 $choices['2'] = get_string('emaildisplaycourse');
164 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
165 $mform->setDefault('maildisplay', 2);
167 $choices = array();
168 $choices['0'] = get_string('textformat');
169 $choices['1'] = get_string('htmlformat');
170 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
171 $mform->setDefault('mailformat', 1);
173 if (!empty($CFG->allowusermailcharset)) {
174 $choices = array();
175 $charsets = get_list_of_charsets();
176 if (!empty($CFG->sitemailcharset)) {
177 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
178 } else {
179 $choices['0'] = get_string('site').' (UTF-8)';
181 $choices = array_merge($choices, $charsets);
182 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
185 $choices = array();
186 $choices['0'] = get_string('emaildigestoff');
187 $choices['1'] = get_string('emaildigestcomplete');
188 $choices['2'] = get_string('emaildigestsubjects');
189 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
190 $mform->setDefault('maildigest', 0);
192 $choices = array();
193 $choices['1'] = get_string('autosubscribeyes');
194 $choices['0'] = get_string('autosubscribeno');
195 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
196 $mform->setDefault('autosubscribe', 1);
198 if (!empty($CFG->forum_trackreadposts)) {
199 $choices = array();
200 $choices['0'] = get_string('trackforumsno');
201 $choices['1'] = get_string('trackforumsyes');
202 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
203 $mform->setDefault('trackforums', 0);
206 $editors = editors_get_enabled();
207 if (count($editors) > 1) {
208 $choices = array();
209 $choices['0'] = get_string('texteditor');
210 $choices['1'] = get_string('htmleditor');
211 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
212 $mform->setDefault('htmleditor', 1);
213 } else {
214 $mform->addElement('hidden', 'htmleditor');
215 $mform->setDefault('htmleditor', 1);
216 $mform->setType('htmleditor', PARAM_INT);
219 $choices = array();
220 $choices['0'] = get_string('screenreaderno');
221 $choices['1'] = get_string('screenreaderyes');
222 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
223 $mform->setDefault('screenreader', 0);
224 $mform->addHelpButton('screenreader', 'screenreaderuse');
226 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
227 $mform->setType('city', PARAM_MULTILANG);
228 $mform->addRule('city', $strrequired, 'required', null, 'client');
229 if (!empty($CFG->defaultcity)) {
230 $mform->setDefault('city', $CFG->defaultcity);
233 $choices = get_string_manager()->get_list_of_countries();
234 $choices= array(''=>get_string('selectacountry').'...') + $choices;
235 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
236 $mform->addRule('country', $strrequired, 'required', null, 'client');
237 if (!empty($CFG->country)) {
238 $mform->setDefault('country', $CFG->country);
241 $choices = get_list_of_timezones();
242 $choices['99'] = get_string('serverlocaltime');
243 if ($CFG->forcetimezone != 99) {
244 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
245 } else {
246 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
247 $mform->setDefault('timezone', '99');
250 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
251 $mform->setDefault('lang', $CFG->lang);
253 if (!empty($CFG->allowuserthemes)) {
254 $choices = array();
255 $choices[''] = get_string('default');
256 $themes = get_list_of_themes();
257 foreach ($themes as $key=>$theme) {
258 if (empty($theme->hidefromselector)) {
259 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
262 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
265 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
266 $mform->setType('description_editor', PARAM_CLEANHTML);
267 $mform->addHelpButton('description_editor', 'userdescription');
269 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
270 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
272 if (!empty($CFG->enablegravatar)) {
273 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
276 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
278 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
279 $mform->setDefault('deletepicture', 0);
281 $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
282 $mform->addHelpButton('imagefile', 'newpicture');
284 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
285 $mform->setType('imagealt', PARAM_MULTILANG);
289 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
290 $mform->addElement('header', 'moodle_interests', get_string('interests'));
291 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
292 $mform->addHelpButton('interests', 'interestslist');
295 /// Moodle optional fields
296 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
298 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
299 $mform->setType('url', PARAM_URL);
301 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
302 $mform->setType('icq', PARAM_NOTAGS);
304 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
305 $mform->setType('skype', PARAM_NOTAGS);
307 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
308 $mform->setType('aim', PARAM_NOTAGS);
310 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
311 $mform->setType('yahoo', PARAM_NOTAGS);
313 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
314 $mform->setType('msn', PARAM_NOTAGS);
316 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
317 $mform->setType('idnumber', PARAM_NOTAGS);
319 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
320 $mform->setType('institution', PARAM_MULTILANG);
322 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
323 $mform->setType('department', PARAM_MULTILANG);
325 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
326 $mform->setType('phone1', PARAM_NOTAGS);
328 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
329 $mform->setType('phone2', PARAM_NOTAGS);
331 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
332 $mform->setType('address', PARAM_MULTILANG);