MDL-35465 fix cohort_get_visible_list() and add tests

This commit is contained in:
Petr Škoda 2012-09-16 21:07:53 +02:00
parent 607fdf632d
commit a09ae434ef
2 changed files with 105 additions and 15 deletions

View File

@ -167,34 +167,46 @@ function cohort_is_member($cohortid, $userid) {
}
/**
* Returns list of visible cohorts in course.
* Returns list of cohorts from course parent contexts.
*
* @param object $course
* @param bool $enrolled true means include only cohorts with enrolled users
* @return array
* Note: this function does not implement any capability checks,
* it means it may disclose existence of cohorts,
* make sure it is displayed to users with appropriate rights only.
*
* @param stdClass $course
* @param bool $onlyenrolled true means include only cohorts with enrolled users
* @return array of cohort names with number of enrolled users
*/
function cohort_get_visible_list($course) {
global $DB, $USER;
function cohort_get_visible_list($course, $onlyenrolled=true) {
global $DB;
$context = context_course::instance($course->id);
list($esql, $params) = get_enrolled_sql($context);
$parentsql = get_related_contexts_string($context);
list($parentsql, $params2) = $DB->get_in_or_equal($context->get_parent_context_ids(), SQL_PARAMS_NAMED);
$params = array_merge($params, $params2);
$sql = "SELECT c.id, c.name, c.idnumber, COUNT(u.id) AS cnt
if ($onlyenrolled) {
$left = "";
$having = "HAVING COUNT(u.id) > 0";
} else {
$left = "LEFT";
$having = "";
}
$sql = "SELECT c.id, c.name, c.contextid, c.idnumber, COUNT(u.id) AS cnt
FROM {cohort} c
JOIN {cohort_members} cm ON cm.cohortid = c.id
JOIN ($esql) u ON u.id = cm.userid
$left JOIN ({cohort_members} cm
JOIN ($esql) u ON u.id = cm.userid) ON cm.cohortid = c.id
WHERE c.contextid $parentsql
GROUP BY c.id, c.name, c.idnumber
HAVING COUNT(u.id) > 0
GROUP BY c.id, c.name, c.contextid, c.idnumber
$having
ORDER BY c.name, c.idnumber";
$params['ctx'] = $context->id;
$cohorts = $DB->get_records_sql($sql, $params);
foreach ($cohorts as $cid=>$cohort) {
$cohorts[$cid] = format_string($cohort->name);
if ($cohort->idnumber) {
$cohorts[$cid] = format_string($cohort->name, true, array('context'=>$cohort->contextid));
if ($cohort->cnt) {
$cohorts[$cid] .= ' (' . $cohort->cnt . ')';
}
}

View File

@ -178,4 +178,82 @@ class cohort_testcase extends advanced_testcase {
cohort_add_member($cohort->id, $user->id);
$this->assertTrue(cohort_is_member($cohort->id, $user->id));
}
public function test_cohort_get_visible_list() {
global $DB;
$this->resetAfterTest();
$category1 = $this->getDataGenerator()->create_category();
$category2 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
$course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
$course3 = $this->getDataGenerator()->create_course();
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id));
$cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category2->id)->id));
$cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
$cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$user5 = $this->getDataGenerator()->create_user();
$manualenrol = enrol_get_plugin('manual');
$enrol1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'));
$enrol2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'));
$manualenrol->enrol_user($enrol1, $user1->id);
$manualenrol->enrol_user($enrol1, $user3->id);
$manualenrol->enrol_user($enrol1, $user4->id);
$manualenrol->enrol_user($enrol2, $user2->id);
cohort_add_member($cohort1->id, $user1->id);
cohort_add_member($cohort3->id, $user1->id);
cohort_add_member($cohort1->id, $user3->id);
cohort_add_member($cohort2->id, $user2->id);
$list = cohort_get_visible_list($course1);
$this->assertEquals(2, count($list));
$this->assertNotEmpty($list[$cohort1->id]);
$this->assertRegExp('/\(2\)$/', $list[$cohort1->id]);
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort3->id]);
$list = cohort_get_visible_list($course1, false);
$this->assertEquals(3, count($list));
$this->assertNotEmpty($list[$cohort1->id]);
$this->assertRegExp('/\(2\)$/', $list[$cohort1->id]);
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort3->id]);
$this->assertNotEmpty($list[$cohort4->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
$list = cohort_get_visible_list($course2);
$this->assertEquals(1, count($list));
$this->assertNotEmpty($list[$cohort2->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort2->id]);
$list = cohort_get_visible_list($course2, false);
$this->assertEquals(3, count($list));
$this->assertNotEmpty($list[$cohort2->id]);
$this->assertRegExp('/\(1\)$/', $list[$cohort2->id]);
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort3->id]);
$this->assertNotEmpty($list[$cohort4->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
$list = cohort_get_visible_list($course3);
$this->assertEquals(0, count($list));
$list = cohort_get_visible_list($course3, false);
$this->assertEquals(2, count($list));
$this->assertNotEmpty($list[$cohort3->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort3->id]);
$this->assertNotEmpty($list[$cohort4->id]);
$this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
}
}