MDL-47354 gradereport_singleview: allow choice of 'perpage' values

This commit is contained in:
Davo Smith 2016-12-16 11:42:13 +00:00
parent 3c45d26f58
commit c6dd683cd0
3 changed files with 38 additions and 2 deletions

View File

@ -64,6 +64,9 @@ abstract class screen {
/** @var array $items List of items on the page, they could be users or grade_items */
protected $items;
/** @var array $validperpage List of allowed values for 'perpage' setting */
protected static $validperpage = [20, 50, 100, 200, 400, 1000, 5000];
/**
* Constructor
*
@ -82,8 +85,19 @@ abstract class screen {
$this->course = $DB->get_record('course', array('id' => $courseid));
$this->page = optional_param('page', 0, PARAM_INT);
$this->perpage = optional_param('perpage', 100, PARAM_INT);
if ($this->perpage > 100) {
$cache = \cache::make_from_params(\cache_store::MODE_SESSION, 'gradereport_singleview', 'perpage');
$perpage = optional_param('perpage', null, PARAM_INT);
if (!in_array($perpage, self::$validperpage)) {
// Get from cache.
$perpage = $cache->get(get_class($this));
} else {
// Save to cache.
$cache->set(get_class($this), $perpage);
}
if ($perpage) {
$this->perpage = $perpage;
} else {
$this->perpage = 100;
}
@ -407,4 +421,24 @@ abstract class screen {
}
return $users;
}
/**
* Allow selection of number of items to display per page.
* @return string
*/
public function perpage_select() {
global $PAGE, $OUTPUT;
$options = array_combine(self::$validperpage, self::$validperpage);
$url = new moodle_url($PAGE->url);
$url->remove_params(['page', 'perpage']);
$out = '';
$select = new \single_select($url, 'perpage', $options, $this->perpage, null, 'perpagechanger');
$select->label = get_string('itemsperpage', 'gradereport_singleview');
$out .= $OUTPUT->render($select);
return $out;
}
}

View File

@ -184,6 +184,7 @@ if ($report->screen->display_group_selector()) {
echo $report->output();
if ($report->screen->supports_paging()) {
echo $report->screen->perpage_select();
echo $report->screen->pager();
}

View File

@ -44,6 +44,7 @@ $string['gradeitem'] = 'Grade item: {$a}';
$string['gradeuser'] = 'Grade user: {$a}';
$string['noscreens'] = 'Could not find a suitable single view screen.';
$string['gradeitemcannotbeoverridden'] = 'This grade item cannot be overridden.';
$string['itemsperpage'] = 'Items per page';
$string['notvalid'] = 'Not a valid Single view screen: {$a}';
$string['override'] = 'Override';
$string['overrideall'] = 'Override all grades';