Merge branch 'MDL-74283' of https://github.com/paulholden/moodle
[moodle.git] / admin / filters.php
bloba79c2ecf617e4f85ea10af8aa4e732c0a782171f
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Filter management page.
20 * @package core
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(__DIR__ . '/../config.php');
26 require_once($CFG->libdir . '/adminlib.php');
28 $action = optional_param('action', '', PARAM_ALPHA);
29 $filterpath = optional_param('filterpath', '', PARAM_PLUGIN);
31 admin_externalpage_setup('managefilters');
33 // Clean up bogus filter states first.
34 $plugininfos = core_plugin_manager::instance()->get_plugins_of_type('filter');
35 $filters = array();
36 $states = filter_get_global_states();
37 foreach ($states as $state) {
38 if (!isset($plugininfos[$state->filter]) and !get_config('filter_'.$state->filter, 'version')) {
39 // Purge messy leftovers after incorrectly uninstalled plugins and unfinished installs.
40 $DB->delete_records('filter_active', array('filter' => $state->filter));
41 $DB->delete_records('filter_config', array('filter' => $state->filter));
42 error_log('Deleted bogus "filter_'.$state->filter.'" states and config data.');
43 } else {
44 $filters[$state->filter] = $state;
48 // Add properly installed and upgraded filters to the global states table.
49 foreach ($plugininfos as $filter => $info) {
50 if (isset($filters[$filter])) {
51 continue;
53 /** @var \core\plugininfo\base $info */
54 if ($info->is_installed_and_upgraded()) {
55 filter_set_global_state($filter, TEXTFILTER_DISABLED);
56 $states = filter_get_global_states();
57 foreach ($states as $state) {
58 if ($state->filter === $filter) {
59 $filters[$filter] = $state;
60 break;
66 if ($action) {
67 require_sesskey();
70 // Process actions.
71 switch ($action) {
73 case 'setstate':
74 if (isset($filters[$filterpath]) and $newstate = optional_param('newstate', '', PARAM_INT)) {
75 $class = \core_plugin_manager::resolve_plugininfo_class('filter');
76 $class::enable_plugin($filterpath, $newstate);
78 break;
80 case 'setapplyto':
81 if (isset($filters[$filterpath])) {
82 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
83 filter_set_applies_to_strings($filterpath, $applytostrings);
84 reset_text_filters_cache();
85 core_plugin_manager::reset_caches();
87 break;
89 case 'down':
90 if (isset($filters[$filterpath])) {
91 filter_set_global_state($filterpath, $filters[$filterpath]->active, 1);
92 reset_text_filters_cache();
93 core_plugin_manager::reset_caches();
95 break;
97 case 'up':
98 if (isset($filters[$filterpath])) {
99 $oldpos = $filters[$filterpath]->sortorder;
100 filter_set_global_state($filterpath, $filters[$filterpath]->active, -1);
101 reset_text_filters_cache();
102 core_plugin_manager::reset_caches();
104 break;
107 // Return.
108 if ($action) {
109 redirect(new moodle_url('/admin/filters.php'));
112 // Print the page heading.
113 echo $OUTPUT->header();
114 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
116 $states = filter_get_global_states();
117 $stringfilters = filter_get_string_filters();
119 $table = new html_table();
120 $table->head = array(get_string('filter'), get_string('isactive', 'filters'),
121 get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('uninstallplugin', 'core_admin'));
122 $table->colclasses = array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
123 $table->attributes['class'] = 'admintable generaltable';
124 $table->id = 'filterssetting';
125 $table->data = array();
127 $lastactive = null;
128 foreach ($states as $state) {
129 if ($state->active != TEXTFILTER_DISABLED) {
130 $lastactive = $state->filter;
134 // Iterate through filters adding to display table.
135 $firstrow = true;
136 foreach ($states as $state) {
137 $filter = $state->filter;
138 if (!isset($plugininfos[$filter])) {
139 continue;
141 $plugininfo = $plugininfos[$filter];
142 $applytostrings = isset($stringfilters[$filter]) && $state->active != TEXTFILTER_DISABLED;
143 $row = get_table_row($plugininfo, $state, $firstrow, $filter == $lastactive, $applytostrings);
144 $table->data[] = $row;
145 if ($state->active == TEXTFILTER_DISABLED) {
146 $table->rowclasses[] = 'dimmed_text';
147 } else {
148 $table->rowclasses[] = '';
150 $firstrow = false;
153 echo html_writer::table($table);
154 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
155 echo $OUTPUT->footer();
156 die;
160 * Return action URL.
162 * @param string $filterpath
163 * @param string $action
164 * @return moodle_url
166 function filters_action_url($filterpath, $action) {
167 if ($action === 'delete') {
168 return core_plugin_manager::instance()->get_uninstall_url('filter_'.$filterpath, 'manage');
170 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
174 * Construct table record.
176 * @param \core\plugininfo\filter $plugininfo
177 * @param stdClass $state
178 * @param bool $isfirstrow
179 * @param bool $islastactive
180 * @param bool $applytostrings
181 * @return array data
183 function get_table_row(\core\plugininfo\filter $plugininfo, $state, $isfirstrow, $islastactive, $applytostrings) {
184 global $OUTPUT;
185 $row = array();
186 $filter = $state->filter;
187 $active = $plugininfo->is_installed_and_upgraded();
189 static $activechoices;
190 static $applytochoices;
191 if (!isset($activechoices)) {
192 $activechoices = array(
193 TEXTFILTER_DISABLED => get_string('disabled', 'core_filters'),
194 TEXTFILTER_OFF => get_string('offbutavailable', 'core_filters'),
195 TEXTFILTER_ON => get_string('on', 'core_filters'),
197 $applytochoices = array(
198 0 => get_string('content', 'core_filters'),
199 1 => get_string('contentandheadings', 'core_filters'),
203 // Filter name.
204 $displayname = $plugininfo->displayname;
205 if (!$plugininfo->rootdir) {
206 $displayname = '<span class="error">' . $displayname . ' - ' . get_string('status_missing', 'core_plugin') . '</span>';
207 } else if (!$active) {
208 $displayname = '<span class="error">' . $displayname . ' - ' . get_string('error') . '</span>';
210 $row[] = $displayname;
212 // Disable/off/on.
213 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $state->active, null, 'active' . $filter);
214 $select->set_label(get_string('isactive', 'filters'), array('class' => 'accesshide'));
215 $row[] = $OUTPUT->render($select);
217 // Re-order.
218 $updown = '';
219 $spacer = $OUTPUT->spacer();
220 if ($state->active != TEXTFILTER_DISABLED) {
221 if (!$isfirstrow) {
222 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
223 } else {
224 $updown .= $spacer;
226 if (!$islastactive) {
227 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
228 } else {
229 $updown .= $spacer;
232 $row[] = $updown;
234 // Apply to strings.
235 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
236 $select->set_label(get_string('applyto', 'filters'), array('class' => 'accesshide'));
237 $select->disabled = ($state->active == TEXTFILTER_DISABLED);
238 $row[] = $OUTPUT->render($select);
240 // Settings link, if required.
241 if ($active and filter_has_global_settings($filter)) {
242 $row[] = html_writer::link(new moodle_url('/admin/settings.php', array('section'=>'filtersetting'.$filter)), get_string('settings'));
243 } else {
244 $row[] = '';
247 // Uninstall.
248 $row[] = html_writer::link(filters_action_url($filter, 'delete'), get_string('uninstallplugin', 'core_admin'));
250 return $row;