-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadminlib.php
345 lines (306 loc) · 11.9 KB
/
adminlib.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?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/>.
/**
* This file contains the classes for the admin settings of the capquiz module.
*
* @package mod_capquiz
* @author André Storhaug <[email protected]>
* @copyright 2019 Norwegian University of Science and Technology (NTNU)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/adminlib.php');
/**
* Admin external page that displays a list of the installed capquiz plugins.
*
* @package mod_capquiz
* @author André Storhaug <[email protected]>
* @copyright 2019 Norwegian University of Science and Technology (NTNU)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class capquiz_admin_page_manage_capquiz_plugins extends admin_externalpage {
/** @var string the name of plugin subtype */
private string $subtype;
/**
* Constructor.
*
* @param string $subtype
*/
public function __construct(string $subtype) {
$this->subtype = $subtype;
$url = new moodle_url('/mod/capquiz/adminmanageplugins.php', ['subtype' => $subtype]);
parent::__construct('manage' . $subtype . 'plugins', get_string('manage' . $subtype . 'plugins', 'capquiz'), $url);
}
/**
* Search plugins for the specified string
*
* @param string $query The string to search for
*/
public function search($query): array {
$result = parent::search($query);
if ($result) {
return $result;
}
$found = false;
foreach (core_component::get_plugin_list($this->subtype) as $name => $notused) {
$pluginname = get_string('pluginname', $this->subtype . '_' . $name);
if (str_contains(core_text::strtolower($pluginname), $query)) {
$found = true;
break;
}
}
if ($found) {
$result = new stdClass();
$result->page = $this;
$result->settings = [];
return [$this->name => $result];
} else {
return [];
}
}
}
/**
* Class that handles the display and configuration of the list of capquiz plugins.
*
* @package mod_capquiz
* @author André Storhaug <[email protected]>
* @copyright 2019 Norwegian University of Science and Technology (NTNU)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class capquiz_plugin_manager {
/** @var moodle_url the url of the manage capquiz plugin page */
private moodle_url $pageurl;
/** @var string report */
private string $subtype;
/** @var string any error from the current action */
private string $error = '';
/**
* Constructor.
*
* @param string $subtype
*/
public function __construct(string $subtype) {
$this->pageurl = new moodle_url('/mod/capquiz/adminmanageplugins.php', ['subtype' => $subtype]);
$this->subtype = $subtype;
}
/**
* This is the entry point for this controller class.
*
* @param string $action Action to perform
* @param ?string $plugin Optional name of a plugin type to perform the action on
*/
public function execute(string $action = 'view', ?string $plugin = null): void {
$this->check_permissions();
if ($plugin !== null) {
if ($action === 'hide') {
$action = $this->hide_plugin($plugin);
} else if ($action === 'show') {
$action = $this->show_plugin($plugin);
} else if ($action === 'moveup') {
$action = $this->move_plugin($plugin, 'up');
} else if ($action === 'movedown') {
$action = $this->move_plugin($plugin, 'down');
}
}
if ($action === 'view') {
$this->view_plugins_table();
}
}
/**
* Check this user has permission to edit the list of installed plugins
*/
private function check_permissions(): void {
require_login();
$systemcontext = context_system::instance();
require_capability('moodle/site:config', $systemcontext);
}
/**
* Hide this plugin.
*
* @param string $plugin - The plugin to hide
* @return string The next page to display
*/
public function hide_plugin(string $plugin): string {
set_config('disabled', 1, $this->subtype . '_' . $plugin);
core_plugin_manager::reset_caches();
return 'view';
}
/**
* Show this plugin.
*
* @param string $plugin - The plugin to show
* @return string The next page to display
*/
public function show_plugin(string $plugin): string {
set_config('disabled', 0, $this->subtype . '_' . $plugin);
core_plugin_manager::reset_caches();
return 'view';
}
/**
* Change the order of this plugin.
*
* @param string $plugintomove - The plugin to move
* @param string $dir - up or down
* @return string The next page to display
*/
public function move_plugin(string $plugintomove, string $dir): string {
// Get a list of the current plugins.
$plugins = $this->get_sorted_plugins_list();
$currentindex = 0;
// Throw away the keys.
$plugins = array_values($plugins);
// Find this plugin in the list.
foreach ($plugins as $key => $plugin) {
if ($plugin == $plugintomove) {
$currentindex = $key;
break;
}
}
// Make the switch.
if ($dir == 'up') {
if ($currentindex > 0) {
$tempplugin = $plugins[$currentindex - 1];
$plugins[$currentindex - 1] = $plugins[$currentindex];
$plugins[$currentindex] = $tempplugin;
}
} else if ($dir == 'down') {
if ($currentindex < (count($plugins) - 1)) {
$tempplugin = $plugins[$currentindex + 1];
$plugins[$currentindex + 1] = $plugins[$currentindex];
$plugins[$currentindex] = $tempplugin;
}
}
// Save the new normal order.
foreach ($plugins as $key => $plugin) {
set_config('sortorder', $key, $this->subtype . '_' . $plugin);
}
return 'view';
}
/**
* Return a list of plugins sorted by the order defined in the admin interface
*
* @return array The list of plugins
*/
public function get_sorted_plugins_list(): array {
$names = core_component::get_plugin_list($this->subtype);
$result = [];
foreach ($names as $name => $path) {
$idx = get_config($this->subtype . '_' . $name, 'sortorder');
if (!$idx) {
$idx = 0;
}
while (array_key_exists($idx, $result)) {
$idx += 1;
}
$result[$idx] = $name;
}
ksort($result);
return $result;
}
/**
* Write the HTML for the capquiz plugins table.
*/
private function view_plugins_table(): void {
global $OUTPUT, $CFG;
require_once($CFG->libdir . '/tablelib.php');
// Set up the table.
$this->view_header();
$table = new flexible_table($this->subtype . 'pluginsadminttable');
$table->define_baseurl($this->pageurl);
$table->define_columns(['pluginname', 'version', 'hideshow', 'order', 'settings', 'uninstall']);
$table->define_headers([get_string($this->subtype . 'type', 'capquiz'),
get_string('version'), get_string('hideshow', 'capquiz'),
get_string('order'), get_string('settings'), get_string('uninstallplugin', 'core_admin')]);
$table->set_attribute('id', $this->subtype . 'plugins');
$table->set_attribute('class', 'admintable generaltable');
$table->setup();
$plugins = $this->get_sorted_plugins_list();
$shortsubtype = substr($this->subtype, strlen('capquiz'));
foreach ($plugins as $idx => $plugin) {
$row = [];
$class = '';
$row[] = get_string('pluginname', $this->subtype . '_' . $plugin);
$row[] = get_config($this->subtype . '_' . $plugin, 'version');
$visible = !get_config($this->subtype . '_' . $plugin, 'disabled');
if ($visible) {
$row[] = $this->format_icon_link('hide', $plugin, 't/hide', get_string('disable'));
} else {
$row[] = $this->format_icon_link('show', $plugin, 't/show', get_string('enable'));
$class = 'dimmed_text';
}
$movelinks = '';
if (!$idx == 0) {
$movelinks .= $this->format_icon_link('moveup', $plugin, 't/up', get_string('up'));
} else {
$movelinks .= $OUTPUT->spacer(['width' => 16]);
}
if ($idx != count($plugins) - 1) {
$movelinks .= $this->format_icon_link('movedown', $plugin, 't/down', get_string('down'));
}
$row[] = $movelinks;
$exists = file_exists($CFG->dirroot . '/mod/capquiz/' . $shortsubtype . '/' . $plugin . '/settings.php');
if ($row[1] !== '' && $exists) {
$url = new moodle_url('/admin/settings.php', ['section' => $this->subtype . '_' . $plugin]);
$row[] = html_writer::link($url, get_string('settings'));
} else {
$row[] = ' ';
}
$row[] = $this->format_icon_link('delete', $plugin, 't/delete', get_string('uninstallplugin', 'core_admin'));
$table->add_data($row, $class);
}
$table->finish_output();
$this->view_footer();
}
/**
* Write the page header
*/
private function view_header(): void {
global $OUTPUT;
admin_externalpage_setup('manage' . $this->subtype . 'plugins');
// Print the page heading.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('manage' . $this->subtype . 'plugins', 'capquiz'));
}
/**
* Util function for writing an action icon link
*
* @param string $action URL parameter to include in the link
* @param string $plugin URL parameter to include in the link
* @param string $icon The key to the icon to use (e.g. 't/up')
* @param string $alt The string description of the link used as the title and alt text
* @return string The icon/link
*/
private function format_icon_link(string $action, string $plugin, string $icon, string $alt): string {
global $OUTPUT;
if ($action === 'delete') {
$url = core_plugin_manager::instance()->get_uninstall_url($this->subtype . '_' . $plugin, 'manage');
if (!$url) {
return ' ';
}
return html_writer::link($url, get_string('uninstallplugin', 'core_admin'));
}
$url = new moodle_url($this->pageurl, ['action' => $action, 'plugin' => $plugin, 'sesskey' => sesskey()]);
$icon = new pix_icon($icon, $alt, 'moodle', ['title' => $alt]);
return $OUTPUT->action_icon($url, $icon, null, ['title' => $alt]) . ' ';
}
/**
* Write the page footer
*/
private function view_footer(): void {
global $OUTPUT;
echo $OUTPUT->footer();
}
}