From d064a93d290f71245397f25477f1ea7340bc9c21 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 19 Oct 2021 16:58:48 +0100 Subject: [PATCH] MDL-72598 reportbuilder: elements to allow audiences to be renamed. This allows the user to give the audience more context, particularly when they are used for selection on the forthcoming schedule form editing. --- lang/en/reportbuilder.php | 3 +- .../output/audience_heading_editable.php | 85 +++++++++++++++++++ .../classes/output/dynamictabs/audience.php | 10 ++- reportbuilder/lib.php | 16 ++-- .../templates/local/audience/form.mustache | 15 ++-- .../local/dynamictabs/audience.mustache | 5 +- reportbuilder/tests/behat/audience.feature | 16 ++-- 7 files changed, 124 insertions(+), 26 deletions(-) create mode 100644 reportbuilder/classes/output/audience_heading_editable.php diff --git a/lang/en/reportbuilder.php b/lang/en/reportbuilder.php index 0ce7787fd4e..d1fc19544ac 100644 --- a/lang/en/reportbuilder.php +++ b/lang/en/reportbuilder.php @@ -25,7 +25,6 @@ $string['access'] = 'Access'; $string['actions'] = 'Actions'; $string['addaudience'] = 'Add audience \'{$a}\''; -$string['addaudiences'] = 'Add an audience to this report'; $string['addcolumn'] = 'Add column \'{$a}\''; $string['addusers'] = 'Add users manually'; $string['aggregatecolumn'] = 'Aggregate column \'{$a}\''; @@ -147,6 +146,7 @@ $string['movecondition'] = 'Move condition \'{$a}\''; $string['movefilter'] = 'Move filter \'{$a}\''; $string['movesorting'] = 'Move sorting for column \'{$a}\''; $string['newreport'] = 'New report'; +$string['noaudiences'] = 'There are no audiences for this report'; $string['noconditions'] = 'There are no conditions selected'; $string['nofilters'] = 'There are no filters selected'; $string['nosortablecolumns'] = 'There are no sortable columns'; @@ -173,6 +173,7 @@ $string['privacy:metadata:schedule:name'] = 'The name of the schedule'; $string['privacy:metadata:schedule:usercreated'] = 'The ID of the user who created the schedule'; $string['privacy:metadata:schedule:usermodified'] = 'The ID of the user who last modified the schedule'; $string['privacy:metadata:schedule:userviewas'] = 'The ID of the user who the schedule will be viewed as'; +$string['renameaudience'] = 'Rename audience \'{$a}\''; $string['renamecolumn'] = 'Rename column \'{$a}\''; $string['renamefilter'] = 'Rename filter \'{$a}\''; $string['reportbuilder'] = 'Report builder'; diff --git a/reportbuilder/classes/output/audience_heading_editable.php b/reportbuilder/classes/output/audience_heading_editable.php new file mode 100644 index 00000000000..cbb119e0539 --- /dev/null +++ b/reportbuilder/classes/output/audience_heading_editable.php @@ -0,0 +1,85 @@ +. + +declare(strict_types=1); + +namespace core_reportbuilder\output; + +use core_external; +use core\output\inplace_editable; +use core_reportbuilder\permission; +use core_reportbuilder\local\audiences\base; +use core_reportbuilder\local\models\audience; + +/** + * Audience heading editable component + * + * @package core_reportbuilder + * @copyright 2021 Paul Holden + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class audience_heading_editable extends inplace_editable { + + /** + * Class constructor + * + * @param int $audienceid + * @param audience|null $audience + */ + public function __construct(int $audienceid, ?audience $audience = null) { + if ($audience === null) { + $audience = new audience($audienceid); + } + + $report = $audience->get_report(); + $editable = permission::can_edit_report($report); + + $audienceinstance = base::instance(0, $audience->to_record()); + + // Use audience defined title if custom heading not set. + if ('' !== $value = (string) $audience->get('heading')) { + $displayvalue = $audience->get_formatted_heading($report->get_context()); + } else { + $displayvalue = $value = $audienceinstance->get_name(); + } + + parent::__construct('core_reportbuilder', 'audienceheading', $audience->get('id'), $editable, $displayvalue, $value, + get_string('renameaudience', 'core_reportbuilder', $audienceinstance->get_name())); + } + + /** + * Update audience persistent and return self, called from inplace_editable callback + * + * @param int $audienceid + * @param string $value + * @return self + */ + public static function update(int $audienceid, string $value): self { + $audience = new audience($audienceid); + + $report = $audience->get_report(); + + core_external::validate_context($report->get_context()); + permission::require_can_edit_report($report); + + $value = clean_param($value, PARAM_TEXT); + $audience + ->set('heading', $value) + ->update(); + + return new self(0, $audience); + } +} diff --git a/reportbuilder/classes/output/dynamictabs/audience.php b/reportbuilder/classes/output/dynamictabs/audience.php index 3503177c39d..dba9bac5e79 100644 --- a/reportbuilder/classes/output/dynamictabs/audience.php +++ b/reportbuilder/classes/output/dynamictabs/audience.php @@ -22,6 +22,7 @@ use core\output\dynamic_tabs\base; use core_reportbuilder\external\custom_report_menu_cards_exporter; use core_reportbuilder\local\helpers\audience as audience_helper; use core_reportbuilder\local\models\report; +use core_reportbuilder\output\audience_heading_editable; use core_reportbuilder\permission; use renderer_base; @@ -96,6 +97,10 @@ class audience extends base { * @return array */ private function get_all_report_audiences(): array { + global $PAGE; + + $renderer = $PAGE->get_renderer('core'); + $audienceinstances = []; $reportaudiences = audience_helper::get_base_records((int)$this->data['reportid']); $showormessage = false; @@ -103,11 +108,14 @@ class audience extends base { $persistent = $reportaudience->get_persistent(); $canedit = $reportaudience->user_can_edit(); + $editable = new audience_heading_editable($persistent->get('id')); + $params = [ 'identifier' => $persistent->get('classname'), 'instanceid' => $persistent->get('id'), 'description' => $reportaudience->get_description(), - 'title' => $reportaudience->get_name(), + 'heading' => $reportaudience->get_name(), + 'headingeditable' => $editable->render($renderer), 'canedit' => $canedit, 'candelete' => $canedit, 'showormessage' => $showormessage, diff --git a/reportbuilder/lib.php b/reportbuilder/lib.php index e5a19533cad..944acb796a1 100644 --- a/reportbuilder/lib.php +++ b/reportbuilder/lib.php @@ -24,6 +24,7 @@ declare(strict_types=1); +use core\output\inplace_editable; use core_reportbuilder\form\audience; use core_reportbuilder\form\filter; @@ -61,7 +62,8 @@ function core_reportbuilder_output_fragment_audience_form(array $params): string $context = [ 'instanceid' => 0, - 'title' => $params['title'], + 'heading' => $params['title'], + 'headingeditable' => $params['title'], 'form' => $audienceform->render(), 'canedit' => true, 'candelete' => true, @@ -78,11 +80,9 @@ function core_reportbuilder_output_fragment_audience_form(array $params): string * @param string $itemtype * @param int $itemid * @param string $newvalue - * @return \core\output\inplace_editable|bool + * @return inplace_editable|null */ -function core_reportbuilder_inplace_editable($itemtype, $itemid, $newvalue) { - $itemid = (int) $itemid; - +function core_reportbuilder_inplace_editable(string $itemtype, int $itemid, string $newvalue): ?inplace_editable { switch ($itemtype) { case 'reportname': return \core_reportbuilder\output\report_name_editable::update($itemid, $newvalue); @@ -96,7 +96,9 @@ function core_reportbuilder_inplace_editable($itemtype, $itemid, $newvalue) { case 'filterheading': return \core_reportbuilder\output\filter_heading_editable::update($itemid, $newvalue); - default: - return false; + case 'audienceheading': + return \core_reportbuilder\output\audience_heading_editable::update($itemid, $newvalue); } + + return null; } diff --git a/reportbuilder/templates/local/audience/form.mustache b/reportbuilder/templates/local/audience/form.mustache index 5dea0b1ad9b..152e0d772f7 100644 --- a/reportbuilder/templates/local/audience/form.mustache +++ b/reportbuilder/templates/local/audience/form.mustache @@ -23,7 +23,8 @@ { "instanceid": 1, "classname": "some\\class\\name", - "title": "Title", + "heading": "Title", + "headingeditable": "Title (edit me)", "showormessage": 1, "canedit": 1, "candelete": 1, @@ -31,7 +32,7 @@ "form": "form" } }} -
+
{{#showormessage}} {{#str}} or, core_reportbuilder {{/str}} @@ -41,14 +42,14 @@

- {{title}} + {{{headingeditable}}}

{{#canedit}} @@ -56,8 +57,8 @@ {{#candelete}} diff --git a/reportbuilder/templates/local/dynamictabs/audience.mustache b/reportbuilder/templates/local/dynamictabs/audience.mustache index a8642219e03..e3f89bff7b0 100644 --- a/reportbuilder/templates/local/dynamictabs/audience.mustache +++ b/reportbuilder/templates/local/dynamictabs/audience.mustache @@ -27,7 +27,8 @@ "instances": [{ "instanceid": 1, "classname": "some\\class\\name", - "title": "Title", + "heading": "Title", + "headingeditable": "Title (edit me)", "showormessage": 1, "canedit": 1, "candelete": 1, @@ -54,7 +55,7 @@
{{! No audience container }}
-
{{#str}} addaudiences, core_reportbuilder {{/str}}
+
{{#str}} noaudiences, core_reportbuilder {{/str}}
{{#instances}} {{> core_reportbuilder/local/audience/form}} diff --git a/reportbuilder/tests/behat/audience.feature b/reportbuilder/tests/behat/audience.feature index eae7b4223cd..cf083a05302 100644 --- a/reportbuilder/tests/behat/audience.feature +++ b/reportbuilder/tests/behat/audience.feature @@ -27,7 +27,7 @@ Feature: Configure access to reports based on intended audience And I click on the "Access" dynamic tab And I should see "Nothing to display" And I click on the "Audience" dynamic tab - And I should see "Add an audience to this report" + And I should see "There are no audiences for this report" Then I click on "Add audience 'Manually added users'" "link" And I should see "Added audience 'Manually added users'" And I set the field "Add users manually" to "User 1,User 3" @@ -36,7 +36,7 @@ Feature: Configure access to reports based on intended audience And I should see "User 1" And I should not see "User 2" And I should see "User 3" - And I should not see "Add an audience to this report" + And I should not see "There are no audiences for this report" And I click on the "Access" dynamic tab And I should see "User 1" in the "reportbuilder-table" "table" And I should not see "User 2" in the "reportbuilder-table" "table" @@ -57,7 +57,7 @@ Feature: Configure access to reports based on intended audience And I press "Save changes" Then I should see "Audience saved" And I should see "Test role" - And I should not see "Add an audience to this report" + And I should not see "There are no audiences for this report" And I click on the "Access" dynamic tab And I should not see "User 1" in the "reportbuilder-table" "table" And I should see "User 2" in the "reportbuilder-table" "table" @@ -78,7 +78,7 @@ Feature: Configure access to reports based on intended audience And I press "Save changes" Then I should see "Audience saved" And I should see "Cohort1" - And I should not see "Add an audience to this report" + And I should not see "There are no audiences for this report" And I click on the "Access" dynamic tab And I should not see "User 1" in the "reportbuilder-table" "table" And I should not see "User 2" in the "reportbuilder-table" "table" @@ -99,14 +99,14 @@ Feature: Configure access to reports based on intended audience When I click on "Delete audience 'All users'" "button" And I click on "Delete" "button" in the "Delete audience 'All users'" "dialogue" Then I should see "Deleted audience 'All users'" - And I should see "Add an audience to this report" + And I should see "There are no audiences for this report" Scenario: Edit report audience with manually added users audience type Given I am on the "My report" "reportbuilder > Editor" page logged in as "admin" And I click on the "Access" dynamic tab And I should see "Nothing to display" And I click on the "Audience" dynamic tab - And I should see "Add an audience to this report" + And I should see "There are no audiences for this report" And I click on "Add audience 'Manually added users'" "link" And I set the field "Add users manually" to "User 1,User 3" And I press "Save changes" @@ -148,7 +148,7 @@ Feature: Configure access to reports based on intended audience And I navigate to "Reports > Report builder > Custom reports" in site administration And I click on "My report" "link" in the "My report" "table_row" And I click on the "Audience" dynamic tab - And I should see "Add an audience to this report" + And I should see "There are no audiences for this report" Then I click on "Add audience 'Manually added users'" "link" And I set the field "Add users manually" to "User 1" And I press "Save changes" @@ -192,7 +192,7 @@ Feature: Configure access to reports based on intended audience And I navigate to "Reports > Report builder > Custom reports" in site administration And I click on "My report" "link" in the "My report" "table_row" And I click on the "Audience" dynamic tab - And I should see "Add an audience to this report" + And I should see "There are no audiences for this report" Then I click on "Add audience 'Manually added users'" "link" And I set the field "Add users manually" to "User 1" And I press "Save changes"