MDL-13811: Merged fix to work when email is disabled
[moodle.git] / user / emailupdate.php
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
5 require_once($CFG->dirroot.'/user/editlib.php');
7 $key = required_param('key', PARAM_ALPHANUM);
8 $id  = required_param('id', PARAM_INT);
10 if (!$user = $DB->get_record('user', array('id' => $id))) {
11     error("Unknown user ID");
12 }
14 $preferences = get_user_preferences(null, null, $user->id);
15 $a = new stdClass();
16 $a->fullname = fullname($user, true);
17 $stremailupdate = get_string('auth_emailupdate', 'auth', $a);
18 print_header(format_string($SITE->fullname) . ": $stremailupdate", format_string($SITE->fullname) . ": $stremailupdate");
20 if (empty($preferences['newemailattemptsleft'])) {
21     redirect("$CFG->wwwroot/user/view.php?id=$user->id");
23 } elseif ($preferences['newemailattemptsleft'] < 1) {
24     cancel_email_update($user->id);
25     $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
26     print_box($stroutofattempts, 'center');
28 } elseif ($key == $preferences['newemailkey']) {
29     cancel_email_update($user->id);
30     $user->email = $preferences['newemail'];
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');
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');
42         } else {
43             events_trigger('user_updated', $user);
44             $a->email = $user->email;
45             $stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth', $a);
46             print_box($stremailupdatesuccess, 'center');
47             print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
48         }
49     }
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 }
58 print_footer('none');