Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / user / edit.php
blob2e1e3d5a12b8ccbc4d2326a15929f0dc0f75e75d
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->libdir.'/gdlib.php');
5 require_once($CFG->dirroot.'/user/edit_form.php');
6 require_once($CFG->dirroot.'/user/editlib.php');
7 require_once($CFG->dirroot.'/user/profile/lib.php');
9 httpsrequired();
11 $userid = optional_param('id', $USER->id, PARAM_INT); // user id
12 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
13 $cancelemailchange = optional_param('cancelemailchange', false, PARAM_INT); // course id (defaults to Site)
15 if (!$course = get_record('course', 'id', $course)) {
16 error('Course ID was incorrect');
19 if ($course->id != SITEID) {
20 require_login($course);
21 } else if (!isloggedin()) {
22 if (empty($SESSION->wantsurl)) {
23 $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
25 redirect($CFG->httpswwwroot.'/login/index.php');
28 // Guest can not edit
29 if (isguestuser()) {
30 print_error('guestnoeditprofile');
33 // The user profile we are editing
34 if (!$user = get_record('user', 'id', $userid)) {
35 error('User ID was incorrect');
38 // Guest can not be edited
39 if (isguestuser($user)) {
40 print_error('guestnoeditprofile');
43 // User interests separated by commas
44 if (!empty($CFG->usetags)) {
45 require_once($CFG->dirroot.'/tag/lib.php');
46 $user->interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT);
49 // remote users cannot be edited
50 if (is_mnet_remote_user($user)) {
51 redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
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);
60 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
62 // check access control
63 if ($user->id == $USER->id) {
64 //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
65 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
66 error('Can not edit own profile, sorry.');
69 } else {
70 // teachers, parents, etc.
71 require_capability('moodle/user:editprofile', $personalcontext);
72 // no editing of guest user account
73 if (isguestuser($user->id)) {
74 print_error('guestnoeditprofileother');
76 // no editing of primary admin!
77 if (is_primary_admin($user->id)) {
78 print_error('adminprimarynoedit');
82 if ($user->deleted) {
83 print_header();
84 print_heading(get_string('userdeleted'));
85 print_footer($course);
86 die;
89 // Process email change cancellation
90 if ($cancelemailchange) {
91 cancel_email_update($user->id);
94 //load user preferences
95 useredit_load_preferences($user);
97 //Load custom profile fields data
98 profile_load_data($user);
101 //create form
102 $userform = new user_edit_form();
103 $userform->set_data($user);
105 $email_changed = false;
107 if ($usernew = $userform->get_data()) {
109 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
111 $email_changed_html = '';
113 if ($CFG->emailchangeconfirmation) {
114 // Handle change of email carefully for non-trusted users
115 if ($user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
116 $a = new stdClass();
117 $a->newemail = $usernew->preference_newemail = $usernew->email;
118 $usernew->preference_newemailkey = random_string(20);
119 $usernew->preference_newemailattemptsleft = 3;
120 $a->oldemail = $usernew->email = $user->email;
122 $email_changed_html = print_box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice', true);
123 $email_changed_html .= print_continue("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id", true);
124 $email_changed = true;
128 $authplugin = get_auth_plugin($user->auth);
130 $usernew->timemodified = time();
132 if (!update_record('user', $usernew)) {
133 error('Error updating user record');
136 // pass a true $userold here
137 if (! $authplugin->user_update($user, $userform->get_data(false))) {
138 // auth update failed, rollback for moodle
139 update_record('user', addslashes_object($user));
140 error('Failed to update user data on external auth: '.$user->auth.
141 '. See the server logs for more details.');
144 //update preferences
145 useredit_update_user_preference($usernew);
147 //update interests
148 if (!empty($CFG->usetags)) {
149 useredit_update_interests($usernew, $usernew->interests);
152 //update user picture
153 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
154 useredit_update_picture($usernew, $userform);
157 // update mail bounces
158 useredit_update_bounces($user, $usernew);
160 /// update forum track preference
161 useredit_update_trackforums($user, $usernew);
163 // save custom profile fields data
164 profile_save_data($usernew);
166 // If email was changed, send confirmation email now
167 if ($email_changed && $CFG->emailchangeconfirmation) {
168 $temp_user = fullclone($user);
169 $temp_user->email = $usernew->preference_newemail;
170 $temp_user->emailstop = NULL;
172 $a = new stdClass();
173 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
174 $a->site = $SITE->fullname;
175 $a->fullname = fullname($user, true);
177 $emailupdatemessage = get_string('auth_emailupdatemessage', 'auth', $a);
178 $emailupdatetitle = get_string('auth_emailupdatetitle', 'auth', $a);
180 if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
181 die("could not send email!");
185 // reload from db
186 $usernew = get_record('user', 'id', $user->id);
187 events_trigger('user_updated', $usernew);
189 if ($USER->id == $user->id) {
190 // Override old $USER session variable if needed
191 foreach ((array)$usernew as $variable => $value) {
192 $USER->$variable = $value;
196 if (!$email_changed || !$CFG->emailchangeconfirmation) {
197 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
202 /// Display page header
203 $streditmyprofile = get_string('editmyprofile');
204 $strparticipants = get_string('participants');
205 $userfullname = fullname($user, true);
207 $navlinks = array();
208 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
209 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
211 $navlinks[] = array('name' => $userfullname,
212 'link' => "view.php?id=$user->id&amp;course=$course->id",
213 'type' => 'misc');
214 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
215 $navigation = build_navigation($navlinks);
216 print_header("$course->shortname: $streditmyprofile", $course->fullname, $navigation, "");
218 /// Print tabs at the top
219 $showroles = 1;
220 $currenttab = 'editprofile';
221 require('tabs.php');
223 if ($email_changed) {
224 echo $email_changed_html;
225 } else {
226 /// Finally display THE form
227 $userform->display();
230 /// and proper footer
231 print_footer($course);