Proyectos de Subversion Moodle

Rev

| 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],
70
            'filterspresent' => ['type' => PARAM_BOOL],
71
            'filtersapplied' => ['type' => PARAM_INT],
72
            'filtersform' => ['type' => PARAM_RAW],
73
            'attributes' => [
74
                'type' => [
75
                    'name' => ['type' => PARAM_TEXT],
76
                    'value' => ['type' => PARAM_TEXT]
77
                ],
78
                'multiple' => true,
79
            ],
80
            'classes' => ['type' => PARAM_TEXT],
81
        ];
82
    }
83
 
84
    /**
85
     * Get additional values to inject while exporting
86
     *
87
     * @uses \core_reportbuilder\output\renderer::render_system_report_table()
88
     *
89
     * @param renderer_base $output
90
     * @return array
91
     */
92
    protected function get_other_values(renderer_base $output): array {
93
        /** @var system_report $source */
94
        $source = $this->related['source'];
95
 
96
        /** @var string $parameters */
97
        $parameters = $this->related['parameters'];
98
 
99
        /** @var int $reportid */
100
        $reportid = $this->persistent->get('id');
101
 
102
        // We store the report ID and parameters within the table filterset so that they are available between AJAX requests.
103
        $filterset = new system_report_table_filterset();
104
        $filterset->add_filter(new integer_filter('reportid', null, [$reportid]));
105
        $filterset->add_filter(new string_filter('parameters', null, [$parameters]));
106
 
107
        $params = (array) json_decode($parameters, true);
108
        $table = system_report_table::create($reportid, $params);
109
        $table->set_filterset($filterset);
110
 
111
        // Generate filters form if report uses the default form, and contains any filters.
112
        $filterspresent = $source->get_filter_form_default() && !empty($source->get_active_filters());
113
        if ($filterspresent && empty($params['download'])) {
114
            $filtersform = new filter(null, null, 'post', '', [], true, [
115
                'reportid' => $reportid,
116
                'parameters' => $parameters,
117
            ]);
118
            $filtersform->set_data_for_dynamic_submission();
119
        }
120
 
121
        // Get the report classes and attributes.
122
        $sourceattributes = $source->get_attributes();
123
        if (isset($sourceattributes['class'])) {
124
            $classes = $sourceattributes['class'];
125
            unset($sourceattributes['class']);
126
        }
127
        $attributes = array_map(static function($key, $value): array {
128
            return ['name' => $key, 'value' => $value];
129
        }, array_keys($sourceattributes), $sourceattributes);
130
 
131
        return [
132
            'table' => $output->render($table),
133
            'parameters' => $parameters,
134
            'filterspresent' => $filterspresent,
135
            'filtersapplied' => $source->get_applied_filter_count(),
136
            'filtersform' => $filterspresent ? $filtersform->render() : '',
137
            'attributes' => $attributes,
138
            'classes' => $classes ?? '',
139
        ];
140
    }
141
}