Skip to content

Commit

Permalink
MDL-69201 core: consistent table primary sort column/order.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Nov 9, 2020
1 parent ec58cef commit 898dd7b
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/tablelib.php
Expand Up @@ -1201,6 +1201,12 @@ protected function show_hide_link($column, $index) {
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) {
Expand All @@ -1209,14 +1215,6 @@ function print_headers() {
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':
Expand Down Expand Up @@ -1596,6 +1594,22 @@ protected function sort_link($text, $column, $isprimary, $order) {
]) . ' ' . $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.
*
Expand Down

0 comments on commit 898dd7b

Please sign in to comment.