From 98e7d99720a51f94f41bcbffea8cb2866d8fa791 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 29 Dec 2021 14:20:17 +0000 Subject: [PATCH] MDL-73468 reportbuilder: observe configured sorting when editing. Users preferred sorting (via clicking on column headers) should only take effect when pre/viewing the report, not when editing it. --- .../classes/table/base_report_table.php | 16 ++++++++-------- .../classes/table/custom_report_table.php | 12 +++++++----- .../classes/table/custom_report_table_view.php | 4 ++-- .../tests/behat/columnsortingeditor.feature | 16 ++++++++++++++++ reportbuilder/tests/behat/filtereditor.feature | 5 +++++ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/reportbuilder/classes/table/base_report_table.php b/reportbuilder/classes/table/base_report_table.php index ba3ab5e0c4c..4d2b190e198 100644 --- a/reportbuilder/classes/table/base_report_table.php +++ b/reportbuilder/classes/table/base_report_table.php @@ -55,8 +55,8 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl /** @var string $groupbysql */ protected $groupbysql = ''; - /** @var bool $applyfilters */ - protected $applyfilters = true; + /** @var bool $editing */ + protected $editing = false; /** * Initialises table SQL properties @@ -88,7 +88,7 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl } // For each filter, we also need to apply their values (will differ according to user viewing the report). - if ($this->applyfilters) { + if (!$this->editing) { $filtervalues = $this->report->get_filter_values(); foreach ($this->report->get_active_filters() as $filter) { [$filtersql, $filterparams] = $this->get_filter_sql($filter, $filtervalues); @@ -127,13 +127,13 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl } /** - * Whether active filters should be applied to the report, defaults to true except in the case where we are editing a report - * and we do not want filters to be applied to it + * Whether the current report table is being edited, in which case certain actions are not applied to it, e.g. user filtering + * and sorting. Default class value is false * - * @param bool $applyfilters + * @param bool $editing */ - public function set_filters_applied(bool $applyfilters): void { - $this->applyfilters = $applyfilters; + public function set_report_editing(bool $editing): void { + $this->editing = $editing; } /** diff --git a/reportbuilder/classes/table/custom_report_table.php b/reportbuilder/classes/table/custom_report_table.php index a534e7df303..486457b5b19 100644 --- a/reportbuilder/classes/table/custom_report_table.php +++ b/reportbuilder/classes/table/custom_report_table.php @@ -42,8 +42,8 @@ class custom_report_table extends base_report_table { /** @var string Unique ID prefix for the table */ private const UNIQUEID_PREFIX = 'custom-report-table-'; - /** @var bool Whether filters should be applied in report (we don't want them when editing) */ - protected const REPORT_APPLY_FILTERS = false; + /** @var bool Whether report is being edited (we don't want user filters/sorting to be applied when editing) */ + protected const REPORT_EDITING = true; /** * Table constructor. Note that the passed unique ID value must match the pattern "custom-report-table-(\d+)" so that @@ -144,7 +144,7 @@ class custom_report_table extends base_report_table { $this->pageable(true); // Initialise table SQL properties. - $this->set_filters_applied(static::REPORT_APPLY_FILTERS); + $this->set_report_editing(static::REPORT_EDITING); $fieldsql = implode(', ', $fields); $this->init_sql($fieldsql, "{{$maintable}} {$maintablealias}", $joins, $where, $params, $groupby); @@ -166,9 +166,11 @@ class custom_report_table extends base_report_table { * * @return array */ - public function get_sort_columns() { + public function get_sort_columns(): array { $sortcolumns = parent::get_sort_columns(); - if (empty($sortcolumns)) { + + if ($this->editing || empty($sortcolumns)) { + $sortcolumns = []; $columns = $this->get_active_columns(); $instances = column_model::get_records([ diff --git a/reportbuilder/classes/table/custom_report_table_view.php b/reportbuilder/classes/table/custom_report_table_view.php index e16fd8e6201..3fca2dbf005 100644 --- a/reportbuilder/classes/table/custom_report_table_view.php +++ b/reportbuilder/classes/table/custom_report_table_view.php @@ -29,8 +29,8 @@ use moodle_url; */ class custom_report_table_view extends custom_report_table { - /** @var bool We do want to apply filters when viewing or previewing report */ - protected const REPORT_APPLY_FILTERS = true; + /** @var bool We're pre/viewing the report, not editing it */ + protected const REPORT_EDITING = false; /** * Override printed headers, to use those of grandparent class diff --git a/reportbuilder/tests/behat/columnsortingeditor.feature b/reportbuilder/tests/behat/columnsortingeditor.feature index ec9d42db66b..8e70eaddd78 100644 --- a/reportbuilder/tests/behat/columnsortingeditor.feature +++ b/reportbuilder/tests/behat/columnsortingeditor.feature @@ -53,6 +53,22 @@ Feature: Manage custom report columns sorting And "First name" "text" should appear before "Surname" "text" in the "#settingssorting" "css_element" And "user01" "table_row" should appear before "user02" "table_row" + Scenario: Configured report sorting is always applied when editing + Given I change window size to "large" + And I click on "Show/hide 'Sorting'" "button" + # Sort by last name descending. + When I click on "Enable sorting for column 'Surname'" "checkbox" + Then "user02" "table_row" should appear before "user01" "table_row" + # Switching to preview mode should observe report config. + And I click on "Switch to preview mode" "button" + And "user02" "table_row" should appear before "user01" "table_row" + # Custom sorting for the user. + And I click on "Sort by First name Ascending" "link" + And "user01" "table_row" should appear before "user02" "table_row" + # Switching back to edit mode should observe report config. + And I click on "Switch to edit mode" "button" + And "user02" "table_row" should appear before "user01" "table_row" + Scenario: Sortable columns are updated when column is added to report Given I change window size to "large" And I click on "Show/hide 'Sorting'" "button" diff --git a/reportbuilder/tests/behat/filtereditor.feature b/reportbuilder/tests/behat/filtereditor.feature index 631a87b9108..d4c723a6b92 100644 --- a/reportbuilder/tests/behat/filtereditor.feature +++ b/reportbuilder/tests/behat/filtereditor.feature @@ -114,6 +114,11 @@ Feature: Manage custom report filters And the following should not exist in the "reportbuilder-table" table: | Full name | Email address | | User 2 | user2@example.com | + # Switching back to edit mode should not apply filters. + And I click on "Switch to edit mode" "button" + And I should see "user1@example.com" in the ".reportbuilder-table" "css_element" + And I should see "user2@example.com" in the ".reportbuilder-table" "css_element" + And I should see "user3@example.com" in the ".reportbuilder-table" "css_element" Scenario: Use report filters when previewing report that contains same condition Given the following "users" exist: