MDL-42512 fix and cleanup filter management page

This commit is contained in:
Petr Škoda 2013-10-24 22:41:02 +02:00
parent 5386f0bbfe
commit 137f573cfd

View File

@ -1,85 +1,93 @@
<?php
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// This program 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: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
// 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/>.
/**
* Processes actions from the admin_setting_managefilters object (defined in
* adminlib.php).
* Filter management page.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package administration
*//** */
* @package core
* @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../config.php');
require_once($CFG->libdir . '/adminlib.php');
$action = optional_param('action', '', PARAM_ALPHANUMEXT);
$filterpath = optional_param('filterpath', '', PARAM_SAFEDIR);
$action = optional_param('action', '', PARAM_ALPHA);
$filterpath = optional_param('filterpath', '', PARAM_PLUGIN);
require_login();
$systemcontext = context_system::instance();
require_capability('moodle/site:config', $systemcontext);
$returnurl = "$CFG->wwwroot/$CFG->admin/filters.php";
admin_externalpage_setup('managefilters');
$filters = filter_get_global_states();
// In case any new filters have been installed, but not put in the table yet.
$fitlernames = filter_get_all_installed();
$newfilters = $fitlernames;
foreach ($filters as $filter => $notused) {
unset($newfilters[$filter]);
// Clean up bogus filter states first.
$plugininfos = core_plugin_manager::instance()->get_plugins_of_type('filter');
$filters = array();
$states = filter_get_global_states();
foreach ($states as $state) {
if (!isset($plugininfos[$state->filter]) and !get_config('filter_'.$state->filter, 'version')) {
// Purge messy leftovers after incorrectly uninstalled plugins and unfinished installs.
$DB->delete_records('filter_active', array('filter' => $state->filter));
$DB->delete_records('filter_config', array('filter' => $state->filter));
error_log('Deleted bogus "filter_'.$state->filter.'" states and config data.');
} else {
$filters[$state->filter] = $state;
}
}
/// Process actions ============================================================
// Add properly installed and upgraded filters to the global states table.
foreach ($plugininfos as $filter => $info) {
if (isset($filters[$filter])) {
continue;
}
/** @var \core\plugininfo\base $info */
if ($info->is_installed_and_upgraded()) {
filter_set_global_state($filter, TEXTFILTER_DISABLED);
$states = filter_get_global_states();
foreach ($states as $state) {
if ($state->filter === $filter) {
$filters[$filter] = $state;
break;
}
}
}
}
if ($action) {
if ($action !== 'delete' and !isset($filters[$filterpath]) and !isset($newfilters[$filterpath])) {
throw new moodle_exception('filternotinstalled', 'error', $returnurl, $filterpath);
}
if (!confirm_sesskey()) {
redirect($returnurl);
}
require_sesskey();
}
// Process actions.
switch ($action) {
case 'setstate':
if ($newstate = optional_param('newstate', '', PARAM_INT)) {
if (isset($filters[$filterpath]) and $newstate = optional_param('newstate', '', PARAM_INT)) {
filter_set_global_state($filterpath, $newstate);
if ($newstate == TEXTFILTER_DISABLED) {
filter_set_applies_to_strings($filterpath, false);
}
unset($newfilters[$filterpath]);
}
break;
case 'setapplyto':
$applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
filter_set_applies_to_strings($filterpath, $applytostrings);
if (isset($filters[$filterpath])) {
$applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
filter_set_applies_to_strings($filterpath, $applytostrings);
}
break;
case 'down':
@ -96,42 +104,18 @@
break;
}
// Add any missing filters to the DB table.
foreach ($newfilters as $filter => $notused) {
filter_set_global_state($filter, TEXTFILTER_DISABLED);
}
// Reset caches and return
// Reset caches and return.
if ($action) {
core_plugin_manager::reset_caches();
reset_text_filters_cache();
redirect($returnurl);
core_plugin_manager::reset_caches();
redirect(new moodle_url('/admin/filters.php'));
}
/// End of process actions =====================================================
/// Print the page heading.
// Print the page heading.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
$activechoices = array(
TEXTFILTER_DISABLED => get_string('disabled', 'filters'),
TEXTFILTER_OFF => get_string('offbutavailable', 'filters'),
TEXTFILTER_ON => get_string('on', 'filters'),
);
$applytochoices = array(
0 => get_string('content', 'filters'),
1 => get_string('contentandheadings', 'filters'),
);
$filters = filter_get_global_states();
// In case any new filters have been installed, but not put in the table yet.
$filternames = filter_get_all_installed();
$newfilters = $filternames;
foreach ($filters as $filter => $notused) {
unset($newfilters[$filter]);
}
$states = filter_get_global_states();
$stringfilters = filter_get_string_filters();
$table = new html_table();
@ -143,39 +127,35 @@
$table->data = array();
$lastactive = null;
foreach ($filters as $filter => $filterinfo) {
if ($filterinfo->active != TEXTFILTER_DISABLED) {
$lastactive = $filter;
foreach ($states as $state) {
if ($state->active != TEXTFILTER_DISABLED) {
$lastactive = $state->filter;
}
}
// iterate through filters adding to display table
// Iterate through filters adding to display table.
$firstrow = true;
foreach ($filters as $filter => $filterinfo) {
$applytostrings = isset($stringfilters[$filter]) && $filterinfo->active != TEXTFILTER_DISABLED;
$row = get_table_row($filterinfo, $firstrow, $filter == $lastactive, $applytostrings);
foreach ($states as $state) {
$filter = $state->filter;
if (!isset($plugininfos[$filter])) {
continue;
}
$plugininfo = $plugininfos[$filter];
$applytostrings = isset($stringfilters[$filter]) && $state->active != TEXTFILTER_DISABLED;
$row = get_table_row($plugininfo, $state, $firstrow, $filter == $lastactive, $applytostrings);
$table->data[] = $row;
if ($filterinfo->active == TEXTFILTER_DISABLED) {
if ($state->active == TEXTFILTER_DISABLED) {
$table->rowclasses[] = 'dimmed_text';
} else {
$table->rowclasses[] = '';
}
$firstrow = false;
}
foreach ($newfilters as $filter => $filtername) {
$filterinfo = new stdClass;
$filterinfo->filter = $filter;
$filterinfo->active = TEXTFILTER_DISABLED;
$row = get_table_row($filterinfo, false, false, false);
$table->data[] = $row;
$table->rowclasses[] = 'dimmed_text';
}
echo html_writer::table($table);
echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
echo $OUTPUT->footer();
/// Display helper functions ===================================================
function filters_action_url($filterpath, $action) {
if ($action === 'delete') {
@ -184,27 +164,44 @@ function filters_action_url($filterpath, $action) {
return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
}
function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
global $CFG, $OUTPUT, $activechoices, $applytochoices, $filternames; //TODO: this is sloppy coding style!!
function get_table_row(\core\plugininfo\filter $plugininfo, $state, $isfirstrow, $islastactive, $applytostrings) {
global $OUTPUT;
$row = array();
$filter = $filterinfo->filter;
$filter = $state->filter;
$active = $plugininfo->is_installed_and_upgraded();
// Filter name
if (!empty($filternames[$filter])) {
$row[] = $filternames[$filter];
} else {
$row[] = '<span class="error">' . get_string('filemissing', '', $filter) . '</span>';
static $activechoices;
static $applytochoices;
if (!isset($activechoices)) {
$activechoices = array(
TEXTFILTER_DISABLED => get_string('disabled', 'core_filters'),
TEXTFILTER_OFF => get_string('offbutavailable', 'core_filters'),
TEXTFILTER_ON => get_string('on', 'core_filters'),
);
$applytochoices = array(
0 => get_string('content', 'core_filters'),
1 => get_string('contentandheadings', 'core_filters'),
);
}
// Disable/off/on
$select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $filterinfo->active, null, 'active' . $filter);
// Filter name.
$displayname = $plugininfo->displayname;
if (!$plugininfo->rootdir) {
$displayname = '<span class="error">' . $displayname . ' - ' . get_string('status_missing', 'core_plugin') . '</span>';
} else if (!$active) {
$displayname = '<span class="error">' . $displayname . ' - ' . get_string('error') . '</span>';
}
$row[] = $displayname;
// Disable/off/on.
$select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $state->active, null, 'active' . $filter);
$select->set_label(get_string('isactive', 'filters'), array('class' => 'accesshide'));
$row[] = $OUTPUT->render($select);
// Re-order
// Re-order.
$updown = '';
$spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" />';
if ($filterinfo->active != TEXTFILTER_DISABLED) {
if ($state->active != TEXTFILTER_DISABLED) {
if (!$isfirstrow) {
$updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
} else {
@ -221,18 +218,18 @@ function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings)
// Apply to strings.
$select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
$select->set_label(get_string('applyto', 'filters'), array('class' => 'accesshide'));
$select->disabled = $filterinfo->active == TEXTFILTER_DISABLED;
$select->disabled = ($state->active == TEXTFILTER_DISABLED);
$row[] = $OUTPUT->render($select);
// Settings link, if required
if (filter_has_global_settings($filter)) {
$row[] = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' . $filter . '">' . get_string('settings') . '</a>';
// Settings link, if required.
if ($active and filter_has_global_settings($filter)) {
$row[] = html_writer::link(new moodle_url('/admin/settings.php', array('section'=>'filtersetting'.$filter)), get_string('settings'));
} else {
$row[] = '';
}
// Delete
$row[] = '<a href="' . filters_action_url($filter, 'delete') . '">' . get_string('uninstallplugin', 'core_admin') . '</a>';
// Uninstall.
$row[] = html_writer::link(filters_action_url($filter, 'delete'), get_string('uninstallplugin', 'core_admin'));
return $row;
}