MDL-69201 core: consistent table primary sort column/order.

This commit is contained in:
Paul Holden 2020-10-21 00:22:23 +01:00
parent d330035f11
commit 70936e0c5e

View File

@ -1201,6 +1201,12 @@ class flexible_table {
function print_headers() {
global $CFG, $OUTPUT;
// Set the primary sort column/order where possible, so that sort links/icons are correct.
[
'sortby' => $primarysortcolumn,
'sortorder' => $primarysortorder,
] = $this->get_primary_sort_order();
echo html_writer::start_tag('thead');
echo html_writer::start_tag('tr');
foreach ($this->columns as $column => $index) {
@ -1209,14 +1215,6 @@ class flexible_table {
if ($this->is_collapsible) {
$icon_hide = $this->show_hide_link($column, $index);
}
$primarysortcolumn = '';
$primarysortorder = '';
if (reset($this->prefs['sortby'])) {
$primarysortcolumn = key($this->prefs['sortby']);
$primarysortorder = current($this->prefs['sortby']);
}
switch ($column) {
case 'fullname':
@ -1596,6 +1594,22 @@ class flexible_table {
]) . ' ' . $this->sort_icon($isprimary, $order);
}
/**
* Return primary sorting column/order, either the first preferred "sortby" value or defaults defined for the table
*
* @return array
*/
protected function get_primary_sort_order(): array {
if (reset($this->prefs['sortby'])) {
return $this->get_sort_order();
}
return [
'sortby' => $this->sort_default_column,
'sortorder' => $this->sort_default_order,
];
}
/**
* Return sorting attributes values.
*