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 | ||
3406acde SH |
44 | $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST); |
45 | ||
ce221eb5 | 46 | if (!empty($USER->newadminuser)) { |
47 | $PAGE->set_course($SITE); | |
78946b9b | 48 | $PAGE->set_pagelayout('maintenance'); |
ce221eb5 | 49 | } else { |
50 | require_login($course); | |
3406acde | 51 | $PAGE->set_pagelayout('admin'); |
ce221eb5 | 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 | |
ce221eb5 | 63 | $user = new object(); |
64 | $user->id = -1; | |
6b8ad965 | 65 | $user->auth = 'manual'; |
ce221eb5 | 66 | $user->confirmed = 1; |
67 | $user->deleted = 0; | |
3406acde SH |
68 | require_capability('moodle/user:create', $systemcontext); |
69 | admin_externalpage_setup('addnewuser', '', array('id' => -1)); | |
ce221eb5 | 70 | } else { |
71 | // editing existing user | |
72 | require_capability('moodle/user:update', $systemcontext); | |
3406acde SH |
73 | $user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST); |
74 | $PAGE->set_context(get_context_instance(CONTEXT_USER, $user->id)); | |
75 | $PAGE->navigation->extend_for_user($user); | |
ce221eb5 | 76 | } |
ad6226fb | 77 | |
ce221eb5 | 78 | // remote users cannot be edited |
79 | if ($user->id != -1 and is_mnet_remote_user($user)) { | |
80 | redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}"); | |
81 | } | |
ad6226fb | 82 | |
ce221eb5 | 83 | if ($user->id != $USER->id and is_primary_admin($user->id)) { // Can't edit primary admin |
84 | print_error('adminprimarynoedit'); | |
85 | } | |
ad6226fb | 86 | |
ce221eb5 | 87 | if (isguestuser($user->id)) { // the real guest user can not be edited |
88 | print_error('guestnoeditprofileother'); | |
89 | } | |
ad6226fb | 90 | |
ce221eb5 | 91 | if ($user->deleted) { |
92 | echo $OUTPUT->header(); | |
93 | echo $OUTPUT->heading(get_string('userdeleted')); | |
94 | echo $OUTPUT->footer(); | |
95 | die; | |
96 | } | |
97 | ||
ce221eb5 | 98 | //load user preferences |
99 | useredit_load_preferences($user); | |
100 | ||
101 | //Load custom profile fields data | |
102 | profile_load_data($user); | |
103 | ||
104 | //User interests | |
105 | if (!empty($CFG->usetags)) { | |
106 | require_once($CFG->dirroot.'/tag/lib.php'); | |
107 | $user->interests = tag_get_tags_array('user', $id); | |
108 | } | |
109 | ||
8bdc9cac | 110 | if ($user->id !== -1) { |
4f0c2d00 | 111 | $usercontext = get_context_instance(CONTEXT_USER, $user->id); |
8bdc9cac | 112 | $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'forcehttps'=>false); |
64f93798 | 113 | $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0); |
8bdc9cac | 114 | } else { |
4f0c2d00 | 115 | $usercontext = null; |
8bdc9cac SH |
116 | // This is a new user, we don't want to add files here |
117 | $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false); | |
118 | } | |
119 | ||
ce221eb5 | 120 | //create form |
8bdc9cac | 121 | $userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions)); |
ce221eb5 | 122 | $userform->set_data($user); |
123 | ||
124 | if ($usernew = $userform->get_data()) { | |
125 | add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", ''); | |
126 | ||
127 | if (empty($usernew->auth)) { | |
128 | //user editing self | |
129 | $authplugin = get_auth_plugin($user->auth); | |
130 | unset($usernew->auth); //can not change/remove | |
131 | } else { | |
132 | $authplugin = get_auth_plugin($usernew->auth); | |
1e1c51a3 | 133 | } |
6b8ad965 PS |
134 | |
135 | $usernew->timemodified = time(); | |
ad6226fb | 136 | |
ce221eb5 | 137 | if ($usernew->id == -1) { |
138 | //TODO check out if it makes sense to create account with this auth plugin and what to do with the password | |
139 | unset($usernew->id); | |
64f93798 | 140 | $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null); |
ce221eb5 | 141 | $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user |
d3d393ab RW |
142 | $usernew->confirmed = 1; |
143 | $usernew->timecreated = time(); | |
6b8ad965 | 144 | $usernew->password = hash_internal_user_password($usernew->newpassword); |
ce221eb5 | 145 | $usernew->id = $DB->insert_record('user', $usernew); |
146 | $usercreated = true; | |
ad6226fb | 147 | |
ce221eb5 | 148 | } else { |
64f93798 | 149 | $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0); |
ce221eb5 | 150 | $DB->update_record('user', $usernew); |
151 | // pass a true $userold here | |
152 | if (! $authplugin->user_update($user, $userform->get_data())) { | |
153 | // auth update failed, rollback for moodle | |
154 | $DB->update_record('user', $user); | |
155 | print_error('cannotupdateuseronexauth', '', '', $user->auth); | |
d8734783 | 156 | } |
ad6226fb | 157 | |
ce221eb5 | 158 | //set new password if specified |
159 | if (!empty($usernew->newpassword)) { | |
160 | if ($authplugin->can_change_password()) { | |
161 | if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){ | |
162 | print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth); | |
ad6226fb | 163 | } |
164 | } | |
165 | } | |
ce221eb5 | 166 | $usercreated = false; |
167 | } | |
ad6226fb | 168 | |
ce221eb5 | 169 | $usercontext = get_context_instance(CONTEXT_USER, $usernew->id); |
98bc6446 | 170 | |
ce221eb5 | 171 | //update preferences |
172 | useredit_update_user_preference($usernew); | |
ad6226fb | 173 | |
ce221eb5 | 174 | // update tags |
175 | if (!empty($CFG->usetags)) { | |
176 | useredit_update_interests($usernew, $usernew->interests); | |
177 | } | |
1e1c51a3 | 178 | |
ce221eb5 | 179 | //update user picture |
180 | if (!empty($CFG->gdversion)) { | |
181 | useredit_update_picture($usernew, $userform); | |
182 | } | |
ad6226fb | 183 | |
ce221eb5 | 184 | // update mail bounces |
185 | useredit_update_bounces($user, $usernew); | |
ad6226fb | 186 | |
ce221eb5 | 187 | // update forum track preference |
188 | useredit_update_trackforums($user, $usernew); | |
ad6226fb | 189 | |
ce221eb5 | 190 | // save custom profile fields data |
191 | profile_save_data($usernew); | |
ad6226fb | 192 | |
ce221eb5 | 193 | // reload from db |
194 | $usernew = $DB->get_record('user', array('id'=>$usernew->id)); | |
5e61d1a4 | 195 | |
ce221eb5 | 196 | // trigger events |
197 | if ($usercreated) { | |
198 | //set default message preferences | |
199 | if (!message_set_default_message_preferences( $usernew )){ | |
200 | print_error('cannotsavemessageprefs', 'message'); | |
2942a5cd | 201 | } |
ce221eb5 | 202 | events_trigger('user_created', $usernew); |
203 | } else { | |
204 | events_trigger('user_updated', $usernew); | |
205 | } | |
2942a5cd | 206 | |
ce221eb5 | 207 | if ($user->id == $USER->id) { |
208 | // Override old $USER session variable | |
209 | foreach ((array)$usernew as $variable => $value) { | |
210 | $USER->$variable = $value; | |
211 | } | |
212 | if (!empty($USER->newadminuser)) { | |
213 | unset($USER->newadminuser); | |
214 | // apply defaults again - some of them might depend on admin user info, backup, roles, etc. | |
215 | admin_apply_default_settings(NULL , false); | |
216 | // redirect to admin/ to continue with installation | |
217 | redirect("$CFG->wwwroot/$CFG->admin/"); | |
afb5b0ae | 218 | } else { |
ce221eb5 | 219 | redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id"); |
ad6226fb | 220 | } |
ce221eb5 | 221 | } else { |
6b8ad965 | 222 | session_gc(); // remove stale sessions |
ce221eb5 | 223 | redirect("$CFG->wwwroot/$CFG->admin/user.php"); |
ad6226fb | 224 | } |
ce221eb5 | 225 | //never reached |
226 | } | |
ad6226fb | 227 | |
228 | ||
229 | /// Display page header | |
ce221eb5 | 230 | if ($user->id == -1 or ($user->id != $USER->id)) { |
231 | if ($user->id == -1) { | |
61ef8f9f | 232 | echo $OUTPUT->header(); |
ad6226fb | 233 | } else { |
8cb89f5b | 234 | $PAGE->set_heading($SITE->fullname); |
61ef8f9f | 235 | echo $OUTPUT->header(); |
ce221eb5 | 236 | $userfullname = fullname($user, true); |
237 | echo $OUTPUT->heading($userfullname); | |
238 | } | |
239 | } else if (!empty($USER->newadminuser)) { | |
240 | $strinstallation = get_string('installation', 'install'); | |
241 | $strprimaryadminsetup = get_string('primaryadminsetup'); | |
242 | ||
243 | $PAGE->navbar->add($strprimaryadminsetup); | |
244 | $PAGE->set_title($strinstallation); | |
245 | $PAGE->set_heading($strinstallation); | |
246 | $PAGE->set_cacheable(false); | |
247 | ||
248 | echo $OUTPUT->header(); | |
249 | echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter'); | |
250 | echo '<br />'; | |
251 | } else { | |
252 | $streditmyprofile = get_string('editmyprofile'); | |
253 | $strparticipants = get_string('participants'); | |
254 | $strnewuser = get_string('newuser'); | |
255 | $userfullname = fullname($user, true); | |
256 | ||
ce221eb5 | 257 | $PAGE->set_title("$course->shortname: $streditmyprofile"); |
258 | $PAGE->set_heading($course->fullname); | |
259 | ||
260 | echo $OUTPUT->header(); | |
03d9401e | 261 | echo $OUTPUT->heading($userfullname); |
ce221eb5 | 262 | } |
ad6226fb | 263 | |
264 | /// Finally display THE form | |
ce221eb5 | 265 | $userform->display(); |
ad6226fb | 266 | |
267 | /// and proper footer | |
ce221eb5 | 268 | echo $OUTPUT->footer(); |
ad6226fb | 269 |