Merge branch 'MDL-47990_27' of git://github.com/timhunt/moodle into MOODLE_27_STABLE
[moodle.git] / user / emailupdate.php
blob7adb835888f6c373c52b8dc03c3cb71a7bfe43fe
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Change a users email address
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core_user
25 require_once('../config.php');
26 require_once($CFG->libdir.'/adminlib.php');
27 require_once($CFG->dirroot.'/user/editlib.php');
28 require_once($CFG->dirroot.'/user/lib.php');
30 $key = required_param('key', PARAM_ALPHANUM);
31 $id = required_param('id', PARAM_INT);
33 $PAGE->set_url('/user/emailupdate.php', array('id' => $id, 'key' => $key));
34 $PAGE->set_context(context_system::instance());
36 if (!$user = $DB->get_record('user', array('id' => $id))) {
37 print_error('invaliduserid');
40 $preferences = get_user_preferences(null, null, $user->id);
41 $a = new stdClass();
42 $a->fullname = fullname($user, true);
43 $stremailupdate = get_string('emailupdate', 'auth', $a);
45 $PAGE->set_title(format_string($SITE->fullname) . ": $stremailupdate");
46 $PAGE->set_heading(format_string($SITE->fullname) . ": $stremailupdate");
48 echo $OUTPUT->header();
50 if (empty($preferences['newemailattemptsleft'])) {
51 redirect("$CFG->wwwroot/user/view.php?id=$user->id");
53 } else if ($preferences['newemailattemptsleft'] < 1) {
54 cancel_email_update($user->id);
55 $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
56 echo $OUTPUT->box($stroutofattempts, 'center');
58 } else if ($key == $preferences['newemailkey']) {
59 $olduser = clone($user);
60 cancel_email_update($user->id);
61 $user->email = $preferences['newemail'];
63 // Detect duplicate before saving.
64 if ($DB->get_record('user', array('email' => $user->email))) {
65 $stremailnowexists = get_string('emailnowexists', 'auth');
66 echo $OUTPUT->box($stremailnowexists, 'center');
67 echo $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id");
68 } else {
69 // Update user email.
70 $authplugin = get_auth_plugin($user->auth);
71 $authplugin->user_update($olduser, $user);
72 user_update_user($user, false);
73 $a->email = $user->email;
74 $stremailupdatesuccess = get_string('emailupdatesuccess', 'auth', $a);
75 echo $OUTPUT->box($stremailupdatesuccess, 'center');
76 echo $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id");
79 } else {
80 $preferences['newemailattemptsleft']--;
81 set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
82 $strinvalidkey = get_string('auth_invalidnewemailkey', 'auth');
83 echo $OUTPUT->box($strinvalidkey, 'center');
86 echo $OUTPUT->footer();