MDL-10689:
[moodle-linuxchix.git] / user / editadvanced.php
blob8bdcfa2d34b30d36cd21a101fd66afb90d4eaf25
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->libdir.'/gdlib.php');
5 require_once($CFG->libdir.'/adminlib.php');
6 require_once($CFG->dirroot.'/user/editadvanced_form.php');
7 require_once($CFG->dirroot.'/user/editlib.php');
8 require_once($CFG->dirroot.'/user/profile/lib.php');
10 httpsrequired();
12 $id = optional_param('id', $USER->id, PARAM_INT); // user id; -1 if creating new user
13 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
15 if (!$course = get_record('course', 'id', $course)) {
16 error('Course ID was incorrect');
18 require_login($course->id);
20 if ($id == -1) {
21 // creating new user
22 require_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM));
23 $user = new object();
24 $user->id = -1;
25 $user->auth = 'manual';
26 $user->confirmed = 1;
27 } else {
28 // editing existing user
29 require_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
30 if (!$user = get_record('user', 'id', $id)) {
31 error('User ID was incorrect');
35 // remote users cannot be edited
36 if ($user->id != -1 and is_mnet_remote_user($user)) {
37 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
40 $mainadmin = get_admin();
41 if ($user->id != $USER->id and $user->id == $mainadmin->id) { // Can't edit primary admin
42 print_error('adminprimarynoedit');
45 if (isguestuser($user->id)) { // the real guest user can not be edited
46 print_error('guestnoeditprofileother');
49 //load user preferences
50 useredit_load_preferences($user);
52 //Load custom profile fields data
53 profile_load_data($user);
55 //user interests separated by commas
56 if (!empty($CFG->usetags)) {
57 require_once($CFG->dirroot.'/tag/lib.php');
58 $user->interests = tag_names_csv(get_item_tags('user',$id));
61 //create form
62 $userform = new user_editadvanced_form();
63 $userform->set_data($user);
65 if ($usernew = $userform->get_data()) {
66 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
68 if (empty($usernew->auth)) {
69 //user editing self
70 $authplugin = get_auth_plugin($user->auth);
71 unset($usernew->auth); //can not change/remove
72 } else {
73 $authplugin = get_auth_plugin($usernew->auth);
76 $usernew->username = trim($usernew->username);
77 $usernew->timemodified = time();
79 if ($usernew->id == -1) {
80 //TODO check out if it makes sense to create account with this auth plugin and what to do with the password
81 unset($usernew->id);
82 $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user
83 $usernew->confirmed = 1;
84 $usernew->password = hash_internal_user_password($usernew->newpassword);
85 if (!$usernew->id = insert_record('user', $usernew)) {
86 error('Error creating user record');
88 } else {
89 if (!update_record('user', $usernew)) {
90 error('Error updating user record');
92 // pass a true $userold here
93 if (! $authplugin->user_update($user, $userform->get_data(false))) {
94 // auth update failed, rollback for moodle
95 update_record('user', addslashes_object($user));
96 error('Failed to update user data on external auth: '.$user->auth.
97 '. See the server logs for more details.');
100 //set new password if specified
101 if (!empty($usernew->newpassword)) {
102 if ($authplugin->can_change_password()) {
103 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){
104 error('Failed to update password on external auth: ' . $usernew->auth .
105 '. See the server logs for more details.');
110 // MDL-9983
111 events_trigger('user_updated', $usernew);
114 //update preferences
115 useredit_update_user_preference($usernew);
117 // update tags
118 if (!empty($CFG->usetags)) {
119 useredit_update_interests($usernew, $usernew->interests);
122 //update user picture
123 if (!empty($CFG->gdversion)) {
124 useredit_update_picture($usernew, $userform);
127 // update mail bounces
128 useredit_update_bounces($user, $usernew);
130 // update forum track preference
131 useredit_update_trackforums($user, $usernew);
133 // save custom profile fields data
134 profile_save_data($usernew);
136 if ($user->id == $USER->id) {
137 // Override old $USER session variable
138 $usernew = (array)get_record('user', 'id', $usernew->id); // reload from db
139 foreach ($usernew as $variable => $value) {
140 $USER->$variable = $value;
142 if (!empty($USER->newadminuser)) {
143 unset($USER->newadminuser);
144 // redirect to admin/ to continue with installation
145 redirect("$CFG->wwwroot/$CFG->admin/");
146 } else {
147 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
149 } else {
150 redirect("$CFG->wwwroot/$CFG->admin/user.php");
152 //never reached
156 /// Display page header
157 if ($user->id == -1 or ($user->id != $USER->id)) {
158 if ($user->id == -1) {
159 admin_externalpage_setup('addnewuser');
160 admin_externalpage_print_header();
161 } else {
162 admin_externalpage_setup('editusers');
163 admin_externalpage_print_header();
164 $userfullname = fullname($user, true);
165 print_heading($userfullname);
167 } else if (!empty($USER->newadminuser)) {
168 $strprimaryadminsetup = get_string('primaryadminsetup');
169 print_header($strprimaryadminsetup, $strprimaryadminsetup);
170 print_simple_box(get_string('configintroadmin', 'admin'), 'center', '50%');
171 echo '<br />';
172 } else {
173 $streditmyprofile = get_string('editmyprofile');
174 $strparticipants = get_string('participants');
175 $strnewuser = get_string('newuser');
176 $userfullname = fullname($user, true);
178 $navlinks = array();
179 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
180 $navlinks[] = array('name' => $userfullname,
181 'link' => "view.php?id=$user->id&amp;course=$course->id",
182 'type' => 'misc');
183 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
184 $navigation = build_navigation($navlinks);
185 print_header("$course->shortname: $streditmyprofile", $course->fullname, $navigation, "");
187 /// Print tabs at the top
188 $showroles = 1;
189 $currenttab = 'editprofile';
190 require('tabs.php');
193 /// Finally display THE form
194 $userform->display();
196 /// and proper footer
197 if (!empty($USER->newadminuser)) {
198 print_footer('none');
199 } else {
200 print_footer($course);