Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core\context;
22
use core_form\dynamic_form;
23
use core_reportbuilder\local\helpers\report;
24
use core_reportbuilder\local\models\report as report_model;
25
use core_reportbuilder\permission;
26
use core\url;
27
 
28
/**
29
 * Dynamic duplicate custom reports form
30
 *
31
 * @package     core_reportbuilder
32
 * @copyright   2024 David Carrillo <davidmc@moodle.com>
33
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class duplicate_report extends dynamic_form {
36
 
37
    /**
38
     * Form definition.
39
     */
40
    public function definition() {
41
        $mform = $this->_form;
42
 
43
        $mform->addElement('hidden', 'id');
44
        $mform->setType('id', PARAM_INT);
45
 
46
        $mform->addElement('text', 'name', get_string('name', 'core'));
47
        $mform->setType('name', PARAM_TEXT);
48
        $mform->addRule('name', get_string('required'), 'required');
49
        $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255);
50
 
51
        $mform->addElement('advcheckbox', 'audiences', get_string('duplicateaudiences', 'core_reportbuilder'));
52
        $mform->addElement('advcheckbox', 'schedules', get_string('duplicateschedules', 'core_reportbuilder'));
53
        $mform->disabledIf('schedules', 'audiences', 'notchecked');
54
    }
55
 
56
    /**
57
     * Returns context where this form is used
58
     *
59
     * @return context
60
     */
61
    protected function get_context_for_dynamic_submission(): context {
62
        $report = report_model::get_record(['id' => $this->optional_param('id', 0, PARAM_INT)], MUST_EXIST);
63
        return $report->get_context();
64
    }
65
 
66
    /**
67
     * Ensure current user is able to use this form
68
     *
69
     * A {@see \core_reportbuilder\exception\report_access_exception} will be thrown if they can't
70
     */
71
    protected function check_access_for_dynamic_submission(): void {
72
        $report = report_model::get_record(['id' => $this->optional_param('id', 0, PARAM_INT)], MUST_EXIST);
73
        permission::require_can_duplicate_report($report);
74
    }
75
 
76
    /**
77
     * Process the form submission, used if form was submitted via AJAX
78
     */
79
    public function process_dynamic_submission(): string {
80
        $data = $this->get_data();
81
        $report = report_model::get_record(['id' => $data->id], MUST_EXIST);
82
 
83
        $newreport = report::duplicate_report(
84
            $report,
85
            $data->name,
86
            !empty($data->audiences),
87
            !empty($data->schedules),
88
        );
89
 
90
        return (new url('/reportbuilder/edit.php', ['id' => $newreport->get('id')]))->out(false);
91
    }
92
 
93
    /**
94
     * Load in existing data as form defaults
95
     */
96
    public function set_data_for_dynamic_submission(): void {
97
        $this->set_data($this->_ajaxformdata);
98
    }
99
 
100
    /**
101
     * Page url
102
     *
103
     * @return url
104
     */
105
    protected function get_page_url_for_dynamic_submission(): url {
106
        return new url('/reportbuilder/edit.php', ['id' => $this->optional_param('id', 0, PARAM_INT)]);
107
    }
108
}