Commit | Line | Data |
---|---|---|
ce221eb5 | 1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * Allows you to edit a users profile | |
20 | * | |
21 | * @copyright 1999 Martin Dougiamas http://dougiamas.com | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | * @package user | |
24 | */ | |
25 | ||
26 | require_once('../config.php'); | |
27 | require_once($CFG->libdir.'/gdlib.php'); | |
28 | require_once($CFG->libdir.'/adminlib.php'); | |
29 | require_once($CFG->dirroot.'/user/editadvanced_form.php'); | |
30 | require_once($CFG->dirroot.'/user/editlib.php'); | |
31 | require_once($CFG->dirroot.'/user/profile/lib.php'); | |
ce221eb5 | 32 | |
33 | httpsrequired(); | |
34 | ||
35 | $id = optional_param('id', $USER->id, PARAM_INT); // user id; -1 if creating new user | |
36 | $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site) | |
37 | ||
a6855934 | 38 | $url = new moodle_url('/user/editadvanced.php', array('course'=>$course)); |
ce221eb5 | 39 | if ($id !== $USER->id) { |
40 | $url->param('id', $id); | |
41 | } | |
42 | $PAGE->set_url($url); | |
43 | ||
44 | if (!$course = $DB->get_record('course', array('id'=>$course))) { | |
45 | print_error('invalidcourseid'); | |
46 | } | |
47 | if (!empty($USER->newadminuser)) { | |
48 | $PAGE->set_course($SITE); | |
78946b9b | 49 | $PAGE->set_pagelayout('maintenance'); |
ce221eb5 | 50 | } else { |
51 | require_login($course); | |
52 | } | |
53 | ||
54 | if ($course->id == SITEID) { | |
55 | $coursecontext = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context | |
56 | } else { | |
57 | $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context | |
58 | } | |
59 | $systemcontext = get_context_instance(CONTEXT_SYSTEM); | |
60 | ||
61 | if ($id == -1) { | |
62 | // creating new user | |
63 | require_capability('moodle/user:create', $systemcontext); | |
64 | $user = new object(); | |
65 | $user->id = -1; | |
07ed083e | 66 | $user->auth = 'manual'; |
ce221eb5 | 67 | $user->confirmed = 1; |
68 | $user->deleted = 0; | |
69 | } else { | |
70 | // editing existing user | |
71 | require_capability('moodle/user:update', $systemcontext); | |
72 | if (!$user = $DB->get_record('user', array('id'=>$id))) { | |
73 | print_error('invaliduserid'); | |
29750da1 | 74 | } |
ce221eb5 | 75 | } |
ad6226fb | 76 | |
ce221eb5 | 77 | // remote users cannot be edited |
78 | if ($user->id != -1 and is_mnet_remote_user($user)) { | |
79 | redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}"); | |
80 | } | |
ad6226fb | 81 | |
ce221eb5 | 82 | if ($user->id != $USER->id and is_primary_admin($user->id)) { // Can't edit primary admin |
83 | print_error('adminprimarynoedit'); | |
84 | } | |
ad6226fb | 85 | |
ce221eb5 | 86 | if (isguestuser($user->id)) { // the real guest user can not be edited |
87 | print_error('guestnoeditprofileother'); | |
88 | } | |
ad6226fb | 89 | |
ce221eb5 | 90 | if ($user->deleted) { |
91 | echo $OUTPUT->header(); | |
92 | echo $OUTPUT->heading(get_string('userdeleted')); | |
93 | echo $OUTPUT->footer(); | |
94 | die; | |
95 | } | |
96 | ||
97 | if ($user->id == -1) { | |
98 | admin_externalpage_setup('addnewuser', '', array('id' => -1)); | |
99 | } else if ($user->id != $USER->id) { | |
100 | admin_externalpage_setup('editusers', '', array('id' => $user->id, 'course' => SITEID), $CFG->wwwroot . '/user/editadvanced.php'); | |
101 | } | |
102 | ||
103 | //load user preferences | |
104 | useredit_load_preferences($user); | |
105 | ||
106 | //Load custom profile fields data | |
107 | profile_load_data($user); | |
108 | ||
109 | //User interests | |
110 | if (!empty($CFG->usetags)) { | |
111 | require_once($CFG->dirroot.'/tag/lib.php'); | |
112 | $user->interests = tag_get_tags_array('user', $id); | |
113 | } | |
114 | ||
8bdc9cac | 115 | if ($user->id !== -1) { |
4f0c2d00 | 116 | $usercontext = get_context_instance(CONTEXT_USER, $user->id); |
8bdc9cac | 117 | $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'forcehttps'=>false); |
4f0c2d00 | 118 | $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user_profile', $user->id); |
8bdc9cac | 119 | } else { |
4f0c2d00 | 120 | $usercontext = null; |
8bdc9cac SH |
121 | // This is a new user, we don't want to add files here |
122 | $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false); | |
123 | } | |
124 | ||
ce221eb5 | 125 | //create form |
8bdc9cac | 126 | $userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions)); |
ce221eb5 | 127 | $userform->set_data($user); |
128 | ||
129 | if ($usernew = $userform->get_data()) { | |
130 | add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", ''); | |
131 | ||
132 | if (empty($usernew->auth)) { | |
133 | //user editing self | |
134 | $authplugin = get_auth_plugin($user->auth); | |
135 | unset($usernew->auth); //can not change/remove | |
136 | } else { | |
137 | $authplugin = get_auth_plugin($usernew->auth); | |
1e1c51a3 | 138 | } |
07ed083e RW |
139 | |
140 | $usernew->username = clean_param($usernew->username, PARAM_USERNAME); | |
141 | $usernew->timemodified = time(); | |
ad6226fb | 142 | |
ce221eb5 | 143 | if ($usernew->id == -1) { |
144 | //TODO check out if it makes sense to create account with this auth plugin and what to do with the password | |
145 | unset($usernew->id); | |
8bdc9cac | 146 | $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user_profile', null); |
ce221eb5 | 147 | $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user |
d3d393ab RW |
148 | $usernew->confirmed = 1; |
149 | $usernew->timecreated = time(); | |
07ed083e | 150 | $usernew->password = hash_internal_user_password($usernew->newpassword); |
ce221eb5 | 151 | $usernew->id = $DB->insert_record('user', $usernew); |
152 | $usercreated = true; | |
ad6226fb | 153 | |
ce221eb5 | 154 | } else { |
4f0c2d00 | 155 | $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user_profile', $usernew->id); |
ce221eb5 | 156 | $DB->update_record('user', $usernew); |
157 | // pass a true $userold here | |
158 | if (! $authplugin->user_update($user, $userform->get_data())) { | |
159 | // auth update failed, rollback for moodle | |
160 | $DB->update_record('user', $user); | |
161 | print_error('cannotupdateuseronexauth', '', '', $user->auth); | |
d8734783 | 162 | } |
ad6226fb | 163 | |
ce221eb5 | 164 | //set new password if specified |
165 | if (!empty($usernew->newpassword)) { | |
166 | if ($authplugin->can_change_password()) { | |
167 | if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){ | |
168 | print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth); | |
ad6226fb | 169 | } |
170 | } | |
171 | } | |
ce221eb5 | 172 | $usercreated = false; |
173 | } | |
ad6226fb | 174 | |
ce221eb5 | 175 | $usercontext = get_context_instance(CONTEXT_USER, $usernew->id); |
98bc6446 | 176 | |
ce221eb5 | 177 | //update preferences |
178 | useredit_update_user_preference($usernew); | |
ad6226fb | 179 | |
ce221eb5 | 180 | // update tags |
181 | if (!empty($CFG->usetags)) { | |
182 | useredit_update_interests($usernew, $usernew->interests); | |
183 | } | |
1e1c51a3 | 184 | |
ce221eb5 | 185 | //update user picture |
186 | if (!empty($CFG->gdversion)) { | |
187 | useredit_update_picture($usernew, $userform); | |
188 | } | |
ad6226fb | 189 | |
ce221eb5 | 190 | // update mail bounces |
191 | useredit_update_bounces($user, $usernew); | |
ad6226fb | 192 | |
ce221eb5 | 193 | // update forum track preference |
194 | useredit_update_trackforums($user, $usernew); | |
ad6226fb | 195 | |
ce221eb5 | 196 | // save custom profile fields data |
197 | profile_save_data($usernew); | |
ad6226fb | 198 | |
ce221eb5 | 199 | // reload from db |
200 | $usernew = $DB->get_record('user', array('id'=>$usernew->id)); | |
5e61d1a4 | 201 | |
ce221eb5 | 202 | // trigger events |
203 | if ($usercreated) { | |
204 | //set default message preferences | |
205 | if (!message_set_default_message_preferences( $usernew )){ | |
206 | print_error('cannotsavemessageprefs', 'message'); | |
2942a5cd | 207 | } |
ce221eb5 | 208 | events_trigger('user_created', $usernew); |
209 | } else { | |
210 | events_trigger('user_updated', $usernew); | |
211 | } | |
2942a5cd | 212 | |
ce221eb5 | 213 | if ($user->id == $USER->id) { |
214 | // Override old $USER session variable | |
215 | foreach ((array)$usernew as $variable => $value) { | |
216 | $USER->$variable = $value; | |
217 | } | |
218 | if (!empty($USER->newadminuser)) { | |
219 | unset($USER->newadminuser); | |
220 | // apply defaults again - some of them might depend on admin user info, backup, roles, etc. | |
221 | admin_apply_default_settings(NULL , false); | |
222 | // redirect to admin/ to continue with installation | |
223 | redirect("$CFG->wwwroot/$CFG->admin/"); | |
afb5b0ae | 224 | } else { |
ce221eb5 | 225 | redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id"); |
ad6226fb | 226 | } |
ce221eb5 | 227 | } else { |
07ed083e | 228 | session_gc(); // remove stale sessions |
ce221eb5 | 229 | redirect("$CFG->wwwroot/$CFG->admin/user.php"); |
ad6226fb | 230 | } |
ce221eb5 | 231 | //never reached |
232 | } | |
ad6226fb | 233 | |
234 | ||
235 | /// Display page header | |
ce221eb5 | 236 | if ($user->id == -1 or ($user->id != $USER->id)) { |
237 | if ($user->id == -1) { | |
61ef8f9f | 238 | echo $OUTPUT->header(); |
ad6226fb | 239 | } else { |
61ef8f9f | 240 | echo $OUTPUT->header(); |
ce221eb5 | 241 | $userfullname = fullname($user, true); |
242 | echo $OUTPUT->heading($userfullname); | |
243 | } | |
244 | } else if (!empty($USER->newadminuser)) { | |
245 | $strinstallation = get_string('installation', 'install'); | |
246 | $strprimaryadminsetup = get_string('primaryadminsetup'); | |
247 | ||
248 | $PAGE->navbar->add($strprimaryadminsetup); | |
249 | $PAGE->set_title($strinstallation); | |
250 | $PAGE->set_heading($strinstallation); | |
251 | $PAGE->set_cacheable(false); | |
252 | ||
253 | echo $OUTPUT->header(); | |
254 | echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter'); | |
255 | echo '<br />'; | |
256 | } else { | |
257 | $streditmyprofile = get_string('editmyprofile'); | |
258 | $strparticipants = get_string('participants'); | |
259 | $strnewuser = get_string('newuser'); | |
260 | $userfullname = fullname($user, true); | |
261 | ||
262 | $link = null; | |
263 | if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) { | |
a6855934 | 264 | $link = new moodle_url("/user/index.php", array('id'=>$course->id)); |
ad6226fb | 265 | } |
ce221eb5 | 266 | $PAGE->navbar->add($strparticipants, $link); |
a6855934 | 267 | $link = new moodle_url('/user/view.php', array('id'=>$user->id, 'course'=>$course->id)); |
ce221eb5 | 268 | $PAGE->navbar->add($userfullname, $link); |
269 | $PAGE->navbar->add($streditmyprofile); | |
270 | ||
271 | $PAGE->set_title("$course->shortname: $streditmyprofile"); | |
272 | $PAGE->set_heading($course->fullname); | |
273 | ||
274 | echo $OUTPUT->header(); | |
275 | /// Print tabs at the top | |
276 | $showroles = 1; | |
277 | $currenttab = 'editprofile'; | |
278 | require('tabs.php'); | |
279 | } | |
ad6226fb | 280 | |
281 | /// Finally display THE form | |
ce221eb5 | 282 | $userform->display(); |
ad6226fb | 283 | |
284 | /// and proper footer | |
ce221eb5 | 285 | echo $OUTPUT->footer(); |
ad6226fb | 286 |