MDL-75943 reportbuilder: Add custom attributes to reports

This commit is contained in:
Mikel Martín 2022-10-20 12:27:27 +02:00
parent 40a89d8a9a
commit 91cc98ed3d
8 changed files with 78 additions and 3 deletions

View File

@ -93,6 +93,13 @@ class custom_report_exporter extends persistent_exporter {
'filtersapplied' => ['type' => PARAM_INT],
'filterspresent' => ['type' => PARAM_BOOL],
'filtersform' => ['type' => PARAM_RAW],
'attributes' => [
'type' => [
'name' => ['type' => PARAM_TEXT],
'value' => ['type' => PARAM_TEXT]
],
'multiple' => true,
],
'editmode' => ['type' => PARAM_BOOL],
'sidebarmenucards' => [
'type' => custom_report_column_cards_exporter::read_properties_definition(),
@ -130,6 +137,7 @@ class custom_report_exporter extends persistent_exporter {
$filterspresent = false;
$filtersform = '';
$attributes = [];
if ($this->editmode) {
$table = custom_report_table::create($this->persistent->get('id'));
@ -147,6 +155,10 @@ class custom_report_exporter extends persistent_exporter {
if ($filterspresent) {
$filtersform = $this->generate_filters_form()->render();
}
// Get the report attributes.
$attributes = array_map(static function($key, $value): array {
return ['name' => $key, 'value' => $value];
}, array_keys($report->get_attributes()), $report->get_attributes());
}
// If we are editing we need all this information for the template.
@ -173,6 +185,7 @@ class custom_report_exporter extends persistent_exporter {
'filtersapplied' => $report->get_applied_filter_count(),
'filterspresent' => $filterspresent,
'filtersform' => $filtersform,
'attributes' => $attributes,
'editmode' => $this->editmode,
'javascript' => '',
] + $editordata;

View File

@ -70,6 +70,13 @@ class system_report_exporter extends persistent_exporter {
'filterspresent' => ['type' => PARAM_BOOL],
'filtersapplied' => ['type' => PARAM_INT],
'filtersform' => ['type' => PARAM_RAW],
'attributes' => [
'type' => [
'name' => ['type' => PARAM_TEXT],
'value' => ['type' => PARAM_TEXT]
],
'multiple' => true,
],
];
}
@ -109,12 +116,18 @@ class system_report_exporter extends persistent_exporter {
$filtersform->set_data_for_dynamic_submission();
}
// Get the report attributes.
$attributes = array_map(static function($key, $value): array {
return ['name' => $key, 'value' => $value];
}, array_keys($source->get_attributes()), $source->get_attributes());
return [
'table' => $output->render($table),
'parameters' => $parameters,
'filterspresent' => $filterspresent,
'filtersapplied' => $source->get_applied_filter_count(),
'filtersform' => $filterspresent ? $filtersform->render() : '',
'attributes' => $attributes,
];
}
}

View File

@ -87,6 +87,9 @@ abstract class base {
/** @var int Default paging size */
private $defaultperpage = self::DEFAULT_PAGESIZE;
/** @var array $attributes */
private $attributes = [];
/**
* Base report constructor
*
@ -744,4 +747,24 @@ abstract class base {
public function get_default_per_page(): int {
return $this->defaultperpage;
}
/**
* Add report attributes (data-, class, etc.) that will be included in HTML when report is displayed
*
* @param array $attributes
* @return self
*/
public function add_attributes(array $attributes): self {
$this->attributes = $attributes + $this->attributes;
return $this;
}
/**
* Returns the report HTML attributes
*
* @return array
*/
public function get_attributes(): array {
return $this->attributes;
}
}

View File

@ -25,6 +25,10 @@
"type": 1,
"table": "table",
"editmode": true,
"attributes": [{
"name": "data-custom",
"value": "1"
}],
"sidebarmenucards": [{
"menucards": [{
"name": "General",
@ -93,7 +97,8 @@
data-report-id="{{id}}"
data-report-type="{{type}}"
data-parameter="[]"
{{#editmode}}data-editing{{/editmode}}>
{{#editmode}}data-editing{{/editmode}}
{{#attributes}}{{name}}="{{value}}" {{/attributes}}>
<div class="reportbuilder-wrapper d-flex flex-column flex-lg-row">
{{#editmode}}
{{! Menu sidebar }}

View File

@ -25,6 +25,10 @@
"contextid": 1,
"type": 1,
"parameters": [],
"attributes": [{
"name": "data-custom",
"value": "1"
}],
"table": "table",
"filterspresent": true,
"filtersform": "form"
@ -34,7 +38,8 @@
data-region="core_reportbuilder/report"
data-report-id="{{id}}"
data-report-type="{{type}}"
data-parameter="{{parameters}}">
data-parameter="{{parameters}}"
{{#attributes}}{{name}}="{{value}}" {{/attributes}}>
<div class="reportbuilder-wrapper">
{{#filterspresent}}
<div class="dropdown d-flex justify-content-end">

View File

@ -19,6 +19,7 @@ declare(strict_types=1);
namespace core_reportbuilder\external;
use advanced_testcase;
use core_reportbuilder\manager;
use core_reportbuilder_generator;
use moodle_url;
use core_reportbuilder\local\helpers\user_filter_manager;
@ -46,6 +47,7 @@ class custom_report_exporter_test extends advanced_testcase {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
manager::get_report_from_persistent($report)->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
$PAGE->set_url(new moodle_url('/'));
@ -57,6 +59,7 @@ class custom_report_exporter_test extends advanced_testcase {
$this->assertFalse($export->filterspresent);
$this->assertEmpty($export->filtersform);
$this->assertTrue($export->editmode);
$this->assertEmpty($export->attributes);
// The following are all generated by additional exporters.
$this->assertNotEmpty($export->sidebarmenucards);
@ -77,6 +80,7 @@ class custom_report_exporter_test extends advanced_testcase {
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
manager::get_report_from_persistent($report)->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
$PAGE->set_url(new moodle_url('/'));
@ -88,6 +92,10 @@ class custom_report_exporter_test extends advanced_testcase {
$this->assertFalse($export->filterspresent);
$this->assertEmpty($export->filtersform);
$this->assertFalse($export->editmode);
$this->assertEquals([
['name' => 'data-foo', 'value' => 'bar'],
['name' => 'data-another', 'value' => '1']
], $export->attributes);
// The following are all generated by additional exporters, and should not be present when not editing.
$this->assertObjectNotHasAttribute('sidebarmenucards', $export);

View File

@ -71,7 +71,7 @@ class system_report_exporter_test extends advanced_testcase {
$PAGE->set_url(new moodle_url('/'));
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance(), '', '', 0,
['withfilters' => $withfilters]);
['withfilters' => $withfilters])->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
$exporter = new system_report_exporter($systemreport->get_report_persistent(), [
'source' => $systemreport,
@ -90,5 +90,10 @@ class system_report_exporter_test extends advanced_testcase {
$this->assertFalse($data->filterspresent);
$this->assertEmpty($data->filtersform);
}
$this->assertEquals([
['name' => 'data-foo', 'value' => 'bar'],
['name' => 'data-another', 'value' => '1']
], $data->attributes);
}
}

View File

@ -1,6 +1,9 @@
This file describes API changes in /reportbuilder/*
Information provided here is intended especially for developers.
=== 4.2 ===
* New methods `[add|get]_attributes` added to report base class, for including custom attributes in report container HTML
=== 4.1 ===
* New method `add_action_divider()` in base system report class, to allow adding a divider to the action menu.
* New external method `core_reportbuilder_set_filters` for setting report filter values (plus `setFilters` AJAX repository