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