MDL-24722 adding missing context info
[moodle.git] / user / emailupdate.php
CommitLineData
ce221eb5 1<?php
2
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.
14//
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/>.
17
18/**
19 * Change a users email address
20 *
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
24 */
184c7638 25
d6ace123 26require_once('../config.php');
27require_once($CFG->libdir.'/adminlib.php');
05c38e2b 28require_once($CFG->dirroot.'/user/editlib.php');
d6ace123 29
30$key = required_param('key', PARAM_ALPHANUM);
31$id = required_param('id', PARAM_INT);
32
a6855934 33$PAGE->set_url('/user/emailupdate.php', array('id'=>$id, 'key'=>$key));
5ecfec6d 34$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
ce221eb5 35
d6ace123 36if (!$user = $DB->get_record('user', array('id' => $id))) {
06e84d52 37 print_error('invaliduserid');
d6ace123 38}
39
05c38e2b 40$preferences = get_user_preferences(null, null, $user->id);
d6ace123 41$a = new stdClass();
42$a->fullname = fullname($user, true);
2b06294b 43$stremailupdate = get_string('auth_emailupdate', 'auth_email', $a);
caa8363f 44
45$PAGE->set_title(format_string($SITE->fullname) . ": $stremailupdate");
46$PAGE->set_heading(format_string($SITE->fullname) . ": $stremailupdate");
47
48echo $OUTPUT->header();
d6ace123 49
d6ace123 50if (empty($preferences['newemailattemptsleft'])) {
51 redirect("$CFG->wwwroot/user/view.php?id=$user->id");
52
53} elseif ($preferences['newemailattemptsleft'] < 1) {
05c38e2b 54 cancel_email_update($user->id);
2b06294b 55 $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth_email');
5e61d1a4 56 echo $OUTPUT->box($stroutofattempts, 'center');
d6ace123 57
58} elseif ($key == $preferences['newemailkey']) {
12058e63 59 $olduser = clone($user);
05c38e2b 60 cancel_email_update($user->id);
d6ace123 61 $user->email = $preferences['newemail'];
62
63 // Detect duplicate before saving
64 if ($DB->get_record('user', array('email' => $user->email))) {
2b06294b 65 $stremailnowexists = get_string('auth_emailnowexists', 'auth_email');
5e61d1a4 66 echo $OUTPUT->box($stremailnowexists, 'center');
3aac07d8 67 echo $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id");
d6ace123 68 } else {
69 // update user email
2a7eff41 70 $DB->set_field('user', 'email', $user->email, array('id' => $user->id));
12058e63 71 $authplugin = get_auth_plugin($user->auth);
72 $authplugin->user_update($olduser, $user);
2a7eff41 73 events_trigger('user_updated', $user);
74 $a->email = $user->email;
2b06294b 75 $stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth_email', $a);
5e61d1a4 76 echo $OUTPUT->box($stremailupdatesuccess, 'center');
3aac07d8 77 echo $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id");
d6ace123 78 }
79
80} else {
81 $preferences['newemailattemptsleft']--;
82 set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
2b06294b 83 $strinvalidkey = get_string('auth_invalidnewemailkey', 'auth_email');
5e61d1a4 84 echo $OUTPUT->box($strinvalidkey, 'center');
d6ace123 85}
86
0ab75c34 87echo $OUTPUT->footer();