| 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 core_admin\table;
|
|
|
18 |
|
|
|
19 |
use context_system;
|
|
|
20 |
use core_plugin_manager;
|
|
|
21 |
use core_table\dynamic as dynamic_table;
|
|
|
22 |
use flexible_table;
|
|
|
23 |
use html_writer;
|
|
|
24 |
use moodle_url;
|
|
|
25 |
use stdClass;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
require_once("{$CFG->libdir}/tablelib.php");
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Plugin Management table.
|
|
|
32 |
*
|
|
|
33 |
* @package core_admin
|
|
|
34 |
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
abstract class plugin_management_table extends flexible_table implements dynamic_table {
|
|
|
38 |
|
|
|
39 |
/** @var \core\plugininfo\base[] The plugin list */
|
|
|
40 |
protected array $plugins = [];
|
|
|
41 |
|
|
|
42 |
/** @var int The number of enabled plugins of this type */
|
|
|
43 |
protected int $enabledplugincount = 0;
|
|
|
44 |
|
|
|
45 |
/** @var core_plugin_manager */
|
|
|
46 |
protected core_plugin_manager $pluginmanager;
|
|
|
47 |
|
|
|
48 |
/** @var string The plugininfo class for this plugintype */
|
|
|
49 |
protected string $plugininfoclass;
|
|
|
50 |
|
|
|
51 |
public function __construct() {
|
|
|
52 |
global $CFG;
|
|
|
53 |
|
|
|
54 |
parent::__construct($this->get_table_id());
|
|
|
55 |
require_once($CFG->libdir . '/adminlib.php');
|
|
|
56 |
|
|
|
57 |
// Fetch the plugininfo class.
|
|
|
58 |
$this->pluginmanager = core_plugin_manager::instance();
|
|
|
59 |
$this->plugininfoclass = $this->pluginmanager::resolve_plugininfo_class($this->get_plugintype());
|
|
|
60 |
|
|
|
61 |
$this->guess_base_url();
|
|
|
62 |
|
|
|
63 |
$this->plugins = $this->get_sorted_plugins();
|
|
|
64 |
$this->enabledplugincount = count(array_filter($this->plugins, function ($plugin) {
|
|
|
65 |
return $plugin->is_enabled();
|
|
|
66 |
}));
|
|
|
67 |
|
|
|
68 |
$this->setup_column_configuration();
|
|
|
69 |
$this->set_filterset(new plugin_management_table_filterset());
|
|
|
70 |
$this->setup();
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Get the list of sorted plugins.
|
|
|
75 |
*
|
|
|
76 |
* @return \core\plugininfo\base[]
|
|
|
77 |
*/
|
|
|
78 |
protected function get_sorted_plugins(): array {
|
|
|
79 |
if ($this->plugininfoclass::plugintype_supports_ordering()) {
|
|
|
80 |
return $this->plugininfoclass::get_sorted_plugins();
|
|
|
81 |
} else {
|
|
|
82 |
$plugins = $this->pluginmanager->get_plugins_of_type($this->get_plugintype());
|
|
|
83 |
return self::sort_plugins($plugins);
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Sort the plugins list.
|
|
|
89 |
*
|
|
|
90 |
* Note: This only applies to plugins which do not support ordering.
|
|
|
91 |
*
|
|
|
92 |
* @param \core\plugininfo\base[] $plugins
|
|
|
93 |
* @return \core\plugininfo\base[]
|
|
|
94 |
*/
|
|
|
95 |
protected function sort_plugins(array $plugins): array {
|
|
|
96 |
// The asort functions work by reference.
|
|
|
97 |
\core_collator::asort_objects_by_property($plugins, 'displayname');
|
|
|
98 |
|
|
|
99 |
return $plugins;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Set up the column configuration for this table.
|
|
|
104 |
*/
|
|
|
105 |
protected function setup_column_configuration(): void {
|
|
|
106 |
$columnlist = $this->get_column_list();
|
|
|
107 |
$this->define_columns(array_keys($columnlist));
|
|
|
108 |
$this->define_headers(array_values($columnlist));
|
|
|
109 |
|
|
|
110 |
$columnswithhelp = $this->get_columns_with_help();
|
|
|
111 |
$columnhelp = array_map(function (string $column) use ($columnswithhelp): ?\renderable {
|
|
|
112 |
if (array_key_exists($column, $columnswithhelp)) {
|
|
|
113 |
return $columnswithhelp[$column];
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return null;
|
|
|
117 |
}, array_keys($columnlist));
|
|
|
118 |
$this->define_help_for_headers($columnhelp);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Set the standard order of the plugins.
|
|
|
123 |
*
|
|
|
124 |
* @param array $plugins
|
|
|
125 |
* @return array
|
|
|
126 |
*/
|
|
|
127 |
protected function order_plugins(array $plugins): array {
|
|
|
128 |
uasort($plugins, function ($a, $b) {
|
|
|
129 |
if ($a->is_enabled() && !$b->is_enabled()) {
|
|
|
130 |
return -1;
|
|
|
131 |
} else if (!$a->is_enabled() && $b->is_enabled()) {
|
|
|
132 |
return 1;
|
|
|
133 |
}
|
|
|
134 |
return strnatcasecmp($a->name, $b->name);
|
|
|
135 |
});
|
|
|
136 |
|
|
|
137 |
return $plugins;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Get the plugintype for this table.
|
|
|
142 |
*
|
|
|
143 |
* @return string
|
|
|
144 |
*/
|
|
|
145 |
abstract protected function get_plugintype(): string;
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Get the action URL for this table.
|
|
|
149 |
*
|
|
|
150 |
* The action URL is used to perform all actions when JS is not available.
|
|
|
151 |
*
|
|
|
152 |
* @param array $params
|
|
|
153 |
* @return moodle_url
|
|
|
154 |
*/
|
|
|
155 |
abstract protected function get_action_url(array $params = []): moodle_url;
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Provide a default implementation for guessing the base URL from the action URL.
|
|
|
159 |
*/
|
|
|
160 |
public function guess_base_url(): void {
|
|
|
161 |
$this->define_baseurl($this->get_action_url());
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Get the web service method used to toggle state.
|
|
|
166 |
*
|
|
|
167 |
* @return null|string
|
|
|
168 |
*/
|
|
|
169 |
protected function get_toggle_service(): ?string {
|
|
|
170 |
return 'core_admin_set_plugin_state';
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Get the web service method used to order plugins.
|
|
|
175 |
*
|
|
|
176 |
* @return null|string
|
|
|
177 |
*/
|
|
|
178 |
protected function get_sortorder_service(): ?string {
|
|
|
179 |
return 'core_admin_set_plugin_order';
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Get the ID of the table.
|
|
|
184 |
*
|
|
|
185 |
* @return string
|
|
|
186 |
*/
|
|
|
187 |
protected function get_table_id(): string {
|
|
|
188 |
return 'plugin_management_table-' . $this->get_plugintype();
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Get a list of the column titles
|
|
|
193 |
* @return string[]
|
|
|
194 |
*/
|
|
|
195 |
protected function get_column_list(): array {
|
|
|
196 |
$columns = [
|
|
|
197 |
'name' => get_string('name', 'core'),
|
|
|
198 |
'version' => get_string('version', 'core'),
|
|
|
199 |
];
|
|
|
200 |
|
|
|
201 |
if ($this->supports_disabling()) {
|
|
|
202 |
$columns['enabled'] = get_string('pluginenabled', 'core_plugin');
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
if ($this->supports_ordering()) {
|
|
|
206 |
$columns['order'] = get_string('order', 'core');
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
$columns['settings'] = get_string('settings', 'core');
|
|
|
210 |
$columns['uninstall'] = get_string('uninstallplugin', 'core_admin');
|
|
|
211 |
|
|
|
212 |
return $columns;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
protected function get_columns_with_help(): array {
|
|
|
216 |
return [];
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Get the context for this table.
|
|
|
221 |
*
|
|
|
222 |
* @return context_system
|
|
|
223 |
*/
|
|
|
224 |
public function get_context(): context_system {
|
|
|
225 |
return context_system::instance();
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* Get the table content.
|
|
|
230 |
*/
|
|
|
231 |
public function get_content(): string {
|
|
|
232 |
ob_start();
|
|
|
233 |
$this->out();
|
|
|
234 |
$content = ob_get_contents();
|
|
|
235 |
ob_end_clean();
|
|
|
236 |
return $content;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Print the table.
|
|
|
241 |
*/
|
|
|
242 |
public function out(): void {
|
|
|
243 |
$plugintype = $this->get_plugintype();
|
|
|
244 |
foreach ($this->plugins as $plugininfo) {
|
|
|
245 |
$plugin = "{$plugintype}_{$plugininfo->name}";
|
|
|
246 |
$rowdata = (object) [
|
|
|
247 |
'plugin' => $plugin,
|
|
|
248 |
'plugininfo' => $plugininfo,
|
|
|
249 |
'name' => $plugininfo->displayname,
|
|
|
250 |
'version' => $plugininfo->versiondb,
|
|
|
251 |
];
|
|
|
252 |
$this->add_data_keyed(
|
|
|
253 |
$this->format_row($rowdata),
|
|
|
254 |
$this->get_row_class($rowdata)
|
|
|
255 |
);
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
$this->finish_output(false);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* This table is not downloadable.
|
|
|
263 |
* @param bool $downloadable
|
|
|
264 |
* @return bool
|
|
|
265 |
*/
|
|
|
266 |
// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
|
|
|
267 |
public function is_downloadable($downloadable = null): bool {
|
|
|
268 |
return false;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Show the name column content.
|
|
|
273 |
*
|
|
|
274 |
* @param stdClass $row
|
|
|
275 |
* @return string
|
|
|
276 |
*/
|
|
|
277 |
protected function col_name(stdClass $row): string {
|
|
|
278 |
$status = $row->plugininfo->get_status();
|
|
|
279 |
if ($status === core_plugin_manager::PLUGIN_STATUS_MISSING) {
|
|
|
280 |
return html_writer::span(
|
|
|
281 |
get_string('pluginmissingfromdisk', 'core', $row->plugininfo),
|
|
|
282 |
'notifyproblem'
|
|
|
283 |
);
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
if ($row->plugininfo->is_installed_and_upgraded()) {
|
|
|
287 |
return $row->plugininfo->displayname;
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
return html_writer::span(
|
|
|
291 |
$row->plugininfo->displayname,
|
|
|
292 |
'notifyproblem'
|
|
|
293 |
);
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
/**
|
|
|
297 |
* Show the enable/disable column content.
|
|
|
298 |
*
|
|
|
299 |
* @param stdClass $row
|
|
|
300 |
* @return string
|
|
|
301 |
*/
|
|
|
302 |
protected function col_enabled(stdClass $row): string {
|
|
|
303 |
global $OUTPUT;
|
|
|
304 |
|
|
|
305 |
$enabled = $row->plugininfo->is_enabled();
|
|
|
306 |
if ($enabled) {
|
| 1441 |
ariadna |
307 |
$labelstr = get_string('disableplugin', 'core_admin', $row->plugininfo->displayname);
|
| 1 |
efrain |
308 |
} else {
|
| 1441 |
ariadna |
309 |
$labelstr = get_string('enableplugin', 'core_admin', $row->plugininfo->displayname);
|
| 1 |
efrain |
310 |
}
|
|
|
311 |
|
| 1441 |
ariadna |
312 |
$params = [
|
|
|
313 |
'id' => 'admin-toggle-' . $row->plugininfo->name,
|
|
|
314 |
'checked' => $enabled,
|
|
|
315 |
'dataattributes' => [
|
|
|
316 |
'name' => 'id',
|
|
|
317 |
'value' => $row->plugininfo->name,
|
|
|
318 |
'toggle-method' => $this->get_toggle_service(),
|
|
|
319 |
'action' => 'togglestate',
|
|
|
320 |
'plugin' => $row->plugin,
|
|
|
321 |
'state' => $enabled ? 1 : 0,
|
| 1 |
efrain |
322 |
],
|
| 1441 |
ariadna |
323 |
'title' => $labelstr,
|
|
|
324 |
'label' => $labelstr,
|
|
|
325 |
'labelclasses' => 'visually-hidden',
|
|
|
326 |
];
|
|
|
327 |
|
|
|
328 |
return $OUTPUT->render_from_template('core_admin/setting_configtoggle', $params);
|
| 1 |
efrain |
329 |
}
|
|
|
330 |
|
|
|
331 |
protected function col_order(stdClass $row): string {
|
|
|
332 |
global $OUTPUT;
|
|
|
333 |
|
|
|
334 |
if (!$this->supports_ordering()) {
|
|
|
335 |
return '';
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
if (!$row->plugininfo->is_enabled()) {
|
|
|
339 |
return '';
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
if ($this->enabledplugincount <= 1) {
|
|
|
343 |
// There is only one row.
|
|
|
344 |
return '';
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
$hasup = true;
|
|
|
348 |
$hasdown = true;
|
|
|
349 |
|
|
|
350 |
if (empty($this->currentrow)) {
|
|
|
351 |
// This is the top row.
|
|
|
352 |
$hasup = false;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
if ($this->currentrow === ($this->enabledplugincount - 1)) {
|
|
|
356 |
// This is the last row.
|
|
|
357 |
$hasdown = false;
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
if ($this->supports_ordering()) {
|
|
|
361 |
$dataattributes = [
|
|
|
362 |
'data-method' => $this->get_sortorder_service(),
|
|
|
363 |
'data-action' => 'move',
|
|
|
364 |
'data-plugin' => $row->plugin,
|
|
|
365 |
];
|
|
|
366 |
} else {
|
|
|
367 |
$dataattributes = [];
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
if ($hasup) {
|
|
|
371 |
$upicon = html_writer::link(
|
|
|
372 |
$this->get_action_url([
|
|
|
373 |
'sesskey' => sesskey(),
|
|
|
374 |
'action' => 'up',
|
|
|
375 |
'plugin' => $row->plugininfo->name,
|
|
|
376 |
]),
|
|
|
377 |
$OUTPUT->pix_icon('t/up', get_string('moveup')),
|
|
|
378 |
array_merge($dataattributes, ['data-direction' => 'up']),
|
|
|
379 |
);
|
|
|
380 |
} else {
|
|
|
381 |
$upicon = $OUTPUT->spacer();
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
if ($hasdown) {
|
|
|
385 |
$downicon = html_writer::link(
|
|
|
386 |
$this->get_action_url([
|
|
|
387 |
'sesskey' => sesskey(),
|
|
|
388 |
'action' => 'down',
|
|
|
389 |
'plugin' => $row->plugininfo->name,
|
|
|
390 |
]),
|
|
|
391 |
$OUTPUT->pix_icon('t/down', get_string('movedown')),
|
|
|
392 |
array_merge($dataattributes, ['data-direction' => 'down']),
|
|
|
393 |
);
|
|
|
394 |
} else {
|
|
|
395 |
$downicon = $OUTPUT->spacer();
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
// For now just add the up/down icons.
|
|
|
399 |
return html_writer::span($upicon . $downicon);
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
/**
|
|
|
403 |
* Show the settings column content.
|
|
|
404 |
*
|
|
|
405 |
* @param stdClass $row
|
|
|
406 |
* @return string
|
|
|
407 |
*/
|
|
|
408 |
protected function col_settings(stdClass $row): string {
|
|
|
409 |
if ($settingsurl = $row->plugininfo->get_settings_url()) {
|
|
|
410 |
return html_writer::link($settingsurl, get_string('settings'));
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
return '';
|
|
|
414 |
}
|
|
|
415 |
|
|
|
416 |
/**
|
|
|
417 |
* Show the Uninstall column content.
|
|
|
418 |
*
|
|
|
419 |
* @param stdClass $row
|
|
|
420 |
* @return string
|
|
|
421 |
*/
|
|
|
422 |
protected function col_uninstall(stdClass $row): string {
|
|
|
423 |
$status = $row->plugininfo->get_status();
|
|
|
424 |
|
|
|
425 |
if ($status === core_plugin_manager::PLUGIN_STATUS_NEW) {
|
|
|
426 |
return get_string('status_new', 'core_plugin');
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
if ($status === core_plugin_manager::PLUGIN_STATUS_MISSING) {
|
|
|
430 |
$uninstall = get_string('status_missing', 'core_plugin') . '<br/>';
|
|
|
431 |
} else {
|
|
|
432 |
$uninstall = '';
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
if ($uninstallurl = $this->pluginmanager->get_uninstall_url($row->plugin)) {
|
|
|
436 |
$uninstall .= html_writer::link($uninstallurl, get_string('uninstallplugin', 'core_admin'));
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
return $uninstall;
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
/**
|
|
|
443 |
* Get the JS module used to manage this table.
|
|
|
444 |
*
|
|
|
445 |
* This should be a class which extends 'core_admin/plugin_management_table'.
|
|
|
446 |
*
|
|
|
447 |
* @return string
|
|
|
448 |
*/
|
|
|
449 |
protected function get_table_js_module(): string {
|
|
|
450 |
return 'core_admin/plugin_management_table';
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
/**
|
|
|
454 |
* Add JS specific to this implementation.
|
|
|
455 |
*
|
|
|
456 |
* @return string
|
|
|
457 |
*/
|
|
|
458 |
protected function get_dynamic_table_html_end(): string {
|
|
|
459 |
global $PAGE;
|
|
|
460 |
|
|
|
461 |
$PAGE->requires->js_call_amd($this->get_table_js_module(), 'init');
|
|
|
462 |
return parent::get_dynamic_table_html_end();
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
/**
|
|
|
466 |
* Get any class to add to the row.
|
|
|
467 |
*
|
|
|
468 |
* @param mixed $row
|
|
|
469 |
* @return string
|
|
|
470 |
*/
|
|
|
471 |
protected function get_row_class($row): string {
|
|
|
472 |
$plugininfo = $row->plugininfo;
|
|
|
473 |
if ($plugininfo->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
|
|
|
474 |
return '';
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
if (!$plugininfo->is_enabled()) {
|
|
|
478 |
return 'dimmed_text';
|
|
|
479 |
}
|
|
|
480 |
return '';
|
|
|
481 |
}
|
|
|
482 |
|
|
|
483 |
public static function get_filterset_class(): string {
|
|
|
484 |
return self::class . '_filterset';
|
|
|
485 |
}
|
|
|
486 |
|
|
|
487 |
/**
|
|
|
488 |
* Whether this plugin type supports the disabling of plugins.
|
|
|
489 |
*
|
|
|
490 |
* @return bool
|
|
|
491 |
*/
|
|
|
492 |
protected function supports_disabling(): bool {
|
|
|
493 |
return $this->plugininfoclass::plugintype_supports_disabling();
|
|
|
494 |
}
|
|
|
495 |
|
|
|
496 |
/**
|
|
|
497 |
* Whether this table should show ordering fields.
|
|
|
498 |
*
|
|
|
499 |
* @return bool
|
|
|
500 |
*/
|
|
|
501 |
protected function supports_ordering(): bool {
|
|
|
502 |
return $this->plugininfoclass::plugintype_supports_ordering();
|
|
|
503 |
}
|
| 1441 |
ariadna |
504 |
|
|
|
505 |
/**
|
|
|
506 |
* Check if the user has the capability to access this table.
|
|
|
507 |
*
|
|
|
508 |
* Default implementation for plugin management tables is to require 'moodle/site:config' capability
|
|
|
509 |
*
|
|
|
510 |
* @return bool Return true if capability check passed.
|
|
|
511 |
*/
|
|
|
512 |
public function has_capability(): bool {
|
|
|
513 |
return has_capability('moodle/site:config', $this->get_context());
|
|
|
514 |
}
|
| 1 |
efrain |
515 |
}
|