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\form;
20
 
21
use context;
22
use core_reportbuilder\local\report\base;
23
use core_reportbuilder\permission;
24
use moodle_url;
25
use core_form\dynamic_form;
26
use core_reportbuilder\manager;
27
use core_reportbuilder\local\models\report;
28
use core_reportbuilder\local\models\filter as filter_model;
29
 
30
/**
31
 * Dynamic filter form
32
 *
33
 * @package     core_reportbuilder
34
 * @copyright   2021 Paul Holden <paulh@moodle.com>
35
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class filter extends dynamic_form {
38
 
39
    /**
40
     * Return instance of the system report using the filter form
41
     *
42
     * @return base
43
     */
44
    private function get_report(): base {
45
        $reportpersistent = new report($this->optional_param('reportid', 0, PARAM_INT));
46
        $parameters = (array) json_decode($this->optional_param('parameters', '', PARAM_RAW));
47
 
48
        return manager::get_report_from_persistent($reportpersistent, $parameters);
49
    }
50
 
51
    /**
52
     * Return the context for the form, it should be that of the system report itself
53
     *
54
     * @return context
55
     */
56
    protected function get_context_for_dynamic_submission(): context {
57
        return ($this->get_report())->get_context();
58
    }
59
 
60
    /**
61
     * Ensure current user is able to use this form
62
     *
63
     * A {@see \core_reportbuilder\report_access_exception} will be thrown if they can't
64
     */
65
    protected function check_access_for_dynamic_submission(): void {
66
        $reportpersistent = $this->get_report()->get_report_persistent();
67
        if ($reportpersistent->get('type') === base::TYPE_CUSTOM_REPORT) {
68
            permission::require_can_view_report($reportpersistent);
69
        } else {
70
            $this->get_report()->require_can_view();
71
        }
72
    }
73
 
74
    /**
75
     * Process the form submission
76
     *
77
     * @return int Number of applied filter instances
78
     */
79
    public function process_dynamic_submission() {
80
        $values = $this->get_data();
81
 
82
        // Remove some unneeded fields, apply filters.
83
        unset($values->reportid, $values->parameters);
84
        $this->get_report()->set_filter_values((array) $values);
85
 
86
        return $this->get_report()->get_applied_filter_count();
87
    }
88
 
89
    /**
90
     * Load in existing data as form defaults
91
     */
92
    public function set_data_for_dynamic_submission(): void {
93
        $defaults = [
94
            'reportid' => $this->optional_param('reportid', 0, PARAM_INT),
95
            'parameters' => $this->optional_param('parameters', 0, PARAM_RAW),
96
        ];
97
 
98
        $this->set_data(array_merge($defaults, $this->get_report()->get_filter_values()));
99
    }
100
 
101
    /**
102
     * URL of the page using this form
103
     *
104
     * @return moodle_url
105
     */
106
    protected function get_page_url_for_dynamic_submission(): moodle_url {
107
        return new moodle_url('/');
108
    }
109
 
110
    /**
111
     * Filter form definition. It should provide necessary field itself, then allow all report filters to add their own elements
112
     */
113
    protected function definition() {
114
        global $OUTPUT;
115
 
116
        $mform = $this->_form;
117
 
118
        $mform->addElement('hidden', 'reportid');
119
        $mform->setType('reportid', PARAM_INT);
120
 
121
        $mform->addElement('hidden', 'parameters');
122
        $mform->setType('parameters', PARAM_RAW);
123
 
124
        // Allow each filter instance to add itself to this form, wrapping each inside custom header/footer template.
125
        $filterinstances = $this->get_report()->get_filter_instances();
126
        foreach ($filterinstances as $filterinstance) {
127
            $header = $filterinstance->get_header();
128
 
129
            // Check if filter has a custom header set.
130
            if ($persistent = $filterinstance->get_filter_persistent()) {
131
                if ('' !== (string) $persistent->get('heading')) {
132
                    $header = $persistent->get_formatted_heading($this->get_report()->get_context());
133
                }
134
            }
135
 
136
            $mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/filters/header', [
137
                'name' => $header,
138
            ]));
139
 
140
            $filterinstance->setup_form($mform);
141
            $mform->addElement('html', $OUTPUT->render_from_template('core_reportbuilder/local/filters/footer', []));
142
        }
143
 
144
        $this->set_display_vertical();
145
 
146
        // We'll add a second submit button to the form that will be used to reset current report filters.
147
        $mform->registerNoSubmitButton('resetfilters');
148
 
149
        $buttons = [];
150
        $buttons[] = $mform->createElement('submit', 'submitbutton', get_string('apply', 'core_reportbuilder'));
151
        $buttons[] = $mform->createElement('submit', 'resetfilters',  get_string('resetall', 'core_reportbuilder'),
152
            null, null, ['customclassoverride' => 'btn-link ml-1']);
153
 
154
        $mform->addGroup($buttons, 'buttonar', get_string('formactions', 'core_form'), '', false)
155
            ->setHiddenLabel(true);
156
        $mform->closeHeaderBefore('buttonar');
157
 
158
        $mform->disable_form_change_checker();
159
    }
160
}