MDL-13811: Merged fix to work when email is disabled
[moodle.git] / user / edit.php
CommitLineData
1066e0dc 1<?php // $Id$
f9903ed0 2
d8734783 3 require_once('../config.php');
4 require_once($CFG->libdir.'/gdlib.php');
5 require_once($CFG->dirroot.'/user/edit_form.php');
14a6b7e1 6 require_once($CFG->dirroot.'/user/editlib.php');
d8734783 7 require_once($CFG->dirroot.'/user/profile/lib.php');
2d836c0b 8
dcf6d93c 9 httpsrequired();
10
b0c90e6e 11 $userid = optional_param('id', $USER->id, PARAM_INT); // user id
0e72da1a 12 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
d6ace123 13 $cancelemailchange = optional_param('cancelemailchange', false, PARAM_INT); // course id (defaults to Site)
f9903ed0 14
5d910388 15 if (!$course = $DB->get_record('course', array('id'=>$course))) {
4e244ba8 16 print_error('invalidcourseid');
e41ddc4b 17 }
8f0cd6ef 18
03a2aac1 19 if ($course->id != SITEID) {
20 require_login($course);
21 } else if (!isloggedin()) {
b7c3bb21 22 if (empty($SESSION->wantsurl)) {
80c64fc7 23 $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
03a2aac1 24 }
b7c3bb21 25 redirect($CFG->httpswwwroot.'/login/index.php');
03a2aac1 26 }
ff4220f5 27
277fe19d 28 // Guest can not edit
1cb3da36 29 if (isguestuser()) {
d8734783 30 print_error('guestnoeditprofile');
f9903ed0 31 }
b0c90e6e 32
277fe19d 33 // The user profile we are editing
5d910388 34 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
4e244ba8 35 print_error('invaliduserid');
faebaf0f 36 }
f9903ed0 37
277fe19d 38 // Guest can not be edited
d6ace123 39 if (isguestuser($user)) {
277fe19d 40 print_error('guestnoeditprofile');
41 }
42
43 // User interests separated by commas
1e1c51a3 44 if (!empty($CFG->usetags)) {
45 require_once($CFG->dirroot.'/tag/lib.php');
0aa05b32 46 $user->interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT);
1e1c51a3 47 }
48
56f52742 49 // remote users cannot be edited
50 if (is_mnet_remote_user($user)) {
d8734783 51 redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
56f52742 52 }
53
bb9a123a 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 }
e6eea4f2 59 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
60 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
61
b0c90e6e 62 // check access control
1cb3da36 63 if ($user->id == $USER->id) {
8b3dc202 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)) {
4e244ba8 66 print_error('cannotedityourprofile');
8b3dc202 67 }
1cb3da36 68
69 } else {
b0c90e6e 70 // teachers, parents, etc.
b0c90e6e 71 require_capability('moodle/user:editprofile', $personalcontext);
72 // no editing of guest user account
73 if (isguestuser($user->id)) {
74 print_error('guestnoeditprofileother');
75 }
76 // no editing of primary admin!
fc4b2dec 77 if (is_primary_admin($user->id)) {
b0c90e6e 78 print_error('adminprimarynoedit');
79 }
80 }
81
f5fc83e8 82 if ($user->deleted) {
83 print_header();
84 print_heading(get_string('userdeleted'));
85 print_footer($course);
86 die;
87 }
88
05c38e2b 89 // Process email change cancellation
90 if ($cancelemailchange) {
91 cancel_email_update($user->id);
92 }
93
14a6b7e1 94 //load user preferences
95 useredit_load_preferences($user);
96
97 //Load custom profile fields data
98 profile_load_data($user);
f9903ed0 99
0be6f678 100
d8734783 101 //create form
102 $userform = new user_edit_form();
e8e0bb2d 103 $userform->set_data($user);
2d836c0b 104
d6ace123 105 $email_changed = false;
106
294ce987 107 if ($usernew = $userform->get_data()) {
1e1c51a3 108
d8734783 109 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
f9903ed0 110
d6ace123 111 $email_changed_html = '';
112
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;
121
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;
125 }
126 }
127
d8734783 128 $authplugin = get_auth_plugin($user->auth);
a3447e10 129
2d836c0b 130 $usernew->timemodified = time();
8f0cd6ef 131
5d910388 132 if (!$DB->update_record('user', $usernew)) {
4e244ba8 133 print_error('cannotupdateprofile');
d8734783 134 }
2d836c0b 135
6bc1e5d5 136 // pass a true $userold here
294ce987 137 if (! $authplugin->user_update($user, $userform->get_data())) {
6bc1e5d5 138 // auth update failed, rollback for moodle
5d910388 139 $DB->update_record('user', $user);
4e244ba8 140 print_error('cannotupdateprofile');
6bc1e5d5 141 }
142
d8734783 143 //update preferences
14a6b7e1 144 useredit_update_user_preference($usernew);
0be6f678 145
1e1c51a3 146 //update interests
147 if (!empty($CFG->usetags)) {
148 useredit_update_interests($usernew, $usernew->interests);
149 }
0be6f678 150
14a6b7e1 151 //update user picture
d8734783 152 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
14a6b7e1 153 useredit_update_picture($usernew, $userform);
dc2590e5 154 }
d1c8eb14 155
d8734783 156 // update mail bounces
14a6b7e1 157 useredit_update_bounces($user, $usernew);
7cbb4c96 158
14a6b7e1 159 /// update forum track preference
160 useredit_update_trackforums($user, $usernew);
f9903ed0 161
14a6b7e1 162 // save custom profile fields data
163 profile_save_data($usernew);
f9a0ea69 164
d6ace123 165 // If email was changed, send confirmation email now
166 if ($email_changed && $CFG->emailchangeconfirmation) {
167 $temp_user = fullclone($user);
168 $temp_user->email = $usernew->preference_newemail;
184c7638 169 $temp_user->emailstop = NULL;
d6ace123 170
171 $a = new stdClass();
172 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
173 $a->site = $SITE->fullname;
174 $a->fullname = fullname($user, true);
175
176 $emailupdatemessage = get_string('auth_emailupdatemessage', 'auth', $a);
177 $emailupdatetitle = get_string('auth_emailupdatetitle', 'auth', $a);
178
184c7638 179 if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
d6ace123 180 die("could not send email!");
181 }
182 }
183
2942a5cd 184 // reload from db
185 $usernew = $DB->get_record('user', array('id'=>$user->id));
186 events_trigger('user_updated', $usernew);
187
b0c90e6e 188 if ($USER->id == $user->id) {
189 // Override old $USER session variable if needed
2942a5cd 190 foreach ((array)$usernew as $variable => $value) {
b0c90e6e 191 $USER->$variable = $value;
192 }
4332512e 193 }
d6ace123 194
195 if (!$email_changed || !$CFG->emailchangeconfirmation) {
196 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
197 }
1f33691c 198 }
199
a3f1f815 200
d8734783 201/// Display page header
202 $streditmyprofile = get_string('editmyprofile');
203 $strparticipants = get_string('participants');
204 $userfullname = fullname($user, true);
0be6f678 205
206 $navlinks = array();
bb9a123a 207 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
208 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
209 }
0be6f678 210 $navlinks[] = array('name' => $userfullname,
211 'link' => "view.php?id=$user->id&amp;course=$course->id",
212 'type' => 'misc');
213 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
214 $navigation = build_navigation($navlinks);
215 print_header("$course->shortname: $streditmyprofile", $course->fullname, $navigation, "");
216
d8734783 217 /// Print tabs at the top
218 $showroles = 1;
219 $currenttab = 'editprofile';
220 require('tabs.php');
0236ec73 221
d6ace123 222 if ($email_changed) {
223 echo $email_changed_html;
224 } else {
225 /// Finally display THE form
226 $userform->display();
227 }
a3f1f815 228
d8734783 229/// and proper footer
230 print_footer($course);
1f33691c 231
f9903ed0 232?>