mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
base.php
206 lines (173 loc) · 6.21 KB
/
base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_admin_presets\local\action;
use context_system;
use moodle_url;
use core_adminpresets\manager;
use tool_admin_presets\output\presets_list;
use tool_admin_presets\output\export_import;
/**
* Admin tool presets main controller class.
*
* @package tool_admin_presets
* @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
* @author Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class base {
/** @var array Array map for the events. **/
protected static $eventsactionsmap = [
'base' => 'presets_listed',
'delete' => 'preset_deleted',
'export' => 'preset_exported',
'import' => 'preset_imported',
'preview' => 'preset_previewed',
'load' => 'preset_loaded',
'rollback' => 'preset_reverted',
'download_xml' => 'preset_downloaded'
];
/** @var string The main action (delete, export, import, load...). **/
protected $action;
/** @var string The mode (show, execute...). **/
protected $mode;
/** @var int Admin preset identifier. **/
protected $id;
/** @var int The output content to display in the page. **/
protected $outputs;
/** @var int The moodle form to display in the page. **/
protected $moodleform;
/** @var manager The manager helper class instance. **/
protected $manager;
/**
* Loads common class attributes.
*/
public function __construct() {
$this->manager = new manager();
$this->action = optional_param('action', 'base', PARAM_ALPHA);
$this->mode = optional_param('mode', 'show', PARAM_ALPHAEXT);
$this->id = optional_param('id', false, PARAM_INT);
}
/**
* Method to list the presets available on the system
*
* It allows users to access the different preset
* actions (preview, load, download, delete and rollback)
*/
public function show(): void {
global $DB, $OUTPUT;
$options = new export_import();
$this->outputs = $OUTPUT->render($options);
$presets = $DB->get_records('adminpresets');
$list = new presets_list($presets, true);
$this->outputs .= $OUTPUT->render($list);
}
/**
* Main display method
*
* Prints the block header and the common block outputs, the
* selected action outputs, his form and the footer
*
* $outputs value depends on $mode and $action selected
*/
public function display(): void {
global $OUTPUT;
$this->display_header();
// Other outputs.
if (!empty($this->outputs)) {
echo $this->outputs;
}
// Form.
if ($this->moodleform) {
$this->moodleform->display();
}
// Footer.
echo $OUTPUT->footer();
}
/**
* Displays the header
*/
protected function display_header(): void {
global $PAGE, $OUTPUT, $SITE;
// Strings.
$titlestr = get_string('pluginname', 'tool_admin_presets');
// Header.
$PAGE->set_title($titlestr);
$PAGE->set_heading($SITE->fullname);
$PAGE->navbar->add(get_string('pluginname', 'tool_admin_presets'),
new moodle_url('/admin/tool/admin_presets/index.php')
);
$title = $this->get_title();
$text = $this->get_explanatory_description();
$PAGE->navbar->add($title);
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
if ($text) {
echo $OUTPUT->box($text);
}
}
/**
* Get page title for this action.
*
* @return string The page title to display into the page.
*/
protected function get_title(): string {
if ($this->action == 'base') {
return get_string('pluginname', 'tool_admin_presets');
}
return get_string($this->action . $this->mode, 'tool_admin_presets');
}
/**
* Get explanatory description to be displayed below the heading. It's optional and might change depending on the
* action and the mode.
*
* @return string|null The explanatory description for the current action and mode.
*/
protected function get_explanatory_description(): ?string {
$text = null;
if ($this->action == 'base') {
$text = get_string('basedescription', 'tool_admin_presets');
}
return $text;
}
/**
* Trigger an event based on the current action.
*
* @return void
*/
public function log(): void {
// The only read action we store is list presets and preview.
$islist = ($this->action == 'base' && $this->mode == 'show');
$ispreview = ($this->action == 'load' && $this->mode == 'show');
if ($this->mode != 'show' || $islist || $ispreview) {
$action = $this->action;
if ($ispreview) {
$action = 'preview';
}
if ($this->mode != 'execute' && $this->mode != 'show') {
$action = $this->mode;
}
if (array_key_exists($action, self::$eventsactionsmap)) {
$eventnamespace = '\\tool_admin_presets\\event\\' . self::$eventsactionsmap[$action];
$eventdata = [
'context' => context_system::instance(),
'objectid' => $this->id
];
$event = $eventnamespace::create($eventdata);
$event->trigger();
}
}
}
}