Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
MDL-35465 improve cohort paging
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Sep 16, 2012
1 parent 6eb8bf7 commit 9a2f677
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
14 changes: 12 additions & 2 deletions cohort/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,18 @@

echo $OUTPUT->header();

echo $OUTPUT->heading(get_string('cohortsin', 'cohort', print_context_name($context)));
$cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);

$count = '';
if ($cohorts['allcohorts'] > 0) {
if ($searchquery === '') {
$count = ' ('.$cohorts['allcohorts'].')';
} else {
$count = ' ('.$cohorts['totalcohorts'].'/'.$cohorts['allcohorts'].')';
}
}

echo $OUTPUT->heading(get_string('cohortsin', 'cohort', $context->get_context_name()).$count);

// Add search form.
$search = html_writer::start_tag('form', array('id'=>'searchcohortquery', 'method'=>'get'));
Expand All @@ -79,7 +90,6 @@
$search .= html_writer::end_tag('form');
echo $search;

$cohorts = cohort_get_cohorts($context->id, $page, 25, $searchquery);

// Output pagination bar.
$params = array('page' => $page);
Expand Down
5 changes: 3 additions & 2 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function cohort_get_visible_list($course, $onlyenrolled=true) {
* @param int $page number of the current page
* @param int $perpage items per page
* @param string $search search string
* @return array Array(totalcohorts => int, cohorts => array)
* @return array Array(totalcohorts => int, cohorts => array, allcohorts => int)
*/
function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '') {
global $DB;
Expand All @@ -250,8 +250,9 @@ function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '')
$sql = " FROM {cohort}
WHERE $wherecondition";
$order = " ORDER BY name ASC, idnumber ASC";
$allcohorts = $DB->count_records('cohort', array('contextid'=>$contextid));
$totalcohorts = $DB->count_records_sql($countfields . $sql, $params);
$cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);

return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts);
return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts'=>$allcohorts);
}
2 changes: 1 addition & 1 deletion cohort/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/cohot/lib.php');
require_once($CFG->dirroot . '/cohort/lib.php');
require_once($CFG->dirroot . '/user/selector/lib.php');


Expand Down
6 changes: 6 additions & 0 deletions cohort/tests/cohortlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,25 +273,31 @@ public function test_cohort_get_cohorts() {
$result = cohort_get_cohorts(context_coursecat::instance($category2->id)->id);
$this->assertEquals(0, $result['totalcohorts']);
$this->assertEquals(0, count($result['cohorts']));
$this->assertEquals(0, $result['allcohorts']);

$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']);
$this->assertEquals(3, $result['allcohorts']);

$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']);
$this->assertEquals(3, $result['allcohorts']);

$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']);
$this->assertEquals(3, $result['allcohorts']);

$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']);
$this->assertEquals(3, $result['allcohorts']);

$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']);
$this->assertEquals(3, $result['allcohorts']);
}
}

0 comments on commit 9a2f677

Please sign in to comment.