Skip to content

Commit

Permalink
MDL-29695 cohorts: Add pagination and search to cohorts adminstartion…
Browse files Browse the repository at this point in the history
… page
  • Loading branch information
Ruslan Kabalin authored and Sam Hemelryk committed Oct 17, 2011
1 parent 22a811b commit 543009c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cohort/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
*/

require('../config.php');
require($CFG->dirroot.'/cohort/lib.php');
require_once($CFG->libdir.'/adminlib.php');

$contextid = optional_param('contextid', 0, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
$searchquery = optional_param('search', '', PARAM_RAW);

require_login();

Expand Down Expand Up @@ -68,10 +71,31 @@

echo $OUTPUT->heading(get_string('cohortsin', 'cohort', print_context_name($context)));

$cohorts = $DB->get_records('cohort', array('contextid'=>$context->id));
// add search form
$search = html_writer::start_tag('form', array('id'=>'searchcohortquery', 'method'=>'get'));
$search .= html_writer::start_tag('div');
$search .= html_writer::label(get_string('searchcohort', 'cohort').':', 'cohort_search_q');
$search .= html_writer::empty_tag('input', array('id'=>'cohort_search_q', 'type'=>'text', 'name'=>'search', 'value'=>$searchquery));
$search .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('search', 'cohort')));
$search .= html_writer::end_tag('div');
$search .= html_writer::end_tag('form');
echo $search;

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

// output pagination bar
$params = array('page' => $page);
if ($contextid) {
$params['contextid'] = $contextid;
}
if ($search) {
$params['search'] = $searchquery;
}
$baseurl = new moodle_url('/cohort/index.php', $params);
echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);

$data = array();
foreach($cohorts as $cohort) {
foreach($cohorts['cohorts'] as $cohort) {
$line = array();
$line[] = format_string($cohort->name);
$line[] = $cohort->idnumber;
Expand Down Expand Up @@ -107,6 +131,7 @@
$table->width = '80%';
$table->data = $data;
echo html_writer::table($table);
echo $OUTPUT->paging_bar($cohorts['totalcohorts'], $page, 25, $baseurl);

if ($manager) {
echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
Expand Down
45 changes: 45 additions & 0 deletions cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,51 @@ function cohort_get_visible_list($course) {
return $cohorts;
}

/**
* Get all the cohorts.
*
* @global moodle_database $DB
* @param int $contextid
* @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)
*/
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 . '%';
foreach ($conditions as $key=>$condition) {
$conditions[$key] = $DB->sql_like($condition,"?", false);
$params[] = $searchparam;
}
$tests[] = '(' . implode(' OR ', $conditions) . ')';
}
$wherecondition = implode(' AND ', $tests);

$fields = 'SELECT *';
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {cohort}
WHERE $wherecondition";
$order = ' ORDER BY name ASC';
$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);
}

/**
* Cohort assignment candidates
*/
Expand Down
2 changes: 2 additions & 0 deletions lang/en/cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@
$string['selectfromcohort'] = 'Select members from cohort';
$string['unknowncohort'] = 'Unknown cohort ({$a})!';
$string['useradded'] = 'User added to cohort "{$a}"';
$string['search'] = 'Search';
$string['searchcohort'] = 'Search cohort';

0 comments on commit 543009c

Please sign in to comment.