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 core\external\persistent_exporter;
22
use core_table\local\filter\integer_filter;
23
use core_table\local\filter\string_filter;
24
use core_reportbuilder\system_report;
25
use core_reportbuilder\form\filter;
26
use core_reportbuilder\local\models\report;
27
use core_reportbuilder\table\system_report_table;
28
use core_reportbuilder\table\system_report_table_filterset;
29
use renderer_base;
30
 
31
/**
32
 * Report exporter class
33
 *
34
 * @package     core_reportbuilder
35
 * @copyright   2020 Paul Holden <paulh@moodle.com>
36
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class system_report_exporter extends persistent_exporter {
39
 
40
    /**
41
     * Return the name of the class we are exporting
42
     *
43
     * @return string
44
     */
45
    protected static function define_class(): string {
46
        return report::class;
47
    }
48
 
49
    /**
50
     * Return a list of objects that are related to the persistent
51
     *
52
     * @return array
53
     */
54
    protected static function define_related(): array {
55
        return [
56
            'source' => system_report::class,
57
            'parameters' => 'string',
58
        ];
59
    }
60
 
61
    /**
62
     * Return a list of additional properties used only for display
63
     *
64
     * @return array
65
     */
66
    protected static function define_other_properties(): array {
67
        return [
68
            'table' => ['type' => PARAM_RAW],
69
            'parameters' => ['type' => PARAM_RAW],
1441 ariadna 70
            'button' => [
71
                'type' => report_action_exporter::read_properties_definition(),
72
                'optional' => true,
73
            ],
74
            'infocontainer' => ['type' => PARAM_RAW],
1 efrain 75
            'filterspresent' => ['type' => PARAM_BOOL],
76
            'filtersapplied' => ['type' => PARAM_INT],
77
            'filtersform' => ['type' => PARAM_RAW],
78
            'attributes' => [
79
                'type' => [
80
                    'name' => ['type' => PARAM_TEXT],
81
                    'value' => ['type' => PARAM_TEXT]
82
                ],
83
                'multiple' => true,
84
            ],
85
            'classes' => ['type' => PARAM_TEXT],
86
        ];
87
    }
88
 
89
    /**
90
     * Get additional values to inject while exporting
91
     *
92
     * @uses \core_reportbuilder\output\renderer::render_system_report_table()
93
     *
94
     * @param renderer_base $output
95
     * @return array
96
     */
97
    protected function get_other_values(renderer_base $output): array {
98
        /** @var system_report $source */
99
        $source = $this->related['source'];
100
 
101
        /** @var string $parameters */
102
        $parameters = $this->related['parameters'];
103
 
104
        /** @var int $reportid */
105
        $reportid = $this->persistent->get('id');
106
 
107
        // We store the report ID and parameters within the table filterset so that they are available between AJAX requests.
108
        $filterset = new system_report_table_filterset();
109
        $filterset->add_filter(new integer_filter('reportid', null, [$reportid]));
110
        $filterset->add_filter(new string_filter('parameters', null, [$parameters]));
111
 
112
        $params = (array) json_decode($parameters, true);
113
        $table = system_report_table::create($reportid, $params);
114
        $table->set_filterset($filterset);
115
 
1441 ariadna 116
        // Export global report action.
117
        $optionalvalues = [];
118
        if ($reportaction = $source->get_report_action()) {
119
            $optionalvalues['button'] = $reportaction->export_for_template($output);
120
        }
121
 
1 efrain 122
        // Generate filters form if report uses the default form, and contains any filters.
123
        $filterspresent = $source->get_filter_form_default() && !empty($source->get_active_filters());
124
        if ($filterspresent && empty($params['download'])) {
125
            $filtersform = new filter(null, null, 'post', '', [], true, [
126
                'reportid' => $reportid,
127
                'parameters' => $parameters,
128
            ]);
129
            $filtersform->set_data_for_dynamic_submission();
130
        }
131
 
132
        // Get the report classes and attributes.
133
        $sourceattributes = $source->get_attributes();
134
        if (isset($sourceattributes['class'])) {
135
            $classes = $sourceattributes['class'];
136
            unset($sourceattributes['class']);
137
        }
138
        $attributes = array_map(static function($key, $value): array {
139
            return ['name' => $key, 'value' => $value];
140
        }, array_keys($sourceattributes), $sourceattributes);
141
 
142
        return [
143
            'table' => $output->render($table),
144
            'parameters' => $parameters,
1441 ariadna 145
            'infocontainer' => $source->get_report_info_container(),
1 efrain 146
            'filterspresent' => $filterspresent,
147
            'filtersapplied' => $source->get_applied_filter_count(),
148
            'filtersform' => $filterspresent ? $filtersform->render() : '',
149
            'attributes' => $attributes,
150
            'classes' => $classes ?? '',
1441 ariadna 151
        ] + $optionalvalues;
1 efrain 152
    }
153
}