MDL-74714 reportbuilder: clarify optional properties of exporters.
[moodle.git] / reportbuilder / classes / external / custom_report_conditions_exporter.php
blobf376adb977bd9883b51debf877f8a11e17bee0d8
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 declare(strict_types=1);
19 namespace core_reportbuilder\external;
21 use renderer_base;
22 use core\external\exporter;
23 use core_reportbuilder\form\condition;
24 use core_reportbuilder\local\report\base;
25 use core_reportbuilder\local\models\filter;
27 /**
28 * Custom report conditions exporter class
30 * @package core_reportbuilder
31 * @copyright 2021 Paul Holden <paulh@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class custom_report_conditions_exporter extends exporter {
36 /**
37 * Return a list of objects that are related to the exporter
39 * @return array
41 protected static function define_related(): array {
42 return [
43 'report' => base::class,
47 /**
48 * Return the list of additional properties for read structure and export
50 * @return array[]
52 protected static function define_other_properties(): array {
53 return [
54 'hasavailableconditions' => [
55 'type' => PARAM_BOOL,
57 'availableconditions' => [
58 'type' => [
59 'optiongroup' => [
60 'type' => [
61 'text' => ['type' => PARAM_TEXT],
62 'values' => [
63 'type' => [
64 'value' => ['type' => PARAM_TEXT],
65 'visiblename' => ['type' => PARAM_TEXT],
67 'multiple' => true,
72 'multiple' => true,
74 'hasactiveconditions' => [
75 'type' => PARAM_BOOL,
77 'activeconditionsform' => [
78 'type' => PARAM_RAW,
80 'helpicon' => [
81 'type' => PARAM_RAW,
83 'javascript' => [
84 'type' => PARAM_RAW,
85 'optional' => true,
90 /**
91 * Get the additional values to inject while exporting
93 * @param renderer_base $output
94 * @return array
96 protected function get_other_values(renderer_base $output): array {
97 /** @var base $report */
98 $report = $this->related['report'];
99 $reportid = $report->get_report_persistent()->get('id');
101 // Current condition instances contained in the report.
102 $conditioninstances = filter::get_condition_records($reportid, 'filterorder');
103 $conditionidentifiers = array_map(static function(filter $condition): string {
104 return $condition->get('uniqueidentifier');
105 }, $conditioninstances);
107 $availableconditions = [];
109 // Populate available conditions.
110 foreach ($report->get_conditions() as $condition) {
111 if (in_array($condition->get_unique_identifier(), $conditionidentifiers)) {
112 continue;
115 $entityname = $condition->get_entity_name();
116 if (!array_key_exists($entityname, $availableconditions)) {
117 $availableconditions[$entityname] = [
118 'optiongroup' => [
119 'text' => $report->get_entity_title($entityname)->out(),
120 'values' => [],
125 $availableconditions[$entityname]['optiongroup']['values'][] = [
126 'value' => $condition->get_unique_identifier(),
127 'visiblename' => $condition->get_header(),
131 // Generate filters form if report contains any filters.
132 $conditionspresent = !empty($conditioninstances);
133 if ($conditionspresent) {
134 $conditionsform = new condition(null, null, 'post', '', [], true, [
135 'reportid' => $reportid,
137 $conditionsform->set_data_for_dynamic_submission();
140 return [
141 'hasavailableconditions' => !empty($availableconditions),
142 'availableconditions' => array_values($availableconditions),
143 'hasactiveconditions' => $conditionspresent,
144 'activeconditionsform' => $conditionspresent ? $conditionsform->render() : '',
145 'helpicon' => $output->help_icon('conditions', 'core_reportbuilder'),