MDL-24471, FILEMANAGER change <button> to <input type="button" />, moodle form may...
[moodle.git] / user / edit.php
blob5d2a02f3d76311e1cf710a80fb62b2713e471336
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->dirroot.'/user/edit_form.php');
29 require_once($CFG->dirroot.'/user/editlib.php');
30 require_once($CFG->dirroot.'/user/profile/lib.php');
32 httpsrequired();
34 $userid = optional_param('id', $USER->id, PARAM_INT); // user id
35 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
36 $cancelemailchange = optional_param('cancelemailchange', false, PARAM_INT); // course id (defaults to Site)
38 $url = new moodle_url('/user/edit.php', array('course'=>$course));
39 if ($userid !== $USER->id) {
40 $url->param('id', $userid);
42 $PAGE->set_url($url);
44 if (!$course = $DB->get_record('course', array('id'=>$course))) {
45 print_error('invalidcourseid');
48 if ($course->id != SITEID) {
49 require_login($course);
50 } else if (!isloggedin()) {
51 if (empty($SESSION->wantsurl)) {
52 $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
54 redirect(get_login_url());
55 } else {
56 $PAGE->set_course($course);
59 // Guest can not edit
60 if (isguestuser()) {
61 print_error('guestnoeditprofile');
64 // The user profile we are editing
65 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
66 print_error('invaliduserid');
69 // Guest can not be edited
70 if (isguestuser($user)) {
71 print_error('guestnoeditprofile');
74 // User interests separated by commas
75 if (!empty($CFG->usetags)) {
76 require_once($CFG->dirroot.'/tag/lib.php');
77 $user->interests = tag_get_tags_array('user', $user->id);
80 // remote users cannot be edited
81 if (is_mnet_remote_user($user)) {
82 if (user_not_fully_set_up($user)) {
83 $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id'=>$user->mnethostid));
84 print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
86 redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
89 // load the appropriate auth plugin
90 $userauth = get_auth_plugin($user->auth);
92 if (!$userauth->can_edit_profile()) {
93 print_error('noprofileedit', 'auth');
96 if ($editurl = $userauth->edit_profile_url()) {
97 // this internal script not used
98 redirect($editurl);
101 if ($course->id == SITEID) {
102 $coursecontext = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
103 } else {
104 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context
106 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
107 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
109 // check access control
110 if ($user->id == $USER->id) {
111 //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
112 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
113 print_error('cannotedityourprofile');
116 } else {
117 // teachers, parents, etc.
118 require_capability('moodle/user:editprofile', $personalcontext);
119 // no editing of guest user account
120 if (isguestuser($user->id)) {
121 print_error('guestnoeditprofileother');
123 // no editing of primary admin!
124 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins
125 print_error('useradmineditadmin');
129 if ($user->deleted) {
130 echo $OUTPUT->header();
131 echo $OUTPUT->heading(get_string('userdeleted'));
132 echo $OUTPUT->footer();
133 die;
136 // Process email change cancellation
137 if ($cancelemailchange) {
138 cancel_email_update($user->id);
141 //load user preferences
142 useredit_load_preferences($user);
144 //Load custom profile fields data
145 profile_load_data($user);
148 // Prepare the editor and create form
149 $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'forcehttps'=>false);
150 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
151 $userform = new user_edit_form(null, array('editoroptions'=>$editoroptions));
152 if (empty($user->country)) {
153 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
154 unset($user->country);
156 $userform->set_data($user);
158 $email_changed = false;
160 if ($usernew = $userform->get_data()) {
162 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
164 $email_changed_html = '';
166 if ($CFG->emailchangeconfirmation) {
167 // Handle change of email carefully for non-trusted users
168 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
169 $a = new stdClass();
170 $a->newemail = $usernew->preference_newemail = $usernew->email;
171 $usernew->preference_newemailkey = random_string(20);
172 $usernew->preference_newemailattemptsleft = 3;
173 $a->oldemail = $usernew->email = $user->email;
175 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth_email', $a), 'generalbox', 'notice');
176 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
177 $email_changed = true;
181 $authplugin = get_auth_plugin($user->auth);
183 $usernew->timemodified = time();
185 // description editor element may not exist!
186 if (isset($usernew->description_editor)) {
187 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
190 $DB->update_record('user', $usernew);
192 // pass a true $userold here
193 if (! $authplugin->user_update($user, $usernew)) {
194 // auth update failed, rollback for moodle
195 $DB->update_record('user', $user);
196 print_error('cannotupdateprofile');
199 //update preferences
200 useredit_update_user_preference($usernew);
202 //update interests
203 if (!empty($CFG->usetags)) {
204 useredit_update_interests($usernew, $usernew->interests);
207 //update user picture
208 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
209 useredit_update_picture($usernew, $userform);
212 // update mail bounces
213 useredit_update_bounces($user, $usernew);
215 /// update forum track preference
216 useredit_update_trackforums($user, $usernew);
218 // save custom profile fields data
219 profile_save_data($usernew);
221 // If email was changed, send confirmation email now
222 if ($email_changed && $CFG->emailchangeconfirmation) {
223 $temp_user = fullclone($user);
224 $temp_user->email = $usernew->preference_newemail;
225 $temp_user->emailstop = NULL;
227 $a = new stdClass();
228 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
229 $a->site = $SITE->fullname;
230 $a->fullname = fullname($user, true);
232 $emailupdatemessage = get_string('auth_emailupdatemessage', 'auth_email', $a);
233 $emailupdatetitle = get_string('auth_emailupdatetitle', 'auth_email', $a);
235 if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
236 die("could not send email!");
240 // reload from db
241 $usernew = $DB->get_record('user', array('id'=>$user->id));
242 events_trigger('user_updated', $usernew);
244 if ($USER->id == $user->id) {
245 // Override old $USER session variable if needed
246 foreach ((array)$usernew as $variable => $value) {
247 $USER->$variable = $value;
249 // preload custom fields
250 profile_load_custom_fields($USER);
253 if (is_siteadmin() and empty($SITE->shortname)) {
254 // fresh cli install - we need to finish site settings
255 redirect(new moodle_url('/admin/index.php'));
258 if (!$email_changed || !$CFG->emailchangeconfirmation) {
259 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
264 /// Display page header
265 $streditmyprofile = get_string('editmyprofile');
266 $strparticipants = get_string('participants');
267 $userfullname = fullname($user, true);
269 $PAGE->set_title("$course->shortname: $streditmyprofile");
270 $PAGE->set_heading($course->fullname);
272 echo $OUTPUT->header();
274 if ($email_changed) {
275 echo $email_changed_html;
276 } else {
277 /// Finally display THE form
278 $userform->display();
281 /// and proper footer
282 echo $OUTPUT->footer();