Merge branch 'MDL-33487' of git://github.com/danpoltawski/moodle
[moodle.git] / user / emailupdate.php
blob602aa164017911d67f4014830068096cd9874b2d
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');
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(get_context_instance(CONTEXT_SYSTEM));
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 } elseif ($preferences['newemailattemptsleft'] < 1) {
54 cancel_email_update($user->id);
55 $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
56 echo $OUTPUT->box($stroutofattempts, 'center');
58 } elseif ($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 $DB->set_field('user', 'email', $user->email, array('id' => $user->id));
71 $authplugin = get_auth_plugin($user->auth);
72 $authplugin->user_update($olduser, $user);
73 events_trigger('user_updated', $user);
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();