Index: badges/criteria/award_criteria.php
===================================================================
--- badges/criteria/award_criteria.php	(revision 33904)
+++ badges/criteria/award_criteria.php	(working copy)
@@ -292,7 +292,16 @@
      */
     abstract public function get_completed_criteria_sql();
 
+    // @EC
     /**
+     * Filters array using completion criteria.
+     *
+     * @return array
+     */
+    abstract public function filter_criteria_sql($toearn);
+    // @EC END
+
+    /**
      * Mark this criteria as complete for a user
      *
      * @param int $userid User whose criteria is completed.
Index: badges/criteria/award_criteria_activity.php
===================================================================
--- badges/criteria/award_criteria_activity.php	(revision 33904)
+++ badges/criteria/award_criteria_activity.php	(working copy)
@@ -231,36 +231,59 @@
      * @return array list($join, $where, $params)
      */
     public function get_completed_criteria_sql() {
-        $join = '';
-        $where = '';
-        $params = array();
+        // @EC
+        return array('', '', array());
+        // @EC END
+    }
 
+    // @EC
+    /**
+     * Filters array using completion criteria.
+     *
+     * @return array
+     */
+    public function filter_criteria_sql($toearn) {
+        global $DB;
+
+        $completionstates = implode(',', array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS));
+
         if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
-            foreach ($this->params as $param) {
-                $moduledata[] = " cmc.coursemoduleid = :completedmodule{$param['module']} ";
-                $params["completedmodule{$param['module']}"] = $param['module'];
+            foreach ($toearn as $key => $uid) {
+                $coursemodules = array();
+                foreach ($this->params as $param) {
+                    $coursemodules[] = $param['module'];
+                }
+                if (!empty($coursemodules)) {
+                    $coursemodules = implode(',', $coursemodules);
+                    $sql = "SELECT * FROM {course_modules_completion}
+                        WHERE userid = :userid 
+                        AND coursemoduleid IN ($coursemodules) 
+                        AND completionstate IN ($completionstates)";
+                    $params = array('userid' => $uid);
+                    $results = $DB->get_records_sql($sql, $params);
+                    if (count($results) === 0) {
+                        unset($toearn[$key]);
+                        break;
+                    }
+                }
             }
-            if (!empty($moduledata)) {
-                $extraon = implode(' OR ', $moduledata);
-                $join = " JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND
-                          ( cmc.completionstate = :completionpass OR cmc.completionstate = :completioncomplete ) AND ({$extraon})";
-                $params["completionpass"] = COMPLETION_COMPLETE_PASS;
-                $params["completioncomplete"] = COMPLETION_COMPLETE;
-            }
-            return array($join, $where, $params);
         } else {
-            foreach ($this->params as $param) {
-                $join .= " LEFT JOIN {course_modules_completion} cmc{$param['module']} ON
-                          cmc{$param['module']}.userid = u.id AND
-                          cmc{$param['module']}.coursemoduleid = :completedmodule{$param['module']} AND
-                          ( cmc{$param['module']}.completionstate = :completionpass{$param['module']} OR
-                            cmc{$param['module']}.completionstate = :completioncomplete{$param['module']} )";
-                $where .= " AND cmc{$param['module']}.coursemoduleid IS NOT NULL ";
-                $params["completedmodule{$param['module']}"] = $param['module'];
-                $params["completionpass{$param['module']}"] = COMPLETION_COMPLETE_PASS;
-                $params["completioncomplete{$param['module']}"] = COMPLETION_COMPLETE;
+            foreach ($toearn as $key => $uid) {
+                foreach ($this->params as $param) {
+                    $sql = "SELECT * FROM {course_modules_completion}
+                        WHERE userid = :userid AND coursemoduleid = :coursemoduleid
+                        AND completionstate IN ($completionstates)";
+                    $params = array('coursemoduleid' => $param['module'], 'userid' => $uid);
+                    $results = $DB->get_records_sql($sql, $params);
+                    if (count($results) === 0) {
+                        unset($toearn[$key]);
+                        break;
+                    }
+                }
             }
-            return array($join, $where, $params);
         }
+
+        return $toearn;
     }
+    // @EC END
 }
Index: badges/criteria/award_criteria_course.php
===================================================================
--- badges/criteria/award_criteria_course.php	(revision 33904)
+++ badges/criteria/award_criteria_course.php	(working copy)
@@ -236,4 +236,15 @@
 
         return array($join, $where, $params);
     }
+
+    // @EC
+    /**
+     * Filters array using completion criteria.
+     *
+     * @return array
+     */
+    public function filter_criteria_sql($toearn) {
+        return $toearn;
+    }
+    // @EC END
 }
Index: badges/criteria/award_criteria_courseset.php
===================================================================
--- badges/criteria/award_criteria_courseset.php	(revision 33904)
+++ badges/criteria/award_criteria_courseset.php	(working copy)
@@ -287,4 +287,15 @@
             return array($join, $where, $params);
         }
     }
+
+    // @EC
+    /**
+     * Filters array using completion criteria.
+     *
+     * @return array
+     */
+    public function filter_criteria_sql($toearn) {
+        return $toearn;
+    }
+    // @EC END
 }
Index: badges/criteria/award_criteria_manual.php
===================================================================
--- badges/criteria/award_criteria_manual.php	(revision 33904)
+++ badges/criteria/award_criteria_manual.php	(working copy)
@@ -220,7 +220,18 @@
         }
     }
 
+    // @EC
     /**
+     * Filters array using completion criteria.
+     *
+     * @return array
+     */
+    public function filter_criteria_sql($toearn) {
+        return $toearn;
+    }
+    // @EC END
+
+    /**
      * Delete this criterion
      *
      */
Index: badges/criteria/award_criteria_overall.php
===================================================================
--- badges/criteria/award_criteria_overall.php	(revision 33904)
+++ badges/criteria/award_criteria_overall.php	(working copy)
@@ -152,7 +152,18 @@
         return array('', '', array());
     }
 
+    // @EC
     /**
+     * Filters array using completion criteria.
+     *
+     * @return array
+     */
+    public function filter_criteria_sql($toearn) {
+        return $toearn;
+    }
+    // @EC END
+
+    /**
      * Add appropriate criteria elements to the form
      *
      */
Index: badges/criteria/award_criteria_profile.php
===================================================================
--- badges/criteria/award_criteria_profile.php	(revision 33904)
+++ badges/criteria/award_criteria_profile.php	(working copy)
@@ -233,4 +233,15 @@
         }
         return array($join, $where, $params);
     }
+
+    // @EC
+    /**
+     * Filters array using completion criteria.
+     *
+     * @return array
+     */
+    public function filter_criteria_sql($toearn) {
+        return $toearn;
+    }
+    // @EC END
 }
Index: lib/badgeslib.php
===================================================================
--- lib/badgeslib.php	(revision 33904)
+++ lib/badgeslib.php	(working copy)
@@ -480,6 +480,10 @@
                 $toearn = $DB->get_fieldset_sql($sql, $params);
             }
 
+            // @EC
+            $toearn = $crit->filter_criteria_sql($toearn);
+            // @EC END
+
             foreach ($toearn as $uid) {
                 $reviewoverall = false;
                 if ($crit->review($uid, true)) {
