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
namespace tool_admin_presets\local\action;
18
 
19
use context_system;
20
use core_adminpresets\manager;
21
use tool_admin_presets\output\export_import;
22
 
23
/**
24
 * Admin tool presets main controller class.
25
 *
26
 * @package          tool_admin_presets
27
 * @copyright        2021 Pimenko <support@pimenko.com><pimenko.com>
28
 * @author           Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
29
 * @license          http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class base {
32
 
33
    /** @var array Array map for the events. **/
34
    protected static $eventsactionsmap = [
35
        'base' => 'presets_listed',
36
        'delete' => 'preset_deleted',
37
        'export' => 'preset_exported',
38
        'import' => 'preset_imported',
39
        'preview' => 'preset_previewed',
40
        'load' => 'preset_loaded',
41
        'rollback' => 'preset_reverted',
1441 ariadna 42
        'download_xml' => 'preset_downloaded',
1 efrain 43
    ];
44
 
45
    /** @var string The main action (delete, export, import, load...). **/
46
    protected $action;
47
 
48
    /** @var string The mode (show, execute...). **/
49
    protected $mode;
50
 
51
    /** @var int Admin preset identifier. **/
52
    protected $id;
53
 
54
    /** @var int The output content to display in the page. **/
55
    protected $outputs;
56
 
57
    /** @var \moodleform The moodle form to display in the page. **/
58
    protected $moodleform;
59
 
60
    /** @var manager The manager helper class instance. **/
61
    protected $manager;
62
 
63
    /**
64
     * Loads common class attributes.
65
     */
66
    public function __construct() {
67
        $this->manager = new manager();
68
        $this->action = optional_param('action', 'base', PARAM_ALPHA);
69
        $this->mode = optional_param('mode', 'show', PARAM_ALPHAEXT);
70
        $this->id = optional_param('id', false, PARAM_INT);
71
    }
72
 
73
    /**
74
     * Method to list the presets available on the system
75
     *
76
     * It allows users to access the different preset
77
     * actions (preview, load, download, delete and rollback)
78
     */
79
    public function show(): void {
1441 ariadna 80
        global $DB, $OUTPUT, $PAGE;
1 efrain 81
 
1441 ariadna 82
        $this->outputs = $OUTPUT->container_start('d-flex flex-wrap justify-content-end');
1 efrain 83
        $options = new export_import();
1441 ariadna 84
        $this->outputs .= $OUTPUT->render($options);
85
        $this->outputs .= $OUTPUT->container_end();
1 efrain 86
 
1441 ariadna 87
        $PAGE->requires->js_call_amd('tool_admin_presets/admin_presets_list', 'init');
88
 
89
        $report = \core_reportbuilder\system_report_factory::create(
90
            \tool_admin_presets\reportbuilder\local\systemreports\admin_presets::class,
91
            $PAGE->context, '', '', 0, []);
92
        $this->outputs .= $report->output();
1 efrain 93
    }
94
 
95
    /**
96
     * Main display method
97
     *
98
     * Prints the block header and the common block outputs, the
99
     * selected action outputs, his form and the footer
100
     *
101
     * $outputs value depends on $mode and $action selected
102
     */
103
    public function display(): void {
104
        global $OUTPUT;
105
 
106
        $this->display_header();
107
 
108
        // Other outputs.
109
        if (!empty($this->outputs)) {
110
            echo $this->outputs;
111
        }
112
 
113
        // Form.
114
        if ($this->moodleform) {
115
            $this->moodleform->display();
116
        }
117
 
118
        // Footer.
119
        echo $OUTPUT->footer();
120
    }
121
 
122
    /**
123
     * Displays the header
124
     */
125
    protected function display_header(): void {
126
        global $PAGE, $OUTPUT, $SITE;
127
 
128
        // Strings.
129
        $titlestr = get_string('pluginname', 'tool_admin_presets');
130
 
131
        // Header.
132
        $PAGE->set_title($titlestr);
133
        $PAGE->set_heading($SITE->fullname);
134
 
135
        $title = $this->get_title();
136
        $text = $this->get_explanatory_description();
137
 
138
        // Only add it to the navbar if it's different to the plugin name (to avoid duplicates in the navbar).
139
        if ($title != get_string('pluginname', 'tool_admin_presets')) {
140
            $PAGE->navbar->add($title);
141
        }
142
 
143
        if ($node = $PAGE->settingsnav->find('tool_admin_presets', \navigation_node::TYPE_SETTING)) {
144
            $node->make_active();
145
        }
146
 
147
        echo $OUTPUT->header();
148
        echo $OUTPUT->heading($title);
149
        if ($text) {
150
            echo $OUTPUT->box($text);
151
        }
152
    }
153
 
154
    /**
155
     * Get page title for this action.
156
     *
157
     * @return string The page title to display into the page.
158
     */
159
    protected function get_title(): string {
160
        if ($this->action == 'base') {
161
            return get_string('pluginname', 'tool_admin_presets');
162
        }
163
 
164
        return get_string($this->action . $this->mode, 'tool_admin_presets');
165
    }
166
 
167
    /**
168
     * Get explanatory description to be displayed below the heading. It's optional and might change depending on the
169
     * action and the mode.
170
     *
171
     * @return string|null The explanatory description for the current action and mode.
172
     */
173
    protected function get_explanatory_description(): ?string {
174
        $text = null;
175
        if ($this->action == 'base') {
176
            $text = get_string('basedescription', 'tool_admin_presets');
177
        }
178
 
179
        return $text;
180
    }
181
 
182
    /**
183
     * Trigger an event based on the current action.
184
     *
185
     * @return void
186
     */
187
    public function log(): void {
188
        // The only read action we store is list presets and preview.
189
        $islist = ($this->action == 'base' && $this->mode == 'show');
190
        $ispreview = ($this->action == 'load' && $this->mode == 'show');
191
        if ($this->mode != 'show' || $islist || $ispreview) {
192
            $action = $this->action;
193
            if ($ispreview) {
194
                $action = 'preview';
195
            }
196
 
197
            if ($this->mode != 'execute' && $this->mode != 'show') {
198
                $action = $this->mode;
199
            }
200
 
201
            if (array_key_exists($action, self::$eventsactionsmap)) {
202
                $eventnamespace = '\\tool_admin_presets\\event\\' . self::$eventsactionsmap[$action];
203
                $eventdata = [
204
                    'context' => context_system::instance(),
205
                    'objectid' => $this->id
206
                ];
207
                $event = $eventnamespace::create($eventdata);
208
                $event->trigger();
209
            }
210
        }
211
    }
212
}