MDL-13811: Merged fix to work when email is disabled
[moodle.git] / user / emailupdate.php
CommitLineData
d6ace123 1<?php // $Id$
184c7638 2
d6ace123 3require_once('../config.php');
4require_once($CFG->libdir.'/adminlib.php');
05c38e2b 5require_once($CFG->dirroot.'/user/editlib.php');
d6ace123 6
7$key = required_param('key', PARAM_ALPHANUM);
8$id = required_param('id', PARAM_INT);
9
10if (!$user = $DB->get_record('user', array('id' => $id))) {
11 error("Unknown user ID");
12}
13
05c38e2b 14$preferences = get_user_preferences(null, null, $user->id);
d6ace123 15$a = new stdClass();
16$a->fullname = fullname($user, true);
17$stremailupdate = get_string('auth_emailupdate', 'auth', $a);
18print_header(format_string($SITE->fullname) . ": $stremailupdate", format_string($SITE->fullname) . ": $stremailupdate");
19
d6ace123 20if (empty($preferences['newemailattemptsleft'])) {
21 redirect("$CFG->wwwroot/user/view.php?id=$user->id");
22
23} elseif ($preferences['newemailattemptsleft'] < 1) {
05c38e2b 24 cancel_email_update($user->id);
d6ace123 25 $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
26 print_box($stroutofattempts, 'center');
27
28} elseif ($key == $preferences['newemailkey']) {
05c38e2b 29 cancel_email_update($user->id);
d6ace123 30 $user->email = $preferences['newemail'];
31
32 // Detect duplicate before saving
33 if ($DB->get_record('user', array('email' => $user->email))) {
34 $stremailnowexists = get_string('auth_emailnowexists', 'auth');
35 print_box($stremailnowexists, 'center');
d6ace123 36 print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
37 } else {
38 // update user email
39 if (!$DB->set_field('user', 'email', $user->email, array('id' => $user->id))) {
40 error('Error updating user record');
41
42 } else {
43 events_trigger('user_updated', $user);
05c38e2b 44 $a->email = $user->email;
45 $stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth', $a);
d6ace123 46 print_box($stremailupdatesuccess, 'center');
47 print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
d6ace123 48 }
49 }
50
51} else {
52 $preferences['newemailattemptsleft']--;
53 set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
54 $strinvalidkey = get_string('auth_invalidnewemailkey', 'auth');
55 print_box($strinvalidkey, 'center');
56}
57
05c38e2b 58print_footer('none');