mirror of
https://github.com/moodle/moodle.git
synced 2025-04-22 08:55:15 +02:00
MDL-29695 cohorts: Add pagination and search to cohorts adminstartion page
This commit is contained in:
parent
22a811be2d
commit
543009cba4
@ -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->header();
|
||||
|
||||
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->align = array('left', 'left', 'left', 'left','center', 'center');
|
||||
$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'));
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -56,3 +56,5 @@ $string['removeuserwarning'] = 'Removing users from a cohort may result in unenr
|
||||
$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';
|
||||
|
Loading…
x
Reference in New Issue
Block a user