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 tool_admin_presets\reportbuilder\local\systemreports;
20
 
21
use tool_admin_presets\reportbuilder\local\entities\admin_preset;
22
use core_reportbuilder\local\helpers\database;
23
use core_reportbuilder\local\report\action;
24
use core_reportbuilder\system_report;
25
 
26
/**
27
 * Admin presets system report class implementation
28
 *
29
 * @package    tool_admin_presets
30
 * @copyright  2024 David Carrillo <davidmc@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class admin_presets extends system_report {
34
 
35
    /**
36
     * Initialise report, we need to set the main table, load our entities and set columns/filters
37
     */
38
    protected function initialise(): void {
39
        // Our main entity, it contains all of the column definitions that we need.
40
        $apentity = new admin_preset();
41
        $entityalias = $apentity->get_table_alias('adminpresets');
42
 
43
        $this->set_main_table('adminpresets', $entityalias);
44
        $this->add_entity($apentity);
45
 
46
        $apappalias = database::generate_alias();
47
 
48
        // We need to join the adminpresets_app table to check if the preset rollback has ben used.
49
        $this->add_join("LEFT JOIN {adminpresets_app} {$apappalias} ON {$entityalias}.id = {$apappalias}.adminpresetid");
50
 
51
        // Any columns required by actions should be defined here to ensure they're always available.
52
        $this->add_base_fields("{$entityalias}.id, {$entityalias}.name, {$entityalias}.iscore, {$apappalias}.id as appid");
53
 
54
        // Now we can call our helper methods to add the content we want to include in the report.
55
        $this->add_columns();
56
        $this->add_filters();
57
        $this->add_actions();
58
 
59
        // Set if report can be downloaded.
60
        $this->set_downloadable(false);
61
    }
62
 
63
    /**
64
     * Validates access to view this report
65
     *
66
     * @return bool
67
     */
68
    protected function can_view(): bool {
69
        return has_capability('moodle/site:config', \context_system::instance());
70
    }
71
 
72
    /**
73
     * Adds the columns we want to display in the report
74
     *
75
     * They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
76
     * unique identifier. If custom columns are needed just for this report, they can be defined here.
77
     */
78
    public function add_columns(): void {
79
        $columns = [
80
            'admin_preset:name',
81
            'admin_preset:description',
82
        ];
83
        $this->add_columns_from_entities($columns);
84
    }
85
 
86
    /**
87
     * Adds the filters we want to display in the report
88
     *
89
     * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
90
     * unique identifier
91
     */
92
    protected function add_filters(): void {
93
        $filters = [
94
            'admin_preset:name',
95
        ];
96
        $this->add_filters_from_entities($filters);
97
    }
98
 
99
    /**
100
     * Add the system report actions. An extra column will be appended to each row, containing all actions added here
101
     *
102
     * Note the use of ":id" placeholder which will be substituted according to actual values in the row
103
     */
104
    protected function add_actions(): void {
105
 
106
        // Review settings and apply.
107
        $this->add_action((new action(
108
            new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'id' => ':id']),
109
            new \pix_icon('t/play', '', 'core'),
110
            [],
111
            false,
112
            new \lang_string('applyaction', 'tool_admin_presets')
113
        )));
114
 
115
        // Download.
116
        $this->add_action((new action(
117
            new \moodle_url('/admin/tool/admin_presets/index.php',
118
                ['action' => 'export', 'mode' => 'download_xml', 'sesskey' => sesskey(), 'id' => ':id']),
119
            new \pix_icon('t/download', '', 'core'),
120
            [],
121
            false,
122
            new \lang_string('download', 'core')
123
        )));
124
 
125
        // Delete button won't be displayed for the pre-installed core "Starter" and "Full" presets.
126
        $this->add_action((new action(
127
            new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'delete', 'id' => ':id']),
128
            new \pix_icon('i/delete', '', 'core'),
129
            [
130
                'data-action' => 'admin-preset-delete',
131
                'data-preset-name' => ':name',
132
                'data-preset-id' => ':id',
133
                'data-preset-rollback' => ':appid',
134
            ],
135
            false,
136
            new \lang_string('delete', 'core')
137
        ))->add_callback(function(\stdClass $row): bool {
138
            return (int)$row->iscore === \core_adminpresets\manager::NONCORE_PRESET;
139
        }));
140
 
141
        // Look for preset applications.
142
        $this->add_action((new action(
143
            new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'rollback', 'id' => ':id']),
144
            new \pix_icon('i/reload', '', 'core'),
145
            [],
146
            false,
147
            new \lang_string('showhistory', 'tool_admin_presets')
148
        ))->add_callback(function(\stdClass $row): bool {
149
            return (bool) $row->appid;
150
        }));
151
    }
152
}