Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\external;
20
 
21
use renderer_base;
22
use core\persistent;
23
use core\external\persistent_exporter;
24
use core_reportbuilder\manager;
25
use core_reportbuilder\datasource;
26
use core_reportbuilder\form\filter as form_filter;
27
use core_reportbuilder\local\models\report;
28
use core_reportbuilder\table\custom_report_table;
29
use core_reportbuilder\table\custom_report_table_filterset;
30
use core_reportbuilder\table\custom_report_table_view;
31
use core_reportbuilder\table\custom_report_table_view_filterset;
32
use core_table\local\filter\integer_filter;
33
 
34
/**
35
 * Custom report exporter class
36
 *
37
 * @package     core_reportbuilder
38
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class custom_report_exporter extends persistent_exporter {
42
 
43
    /** @var report The persistent object we will export. */
44
    protected $persistent = null;
45
 
46
    /** @var bool */
47
    protected $editmode;
48
 
49
    /** @var string */
50
    protected $download;
51
 
52
    /**
53
     * report_exporter constructor.
54
     *
55
     * @param persistent $persistent
56
     * @param array $related
57
     * @param bool $editmode
58
     * @param string $download
59
     */
60
    public function __construct(persistent $persistent, array $related = [], bool $editmode = true, string $download = '') {
61
        parent::__construct($persistent, $related);
62
        $this->editmode = $editmode;
63
        $this->download = $download;
64
    }
65
    /**
66
     * Return the name of the class we are exporting
67
     *
68
     * @return string
69
     */
70
    protected static function define_class(): string {
71
        return report::class;
72
    }
73
 
74
    /**
75
     * Return a list of objects that are related to the persistent
76
     *
77
     * @return array
78
     */
79
    protected static function define_related(): array {
80
        return [
81
            'pagesize' => 'int?',
82
        ];
83
    }
84
 
85
    /**
86
     * Return a list of additional properties used only for display
87
     *
88
     * @return array
89
     */
90
    protected static function define_other_properties(): array {
91
        return [
92
            'table' => ['type' => PARAM_RAW],
1441 ariadna 93
            'button' => [
94
                'type' => report_action_exporter::read_properties_definition(),
95
                'optional' => true,
96
            ],
97
            'infocontainer' => ['type' => PARAM_RAW],
1 efrain 98
            'filtersapplied' => ['type' => PARAM_INT],
99
            'filterspresent' => ['type' => PARAM_BOOL],
100
            'filtersform' => ['type' => PARAM_RAW],
101
            'attributes' => [
102
                'type' => [
103
                    'name' => ['type' => PARAM_TEXT],
104
                    'value' => ['type' => PARAM_TEXT]
105
                ],
106
                'multiple' => true,
107
            ],
108
            'classes' => ['type' => PARAM_TEXT],
109
            'editmode' => ['type' => PARAM_BOOL],
110
            'sidebarmenucards' => [
111
                'type' => custom_report_column_cards_exporter::read_properties_definition(),
112
                'optional' => true,
113
            ],
114
            'conditions' => [
115
                'type' => custom_report_conditions_exporter::read_properties_definition(),
116
                'optional' => true,
117
            ],
118
            'filters' => [
119
                'type' => custom_report_filters_exporter::read_properties_definition(),
120
                'optional' => true,
121
            ],
122
            'sorting' => [
123
                'type' => custom_report_columns_sorting_exporter::read_properties_definition(),
124
                'optional' => true,
125
            ],
126
            'cardview' => [
127
                'type' => custom_report_card_view_exporter::read_properties_definition(),
128
                'optional' => true,
129
            ],
130
            'javascript' => ['type' => PARAM_RAW],
131
        ];
132
    }
133
 
134
    /**
135
     * Get additional values to inject while exporting
136
     *
137
     * @param renderer_base $output
138
     * @return array
139
     */
140
    protected function get_other_values(renderer_base $output): array {
141
        /** @var datasource $report */
142
        $report = manager::get_report_from_persistent($this->persistent);
143
 
1441 ariadna 144
        $optionalvalues = [];
1 efrain 145
        $filterspresent = false;
146
        $filtersform = '';
147
        $attributes = [];
148
 
149
        if ($this->editmode) {
150
            $table = custom_report_table::create($this->persistent->get('id'));
151
            $table->set_filterset(new custom_report_table_filterset());
152
        } else {
153
            // We store the pagesize within the table filterset so that it's available between AJAX requests.
154
            $filterset = new custom_report_table_view_filterset();
155
            $filterset->add_filter(new integer_filter('pagesize', null, [$this->related['pagesize']]));
156
 
157
            $table = custom_report_table_view::create($this->persistent->get('id'), $this->download);
158
            $table->set_filterset($filterset);
159
 
1441 ariadna 160
            // Export global report action.
161
            if ($reportaction = $report->get_report_action()) {
162
                $optionalvalues['button'] = $reportaction->export_for_template($output);
163
            }
164
 
1 efrain 165
            // Generate filters form if report contains any filters.
166
            $filterspresent = !empty($report->get_active_filters());
167
            if ($filterspresent && empty($this->download)) {
168
                $filtersform = $this->generate_filters_form()->render();
169
            }
170
 
171
            // Get the report classes and attributes.
172
            $reportattributes = $report->get_attributes();
173
            if (isset($reportattributes['class'])) {
174
                $classes = $reportattributes['class'];
175
                unset($reportattributes['class']);
176
            }
177
            $attributes = array_map(static function($key, $value): array {
178
                return ['name' => $key, 'value' => $value];
179
            }, array_keys($reportattributes), $reportattributes);
180
        }
181
 
182
        // If we are editing we need all this information for the template.
183
        if ($this->editmode) {
184
            $menucardsexporter = new custom_report_column_cards_exporter(null, ['report' => $report]);
1441 ariadna 185
            $optionalvalues['sidebarmenucards'] = (array) $menucardsexporter->export($output);
1 efrain 186
 
187
            $conditionsexporter = new custom_report_conditions_exporter(null, ['report' => $report]);
1441 ariadna 188
            $optionalvalues['conditions'] = (array) $conditionsexporter->export($output);
1 efrain 189
 
190
            $filtersexporter = new custom_report_filters_exporter(null, ['report' => $report]);
1441 ariadna 191
            $optionalvalues['filters'] = (array) $filtersexporter->export($output);
1 efrain 192
 
193
            $sortingexporter = new custom_report_columns_sorting_exporter(null, ['report' => $report]);
1441 ariadna 194
            $optionalvalues['sorting'] = (array) $sortingexporter->export($output);
1 efrain 195
 
196
            $cardviewexporter = new custom_report_card_view_exporter(null, ['report' => $report]);
1441 ariadna 197
            $optionalvalues['cardview'] = (array) $cardviewexporter->export($output);
1 efrain 198
        }
199
 
200
        return [
201
            'table' => $output->render($table),
1441 ariadna 202
            'infocontainer' => $report->get_report_info_container(),
1 efrain 203
            'filtersapplied' => $report->get_applied_filter_count(),
204
            'filterspresent' => $filterspresent,
205
            'filtersform' => $filtersform,
206
            'attributes' => $attributes,
207
            'classes' => $classes ?? '',
208
            'editmode' => $this->editmode,
209
            'javascript' => '',
1441 ariadna 210
        ] + $optionalvalues;
1 efrain 211
    }
212
 
213
    /**
214
     * Generate filters form for the report
215
     *
216
     * @return form_filter
217
     */
218
    private function generate_filters_form(): form_filter {
219
        $filtersform = new form_filter(null, null, 'post', '', [], true, [
220
            'reportid' => $this->persistent->get('id'),
221
            'parameters' => json_encode([]),
222
        ]);
223
        $filtersform->set_data_for_dynamic_submission();
224
 
225
        return $filtersform;
226
    }
227
}