diff --git a/badges/criteria/award_criteria_cohort.php b/badges/criteria/award_criteria_cohort.php index 1e0bb80ad53..81290eb07a8 100644 --- a/badges/criteria/award_criteria_cohort.php +++ b/badges/criteria/award_criteria_cohort.php @@ -240,13 +240,23 @@ class award_criteria_cohort extends award_criteria { return array($join, $where, $params); } else { // User is a member of ALL of the specified cohorts. - $join = " LEFT JOIN {cohort_members} cm ON cm.userid = u.id"; + $join = ' LEFT JOIN {cohort_members} cm ON cm.userid = u.id'; + $where = ' AND ('; $i = 0; foreach ($this->params as $param) { - $i++; - $where = ' AND cm.cohortid = :cohortid'.$i; + if ($i == 0) { + $where .= 'cm.cohortid = :cohortid'.$i; + } else { + $where .= ' OR cm.cohortid = :cohortid'.$i; + } $params['cohortid'.$i] = $param['cohort']; + $i++; } + $where .= ') + GROUP BY u.id, bi.badgeid + HAVING COUNT(cm.cohortid) = :cohortcount'; + $params['cohortcount'] = $i; + return array($join, $where, $params); } }