MDL-23479 scorm restore code - correct time shifting
[moodle.git] / user / editadvanced.php
blob8fadc91e238f8ed7e46cc497f106b5b11aa00b63
1 <?php
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.
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/>.
18 /**
19 * Allows you to edit a users profile
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
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');
33 httpsrequired();
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)
38 $url = new moodle_url('/user/editadvanced.php', array('course'=>$course));
39 if ($id !== $USER->id) {
40 $url->param('id', $id);
42 $PAGE->set_url($url);
44 $course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
46 if (!empty($USER->newadminuser)) {
47 $PAGE->set_course($SITE);
48 $PAGE->set_pagelayout('maintenance');
49 } else {
50 require_login($course);
51 $PAGE->set_pagelayout('admin');
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
59 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
61 if ($id == -1) {
62 // creating new user
63 $user = new object();
64 $user->id = -1;
65 $user->auth = 'manual';
66 $user->confirmed = 1;
67 $user->deleted = 0;
68 require_capability('moodle/user:create', $systemcontext);
69 admin_externalpage_setup('addnewuser', '', array('id' => -1));
70 } else {
71 // editing existing user
72 require_capability('moodle/user:update', $systemcontext);
73 $user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST);
74 $PAGE->set_context(get_context_instance(CONTEXT_USER, $user->id));
75 if ($user->id == $USER->id) {
76 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
77 $node->make_active();
78 $PAGE->navbar->includesettingsbase = true;
80 } else {
81 $PAGE->navigation->extend_for_user($user);
85 // remote users cannot be edited
86 if ($user->id != -1 and is_mnet_remote_user($user)) {
87 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
90 if ($user->id != $USER->id and is_primary_admin($user->id)) { // Can't edit primary admin
91 print_error('adminprimarynoedit');
94 if (isguestuser($user->id)) { // the real guest user can not be edited
95 print_error('guestnoeditprofileother');
98 if ($user->deleted) {
99 echo $OUTPUT->header();
100 echo $OUTPUT->heading(get_string('userdeleted'));
101 echo $OUTPUT->footer();
102 die;
105 //load user preferences
106 useredit_load_preferences($user);
108 //Load custom profile fields data
109 profile_load_data($user);
111 //User interests
112 if (!empty($CFG->usetags)) {
113 require_once($CFG->dirroot.'/tag/lib.php');
114 $user->interests = tag_get_tags_array('user', $id);
117 if ($user->id !== -1) {
118 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
119 $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'forcehttps'=>false);
120 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
121 } else {
122 $usercontext = null;
123 // This is a new user, we don't want to add files here
124 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
127 //create form
128 $userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions));
129 $userform->set_data($user);
131 if ($usernew = $userform->get_data()) {
132 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
134 if (empty($usernew->auth)) {
135 //user editing self
136 $authplugin = get_auth_plugin($user->auth);
137 unset($usernew->auth); //can not change/remove
138 } else {
139 $authplugin = get_auth_plugin($usernew->auth);
142 $usernew->timemodified = time();
144 if ($usernew->id == -1) {
145 //TODO check out if it makes sense to create account with this auth plugin and what to do with the password
146 unset($usernew->id);
147 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
148 $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user
149 $usernew->confirmed = 1;
150 $usernew->timecreated = time();
151 $usernew->password = hash_internal_user_password($usernew->newpassword);
152 $usernew->id = $DB->insert_record('user', $usernew);
153 $usercreated = true;
155 } else {
156 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
157 $DB->update_record('user', $usernew);
158 // pass a true $userold here
159 if (! $authplugin->user_update($user, $userform->get_data())) {
160 // auth update failed, rollback for moodle
161 $DB->update_record('user', $user);
162 print_error('cannotupdateuseronexauth', '', '', $user->auth);
165 //set new password if specified
166 if (!empty($usernew->newpassword)) {
167 if ($authplugin->can_change_password()) {
168 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){
169 print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth);
173 $usercreated = false;
176 $usercontext = get_context_instance(CONTEXT_USER, $usernew->id);
178 //update preferences
179 useredit_update_user_preference($usernew);
181 // update tags
182 if (!empty($CFG->usetags)) {
183 useredit_update_interests($usernew, $usernew->interests);
186 //update user picture
187 if (!empty($CFG->gdversion)) {
188 useredit_update_picture($usernew, $userform);
191 // update mail bounces
192 useredit_update_bounces($user, $usernew);
194 // update forum track preference
195 useredit_update_trackforums($user, $usernew);
197 // save custom profile fields data
198 profile_save_data($usernew);
200 // reload from db
201 $usernew = $DB->get_record('user', array('id'=>$usernew->id));
203 // trigger events
204 if ($usercreated) {
205 //set default message preferences
206 if (!message_set_default_message_preferences( $usernew )){
207 print_error('cannotsavemessageprefs', 'message');
209 events_trigger('user_created', $usernew);
210 } else {
211 events_trigger('user_updated', $usernew);
214 if ($user->id == $USER->id) {
215 // Override old $USER session variable
216 foreach ((array)$usernew as $variable => $value) {
217 $USER->$variable = $value;
219 // preload custom fields
220 profile_load_custom_fields($USER);
222 if (!empty($USER->newadminuser)) {
223 unset($USER->newadminuser);
224 // apply defaults again - some of them might depend on admin user info, backup, roles, etc.
225 admin_apply_default_settings(NULL , false);
226 // redirect to admin/ to continue with installation
227 redirect("$CFG->wwwroot/$CFG->admin/");
228 } else {
229 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
231 } else {
232 session_gc(); // remove stale sessions
233 redirect("$CFG->wwwroot/$CFG->admin/user.php");
235 //never reached
239 /// Display page header
240 if ($user->id == -1 or ($user->id != $USER->id)) {
241 if ($user->id == -1) {
242 echo $OUTPUT->header();
243 } else {
244 $PAGE->set_heading($SITE->fullname);
245 echo $OUTPUT->header();
246 $userfullname = fullname($user, true);
247 echo $OUTPUT->heading($userfullname);
249 } else if (!empty($USER->newadminuser)) {
250 $strinstallation = get_string('installation', 'install');
251 $strprimaryadminsetup = get_string('primaryadminsetup');
253 $PAGE->navbar->add($strprimaryadminsetup);
254 $PAGE->set_title($strinstallation);
255 $PAGE->set_heading($strinstallation);
256 $PAGE->set_cacheable(false);
258 echo $OUTPUT->header();
259 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
260 echo '<br />';
261 } else {
262 $streditmyprofile = get_string('editmyprofile');
263 $strparticipants = get_string('participants');
264 $strnewuser = get_string('newuser');
265 $userfullname = fullname($user, true);
267 $PAGE->set_title("$course->shortname: $streditmyprofile");
268 $PAGE->set_heading($course->fullname);
270 echo $OUTPUT->header();
271 echo $OUTPUT->heading($userfullname);
274 /// Finally display THE form
275 $userform->display();
277 /// and proper footer
278 echo $OUTPUT->footer();