Merge branch 'MDL-58651_32' of git://github.com/aolley/moodle into MOODLE_32_STABLE
[moodle.git] / admin / filters.php
blobb1127678d18de1e5636e68171022f58282559040
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 require_login();
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');
39 $filters = array();
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.');
47 } else {
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])) {
55 continue;
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;
64 break;
70 if ($action) {
71 require_sesskey();
74 // Process actions.
75 switch ($action) {
77 case 'setstate':
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);
84 break;
86 case 'setapplyto':
87 if (isset($filters[$filterpath])) {
88 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
89 filter_set_applies_to_strings($filterpath, $applytostrings);
91 break;
93 case 'down':
94 if (isset($filters[$filterpath])) {
95 filter_set_global_state($filterpath, $filters[$filterpath]->active, 1);
97 break;
99 case 'up':
100 if (isset($filters[$filterpath])) {
101 $oldpos = $filters[$filterpath]->sortorder;
102 filter_set_global_state($filterpath, $filters[$filterpath]->active, -1);
104 break;
107 // Reset caches and return.
108 if ($action) {
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();
129 $lastactive = null;
130 foreach ($states as $state) {
131 if ($state->active != TEXTFILTER_DISABLED) {
132 $lastactive = $state->filter;
136 // Iterate through filters adding to display table.
137 $firstrow = true;
138 foreach ($states as $state) {
139 $filter = $state->filter;
140 if (!isset($plugininfos[$filter])) {
141 continue;
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';
149 } else {
150 $table->rowclasses[] = '';
152 $firstrow = false;
155 echo html_writer::table($table);
156 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
157 echo $OUTPUT->footer();
158 die;
162 * Return action URL.
164 * @param string $filterpath
165 * @param string $action
166 * @return moodle_url
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
183 * @return array data
185 function get_table_row(\core\plugininfo\filter $plugininfo, $state, $isfirstrow, $islastactive, $applytostrings) {
186 global $OUTPUT;
187 $row = array();
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'),
205 // Filter name.
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;
214 // Disable/off/on.
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);
219 // Re-order.
220 $updown = '';
221 $spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" />';
222 if ($state->active != TEXTFILTER_DISABLED) {
223 if (!$isfirstrow) {
224 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
225 } else {
226 $updown .= $spacer;
228 if (!$islastactive) {
229 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
230 } else {
231 $updown .= $spacer;
234 $row[] = $updown;
236 // Apply to strings.
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'));
245 } else {
246 $row[] = '';
249 // Uninstall.
250 $row[] = html_writer::link(filters_action_url($filter, 'delete'), get_string('uninstallplugin', 'core_admin'));
252 return $row;