MDL-20204 added forgotten support for disabled in single_select
[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');
456c8cc7 36
37 $action = optional_param('action', '', PARAM_ACTION);
220a90c5 38 $filterpath = optional_param('filterpath', '', PARAM_PATH);
828aeff2 39
220a90c5 40 require_login();
456c8cc7 41 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
42 require_capability('moodle/site:config', $systemcontext);
828aeff2 43
dcdf3b29 44 $returnurl = "$CFG->wwwroot/$CFG->admin/filters.php";
45 admin_externalpage_setup('managefilters');
5b127e24 46
456c8cc7 47 $filters = filter_get_global_states();
828aeff2 48
456c8cc7 49 // In case any new filters have been installed, but not put in the table yet.
50 $fitlernames = filter_get_all_installed();
51 $newfilters = $fitlernames;
52 foreach ($filters as $filter => $notused) {
53 unset($newfilters[$filter]);
828aeff2 54 }
5b127e24 55
dcdf3b29 56/// Process actions ============================================================
57
58 if ($action) {
59 if (!isset($filters[$filterpath]) && !isset($newfilters[$filterpath])) {
60 throw new moodle_exception('filternotinstalled', 'error', $returnurl, $filterpath);
61 }
62
63 if (!confirm_sesskey()) {
64 redirect($returnurl);
65 }
456c8cc7 66 }
5b127e24 67
220a90c5 68 switch ($action) {
69
456c8cc7 70 case 'setstate':
71 if ($newstate = optional_param('newstate', '', PARAM_INTEGER)) {
72 filter_set_global_state($filterpath, $newstate);
5b8fa09b 73 if ($newstate == TEXTFILTER_DISABLED) {
74 filter_set_applies_to_strings($filterpath, false);
75 }
456c8cc7 76 unset($newfilters[$filterpath]);
12afe279 77 }
220a90c5 78 break;
79
5b8fa09b 80 case 'setapplyto':
81 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
82 filter_set_applies_to_strings($filterpath, $applytostrings);
83 break;
84
220a90c5 85 case 'down':
456c8cc7 86 if (isset($filters[$filterpath])) {
87 $oldpos = $filters[$filterpath]->sortorder;
88 if ($oldpos <= count($filters)) {
89 filter_set_global_state($filterpath, $filters[$filterpath]->active, $oldpos + 1);
90 }
12afe279 91 }
220a90c5 92 break;
93
94 case 'up':
456c8cc7 95 if (isset($filters[$filterpath])) {
96 $oldpos = $filters[$filterpath]->sortorder;
97 if ($oldpos >= 1) {
98 filter_set_global_state($filterpath, $filters[$filterpath]->active, $oldpos - 1);
99 }
d1fd6426 100 }
220a90c5 101 break;
456c8cc7 102
103 case 'delete':
86b455cf 104 if (!empty($filternames[$filterpath])) {
105 $filtername = $filternames[$filterpath];
106 } else {
107 $filtername = $filterpath;
108 }
109
456c8cc7 110 if (substr($filterpath, 0, 4) == 'mod/') {
111 $mod = basename($filterpath);
112 $a = new stdClass;
113 $a->filter = $filtername;
114 $a->module = get_string('modulename', $mod);
9434fef4 115 print_error('cannotdeletemodfilter', 'admin', $returnurl, $a);
456c8cc7 116 }
117
118 // If not yet confirmed, display a confirmation message.
119 if (!optional_param('confirm', '', PARAM_BOOL)) {
120 $title = get_string('deletefilterareyousure', 'admin', $filtername);
dcdf3b29 121 admin_externalpage_print_header();
2fff8846 122 echo $OUTPUT->heading($title);
20207b82 123
414a4a91 124 $linkcontinue = new moodle_url($returnurl, array('action' => 'delete', 'filterpath' => $filterpath, 'confirm' => 1));
0b2cb132 125 $formcancel = new single_button(new moodle_url($returnurl), get_string('no'), 'get');
414a4a91 126 echo $OUTPUT->confirm(get_string('deletefilterareyousuremessage', 'admin', $filtername), $linkcontinue, $formcancel);
73d6f52f 127 echo $OUTPUT->footer();
456c8cc7 128 exit;
129 }
130
131 // Do the deletion.
132 $title = get_string('deletingfilter', 'admin', $filtername);
dcdf3b29 133 admin_externalpage_print_header();
2fff8846 134 echo $OUTPUT->heading($title);
456c8cc7 135
136 // Delete all data for this plugin.
9434fef4 137 filter_delete_all_for_filter($filterpath);
456c8cc7 138
139 $a = new stdClass;
86b455cf 140 $a->filter = $filtername;
141 $a->directory = $filterpath;
20486a5a 142 echo $OUTPUT->box(get_string('deletefilterfiles', 'admin', $a), 'generalbox', 'notice');
8fbce1c8 143 echo $OUTPUT->continue_button($returnurl);
73d6f52f 144 echo $OUTPUT->footer();
456c8cc7 145 exit;
d1fd6426 146 }
147
456c8cc7 148 // Add any missing filters to the DB table.
149 foreach ($newfilters as $filter => $notused) {
150 filter_set_global_state($filter, TEXTFILTER_DISABLED);
151 }
152
153 // Reset caches and return
dcdf3b29 154 if ($action) {
155 reset_text_filters_cache();
156 redirect($returnurl);
157 }
158
159/// End of process actions =====================================================
160
161/// Print the page heading.
162 admin_externalpage_print_header();
2fff8846 163 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
dcdf3b29 164
165 $activechoices = array(
166 TEXTFILTER_DISABLED => get_string('disabled', 'filters'),
167 TEXTFILTER_OFF => get_string('offbutavailable', 'filters'),
168 TEXTFILTER_ON => get_string('on', 'filters'),
169 );
170 $applytochoices = array(
171 0 => get_string('content', 'filters'),
172 1 => get_string('contentandheadings', 'filters'),
173 );
174
175 $filters = filter_get_global_states();
176
177 // In case any new filters have been installed, but not put in the table yet.
178 $filternames = filter_get_all_installed();
179 $newfilters = $filternames;
180 foreach ($filters as $filter => $notused) {
181 unset($newfilters[$filter]);
182 }
183 $stringfilters = filter_get_string_filters();
184
414a4a91 185 $table = new html_table();
dcdf3b29 186 $table->head = array(get_string('filter'), get_string('isactive', 'filters'),
187 get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('delete'));
188 $table->align = array('left', 'left', 'center', 'left', 'left');
189 $table->width = '100%';
190 $table->data = array();
191
192 $lastactive = null;
193 foreach ($filters as $filter => $filterinfo) {
194 if ($filterinfo->active != TEXTFILTER_DISABLED) {
195 $lastactive = $filter;
196 }
197 }
198
199 // iterate through filters adding to display table
200 $firstrow = true;
201 foreach ($filters as $filter => $filterinfo) {
202 $applytostrings = isset($stringfilters[$filter]) && $filterinfo->active != TEXTFILTER_DISABLED;
203 $row = get_table_row($filterinfo, $firstrow, $filter == $lastactive, $applytostrings);
204 $table->data[] = $row;
205 if ($filterinfo->active == TEXTFILTER_DISABLED) {
704ac344 206 $table->rowclasses[] = 'dimmed_text';
dcdf3b29 207 } else {
704ac344 208 $table->rowclasses[] = '';
dcdf3b29 209 }
210 $firstrow = false;
211 }
212 foreach ($newfilters as $filter => $filtername) {
213 $filterinfo = new stdClass;
214 $filterinfo->filter = $filter;
215 $filterinfo->active = TEXTFILTER_DISABLED;
216 $row = get_table_row($filterinfo, false, false, false);
217 $table->data[] = $row;
704ac344 218 $table->rowclasses[] = 'dimmed_text';
dcdf3b29 219 }
220
414a4a91 221 echo $OUTPUT->table($table);
dcdf3b29 222 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
73d6f52f 223 echo $OUTPUT->footer();
dcdf3b29 224
225/// Display helper functions ===================================================
226
8ae8bf8a 227function filters_action_url($filterpath, $action) {
0b2cb132 228 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
dcdf3b29 229}
230
dcdf3b29 231function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
d128a46d 232 global $CFG, $OUTPUT, $activechoices, $applytochoices, $filternames;
dcdf3b29 233 $row = array();
234 $filter = $filterinfo->filter;
235
236 // Filter name
237 if (!empty($filternames[$filter])) {
238 $row[] = $filternames[$filter];
239 } else {
240 $row[] = '<span class="error">' . get_string('filemissing', '', $filter) . '</span>';
241 }
242
243 // Disable/off/on
0b2cb132
PS
244 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $filterinfo->active, null, 'active' . basename($filter));
245 $row[] = $OUTPUT->render($select);
dcdf3b29 246
247 // Re-order
248 $updown = '';
b5d0cafc 249 $spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';
dcdf3b29 250 if ($filterinfo->active != TEXTFILTER_DISABLED) {
251 if (!$isfirstrow) {
8ae8bf8a 252 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), get_string('up'), 't/up');
dcdf3b29 253 } else {
254 $updown .= $spacer;
255 }
256 if (!$islastactive) {
8ae8bf8a 257 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), get_string('down'), 't/down');
dcdf3b29 258 } else {
259 $updown .= $spacer;
260 }
261 }
262 $row[] = $updown;
263
264 // Apply to strings.
0b2cb132 265 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . basename($filter));
4784db9a 266 $select->disabled = $filterinfo->active == TEXTFILTER_DISABLED;
0b2cb132 267 $row[] = $OUTPUT->render($select);
dcdf3b29 268
269 // Settings link, if required
dcdf3b29 270 if (filter_has_global_settings($filter)) {
9434fef4 271 $row[] = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' .
dcdf3b29 272 str_replace('/', '',$filter) . '">' . get_string('settings') . '</a>';
9434fef4 273 } else {
274 $row[] = '';
dcdf3b29 275 }
dcdf3b29 276
277 // Delete
9434fef4 278 if (substr($filter, 0, 4) != 'mod/') {
8ae8bf8a 279 $row[] = '<a href="' . filters_action_url($filter, 'delete') . '">' . get_string('delete') . '</a>';
9434fef4 280 } else {
281 $row[] = '';
282 }
dcdf3b29 283
284 return $row;
285}