MDL-16553 Assignment, student view: whitespace fix
[moodle.git] / user / edit.php
blob6da79f65bb2733989564d1534a85d85a35280578
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 if (empty($user->country)) {
104 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
105 unset($user->country);
107 $userform->set_data($user);
109 $email_changed = false;
111 if ($usernew = $userform->get_data()) {
113 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
115 $email_changed_html = '';
117 if ($CFG->emailchangeconfirmation) {
118 // Handle change of email carefully for non-trusted users
119 if (isset($usernew->email) and $user->email != stripslashes($usernew->email) && !has_capability('moodle/user:update', $systemcontext)) {
120 $a = new stdClass();
121 $a->newemail = s(stripslashes($usernew->email));
122 $usernew->preference_newemail = $usernew->email;
123 $usernew->preference_newemailkey = random_string(20);
124 $usernew->preference_newemailattemptsleft = 3;
125 $a->oldemail = s($user->email);
126 $usernew->email = addslashes($user->email);
128 $email_changed_html = print_box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice', true);
129 $email_changed_html .= print_continue("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id", true);
130 $email_changed = true;
134 $authplugin = get_auth_plugin($user->auth);
136 $usernew->timemodified = time();
138 if (!update_record('user', $usernew)) {
139 error('Error updating user record');
142 // pass a true $userold here
143 if (! $authplugin->user_update($user, stripslashes_recursive($usernew))) {
144 // auth update failed, rollback for moodle
145 update_record('user', addslashes_object($user));
146 error('Failed to update user data on external auth: '.$user->auth.
147 '. See the server logs for more details.');
150 //update preferences
151 useredit_update_user_preference($usernew);
153 //update interests
154 if (!empty($CFG->usetags)) {
155 useredit_update_interests($usernew, $usernew->interests);
158 //update user picture
159 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
160 useredit_update_picture($usernew, $userform);
163 // update mail bounces
164 useredit_update_bounces($user, $usernew);
166 /// update forum track preference
167 useredit_update_trackforums($user, $usernew);
169 // save custom profile fields data
170 profile_save_data($usernew);
172 // If email was changed, send confirmation email now
173 if ($email_changed && $CFG->emailchangeconfirmation) {
174 $temp_user = fullclone($user);
175 $temp_user->email = $usernew->preference_newemail;
176 $temp_user->emailstop = NULL;
178 $a = new stdClass();
179 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
180 $a->site = $SITE->fullname;
181 $a->fullname = fullname($user, true);
183 $emailupdatemessage = get_string('auth_emailupdatemessage', 'auth', $a);
184 $emailupdatetitle = get_string('auth_emailupdatetitle', 'auth', $a);
186 if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
187 die("could not send email!");
191 // reload from db
192 $usernew = get_record('user', 'id', $user->id);
193 events_trigger('user_updated', $usernew);
195 if ($USER->id == $user->id) {
196 // Override old $USER session variable if needed
197 foreach ((array)$usernew as $variable => $value) {
198 $USER->$variable = $value;
202 if (!$email_changed || !$CFG->emailchangeconfirmation) {
203 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
208 /// Display page header
209 $streditmyprofile = get_string('editmyprofile');
210 $strparticipants = get_string('participants');
211 $userfullname = fullname($user, true);
213 $navlinks = array();
214 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
215 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
217 $navlinks[] = array('name' => $userfullname,
218 'link' => "view.php?id=$user->id&amp;course=$course->id",
219 'type' => 'misc');
220 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
221 $navigation = build_navigation($navlinks);
222 print_header("$course->shortname: $streditmyprofile", $course->fullname, $navigation, "");
224 /// Print tabs at the top
225 $showroles = 1;
226 $currenttab = 'editprofile';
227 require('tabs.php');
229 if ($email_changed) {
230 echo $email_changed_html;
231 } else {
232 /// Finally display THE form
233 $userform->display();
236 /// and proper footer
237 print_footer($course);