Merge branch 'w23_MDL-39882_m25_behatconfig' of git://github.com/skodak/moodle into...
[moodle.git] / admin / filters.php
blob6c585046ab9bc6c96fbd6e73b84c04f9193dcc37
1 <?php
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 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Processes actions from the admin_setting_managefilters object (defined in
28 * adminlib.php).
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package administration
32 *//** */
34 require_once(dirname(__FILE__) . '/../config.php');
35 require_once($CFG->libdir . '/adminlib.php');
37 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
38 $filterpath = optional_param('filterpath', '', PARAM_SAFEDIR);
40 require_login();
41 $systemcontext = context_system::instance();
42 require_capability('moodle/site:config', $systemcontext);
44 $returnurl = "$CFG->wwwroot/$CFG->admin/filters.php";
45 admin_externalpage_setup('managefilters');
47 // Purge all caches related to filter administration.
48 cache::make('core', 'plugininfo_filter')->purge();
50 $filters = filter_get_global_states();
52 // In case any new filters have been installed, but not put in the table yet.
53 $fitlernames = filter_get_all_installed();
54 $newfilters = $fitlernames;
55 foreach ($filters as $filter => $notused) {
56 unset($newfilters[$filter]);
59 /// Process actions ============================================================
61 if ($action) {
62 if (!isset($filters[$filterpath]) && !isset($newfilters[$filterpath])) {
63 throw new moodle_exception('filternotinstalled', 'error', $returnurl, $filterpath);
66 if (!confirm_sesskey()) {
67 redirect($returnurl);
71 switch ($action) {
73 case 'setstate':
74 if ($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);
79 unset($newfilters[$filterpath]);
81 break;
83 case 'setapplyto':
84 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
85 filter_set_applies_to_strings($filterpath, $applytostrings);
86 break;
88 case 'down':
89 if (isset($filters[$filterpath])) {
90 filter_set_global_state($filterpath, $filters[$filterpath]->active, 1);
92 break;
94 case 'up':
95 if (isset($filters[$filterpath])) {
96 $oldpos = $filters[$filterpath]->sortorder;
97 filter_set_global_state($filterpath, $filters[$filterpath]->active, -1);
99 break;
101 case 'delete':
102 // If not yet confirmed, display a confirmation message.
103 if (!optional_param('confirm', '', PARAM_BOOL)) {
104 $filtername = filter_get_name($filterpath);
106 $title = get_string('deletefilterareyousure', 'admin', $filtername);
107 echo $OUTPUT->header();
108 echo $OUTPUT->heading($title);
110 $linkcontinue = new moodle_url($returnurl, array('action' => 'delete', 'filterpath' => $filterpath, 'confirm' => 1));
111 $formcancel = new single_button(new moodle_url($returnurl), get_string('no'), 'get');
112 echo $OUTPUT->confirm(get_string('deletefilterareyousuremessage', 'admin', $filtername), $linkcontinue, $formcancel);
113 echo $OUTPUT->footer();
114 exit;
117 // Do the deletion.
118 $title = get_string('deletingfilter', 'admin', $filterpath);
119 echo $OUTPUT->header();
120 echo $OUTPUT->heading($title);
122 // Delete all data for this plugin.
123 filter_delete_all_for_filter($filterpath);
125 $a = new stdClass;
126 $a->filter = $filterpath;
127 $a->directory = "$CFG->dirroot/filter/$filterpath";
128 echo $OUTPUT->box(get_string('deletefilterfiles', 'admin', $a), 'generalbox', 'notice');
129 echo $OUTPUT->continue_button($returnurl);
130 echo $OUTPUT->footer();
131 exit;
134 // Add any missing filters to the DB table.
135 foreach ($newfilters as $filter => $notused) {
136 filter_set_global_state($filter, TEXTFILTER_DISABLED);
139 // Reset caches and return
140 if ($action) {
141 reset_text_filters_cache();
142 redirect($returnurl);
145 /// End of process actions =====================================================
147 /// Print the page heading.
148 echo $OUTPUT->header();
149 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
151 $activechoices = array(
152 TEXTFILTER_DISABLED => get_string('disabled', 'filters'),
153 TEXTFILTER_OFF => get_string('offbutavailable', 'filters'),
154 TEXTFILTER_ON => get_string('on', 'filters'),
156 $applytochoices = array(
157 0 => get_string('content', 'filters'),
158 1 => get_string('contentandheadings', 'filters'),
161 $filters = filter_get_global_states();
163 // In case any new filters have been installed, but not put in the table yet.
164 $filternames = filter_get_all_installed();
165 $newfilters = $filternames;
166 foreach ($filters as $filter => $notused) {
167 unset($newfilters[$filter]);
169 $stringfilters = filter_get_string_filters();
171 $table = new html_table();
172 $table->head = array(get_string('filter'), get_string('isactive', 'filters'),
173 get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('delete'));
174 $table->colclasses = array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
175 $table->attributes['class'] = 'admintable generaltable';
176 $table->id = 'filterssetting';
177 $table->data = array();
179 $lastactive = null;
180 foreach ($filters as $filter => $filterinfo) {
181 if ($filterinfo->active != TEXTFILTER_DISABLED) {
182 $lastactive = $filter;
186 // iterate through filters adding to display table
187 $firstrow = true;
188 foreach ($filters as $filter => $filterinfo) {
189 $applytostrings = isset($stringfilters[$filter]) && $filterinfo->active != TEXTFILTER_DISABLED;
190 $row = get_table_row($filterinfo, $firstrow, $filter == $lastactive, $applytostrings);
191 $table->data[] = $row;
192 if ($filterinfo->active == TEXTFILTER_DISABLED) {
193 $table->rowclasses[] = 'dimmed_text';
194 } else {
195 $table->rowclasses[] = '';
197 $firstrow = false;
199 foreach ($newfilters as $filter => $filtername) {
200 $filterinfo = new stdClass;
201 $filterinfo->filter = $filter;
202 $filterinfo->active = TEXTFILTER_DISABLED;
203 $row = get_table_row($filterinfo, false, false, false);
204 $table->data[] = $row;
205 $table->rowclasses[] = 'dimmed_text';
208 echo html_writer::table($table);
209 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
210 echo $OUTPUT->footer();
212 /// Display helper functions ===================================================
214 function filters_action_url($filterpath, $action) {
215 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
218 function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
219 global $CFG, $OUTPUT, $activechoices, $applytochoices, $filternames; //TODO: this is sloppy coding style!!
220 $row = array();
221 $filter = $filterinfo->filter;
223 // Filter name
224 if (!empty($filternames[$filter])) {
225 $row[] = $filternames[$filter];
226 } else {
227 $row[] = '<span class="error">' . get_string('filemissing', '', $filter) . '</span>';
230 // Disable/off/on
231 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $filterinfo->active, null, 'active' . $filter);
232 $select->set_label(get_string('isactive', 'filters'), array('class' => 'accesshide'));
233 $row[] = $OUTPUT->render($select);
235 // Re-order
236 $updown = '';
237 $spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" />';
238 if ($filterinfo->active != TEXTFILTER_DISABLED) {
239 if (!$isfirstrow) {
240 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
241 } else {
242 $updown .= $spacer;
244 if (!$islastactive) {
245 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
246 } else {
247 $updown .= $spacer;
250 $row[] = $updown;
252 // Apply to strings.
253 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
254 $select->set_label(get_string('applyto', 'filters'), array('class' => 'accesshide'));
255 $select->disabled = $filterinfo->active == TEXTFILTER_DISABLED;
256 $row[] = $OUTPUT->render($select);
258 // Settings link, if required
259 if (filter_has_global_settings($filter)) {
260 $row[] = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' . $filter . '">' . get_string('settings') . '</a>';
261 } else {
262 $row[] = '';
265 // Delete
266 $row[] = '<a href="' . filters_action_url($filter, 'delete') . '">' . get_string('delete') . '</a>';
268 return $row;