MDL-21818 fixed globals problems
[moodle.git] / user / editlib.php
blob57a419141e3308fc4e24b68ef1e37ae9a545f953
1 <?php //$Id$
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, stripslashes_recursive($value), $usernew->id);
36 function useredit_update_picture(&$usernew, &$userform) {
37 global $CFG;
39 if (isset($usernew->deletepicture) and $usernew->deletepicture) {
40 $location = make_user_directory($usernew->id, true);
41 @remove_dir($location);
42 set_field('user', 'picture', 0, 'id', $usernew->id);
43 } else if ($usernew->picture = save_profile_image($usernew->id, $userform->get_um(), 'user')) {
44 set_field('user', 'picture', 1, 'id', $usernew->id);
48 function useredit_update_bounces($user, $usernew) {
49 if (!isset($usernew->email)) {
50 //locked field
51 return;
53 if (!isset($user->email) || $user->email !== stripslashes($usernew->email)) {
54 set_bounce_count($usernew,true);
55 set_send_count($usernew,true);
59 function useredit_update_trackforums($user, $usernew) {
60 global $CFG;
61 if (!isset($usernew->trackforums)) {
62 //locked field
63 return;
65 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
66 require_once($CFG->dirroot.'/mod/forum/lib.php');
67 forum_tp_delete_read_records($usernew->id);
71 function useredit_update_interests($user, $csv_tag_names) {
72 tag_set('user', $user->id, explode(',', $csv_tag_names));
75 function useredit_shared_definition(&$mform) {
76 global $CFG, $USER;
78 $user = get_record('user', 'id', $USER->id);
79 useredit_load_preferences($user, false);
81 $strrequired = get_string('required');
83 $nameordercheck = new object();
84 $nameordercheck->firstname = 'a';
85 $nameordercheck->lastname = 'b';
86 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
87 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
88 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
89 } else {
90 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
91 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
94 $mform->addRule('firstname', $strrequired, 'required', null, 'client');
95 $mform->setType('firstname', PARAM_NOTAGS);
97 $mform->addRule('lastname', $strrequired, 'required', null, 'client');
98 $mform->setType('lastname', PARAM_NOTAGS);
100 // Do not show email field if change confirmation is pending
101 if ($CFG->emailchangeconfirmation && !empty($user->preference_newemail)) {
102 $notice = get_string('auth_emailchangepending', 'auth', $user);
103 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
104 . get_string('auth_emailchangecancel', 'auth') . '</a>';
105 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
106 } else {
107 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
108 $mform->addRule('email', $strrequired, 'required', null, 'client');
111 $choices = array();
112 $choices['0'] = get_string('emaildisplayno');
113 $choices['1'] = get_string('emaildisplayyes');
114 $choices['2'] = get_string('emaildisplaycourse');
115 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
116 $mform->setDefault('maildisplay', 2);
118 $choices = array();
119 $choices['0'] = get_string('emailenable');
120 $choices['1'] = get_string('emaildisable');
121 $mform->addElement('select', 'emailstop', get_string('emailactive'), $choices);
122 $mform->setDefault('emailenable', 1);
124 $choices = array();
125 $choices['0'] = get_string('textformat');
126 $choices['1'] = get_string('htmlformat');
127 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
128 $mform->setDefault('mailformat', 1);
129 $mform->setAdvanced('mailformat');
131 if (!empty($CFG->allowusermailcharset)) {
132 $choices = array();
133 $charsets = get_list_of_charsets();
134 if (!empty($CFG->sitemailcharset)) {
135 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
136 } else {
137 $choices['0'] = get_string('site').' (UTF-8)';
139 $choices = array_merge($choices, $charsets);
140 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
141 $mform->setAdvanced('preference_mailcharset');
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);
150 $mform->setAdvanced('maildigest');
152 $choices = array();
153 $choices['1'] = get_string('autosubscribeyes');
154 $choices['0'] = get_string('autosubscribeno');
155 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
156 $mform->setDefault('autosubscribe', 1);
157 $mform->setAdvanced('autosubscribe');
159 if (!empty($CFG->forum_trackreadposts)) {
160 $choices = array();
161 $choices['0'] = get_string('trackforumsno');
162 $choices['1'] = get_string('trackforumsyes');
163 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
164 $mform->setDefault('trackforums', 0);
165 $mform->setAdvanced('trackforums');
168 if ($CFG->htmleditor) {
169 $choices = array();
170 $choices['0'] = get_string('texteditor');
171 $choices['1'] = get_string('htmleditor');
172 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
173 $mform->setDefault('htmleditor', 1);
174 $mform->setAdvanced('htmleditor');
177 if (empty($CFG->enableajax)) {
178 $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
179 $mform->setAdvanced('ajaxdisabled');
180 } else {
181 $choices = array();
182 $choices['0'] = get_string('ajaxno');
183 $choices['1'] = get_string('ajaxyes');
184 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
185 $mform->setDefault('ajax', 0);
186 $mform->setAdvanced('ajax');
189 $choices = array();
190 $choices['0'] = get_string('screenreaderno');
191 $choices['1'] = get_string('screenreaderyes');
192 $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
193 $mform->setDefault('screenreader', 0);
194 $mform->setAdvanced('screenreader');
196 $mform->addElement('text', 'city', get_string('city'), 'maxlength="20" size="21"');
197 $mform->setType('city', PARAM_MULTILANG);
198 $mform->addRule('city', $strrequired, 'required', null, 'client');
201 $choices = get_list_of_countries();
202 $choices= array(''=>get_string('selectacountry').'...') + $choices;
203 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
204 $mform->addRule('country', $strrequired, 'required', null, 'client');
205 if (!empty($CFG->country)) {
206 $mform->setDefault('country', $CFG->country);
209 $choices = get_list_of_timezones();
210 $choices['99'] = get_string('serverlocaltime');
211 if ($CFG->forcetimezone != 99) {
212 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
213 } else {
214 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
215 $mform->setDefault('timezone', '99');
218 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_list_of_languages());
219 $mform->setDefault('lang', $CFG->lang);
221 if (!empty($CFG->allowuserthemes)) {
222 $choices = array();
223 $choices[''] = get_string('default');
224 $choices += get_list_of_themes();
225 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
226 $mform->setAdvanced('theme');
229 $mform->addElement('htmleditor', 'description', get_string('userdescription'));
230 $mform->setType('description', PARAM_CLEAN);
231 $mform->setHelpButton('description', array('text', get_string('helptext')));
233 if (!empty($CFG->gdversion)) {
234 $mform->addElement('header', 'moodle_picture', get_string('pictureof'));//TODO: Accessibility fix fieldset legend
236 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
238 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
239 $mform->setDefault('deletepicture',false);
241 $mform->addElement('file', 'imagefile', get_string('newpicture'));
242 $mform->setHelpButton('imagefile', array('picture', get_string('helppicture')));
244 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
245 $mform->setType('imagealt', PARAM_MULTILANG);
249 if( !empty($CFG->usetags)) {
250 $mform->addElement('header', 'moodle_interests', get_string('interests'));
251 $mform->addElement('textarea', 'interests', get_string('interestslist'), 'cols="45" rows="3"');
252 $mform->setHelpButton('interests', array('interestslist', get_string('helpinterestslist'),
253 false, true, false));
256 /// Moodle optional fields
257 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
258 $mform->setAdvanced('moodle_optional');
260 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
261 $mform->setType('url', PARAM_URL);
263 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
264 $mform->setType('icq', PARAM_CLEAN);
266 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
267 $mform->setType('skype', PARAM_CLEAN);
269 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
270 $mform->setType('aim', PARAM_CLEAN);
272 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
273 $mform->setType('yahoo', PARAM_CLEAN);
275 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
276 $mform->setType('msn', PARAM_CLEAN);
278 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
279 $mform->setType('idnumber', PARAM_CLEAN);
281 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
282 $mform->setType('institution', PARAM_MULTILANG);
284 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
285 $mform->setType('department', PARAM_MULTILANG);
287 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
288 $mform->setType('phone1', PARAM_CLEAN);
290 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
291 $mform->setType('phone2', PARAM_CLEAN);
293 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
294 $mform->setType('address', PARAM_MULTILANG);