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\form;
20
 
21
use context;
22
use context_system;
23
use core_reportbuilder\permission;
24
use moodle_url;
25
use core_form\dynamic_form;
26
use core_reportbuilder\datasource;
27
use core_reportbuilder\manager;
1441 ariadna 28
use core_reportbuilder\customfield\report_handler;
1 efrain 29
use core_reportbuilder\local\helpers\report as reporthelper;
30
use core_tag_tag;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
require_once("$CFG->libdir/formslib.php");
36
 
37
/**
38
 * Report details form
39
 *
40
 * @package     core_reportbuilder
41
 * @copyright   2021 David Matamoros <davidmc@moodle.com>
42
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class report extends dynamic_form {
45
 
46
    /**
47
     * Return instance of the custom report we are editing, or null when creating a new report
48
     *
49
     * @return datasource|null
50
     */
51
    protected function get_custom_report(): ?datasource {
52
        if ($reportid = $this->optional_param('id', 0, PARAM_INT)) {
53
            /** @var datasource $customreport */
54
            $customreport = manager::get_report_from_id($reportid);
55
 
56
            return $customreport;
57
        }
58
 
59
        return null;
60
    }
61
 
62
    /**
63
     * Return the context for the form, it should be that of the custom report itself, or system when creating a new report
64
     *
65
     * @return context
66
     */
67
    public function get_context_for_dynamic_submission(): context {
68
        if ($report = $this->get_custom_report()) {
69
            return $report->get_context();
70
        } else {
71
            return context_system::instance();
72
        }
73
    }
74
 
75
    /**
76
     * Ensure current user is able to use this form
77
     *
1441 ariadna 78
     * A {@see \core_reportbuilder\exception\report_access_exception} will be thrown if they can't
1 efrain 79
     */
80
    protected function check_access_for_dynamic_submission(): void {
81
        $report = $this->get_custom_report();
82
 
83
        if ($report) {
84
            permission::require_can_edit_report($report->get_report_persistent());
85
        } else {
86
            permission::require_can_create_report();
87
        }
88
    }
89
 
90
    /**
91
     * Form definition
92
     */
93
    public function definition() {
94
        $mform = $this->_form;
95
 
1441 ariadna 96
        $mform->addElement('header', 'general', get_string('general', 'form'));
97
 
1 efrain 98
        $mform->addElement('hidden', 'id');
99
        $mform->setType('id', PARAM_INT);
100
 
101
        $mform->addElement('text', 'name', get_string('name'));
102
        $mform->setType('name', PARAM_TEXT);
103
        $mform->addRule('name', null, 'required', null, 'client');
104
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255);
105
 
106
        // Allow user to select report source if creating a new report.
107
        if (!$this->get_custom_report()) {
108
            $default = ['' => ['' => get_string('selectareportsource', 'core_reportbuilder')]];
109
            $mform->addElement('selectgroups', 'source', get_string('reportsource', 'core_reportbuilder'),
110
                array_merge($default, manager::get_report_datasources()));
111
            $mform->addRule('source', null, 'required', null, 'client');
112
            $mform->addHelpButton('source', 'reportsource', 'core_reportbuilder');
113
 
114
            $mform->addElement('advcheckbox', 'includedefaultsetup', get_string('includedefaultsetup', 'core_reportbuilder'));
115
            $mform->setDefault('includedefaultsetup', 1);
116
            $mform->addHelpButton('includedefaultsetup', 'includedefaultsetup', 'core_reportbuilder');
117
        }
118
 
119
        $mform->addElement('advcheckbox', 'uniquerows', get_string('uniquerows', 'core_reportbuilder'));
120
        $mform->addHelpButton('uniquerows', 'uniquerows', 'core_reportbuilder');
121
 
122
        $mform->addElement('tags', 'tags', get_string('tags'), [
123
            'component' => 'core_reportbuilder', 'itemtype' => 'reportbuilder_report',
124
        ]);
1441 ariadna 125
 
126
        // Add custom fields to the form.
127
        $reportid = empty($this->_customdata['id']) ? 0 : $this->_customdata['id'];
128
        report_handler::create()->instance_form_definition($mform, $reportid);
1 efrain 129
    }
130
 
131
    /**
132
     * Process the form submission
133
     *
134
     * @return string The URL to advance to upon completion
135
     */
136
    public function process_dynamic_submission() {
137
        $data = $this->get_data();
138
 
139
        if ($data->id) {
140
            $reportpersistent = reporthelper::update_report($data);
141
        } else {
142
            $reportpersistent = reporthelper::create_report($data, (bool)$data->includedefaultsetup);
143
        }
144
 
145
        return (new moodle_url('/reportbuilder/edit.php', ['id' => $reportpersistent->get('id')]))->out(false);
146
    }
147
 
148
    /**
149
     * Load in existing data as form defaults
150
     */
151
    public function set_data_for_dynamic_submission(): void {
152
        if ($persistent = $this->get_custom_report()?->get_report_persistent()) {
153
            $tags = core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $persistent->get('id'));
1441 ariadna 154
            $data = (object) array_merge((array) $persistent->to_record(), ['tags' => $tags]);
155
            report_handler::create()->instance_form_before_set_data($data);
156
            $this->set_data($data);
1 efrain 157
        }
158
    }
159
 
160
    /**
161
     * URL of the page using this form
162
     *
163
     * @return moodle_url
164
     */
165
    public function get_page_url_for_dynamic_submission(): moodle_url {
166
        return new moodle_url('/reportbuilder/index.php');
167
    }
168
 
169
    /**
170
     * Perform some extra moodle validation
171
     *
172
     * @param array $data
173
     * @param array $files
174
     * @return array
175
     */
176
    public function validation($data, $files): array {
177
        $errors = [];
178
 
179
        if (trim($data['name']) === '') {
180
            $errors['name'] = get_string('required');
181
        }
182
 
183
        return $errors;
184
    }
185
}