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