Skip to content

Commit

Permalink
Merge branch 'MDL-74390' of https://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta authored and snake committed Apr 1, 2022
2 parents 6d58619 + 409b94c commit 20f8914
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
62 changes: 35 additions & 27 deletions admin/filters.php
Expand Up @@ -31,14 +31,15 @@
admin_externalpage_setup('managefilters');

// Clean up bogus filter states first.
/** @var core\plugininfo\filter[] $plugininfos */
$plugininfos = core_plugin_manager::instance()->get_plugins_of_type('filter');
$filters = array();
$filters = [];
$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));
// Purge messy leftovers after incorrectly uninstalled plugins and unfinished installations.
$DB->delete_records('filter_active', ['filter' => $state->filter]);
$DB->delete_records('filter_config', ['filter' => $state->filter]);
error_log('Deleted bogus "filter_'.$state->filter.'" states and config data.');
} else {
$filters[$state->filter] = $state;
Expand All @@ -50,7 +51,6 @@
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();
Expand All @@ -72,7 +72,8 @@

case 'setstate':
if (isset($filters[$filterpath]) and $newstate = optional_param('newstate', '', PARAM_INT)) {
$class = \core_plugin_manager::resolve_plugininfo_class('filter');
/** @var \core\plugininfo\filter $class */
$class = core_plugin_manager::resolve_plugininfo_class('filter');
$class::enable_plugin($filterpath, $newstate);
}
break;
Expand Down Expand Up @@ -117,12 +118,12 @@
$stringfilters = filter_get_string_filters();

$table = new html_table();
$table->head = array(get_string('filter'), get_string('isactive', 'filters'),
get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('uninstallplugin', 'core_admin'));
$table->head = [get_string('filter'), get_string('isactive', 'filters'),
get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('uninstallplugin', 'core_admin')];
$table->colclasses = array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
$table->attributes['class'] = 'admintable generaltable';
$table->id = 'filterssetting';
$table->data = array();
$table->data = [];

$lastactive = null;
foreach ($states as $state) {
Expand Down Expand Up @@ -159,15 +160,16 @@
/**
* Return action URL.
*
* @param string $filterpath
* @param string $action
* @return moodle_url
* @param string $filterpath which filter to get the URL for.
* @param string $action which action to get the URL for.
* @return moodle_url|null the requested URL.
*/
function filters_action_url($filterpath, $action) {
function filters_action_url(string $filterpath, string $action): ?moodle_url {
if ($action === 'delete') {
return core_plugin_manager::instance()->get_uninstall_url('filter_'.$filterpath, 'manage');
}
return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
return new moodle_url('/admin/filters.php',
['sesskey' => sesskey(), 'filterpath' => $filterpath, 'action' => $action]);
}

/**
Expand All @@ -180,24 +182,25 @@ function filters_action_url($filterpath, $action) {
* @param bool $applytostrings
* @return array data
*/
function get_table_row(\core\plugininfo\filter $plugininfo, $state, $isfirstrow, $islastactive, $applytostrings) {
function get_table_row(\core\plugininfo\filter $plugininfo, stdClass $state,
bool $isfirstrow, bool $islastactive, bool $applytostrings): array {
global $OUTPUT;
$row = array();
$row = [];
$filter = $state->filter;
$active = $plugininfo->is_installed_and_upgraded();

static $activechoices;
static $applytochoices;
if (!isset($activechoices)) {
$activechoices = array(
$activechoices = [
TEXTFILTER_DISABLED => get_string('disabled', 'core_filters'),
TEXTFILTER_OFF => get_string('offbutavailable', 'core_filters'),
TEXTFILTER_ON => get_string('on', 'core_filters'),
);
$applytochoices = array(
];
$applytochoices = [
0 => get_string('content', 'core_filters'),
1 => get_string('contentandheadings', 'core_filters'),
);
];
}

// Filter name.
Expand All @@ -211,41 +214,46 @@ function get_table_row(\core\plugininfo\filter $plugininfo, $state, $isfirstrow,

// 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'));
$select->set_label(get_string('isactive', 'filters'), ['class' => 'accesshide']);
$row[] = $OUTPUT->render($select);

// Re-order.
$updown = '';
$spacer = $OUTPUT->spacer();
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')));
$updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'),
new pix_icon('t/up', get_string('up'), '', ['class' => 'iconsmall']));
} else {
$updown .= $spacer;
}
if (!$islastactive) {
$updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
$updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'),
new pix_icon('t/down', get_string('down'), '', ['class' => 'iconsmall']));
} else {
$updown .= $spacer;
}
}
$row[] = $updown;

// 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 = new single_select(filters_action_url($filter, 'setapplyto'),
'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
$select->set_label(get_string('applyto', 'filters'), ['class' => 'accesshide']);
$select->disabled = ($state->active == TEXTFILTER_DISABLED);
$row[] = $OUTPUT->render($select);

// 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'));
$row[] = html_writer::link(new moodle_url('/admin/settings.php',
['section' => 'filtersetting'.$filter]), get_string('settings'));
} else {
$row[] = '';
}

// Uninstall.
$row[] = html_writer::link(filters_action_url($filter, 'delete'), get_string('uninstallplugin', 'core_admin'));
$row[] = html_writer::link(filters_action_url($filter, 'delete'),
get_string('uninstallplugin', 'core_admin'));

return $row;
}
14 changes: 14 additions & 0 deletions lib/filterlib.php
Expand Up @@ -664,6 +664,13 @@ function filter_set_global_state($filtername, $state, $move = 0) {

// Move only active.
if ($move != 0 and isset($on[$filter->filter])) {
// Capture the old order for logging.
$oldorder = implode(', ', array_map(
function($f) {
return $f->filter;
}, $on));

// Work out the new order.
$i = 1;
foreach ($on as $f) {
$f->newsortorder = $i;
Expand All @@ -686,6 +693,13 @@ function filter_set_global_state($filtername, $state, $move = 0) {
}

core_collator::asort_objects_by_property($on, 'newsortorder', core_collator::SORT_NUMERIC);

// Log in config_log.
$neworder = implode(', ', array_map(
function($f) {
return $f->filter;
}, $on));
add_to_config_log('order', $oldorder, $neworder, 'core_filter');
}

// Inactive are sorted by filter name.
Expand Down
20 changes: 20 additions & 0 deletions lib/tests/filterlib_test.php
Expand Up @@ -131,6 +131,8 @@ public function test_update_existing_dont_duplicate() {
}

public function test_update_reorder_down() {
global $DB;

$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
Expand All @@ -141,9 +143,19 @@ public function test_update_reorder_down() {
filter_set_global_state('two', TEXTFILTER_ON, -1);
// Validate.
$this->assert_global_sort_order(array('two', 'one', 'three'));

// Check this was logged in config log.
$logs = $DB->get_records('config_log', null, 'id DESC', '*', 0, 1);
$log = reset($logs);
$this->assertEquals('core_filter', $log->plugin);
$this->assertEquals('order', $log->name);
$this->assertEquals('two, one, three', $log->value);
$this->assertEquals('one, two, three', $log->oldvalue);
}

public function test_update_reorder_up() {
global $DB;

$this->resetAfterTest();
$this->remove_all_filters_from_config(); // Remove all filters.
// Setup fixture.
Expand All @@ -155,6 +167,14 @@ public function test_update_reorder_up() {
filter_set_global_state('two', TEXTFILTER_ON, 1);
// Validate.
$this->assert_global_sort_order(array('one', 'three', 'two', 'four'));

// Check this was logged in config log.
$logs = $DB->get_records('config_log', null, 'id DESC', '*', 0, 1);
$log = reset($logs);
$this->assertEquals('core_filter', $log->plugin);
$this->assertEquals('order', $log->name);
$this->assertEquals('one, three, two, four', $log->value);
$this->assertEquals('one, two, three, four', $log->oldvalue);
}

public function test_auto_sort_order_change_to_enabled() {
Expand Down

0 comments on commit 20f8914

Please sign in to comment.