Merge branch 'install_310_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / admin / filters.php
blob958fc6e16cac86fcdd6f313e8c890e12a9121833
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 filter_set_global_state($filterpath, $newstate);
76 if ($newstate == TEXTFILTER_DISABLED) {
77 filter_set_applies_to_strings($filterpath, false);
80 break;
82 case 'setapplyto':
83 if (isset($filters[$filterpath])) {
84 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
85 filter_set_applies_to_strings($filterpath, $applytostrings);
87 break;
89 case 'down':
90 if (isset($filters[$filterpath])) {
91 filter_set_global_state($filterpath, $filters[$filterpath]->active, 1);
93 break;
95 case 'up':
96 if (isset($filters[$filterpath])) {
97 $oldpos = $filters[$filterpath]->sortorder;
98 filter_set_global_state($filterpath, $filters[$filterpath]->active, -1);
100 break;
103 // Reset caches and return.
104 if ($action) {
105 reset_text_filters_cache();
106 core_plugin_manager::reset_caches();
107 redirect(new moodle_url('/admin/filters.php'));
110 // Print the page heading.
111 echo $OUTPUT->header();
112 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
114 $states = filter_get_global_states();
115 $stringfilters = filter_get_string_filters();
117 $table = new html_table();
118 $table->head = array(get_string('filter'), get_string('isactive', 'filters'),
119 get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('uninstallplugin', 'core_admin'));
120 $table->colclasses = array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
121 $table->attributes['class'] = 'admintable generaltable';
122 $table->id = 'filterssetting';
123 $table->data = array();
125 $lastactive = null;
126 foreach ($states as $state) {
127 if ($state->active != TEXTFILTER_DISABLED) {
128 $lastactive = $state->filter;
132 // Iterate through filters adding to display table.
133 $firstrow = true;
134 foreach ($states as $state) {
135 $filter = $state->filter;
136 if (!isset($plugininfos[$filter])) {
137 continue;
139 $plugininfo = $plugininfos[$filter];
140 $applytostrings = isset($stringfilters[$filter]) && $state->active != TEXTFILTER_DISABLED;
141 $row = get_table_row($plugininfo, $state, $firstrow, $filter == $lastactive, $applytostrings);
142 $table->data[] = $row;
143 if ($state->active == TEXTFILTER_DISABLED) {
144 $table->rowclasses[] = 'dimmed_text';
145 } else {
146 $table->rowclasses[] = '';
148 $firstrow = false;
151 echo html_writer::table($table);
152 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
153 echo $OUTPUT->footer();
154 die;
158 * Return action URL.
160 * @param string $filterpath
161 * @param string $action
162 * @return moodle_url
164 function filters_action_url($filterpath, $action) {
165 if ($action === 'delete') {
166 return core_plugin_manager::instance()->get_uninstall_url('filter_'.$filterpath, 'manage');
168 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
172 * Construct table record.
174 * @param \core\plugininfo\filter $plugininfo
175 * @param stdClass $state
176 * @param bool $isfirstrow
177 * @param bool $islastactive
178 * @param bool $applytostrings
179 * @return array data
181 function get_table_row(\core\plugininfo\filter $plugininfo, $state, $isfirstrow, $islastactive, $applytostrings) {
182 global $OUTPUT;
183 $row = array();
184 $filter = $state->filter;
185 $active = $plugininfo->is_installed_and_upgraded();
187 static $activechoices;
188 static $applytochoices;
189 if (!isset($activechoices)) {
190 $activechoices = array(
191 TEXTFILTER_DISABLED => get_string('disabled', 'core_filters'),
192 TEXTFILTER_OFF => get_string('offbutavailable', 'core_filters'),
193 TEXTFILTER_ON => get_string('on', 'core_filters'),
195 $applytochoices = array(
196 0 => get_string('content', 'core_filters'),
197 1 => get_string('contentandheadings', 'core_filters'),
201 // Filter name.
202 $displayname = $plugininfo->displayname;
203 if (!$plugininfo->rootdir) {
204 $displayname = '<span class="error">' . $displayname . ' - ' . get_string('status_missing', 'core_plugin') . '</span>';
205 } else if (!$active) {
206 $displayname = '<span class="error">' . $displayname . ' - ' . get_string('error') . '</span>';
208 $row[] = $displayname;
210 // Disable/off/on.
211 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $state->active, null, 'active' . $filter);
212 $select->set_label(get_string('isactive', 'filters'), array('class' => 'accesshide'));
213 $row[] = $OUTPUT->render($select);
215 // Re-order.
216 $updown = '';
217 $spacer = $OUTPUT->spacer();
218 if ($state->active != TEXTFILTER_DISABLED) {
219 if (!$isfirstrow) {
220 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
221 } else {
222 $updown .= $spacer;
224 if (!$islastactive) {
225 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
226 } else {
227 $updown .= $spacer;
230 $row[] = $updown;
232 // Apply to strings.
233 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
234 $select->set_label(get_string('applyto', 'filters'), array('class' => 'accesshide'));
235 $select->disabled = ($state->active == TEXTFILTER_DISABLED);
236 $row[] = $OUTPUT->render($select);
238 // Settings link, if required.
239 if ($active and filter_has_global_settings($filter)) {
240 $row[] = html_writer::link(new moodle_url('/admin/settings.php', array('section'=>'filtersetting'.$filter)), get_string('settings'));
241 } else {
242 $row[] = '';
245 // Uninstall.
246 $row[] = html_writer::link(filters_action_url($filter, 'delete'), get_string('uninstallplugin', 'core_admin'));
248 return $row;