2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Filter management page.
21 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(dirname(__FILE__
) . '/../config.php');
26 require_once($CFG->libdir
. '/adminlib.php');
28 $action = optional_param('action', '', PARAM_ALPHA
);
29 $filterpath = optional_param('filterpath', '', PARAM_PLUGIN
);
32 $systemcontext = context_system
::instance();
33 require_capability('moodle/site:config', $systemcontext);
35 admin_externalpage_setup('managefilters');
37 // Clean up bogus filter states first.
38 $plugininfos = core_plugin_manager
::instance()->get_plugins_of_type('filter');
40 $states = filter_get_global_states();
41 foreach ($states as $state) {
42 if (!isset($plugininfos[$state->filter
]) and !get_config('filter_'.$state->filter
, 'version')) {
43 // Purge messy leftovers after incorrectly uninstalled plugins and unfinished installs.
44 $DB->delete_records('filter_active', array('filter' => $state->filter
));
45 $DB->delete_records('filter_config', array('filter' => $state->filter
));
46 error_log('Deleted bogus "filter_'.$state->filter
.'" states and config data.');
48 $filters[$state->filter
] = $state;
52 // Add properly installed and upgraded filters to the global states table.
53 foreach ($plugininfos as $filter => $info) {
54 if (isset($filters[$filter])) {
57 /** @var \core\plugininfo\base $info */
58 if ($info->is_installed_and_upgraded()) {
59 filter_set_global_state($filter, TEXTFILTER_DISABLED
);
60 $states = filter_get_global_states();
61 foreach ($states as $state) {
62 if ($state->filter
=== $filter) {
63 $filters[$filter] = $state;
78 if (isset($filters[$filterpath]) and $newstate = optional_param('newstate', '', PARAM_INT
)) {
79 filter_set_global_state($filterpath, $newstate);
80 if ($newstate == TEXTFILTER_DISABLED
) {
81 filter_set_applies_to_strings($filterpath, false);
87 if (isset($filters[$filterpath])) {
88 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL
);
89 filter_set_applies_to_strings($filterpath, $applytostrings);
94 if (isset($filters[$filterpath])) {
95 filter_set_global_state($filterpath, $filters[$filterpath]->active
, 1);
100 if (isset($filters[$filterpath])) {
101 $oldpos = $filters[$filterpath]->sortorder
;
102 filter_set_global_state($filterpath, $filters[$filterpath]->active
, -1);
107 // Reset caches and return.
109 reset_text_filters_cache();
110 core_plugin_manager
::reset_caches();
111 redirect(new moodle_url('/admin/filters.php'));
114 // Print the page heading.
115 echo $OUTPUT->header();
116 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
118 $states = filter_get_global_states();
119 $stringfilters = filter_get_string_filters();
121 $table = new html_table();
122 $table->head
= array(get_string('filter'), get_string('isactive', 'filters'),
123 get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('uninstallplugin', 'core_admin'));
124 $table->colclasses
= array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
125 $table->attributes
['class'] = 'admintable generaltable';
126 $table->id
= 'filterssetting';
127 $table->data
= array();
130 foreach ($states as $state) {
131 if ($state->active
!= TEXTFILTER_DISABLED
) {
132 $lastactive = $state->filter
;
136 // Iterate through filters adding to display table.
138 foreach ($states as $state) {
139 $filter = $state->filter
;
140 if (!isset($plugininfos[$filter])) {
143 $plugininfo = $plugininfos[$filter];
144 $applytostrings = isset($stringfilters[$filter]) && $state->active
!= TEXTFILTER_DISABLED
;
145 $row = get_table_row($plugininfo, $state, $firstrow, $filter == $lastactive, $applytostrings);
146 $table->data
[] = $row;
147 if ($state->active
== TEXTFILTER_DISABLED
) {
148 $table->rowclasses
[] = 'dimmed_text';
150 $table->rowclasses
[] = '';
155 echo html_writer
::table($table);
156 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
157 echo $OUTPUT->footer();
164 * @param string $filterpath
165 * @param string $action
168 function filters_action_url($filterpath, $action) {
169 if ($action === 'delete') {
170 return core_plugin_manager
::instance()->get_uninstall_url('filter_'.$filterpath, 'manage');
172 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
176 * Construct table record.
178 * @param \core\plugininfo\filter $plugininfo
179 * @param stdClass $state
180 * @param bool $isfirstrow
181 * @param bool $islastactive
182 * @param bool $applytostrings
185 function get_table_row(\core\plugininfo\filter
$plugininfo, $state, $isfirstrow, $islastactive, $applytostrings) {
188 $filter = $state->filter
;
189 $active = $plugininfo->is_installed_and_upgraded();
191 static $activechoices;
192 static $applytochoices;
193 if (!isset($activechoices)) {
194 $activechoices = array(
195 TEXTFILTER_DISABLED
=> get_string('disabled', 'core_filters'),
196 TEXTFILTER_OFF
=> get_string('offbutavailable', 'core_filters'),
197 TEXTFILTER_ON
=> get_string('on', 'core_filters'),
199 $applytochoices = array(
200 0 => get_string('content', 'core_filters'),
201 1 => get_string('contentandheadings', 'core_filters'),
206 $displayname = $plugininfo->displayname
;
207 if (!$plugininfo->rootdir
) {
208 $displayname = '<span class="error">' . $displayname . ' - ' . get_string('status_missing', 'core_plugin') . '</span>';
209 } else if (!$active) {
210 $displayname = '<span class="error">' . $displayname . ' - ' . get_string('error') . '</span>';
212 $row[] = $displayname;
215 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $state->active
, null, 'active' . $filter);
216 $select->set_label(get_string('isactive', 'filters'), array('class' => 'accesshide'));
217 $row[] = $OUTPUT->render($select);
221 $spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" />';
222 if ($state->active
!= TEXTFILTER_DISABLED
) {
224 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
228 if (!$islastactive) {
229 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
237 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
238 $select->set_label(get_string('applyto', 'filters'), array('class' => 'accesshide'));
239 $select->disabled
= ($state->active
== TEXTFILTER_DISABLED
);
240 $row[] = $OUTPUT->render($select);
242 // Settings link, if required.
243 if ($active and filter_has_global_settings($filter)) {
244 $row[] = html_writer
::link(new moodle_url('/admin/settings.php', array('section'=>'filtersetting'.$filter)), get_string('settings'));
250 $row[] = html_writer
::link(filters_action_url($filter, 'delete'), get_string('uninstallplugin', 'core_admin'));