MDL-42989 Admin: Clean output when building site administration tree for JS
[moodle.git] / user / emailupdate.php
blob256af0f5993600eef4e6ce86054d6cdd1a804c05
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 * Change a users email address
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.'/adminlib.php');
28 require_once($CFG->dirroot.'/user/editlib.php');
29 require_once($CFG->dirroot.'/user/lib.php');
31 $key = required_param('key', PARAM_ALPHANUM);
32 $id = required_param('id', PARAM_INT);
34 $PAGE->set_url('/user/emailupdate.php', array('id'=>$id, 'key'=>$key));
35 $PAGE->set_context(context_system::instance());
37 if (!$user = $DB->get_record('user', array('id' => $id))) {
38 print_error('invaliduserid');
41 $preferences = get_user_preferences(null, null, $user->id);
42 $a = new stdClass();
43 $a->fullname = fullname($user, true);
44 $stremailupdate = get_string('emailupdate', 'auth', $a);
46 $PAGE->set_title(format_string($SITE->fullname) . ": $stremailupdate");
47 $PAGE->set_heading(format_string($SITE->fullname) . ": $stremailupdate");
49 echo $OUTPUT->header();
51 if (empty($preferences['newemailattemptsleft'])) {
52 redirect("$CFG->wwwroot/user/view.php?id=$user->id");
54 } elseif ($preferences['newemailattemptsleft'] < 1) {
55 cancel_email_update($user->id);
56 $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
57 echo $OUTPUT->box($stroutofattempts, 'center');
59 } elseif ($key == $preferences['newemailkey']) {
60 $olduser = clone($user);
61 cancel_email_update($user->id);
62 $user->email = $preferences['newemail'];
64 // Detect duplicate before saving.
65 if ($DB->get_record('user', array('email' => $user->email))) {
66 $stremailnowexists = get_string('emailnowexists', 'auth');
67 echo $OUTPUT->box($stremailnowexists, 'center');
68 echo $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id");
69 } else {
70 // Update user email.
71 $authplugin = get_auth_plugin($user->auth);
72 $authplugin->user_update($olduser, $user);
73 user_update_user($user, false);
74 $a->email = $user->email;
75 $stremailupdatesuccess = get_string('emailupdatesuccess', 'auth', $a);
76 echo $OUTPUT->box($stremailupdatesuccess, 'center');
77 echo $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id");
80 } else {
81 $preferences['newemailattemptsleft']--;
82 set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
83 $strinvalidkey = get_string('auth_invalidnewemailkey', 'auth');
84 echo $OUTPUT->box($strinvalidkey, 'center');
87 echo $OUTPUT->footer();