Merge branch 'MDL-73468' of git://github.com/paulholden/moodle

This commit is contained in:
Jun Pataleta 2022-01-07 14:27:31 +08:00
commit bf1a7b0bb2
5 changed files with 38 additions and 15 deletions

View File

@ -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;
}
/**

View File

@ -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([

View File

@ -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

View File

@ -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"

View File

@ -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: