MDL-35465 fix cohort_get_cohorts() and add tests
This commit is contained in:
+7
-14
@@ -215,9 +215,8 @@ function cohort_get_visible_list($course, $onlyenrolled=true) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the cohorts.
|
||||
* Get all the cohorts defined in given context.
|
||||
*
|
||||
* @global moodle_database $DB
|
||||
* @param int $contextid
|
||||
* @param int $page number of the current page
|
||||
* @param int $perpage items per page
|
||||
@@ -227,32 +226,26 @@ function cohort_get_visible_list($course, $onlyenrolled=true) {
|
||||
function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '') {
|
||||
global $DB;
|
||||
|
||||
$cohorts = array();
|
||||
|
||||
// Add some additional sensible conditions
|
||||
$tests = array('contextid = ?');
|
||||
$params = array($contextid);
|
||||
|
||||
if (!empty($search)) {
|
||||
$conditions = array(
|
||||
'name',
|
||||
'idnumber',
|
||||
'description',
|
||||
);
|
||||
$searchparam = '%' . $search . '%';
|
||||
$conditions = array('name', 'idnumber', 'description');
|
||||
$searchparam = '%' . $DB->sql_like_escape($search) . '%';
|
||||
foreach ($conditions as $key=>$condition) {
|
||||
$conditions[$key] = $DB->sql_like($condition,"?", false);
|
||||
$conditions[$key] = $DB->sql_like($condition, "?", false);
|
||||
$params[] = $searchparam;
|
||||
}
|
||||
$tests[] = '(' . implode(' OR ', $conditions) . ')';
|
||||
}
|
||||
$wherecondition = implode(' AND ', $tests);
|
||||
|
||||
$fields = 'SELECT *';
|
||||
$countfields = 'SELECT COUNT(1)';
|
||||
$fields = "SELECT *";
|
||||
$countfields = "SELECT COUNT(1)";
|
||||
$sql = " FROM {cohort}
|
||||
WHERE $wherecondition";
|
||||
$order = ' ORDER BY name ASC';
|
||||
$order = " ORDER BY name ASC, idnumber ASC";
|
||||
$totalcohorts = $DB->count_records_sql($countfields . $sql, $params);
|
||||
$cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
|
||||
|
||||
|
||||
@@ -256,4 +256,42 @@ class cohort_testcase extends advanced_testcase {
|
||||
$this->assertNotEmpty($list[$cohort4->id]);
|
||||
$this->assertRegExp('/[^\)]$/', $list[$cohort4->id]);
|
||||
}
|
||||
|
||||
public function test_cohort_get_cohorts() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$category1 = $this->getDataGenerator()->create_category();
|
||||
$category2 = $this->getDataGenerator()->create_category();
|
||||
|
||||
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>''));
|
||||
$cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr'));
|
||||
$cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>''));
|
||||
$cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id));
|
||||
|
||||
$result = cohort_get_cohorts(context_coursecat::instance($category2->id)->id);
|
||||
$this->assertEquals(0, $result['totalcohorts']);
|
||||
$this->assertEquals(0, count($result['cohorts']));
|
||||
|
||||
$result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id);
|
||||
$this->assertEquals(3, $result['totalcohorts']);
|
||||
$this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3), $result['cohorts']);
|
||||
|
||||
$result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'arrrgh');
|
||||
$this->assertEquals(1, $result['totalcohorts']);
|
||||
$this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']);
|
||||
|
||||
$result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'brrr');
|
||||
$this->assertEquals(1, $result['totalcohorts']);
|
||||
$this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']);
|
||||
|
||||
$result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'grrr');
|
||||
$this->assertEquals(1, $result['totalcohorts']);
|
||||
$this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']);
|
||||
|
||||
$result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 1, 1, 'yyy');
|
||||
$this->assertEquals(3, $result['totalcohorts']);
|
||||
$this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user