diff --git a/reportbuilder/classes/external/custom_report_exporter.php b/reportbuilder/classes/external/custom_report_exporter.php
index 00de414418c..9fb6f60e8ad 100644
--- a/reportbuilder/classes/external/custom_report_exporter.php
+++ b/reportbuilder/classes/external/custom_report_exporter.php
@@ -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;
diff --git a/reportbuilder/classes/external/system_report_exporter.php b/reportbuilder/classes/external/system_report_exporter.php
index bc7fd29ed60..cabfa2a5457 100644
--- a/reportbuilder/classes/external/system_report_exporter.php
+++ b/reportbuilder/classes/external/system_report_exporter.php
@@ -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,
];
}
}
diff --git a/reportbuilder/classes/local/report/base.php b/reportbuilder/classes/local/report/base.php
index 5abe25d932f..c9c19922d40 100644
--- a/reportbuilder/classes/local/report/base.php
+++ b/reportbuilder/classes/local/report/base.php
@@ -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;
+ }
}
diff --git a/reportbuilder/templates/local/dynamictabs/editor.mustache b/reportbuilder/templates/local/dynamictabs/editor.mustache
index 92f0d18e1ba..8f04f4d30eb 100644
--- a/reportbuilder/templates/local/dynamictabs/editor.mustache
+++ b/reportbuilder/templates/local/dynamictabs/editor.mustache
@@ -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}}>
{{#editmode}}
{{! Menu sidebar }}
diff --git a/reportbuilder/templates/report.mustache b/reportbuilder/templates/report.mustache
index 8912ccfebcc..ff28539a42f 100644
--- a/reportbuilder/templates/report.mustache
+++ b/reportbuilder/templates/report.mustache
@@ -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}}>
{{#filterspresent}}
diff --git a/reportbuilder/tests/external/custom_report_exporter_test.php b/reportbuilder/tests/external/custom_report_exporter_test.php
index b50c409c112..297db31ddb0 100644
--- a/reportbuilder/tests/external/custom_report_exporter_test.php
+++ b/reportbuilder/tests/external/custom_report_exporter_test.php
@@ -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);
diff --git a/reportbuilder/tests/external/system_report_exporter_test.php b/reportbuilder/tests/external/system_report_exporter_test.php
index f8ce6f3174c..3a2c90e0641 100644
--- a/reportbuilder/tests/external/system_report_exporter_test.php
+++ b/reportbuilder/tests/external/system_report_exporter_test.php
@@ -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);
}
}
diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt
index 35c38c6fb7f..a366e6a30ea 100644
--- a/reportbuilder/upgrade.txt
+++ b/reportbuilder/upgrade.txt
@@ -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