From 8f7376df56f779d801387f2a8b0a403bbbdb9cf4 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 16 Feb 2024 14:56:52 +0000 Subject: [PATCH] MDL-80912 reportbuilder: output table caption when rendering reports. Move the `get_name` method to the base report class, so that it can be implemented by all report types. Provide a default implementation for system reports based on the name of the class. Use the name of the report as the caption. Target via Behat selectors. --- reportbuilder/classes/datasource.php | 7 ----- reportbuilder/classes/local/report/base.php | 13 ++++++-- reportbuilder/classes/system_report.php | 13 ++++++++ .../classes/table/base_report_table.php | 4 ++- .../tests/behat/customreports.feature | 30 +++++++++---------- reportbuilder/tests/behat/schedules.feature | 10 +++---- reportbuilder/upgrade.txt | 6 ++++ 7 files changed, 52 insertions(+), 31 deletions(-) diff --git a/reportbuilder/classes/datasource.php b/reportbuilder/classes/datasource.php index a0250e22642..fa5be6a7cb3 100644 --- a/reportbuilder/classes/datasource.php +++ b/reportbuilder/classes/datasource.php @@ -47,13 +47,6 @@ abstract class datasource extends base { /** @var array $activeconditions */ private $activeconditions; - /** - * Return user friendly name of the datasource - * - * @return string - */ - abstract public static function get_name(): string; - /** * Add columns from the given entity name to be available to use in a custom report * diff --git a/reportbuilder/classes/local/report/base.php b/reportbuilder/classes/local/report/base.php index c9c19922d40..a0a8f5821bf 100644 --- a/reportbuilder/classes/local/report/base.php +++ b/reportbuilder/classes/local/report/base.php @@ -112,6 +112,13 @@ abstract class base { return $this->report; } + /** + * Return user friendly name of the report + * + * @return string + */ + abstract public static function get_name(): string; + /** * Initialise report. Specify which columns, filters, etc should be present * @@ -696,11 +703,11 @@ abstract class base { * Set if the report can be downloaded. * * @param bool $downloadable - * @param string $downloadfilename If the report is downloadable, then a filename should be provided here + * @param string|null $downloadfilename If downloadable, then the name of the file (defaults to the name of the current report) */ - final public function set_downloadable(bool $downloadable, string $downloadfilename = 'export'): void { + final public function set_downloadable(bool $downloadable, ?string $downloadfilename = null): void { $this->downloadable = $downloadable; - $this->downloadfilename = $downloadfilename; + $this->downloadfilename = $downloadfilename ?? static::get_name(); } /** diff --git a/reportbuilder/classes/system_report.php b/reportbuilder/classes/system_report.php index 84c07291f0a..ba2c9ade561 100644 --- a/reportbuilder/classes/system_report.php +++ b/reportbuilder/classes/system_report.php @@ -70,6 +70,19 @@ abstract class system_report extends base { parent::__construct($report); } + /** + * Provide default implementation of the report name. Extending classes can implement this method to provide their own name + * + * @return string + */ + public static function get_name(): string { + $classparts = explode('\\', get_called_class()); + $classname = end($classparts); + + // Try to make human readable, capitalized and with spaces. + return ucfirst(str_replace('_', ' ', $classname)); + } + /** * Validates access to view this report * diff --git a/reportbuilder/classes/table/base_report_table.php b/reportbuilder/classes/table/base_report_table.php index 82065ccfc82..6b84dda0f54 100644 --- a/reportbuilder/classes/table/base_report_table.php +++ b/reportbuilder/classes/table/base_report_table.php @@ -271,7 +271,9 @@ abstract class base_report_table extends table_sql implements dynamic, renderabl $this->wrap_html_start(); + $this->set_caption($this->report::get_name(), ['class' => 'sr-only']); + echo html_writer::start_tag('div'); - echo html_writer::start_tag('table', $this->attributes); + echo html_writer::start_tag('table', $this->attributes) . $this->render_caption(); } } diff --git a/reportbuilder/tests/behat/customreports.feature b/reportbuilder/tests/behat/customreports.feature index 4337448d2d7..d69bdfe7edc 100644 --- a/reportbuilder/tests/behat/customreports.feature +++ b/reportbuilder/tests/behat/customreports.feature @@ -20,18 +20,18 @@ Feature: Manage custom reports And I click on "Save" "button" in the "New report" "dialogue" Then I should see "My report" # Confirm we see the default columns in the report. - And I should see "Full name" in the "reportbuilder-table" "table" - And I should see "Username" in the "reportbuilder-table" "table" - And I should see "Email address" in the "reportbuilder-table" "table" + And I should see "Full name" in the "Users" "table" + And I should see "Username" in the "Users" "table" + And I should see "Email address" in the "Users" "table" # Confirm we see the default sorting in the report And "Admin User" "table_row" should appear before "User 2" "table_row" And I click on "Show/hide 'Sorting'" "button" And "Disable initial sorting for column 'Full name'" "checkbox" should exist in the "#settingssorting" "css_element" And I click on "Show/hide 'Sorting'" "button" # Confirm we only see not suspended users in the report. - And I should see "Admin User" in the "reportbuilder-table" "table" - And I should see "User 2" in the "reportbuilder-table" "table" - And I should not see "User 1" in the "reportbuilder-table" "table" + And I should see "Admin User" in the "Users" "table" + And I should see "User 2" in the "Users" "table" + And I should not see "User 1" in the "Users" "table" # Confirm we see the default conditions in the report. And I click on "Show/hide 'Conditions'" "button" Then I should see "Full name" in the "[data-region='settings-conditions']" "css_element" @@ -47,7 +47,7 @@ Feature: Manage custom reports Then I should see "Username" in the "[data-region='report-filters']" "css_element" Then I should see "Email address" in the "[data-region='report-filters']" "css_element" And I click on "Close 'My report' editor" "button" - And the following should exist in the "reportbuilder-table" table: + And the following should exist in the "Reports list" table: | Name | Report source | Modified by | | My report | Users | Admin User | @@ -67,7 +67,7 @@ Feature: Manage custom reports Then I should see "My report" And I should see "Nothing to display" And I click on "Close 'My report' editor" "button" - And the following should exist in the "reportbuilder-table" table: + And the following should exist in the "Reports list" table: | Name | Report source | | My report | Users | @@ -110,7 +110,7 @@ Feature: Manage custom reports And I set the field "Edit report name" in the "My report" "table_row" to " " And I set the field "Edit report name" in the "My report" "table_row" to "My renamed report" And I reload the page - Then the following should exist in the "reportbuilder-table" table: + Then the following should exist in the "Reports list" table: | Name | Report source | | My renamed report | Users | @@ -124,8 +124,8 @@ Feature: Manage custom reports When I navigate to "Reports > Report builder > Custom reports" in site administration And I set the field "Edit report name" in the "My report" "table_row" to "EnglishSpanish" And I reload the page - Then I should see "English" in the "reportbuilder-table" "table" - And I should not see "Spanish" in the "reportbuilder-table" "table" + Then I should see "English" in the "Reports list" "table" + And I should not see "Spanish" in the "Reports list" "table" # Confirm report name is correctly shown in action. And I press "Delete report" action in the "English" report row And I should see "Are you sure you want to delete the report 'English' and all associated data?" in the "Delete report" "dialogue" @@ -142,7 +142,7 @@ Feature: Manage custom reports | Name | My renamed report | And I click on "Save" "button" in the "Edit report details" "dialogue" Then I should see "Report updated" - And the following should exist in the "reportbuilder-table" table: + And the following should exist in the "Reports list" table: | Name | Report source | | My renamed report | Users | @@ -158,7 +158,7 @@ Feature: Manage custom reports | value | | And I click on "Apply" "button" in the "[data-region='report-filters']" "css_element" Then I should see "Filters applied" - And I should see "My users" in the "reportbuilder-table" "table" + And I should see "My users" in the "Reports list" "table" Examples: | filter | value | | Name | My users | @@ -247,10 +247,10 @@ Feature: Manage custom reports When I am on the "Report users" "reportbuilder > Editor" page logged in as "admin" Then I should see "Viewing of report data while editing is disabled by the site administrator. Switch to preview mode to view the report." in the "[data-region='core_table/dynamic']" "css_element" And I click on "Switch to preview mode" "button" - And I should see "admin" in the "reportbuilder-table" "table" + And I should see "admin" in the "Users" "table" And I click on "Close 'Report users' editor" "button" And I press "View" action in the "Report users" report row - And I should see "admin" in the "reportbuilder-table" "table" + And I should see "admin" in the "Users" "table" Scenario Outline: Download custom report in different formats Given the following "users" exist: diff --git a/reportbuilder/tests/behat/schedules.feature b/reportbuilder/tests/behat/schedules.feature index d4153126ac1..231a1d8a236 100644 --- a/reportbuilder/tests/behat/schedules.feature +++ b/reportbuilder/tests/behat/schedules.feature @@ -40,7 +40,7 @@ Feature: Manage custom report schedules And I set the field "Manually added users: User One, User Two" to "1" And I click on "Save" "button" in the "New schedule" "dialogue" Then I should see "Schedule created" - And the following should exist in the "reportbuilder-table" table: + And the following should exist in the "Report schedules" table: | Name | Starting from | Time last sent | Modified by | | My schedule | ##tomorrow 11:00##%A, %d %B %Y, %H:%M## | Never | Admin User | @@ -64,7 +64,7 @@ Feature: Manage custom report schedules And I click on the "Schedules" dynamic tab When I set the field "Edit schedule name" in the "My schedule" "table_row" to "My renamed schedule" And I reload the page - Then I should see "My renamed schedule" in the "reportbuilder-table" "table" + Then I should see "My renamed schedule" in the "Report schedules" "table" Scenario: Rename report schedule using filters Given the "multilang" filter is "on" @@ -76,8 +76,8 @@ Feature: Manage custom report schedules And I click on the "Schedules" dynamic tab When I set the field "Edit schedule name" in the "My schedule" "table_row" to "EnglishSpanish" And I reload the page - Then I should see "English" in the "reportbuilder-table" "table" - And I should not see "Spanish" in the "reportbuilder-table" "table" + Then I should see "English" in the "Report schedules" "table" + And I should not see "Spanish" in the "Report schedules" "table" # Confirm schedule name is correctly shown in actions. And I press "Send schedule" action in the "English" report row And I should see "Are you sure you want to queue the schedule 'English' for sending immediately?" in the "Send schedule" "dialogue" @@ -109,7 +109,7 @@ Feature: Manage custom report schedules | All users: All site users | 1 | And I click on "Save" "button" in the "Edit schedule details" "dialogue" Then I should see "Schedule updated" - And the following should exist in the "reportbuilder-table" table: + And the following should exist in the "Report schedules" table: | Name | Starting from | | My updated schedule | ##tomorrow 11:00##%A, %d %B %Y, %H:%M## | diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index 6cd884281f6..6fe0ddba687 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -1,6 +1,12 @@ This file describes API changes in /reportbuilder/* Information provided here is intended especially for developers. +=== 4.2.7 === + +* The `get_name` method has been moved to the base report class and can now be implemented for both custom and system reports, it + will be used as the caption when rendering tables (which can also be targeted in Behat scenarios) +* The default value for the filename when calling `set_downloadable` is now taken from calling `get_name` on the current report + === 4.2 === * New method `set_checkbox_toggleall` in system report class to allow reports to easily create checkbox toggle columns