MDL-13811 reworked unsetting of preferences and minor tweaks
[moodle.git] / user / emailupdate.php
1 <?php // $Id$
2 require_once('../config.php');
3 require_once($CFG->libdir.'/adminlib.php');
4 require_once($CFG->dirroot.'/user/editlib.php');
6 $key = required_param('key', PARAM_ALPHANUM);
7 $id  = required_param('id', PARAM_INT);
9 if (!$user = $DB->get_record('user', array('id' => $id))) {
10     error("Unknown user ID");
11 }
13 $preferences = get_user_preferences(null, null, $user->id);
14 $a = new stdClass();
15 $a->fullname = fullname($user, true);
16 $stremailupdate = get_string('auth_emailupdate', 'auth', $a);
17 print_header(format_string($SITE->fullname) . ": $stremailupdate", format_string($SITE->fullname) . ": $stremailupdate");
19 if (empty($preferences['newemailattemptsleft'])) {
20     redirect("$CFG->wwwroot/user/view.php?id=$user->id");
22 } elseif ($preferences['newemailattemptsleft'] < 1) {
23     cancel_email_update($user->id);
24     $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
25     print_box($stroutofattempts, 'center');
27 } elseif ($key == $preferences['newemailkey']) {
28     cancel_email_update($user->id);
29     $user->email = $preferences['newemail'];
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');
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');
41         } else {
42             events_trigger('user_updated', $user);
43             $a->email = $user->email;
44             $stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth', $a);
45             print_box($stremailupdatesuccess, 'center');
46             print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
47         }
48     }
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 }
57 print_footer('none');