Skip to content

Commit

Permalink
MDL-55528 admin: Add a new generic admin setting type
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 7, 2017
1 parent 3c45d26 commit 29ce005
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 1 deletion.
60 changes: 60 additions & 0 deletions admin/templates/setting_manage_plugins.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<table class="admintable generaltable">
<thead>
<tr>
<th class="header">{{#str}}name, moodle{{/str}}</th>
{{#infocolumnname}}
<th class="header">{{infocolumnname}}</th>
{{/infocolumnname}}
<th class="header">{{#str}}order, moodle{{/str}}</th>
<th class="header">{{#str}}isenabled, plugin{{/str}}</th>
<th class="header">{{#str}}settings, moodle{{/str}}</th>
<th class="header">{{#str}}uninstall, plugin{{/str}}</th>
</tr>
</thead>
<tbody>
{{#plugins}}
<tr>
<td>{{plugin}}</td>
{{#infocolumnname}}
<td>
{{info}}
</td>
{{/infocolumnname}}
<td>
{{#moveuplink}}
<a href="{{{moveuplink}}}">
{{#moveupicon}}{{>core/pix_icon}}{{/moveupicon}}
</a>
{{/moveuplink}}
{{^moveuplink}}
{{#spacericon}}{{>core/pix_icon}}{{/spacericon}}
{{/moveuplink}}

{{#movedownlink}}
<a href="{{{movedownlink}}}">
{{#movedownicon}}{{>core/pix_icon}}{{/movedownicon}}
</a>
{{/movedownlink}}
{{^movedownlink}}
{{#spacericon}}{{>core/pix_icon}}{{/spacericon}}
{{/movedownlink}}
</td>
<td>
<a href="{{{togglelink}}}">
{{#toggleicon}}{{>core/pix_icon}}{{/toggleicon}}
</a>
</td>
<td>
{{#settingslink}}
<a href="{{{settingslink}}}">{{#str}}settings,plugin{{/str}}</a>
{{/settingslink}}
</td>
<td>
{{#uninstalllink}}
<a href="{{{uninstalllink}}}">{{#str}}uninstall,plugin{{/str}}</a>
{{/uninstalllink}}
</td>
</tr>
{{/plugins}}
</tbody>
</table>
80 changes: 80 additions & 0 deletions admin/updatesetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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/>.

/**
* Generic plugin config manipulation script.
*
* @package admin
* @copyright 2017 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('NO_OUTPUT_BUFFERING', true);

require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');

$action = required_param('action', PARAM_ALPHANUMEXT);
$plugin = required_param('plugin', PARAM_PLUGIN);
$type = required_param('type', PARAM_PLUGIN);

$PAGE->set_url('/admin/updatesetting.php');
$PAGE->set_context(context_system::instance());

require_login();
require_capability('moodle/site:config', context_system::instance());
require_sesskey();

$plugintypeclass = "\\core\\plugininfo\\{$type}";

$plugins = \core_plugin_manager::instance()->get_plugins_of_type($type);
$sortorder = array_values($plugintypeclass::get_enabled_plugins());

$return = $plugintypeclass::get_manage_url();

if (!array_key_exists($plugin, $plugins)) {
redirect($return);
}

switch ($action) {
case 'disable':
$plugins[$plugin]->set_enabled(false);
break;

case 'enable':
$plugins[$plugin]->set_enabled(true);
break;

case 'up':
if (($pos = array_search($plugin, $sortorder)) > 0) {
$tmp = $sortorder[$pos - 1];
$sortorder[$pos - 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
$plugintypeclass::set_enabled_plugins($sortorder);
}
break;

case 'down':
if ((($pos = array_search($plugin, $sortorder)) !== false) && ($pos < count($sortorder) - 1)) {
$tmp = $sortorder[$pos + 1];
$sortorder[$pos + 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
$plugintypeclass::set_enabled_plugins($sortorder);
}
break;
}

redirect($return);
3 changes: 2 additions & 1 deletion lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@
$string['extendedusernamechars'] = 'Allow extended characters in usernames';
$string['extramemorylimit'] = 'Extra PHP memory limit';
$string['fatalsessionautostart'] = '<p>Serious configuration error detected, please notify server administrator.</p><p> To operate properly, Moodle requires that administrator changes PHP settings.</p><p><code>session.auto_start</code> must be set to <code>off</code>.</p><p>This setting is controlled by editing <code>php.ini</code>, Apache/IIS <br />configuration or <code>.htaccess</code> file on the server.</p>';
$string['fileconversioncleanuptask'] = 'Cleanup of temporary records for file conversions.';
$string['filecreated'] = 'New file created';
$string['filestoredin'] = 'Save file into folder :';
$string['filestoredinhelp'] = 'Where the file will be stored';
Expand Down Expand Up @@ -1245,4 +1246,4 @@
$string['mobile'] = 'Mobile';
// Deprecated since Moodle 3.3.
$string['loginpasswordautocomplete'] = 'Prevent password autocompletion on login form';
$string['loginpasswordautocomplete_help'] = 'If enabled, users are not allowed to save their account password in their browser.';
$string['loginpasswordautocomplete_help'] = 'If enabled, users are not allowed to save their account password in their browser.';
1 change: 1 addition & 0 deletions lang/en/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
$string['filterall'] = 'Show all';
$string['filtercontribonly'] = 'Show additional plugins only';
$string['filterupdatesonly'] = 'Show updateable only';
$string['isenabled'] = 'Enabled?';
$string['misdepinfoplugin'] = 'Plugin info';
$string['misdepinfoversion'] = 'Version info';
$string['misdepsavail'] = 'Available missing dependencies';
Expand Down
205 changes: 205 additions & 0 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7388,6 +7388,211 @@ public function search($query) {
}
}

/**
* 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.
*
Expand Down

0 comments on commit 29ce005

Please sign in to comment.