MDL-26177 fix non-working unique custom field check
authorPetr Skoda <commits@skodak.org>
Tue, 1 Feb 2011 13:34:31 +0000 (14:34 +0100)
committerPetr Skoda <commits@skodak.org>
Tue, 1 Feb 2011 13:34:31 +0000 (14:34 +0100)
user/profile/lib.php

index 05e4655..8fd0612 100644 (file)
@@ -131,14 +131,24 @@ class profile_field_base {
         $errors = array();
         /// Check for uniqueness of data if required
         if ($this->is_unique()) {
-            $userid = $DB->get_field_sql('
-                    SELECT userid
+            $value = (is_array($usernew->{$this->inputname}) and isset($usernew->{$this->inputname}['text'])) ? $usernew->{$this->inputname}['text'] : $usernew->{$this->inputname};
+            $data = $DB->get_records_sql('
+                    SELECT id, userid
                       FROM {user_info_data}
                      WHERE fieldid = ?
                        AND ' . $DB->sql_compare_text('data') . ' = ?',
-                    array($this->field->id, $usernew->{$this->inputname}));
-            if ($userid && $userid != $usernew->id) {
-                $errors[$this->inputname] = get_string('valuealreadyused');
+                    array($this->field->id, $value));
+            if ($data) {
+                $existing = false;
+                foreach ($data as $v) {
+                    if ($v->userid == $usernew->id) {
+                        $existing = true;
+                        break;
+                    }
+                }
+                if (!$existing) {
+                    $errors[$this->inputname] = get_string('valuealreadyused');
+                }
             }
         }
         return $errors;