Permalink
Browse files

MDL-29695 cohorts: Add pagination and search to cohorts adminstartion…

… page
  • Loading branch information...
1 parent 22a811b commit 543009cba4c56d28f5db80f9de535b6003f34c91 Ruslan Kabalin committed with Sam Hemelryk Oct 7, 2011
Showing with 74 additions and 2 deletions.
  1. +27 −2 cohort/index.php
  2. +45 −0 cohort/lib.php
  3. +2 −0 lang/en/cohort.php
View
@@ -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();
@@ -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;
@@ -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'));
View
@@ -191,6 +191,51 @@ function cohort_get_visible_list($course) {
}
/**
+ * 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
*/
class cohort_candidate_selector extends user_selector_base {
View
@@ -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.