From 2f8c69e199f757b10f8521bacb6e58203aade27b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= <commits@skodak.org>
Date: Sun, 16 Sep 2012 21:32:27 +0200
Subject: [PATCH] MDL-35465 move duplicate check to cohort_add_member()

---
 cohort/assign.php | 6 +-----
 cohort/lib.php    | 4 ++++
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/cohort/assign.php b/cohort/assign.php
index 5a3fd64075a..6befd5b0e44 100644
--- a/cohort/assign.php
+++ b/cohort/assign.php
@@ -1,5 +1,4 @@
 <?php
-
 // This file is part of Moodle - http://moodle.org/
 //
 // Moodle is free software: you can redistribute it and/or modify
@@ -80,10 +79,7 @@ if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
     if (!empty($userstoassign)) {
 
         foreach ($userstoassign as $adduser) {
-            // no duplicates please
-            if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
-                cohort_add_member($cohort->id, $adduser->id);
-            }
+            cohort_add_member($cohort->id, $adduser->id);
         }
 
         $potentialuserselector->invalidate_selected_users();
diff --git a/cohort/lib.php b/cohort/lib.php
index d6b3e237172..88b00f15da7 100644
--- a/cohort/lib.php
+++ b/cohort/lib.php
@@ -132,6 +132,10 @@ function cohort_delete_category($category) {
  */
 function cohort_add_member($cohortid, $userid) {
     global $DB;
+    if ($DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid))) {
+        // No duplicates!
+        return;
+    }
     $record = new stdClass();
     $record->cohortid  = $cohortid;
     $record->userid    = $userid;