Commit 3497350e authored by Andrew Nicols's avatar Andrew Nicols
Browse files

Merge branch 'wip-MDL-51241-28' of git://github.com/abgreeve/moodle into MOODLE_28_STABLE

parents 396f954b f9dfe738
...@@ -256,15 +256,17 @@ function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset ...@@ -256,15 +256,17 @@ function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset
$groupbysql = ''; $groupbysql = '';
$havingsql = ''; $havingsql = '';
if ($withmembers) { if ($withmembers) {
$groupbysql = " GROUP BY $fieldssql"; $fieldssql .= ', s.memberscnt';
$subfields = "c.id, COUNT(DISTINCT cm.userid) AS memberscnt";
$groupbysql = " GROUP BY c.id";
$fromsql = " LEFT JOIN {cohort_members} cm ON cm.cohortid = c.id "; $fromsql = " LEFT JOIN {cohort_members} cm ON cm.cohortid = c.id ";
$fieldssql .= ', COUNT(DISTINCT cm.userid) AS memberscnt';
if (in_array($withmembers, if (in_array($withmembers,
array(COHORT_COUNT_ENROLLED_MEMBERS, COHORT_WITH_ENROLLED_MEMBERS_ONLY, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY))) { array(COHORT_COUNT_ENROLLED_MEMBERS, COHORT_WITH_ENROLLED_MEMBERS_ONLY, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY))) {
list($esql, $params2) = get_enrolled_sql($currentcontext); list($esql, $params2) = get_enrolled_sql($currentcontext);
$fromsql .= " LEFT JOIN ($esql) u ON u.id = cm.userid "; $fromsql .= " LEFT JOIN ($esql) u ON u.id = cm.userid ";
$params = array_merge($params2, $params); $params = array_merge($params2, $params);
$fieldssql .= ', COUNT(DISTINCT u.id) AS enrolledcnt'; $fieldssql .= ', s.enrolledcnt';
$subfields .= ', COUNT(DISTINCT u.id) AS enrolledcnt';
} }
if ($withmembers == COHORT_WITH_MEMBERS_ONLY) { if ($withmembers == COHORT_WITH_MEMBERS_ONLY) {
$havingsql = " HAVING COUNT(DISTINCT cm.userid) > 0"; $havingsql = " HAVING COUNT(DISTINCT cm.userid) > 0";
...@@ -280,13 +282,20 @@ function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset ...@@ -280,13 +282,20 @@ function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset
$params = array_merge($params, $searchparams); $params = array_merge($params, $searchparams);
} }
$sql = "SELECT $fieldssql if ($withmembers) {
FROM {cohort} c $sql = "SELECT " . str_replace('c.', 'cohort.', $fieldssql) . "
$fromsql FROM {cohort} cohort
WHERE $wheresql JOIN (SELECT $subfields
$groupbysql FROM {cohort} c $fromsql
$havingsql WHERE $wheresql $groupbysql $havingsql
ORDER BY c.name, c.idnumber"; ) s ON cohort.id = s.id
ORDER BY cohort.name, cohort.idnumber";
} else {
$sql = "SELECT $fieldssql
FROM {cohort} c $fromsql
WHERE $wheresql
ORDER BY c.name, c.idnumber";
}
return $DB->get_records_sql($sql, $params, $offset, $limit); return $DB->get_records_sql($sql, $params, $offset, $limit);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment