MDL-41437 fix and cleanup filter uninstall
[moodle.git] / admin / filters.php
CommitLineData
f578af59 1<?php
828aeff2 2
456c8cc7 3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
828aeff2 25
456c8cc7 26/**
27 * Processes actions from the admin_setting_managefilters object (defined in
28 * adminlib.php).
29 *
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package administration
32 *//** */
33
34 require_once(dirname(__FILE__) . '/../config.php');
dcdf3b29 35 require_once($CFG->libdir . '/adminlib.php');
bde002b8 36 require_once($CFG->libdir . '/pluginlib.php');
456c8cc7 37
405aca35 38 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
0662bd67 39 $filterpath = optional_param('filterpath', '', PARAM_SAFEDIR);
828aeff2 40
220a90c5 41 require_login();
bf006d2c 42 $systemcontext = context_system::instance();
456c8cc7 43 require_capability('moodle/site:config', $systemcontext);
828aeff2 44
dcdf3b29 45 $returnurl = "$CFG->wwwroot/$CFG->admin/filters.php";
46 admin_externalpage_setup('managefilters');
5b127e24 47
456c8cc7 48 $filters = filter_get_global_states();
828aeff2 49
456c8cc7 50 // In case any new filters have been installed, but not put in the table yet.
51 $fitlernames = filter_get_all_installed();
52 $newfilters = $fitlernames;
53 foreach ($filters as $filter => $notused) {
54 unset($newfilters[$filter]);
828aeff2 55 }
5b127e24 56
dcdf3b29 57/// Process actions ============================================================
58
59 if ($action) {
bde002b8 60 if ($action !== 'delete' and !isset($filters[$filterpath]) and !isset($newfilters[$filterpath])) {
dcdf3b29 61 throw new moodle_exception('filternotinstalled', 'error', $returnurl, $filterpath);
62 }
63
64 if (!confirm_sesskey()) {
65 redirect($returnurl);
66 }
456c8cc7 67 }
5b127e24 68
220a90c5 69 switch ($action) {
70
456c8cc7 71 case 'setstate':
1e12c120 72 if ($newstate = optional_param('newstate', '', PARAM_INT)) {
456c8cc7 73 filter_set_global_state($filterpath, $newstate);
5b8fa09b 74 if ($newstate == TEXTFILTER_DISABLED) {
75 filter_set_applies_to_strings($filterpath, false);
76 }
456c8cc7 77 unset($newfilters[$filterpath]);
12afe279 78 }
220a90c5 79 break;
80
5b8fa09b 81 case 'setapplyto':
82 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
83 filter_set_applies_to_strings($filterpath, $applytostrings);
84 break;
85
220a90c5 86 case 'down':
456c8cc7 87 if (isset($filters[$filterpath])) {
0662bd67 88 filter_set_global_state($filterpath, $filters[$filterpath]->active, 1);
12afe279 89 }
220a90c5 90 break;
91
92 case 'up':
456c8cc7 93 if (isset($filters[$filterpath])) {
94 $oldpos = $filters[$filterpath]->sortorder;
0662bd67 95 filter_set_global_state($filterpath, $filters[$filterpath]->active, -1);
d1fd6426 96 }
220a90c5 97 break;
d1fd6426 98 }
99
456c8cc7 100 // Add any missing filters to the DB table.
101 foreach ($newfilters as $filter => $notused) {
102 filter_set_global_state($filter, TEXTFILTER_DISABLED);
103 }
104
105 // Reset caches and return
dcdf3b29 106 if ($action) {
bde002b8 107 plugin_manager::reset_caches();
dcdf3b29 108 reset_text_filters_cache();
109 redirect($returnurl);
110 }
111
112/// End of process actions =====================================================
113
114/// Print the page heading.
61ef8f9f 115 echo $OUTPUT->header();
2fff8846 116 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
dcdf3b29 117
118 $activechoices = array(
119 TEXTFILTER_DISABLED => get_string('disabled', 'filters'),
120 TEXTFILTER_OFF => get_string('offbutavailable', 'filters'),
121 TEXTFILTER_ON => get_string('on', 'filters'),
122 );
123 $applytochoices = array(
124 0 => get_string('content', 'filters'),
125 1 => get_string('contentandheadings', 'filters'),
126 );
127
128 $filters = filter_get_global_states();
129
130 // In case any new filters have been installed, but not put in the table yet.
131 $filternames = filter_get_all_installed();
132 $newfilters = $filternames;
133 foreach ($filters as $filter => $notused) {
134 unset($newfilters[$filter]);
135 }
136 $stringfilters = filter_get_string_filters();
137
414a4a91 138 $table = new html_table();
dcdf3b29 139 $table->head = array(get_string('filter'), get_string('isactive', 'filters'),
140 get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('delete'));
8be244b2
RW
141 $table->colclasses = array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
142 $table->attributes['class'] = 'admintable generaltable';
143 $table->id = 'filterssetting';
dcdf3b29 144 $table->data = array();
145
146 $lastactive = null;
147 foreach ($filters as $filter => $filterinfo) {
148 if ($filterinfo->active != TEXTFILTER_DISABLED) {
149 $lastactive = $filter;
150 }
151 }
152
153 // iterate through filters adding to display table
154 $firstrow = true;
155 foreach ($filters as $filter => $filterinfo) {
156 $applytostrings = isset($stringfilters[$filter]) && $filterinfo->active != TEXTFILTER_DISABLED;
157 $row = get_table_row($filterinfo, $firstrow, $filter == $lastactive, $applytostrings);
158 $table->data[] = $row;
159 if ($filterinfo->active == TEXTFILTER_DISABLED) {
704ac344 160 $table->rowclasses[] = 'dimmed_text';
dcdf3b29 161 } else {
704ac344 162 $table->rowclasses[] = '';
dcdf3b29 163 }
164 $firstrow = false;
165 }
166 foreach ($newfilters as $filter => $filtername) {
167 $filterinfo = new stdClass;
168 $filterinfo->filter = $filter;
169 $filterinfo->active = TEXTFILTER_DISABLED;
170 $row = get_table_row($filterinfo, false, false, false);
171 $table->data[] = $row;
704ac344 172 $table->rowclasses[] = 'dimmed_text';
dcdf3b29 173 }
174
16be8974 175 echo html_writer::table($table);
dcdf3b29 176 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
73d6f52f 177 echo $OUTPUT->footer();
dcdf3b29 178
179/// Display helper functions ===================================================
180
8ae8bf8a 181function filters_action_url($filterpath, $action) {
a31e0b40
PS
182 if ($action === 'delete') {
183 return new moodle_url('/admin/plugins.php', array('sesskey'=>sesskey(), 'uninstall'=>'filter_'.$filterpath));
184 }
0b2cb132 185 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
dcdf3b29 186}
187
dcdf3b29 188function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
1af9ee3d 189 global $CFG, $OUTPUT, $activechoices, $applytochoices, $filternames; //TODO: this is sloppy coding style!!
dcdf3b29 190 $row = array();
191 $filter = $filterinfo->filter;
192
193 // Filter name
194 if (!empty($filternames[$filter])) {
195 $row[] = $filternames[$filter];
196 } else {
197 $row[] = '<span class="error">' . get_string('filemissing', '', $filter) . '</span>';
198 }
199
200 // Disable/off/on
0662bd67 201 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $filterinfo->active, null, 'active' . $filter);
aa414527 202 $select->set_label(get_string('isactive', 'filters'), array('class' => 'accesshide'));
0b2cb132 203 $row[] = $OUTPUT->render($select);
dcdf3b29 204
205 // Re-order
206 $updown = '';
fa9c0aab 207 $spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" />';
dcdf3b29 208 if ($filterinfo->active != TEXTFILTER_DISABLED) {
209 if (!$isfirstrow) {
fa9c0aab 210 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
dcdf3b29 211 } else {
212 $updown .= $spacer;
213 }
214 if (!$islastactive) {
fa9c0aab 215 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
dcdf3b29 216 } else {
217 $updown .= $spacer;
218 }
219 }
220 $row[] = $updown;
221
222 // Apply to strings.
0662bd67 223 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
aa414527 224 $select->set_label(get_string('applyto', 'filters'), array('class' => 'accesshide'));
4784db9a 225 $select->disabled = $filterinfo->active == TEXTFILTER_DISABLED;
0b2cb132 226 $row[] = $OUTPUT->render($select);
dcdf3b29 227
228 // Settings link, if required
dcdf3b29 229 if (filter_has_global_settings($filter)) {
0662bd67 230 $row[] = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' . $filter . '">' . get_string('settings') . '</a>';
9434fef4 231 } else {
232 $row[] = '';
dcdf3b29 233 }
dcdf3b29 234
235 // Delete
0662bd67 236 $row[] = '<a href="' . filters_action_url($filter, 'delete') . '">' . get_string('delete') . '</a>';
dcdf3b29 237
238 return $row;
239}