MDL-13811 reworked unsetting of preferences and minor tweaks
[moodle.git] / user / emailupdate.php
index 93da578..6a68013 100755 (executable)
@@ -1,6 +1,7 @@
 <?php // $Id$
 require_once('../config.php');
 require_once($CFG->libdir.'/adminlib.php');
+require_once($CFG->dirroot.'/user/editlib.php');
 
 $key = required_param('key', PARAM_ALPHANUM);
 $id  = required_param('id', PARAM_INT);
@@ -9,30 +10,28 @@ if (!$user = $DB->get_record('user', array('id' => $id))) {
     error("Unknown user ID");
 }
 
-$preferences = get_user_preferences(null, null, $id);
+$preferences = get_user_preferences(null, null, $user->id);
 $a = new stdClass();
 $a->fullname = fullname($user, true);
 $stremailupdate = get_string('auth_emailupdate', 'auth', $a);
 print_header(format_string($SITE->fullname) . ": $stremailupdate", format_string($SITE->fullname) . ": $stremailupdate");
 
-$cancel_email_update = false;
-
 if (empty($preferences['newemailattemptsleft'])) {
     redirect("$CFG->wwwroot/user/view.php?id=$user->id");
 
 } elseif ($preferences['newemailattemptsleft'] < 1) {
-    $cancel_email_update = true;
+    cancel_email_update($user->id);
     $stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
     print_box($stroutofattempts, 'center');
 
 } elseif ($key == $preferences['newemailkey']) {
+    cancel_email_update($user->id);
     $user->email = $preferences['newemail'];
 
     // Detect duplicate before saving
     if ($DB->get_record('user', array('email' => $user->email))) {
         $stremailnowexists = get_string('auth_emailnowexists', 'auth');
         print_box($stremailnowexists, 'center');
-        $cancel_email_update = true;
         print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
     } else {
         // update user email
@@ -41,11 +40,10 @@ if (empty($preferences['newemailattemptsleft'])) {
 
         } else {
             events_trigger('user_updated', $user);
-            $stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth', $user);
+            $a->email = $user->email;
+            $stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth', $a);
             print_box($stremailupdatesuccess, 'center');
             print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
-
-            $cancel_email_update = true;
         }
     }
 
@@ -56,10 +54,4 @@ if (empty($preferences['newemailattemptsleft'])) {
     print_box($strinvalidkey, 'center');
 }
 
-if ($cancel_email_update) {
-    require_once($CFG->dirroot . '/user/editlib.php');
-    $user->preference_newemail = null;
-    $user->preference_newemailkey = null;
-    $user->preference_newemailattemptsleft = null;
-    useredit_update_user_preference($user);
-}
+print_footer('none');