MDL-55528 admin: Add a new generic admin setting type

This commit is contained in:
Andrew Nicols
2017-03-07 08:22:42 +08:00
parent 3c45d26f58
commit 29ce005862
5 changed files with 348 additions and 1 deletions
+205
View File
@@ -7388,6 +7388,211 @@ class admin_page_managefilters extends admin_externalpage {
}
}
/**
* Generic class for managing plugins in a table that allows re-ordering and enable/disable of each plugin.
* Requires a get_rank method on the plugininfo class for sorting.
*
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class admin_setting_manage_plugins extends admin_setting {
/**
* Get the admin settings section name (just a unique string)
* @return string
*/
public function get_section_name() {
return 'manage' . $this->get_plugin_type() . 'plugins';
}
/**
* Get the admin settings section title (use get_string).
* @return string
*/
abstract public function get_section_title();
/**
* Get the type of plugin to manage.
* @return string
*/
abstract public function get_plugin_type();
/**
* Get the name of the second column.
* @return string
*/
public function get_info_column_name() {
return '';
}
/**
* Get the type of plugin to manage.
*
* @param plugininfo The plugin info class.
* @return string
*/
abstract public function get_info_column($plugininfo);
/**
* Calls parent::__construct with specific arguments
*/
public function __construct() {
$this->nosave = true;
parent::__construct($this->get_section_name(), $this->get_section_title(), '', '');
}
/**
* Always returns true, does nothing
*
* @return true
*/
public function get_setting() {
return true;
}
/**
* Always returns true, does nothing
*
* @return true
*/
public function get_defaultsetting() {
return true;
}
/**
* Always returns '', does not write anything
*
* @param mixed $data
* @return string Always returns ''
*/
public function write_setting($data) {
// Do not write any setting.
return '';
}
/**
* Checks if $query is one of the available plugins of this type
*
* @param string $query The string to search for
* @return bool Returns true if found, false if not
*/
public function is_related($query) {
if (parent::is_related($query)) {
return true;
}
$query = core_text::strtolower($query);
$plugins = core_plugin_manager::instance()->get_plugins_of_type($this->get_plugin_type());
foreach ($plugins as $name => $plugin) {
$localised = $plugin->displayname;
if (strpos(core_text::strtolower($name), $query) !== false) {
return true;
}
if (strpos(core_text::strtolower($localised), $query) !== false) {
return true;
}
}
return false;
}
/**
* The URL for the management page for this plugintype.
*
* @return moodle_url
*/
protected function get_manage_url() {
return new moodle_url('/admin/updatesetting.php');
}
/**
* Builds the HTML to display the control.
*
* @param string $data Unused
* @param string $query
* @return string
*/
public function output_html($data, $query = '') {
global $CFG, $OUTPUT, $DB, $PAGE;
$spacer = new pix_icon('spacer', '', 'moodle');
$moveup = new pix_icon('t/up', get_string('up'), 'moodle');
$movedown = new pix_icon('t/down', get_string('down'), 'moodle');
$context = (object) [
'manageurl' => new moodle_url($this->get_manage_url(), [
'type' => $this->get_plugin_type(),
'sesskey' => sesskey(),
]),
'infocolumnname' => $this->get_info_column_name(),
'spacericon' => $spacer->export_for_template($OUTPUT),
'moveupicon' => $moveup->export_for_template($OUTPUT),
'movedownicon' => $movedown->export_for_template($OUTPUT),
'plugins' => [],
];
$pluginmanager = core_plugin_manager::instance();
$allplugins = $pluginmanager->get_plugins_of_type($this->get_plugin_type());
$enabled = $pluginmanager->get_enabled_plugins($this->get_plugin_type());
$plugins = array_merge($enabled, $allplugins);
foreach ($plugins as $key => $plugin) {
$pluginlink = new moodle_url($context->manageurl, ['plugin' => $key]);
$pluginkey = (object) [
'plugin' => $plugin->displayname,
'enabled' => $plugin->is_enabled(),
'togglelink' => '',
'toggleicon' => '',
'moveuplink' => '',
'movedownlink' => '',
'settingslink' => $plugin->get_settings_url(),
'uninstalllink' => '',
'info' => '',
];
// Enable/Disable link.
$togglelink = new moodle_url($pluginlink);
if ($plugin->is_enabled()) {
$toggleicon = new pix_icon('i/hide', get_string('disable', 'moodle'), 'moodle');
$togglelink->param('action', 'disable');
if (count($context->plugins)) {
// This is not the first plugin.
$pluginkey->moveuplink = new moodle_url($pluginlink, ['action' => 'up']);
}
if (count($enabled) > count($context->plugins) + 1) {
// This is not the last plugin.
$pluginkey->movedownlink = new moodle_url($pluginlink, ['action' => 'down']);
}
$pluginkey->info = $this->get_info_column($plugin);
} else {
$toggleicon = new pix_icon('i/show', get_string('enable', 'moodle'), 'moodle');
$togglelink->param('action', 'enable');
}
$pluginkey->toggleicon = $toggleicon->export_for_template($OUTPUT);
$pluginkey->togglelink = $togglelink;
$frankenstyle = $plugin->type . '_' . $plugin->name;
if ($uninstalllink = core_plugin_manager::instance()->get_uninstall_url($frankenstyle, 'manage')) {
// This plugin supports uninstallation.
$pluginkey->uninstalllink = $uninstalllink;
}
if (!empty($this->get_info_column_name())) {
// This plugintype has an info column.
$pluginkey->info = $this->get_info_column($plugin);
}
$context->plugins[] = $pluginkey;
}
$str = $OUTPUT->render_from_template('core_admin/setting_manage_plugins', $context);
return highlight($query, $str);
}
}
/**
* Special class for media player plugins management.
*