MDL-29320 user: Make email query case-insensitive
[moodle.git] / user / editadvanced_form.php
index 31dd330..6506249 100644 (file)
@@ -298,9 +298,18 @@ class user_editadvanced_form extends moodleform {
         if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
             if (!validate_email($usernew->email)) {
                 $err['email'] = get_string('invalidemail');
-            } else if (empty($CFG->allowaccountssameemail)
-                    and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
-                $err['email'] = get_string('emailexists');
+            } else if (empty($CFG->allowaccountssameemail)) {
+                // Make a case-insensitive query for the given email address.
+                $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
+                $params = array(
+                    'email' => $usernew->email,
+                    'mnethostid' => $CFG->mnet_localhost_id,
+                    'userid' => $usernew->id
+                );
+                // If there are other user(s) that already have the same email, show an error.
+                if ($DB->record_exists_select('user', $select, $params)) {
+                    $err['email'] = get_string('emailexists');
+                }
             }
         }