Moodle 2.0.3 release
[moodle.git] / admin / filters.php
blob0d7faa3c0887c65a31ab70f56590cc957a058159
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_ACTION);
38 $filterpath = optional_param('filterpath', '', PARAM_PATH);
40 require_login();
41 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
42 require_capability('moodle/site:config', $systemcontext);
44 $returnurl = "$CFG->wwwroot/$CFG->admin/filters.php";
45 admin_externalpage_setup('managefilters');
47 $filters = filter_get_global_states();
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]);
56 /// Process actions ============================================================
58 if ($action) {
59 if (!isset($filters[$filterpath]) && !isset($newfilters[$filterpath])) {
60 throw new moodle_exception('filternotinstalled', 'error', $returnurl, $filterpath);
63 if (!confirm_sesskey()) {
64 redirect($returnurl);
68 switch ($action) {
70 case 'setstate':
71 if ($newstate = optional_param('newstate', '', PARAM_INTEGER)) {
72 filter_set_global_state($filterpath, $newstate);
73 if ($newstate == TEXTFILTER_DISABLED) {
74 filter_set_applies_to_strings($filterpath, false);
76 unset($newfilters[$filterpath]);
78 break;
80 case 'setapplyto':
81 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
82 filter_set_applies_to_strings($filterpath, $applytostrings);
83 break;
85 case 'down':
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);
92 break;
94 case 'up':
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);
101 break;
103 case 'delete':
104 if (!empty($filternames[$filterpath])) {
105 $filtername = $filternames[$filterpath];
106 } else {
107 $filtername = $filterpath;
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);
115 print_error('cannotdeletemodfilter', 'admin', $returnurl, $a);
118 // If not yet confirmed, display a confirmation message.
119 if (!optional_param('confirm', '', PARAM_BOOL)) {
120 $title = get_string('deletefilterareyousure', 'admin', $filtername);
121 echo $OUTPUT->header();
122 echo $OUTPUT->heading($title);
124 $linkcontinue = new moodle_url($returnurl, array('action' => 'delete', 'filterpath' => $filterpath, 'confirm' => 1));
125 $formcancel = new single_button(new moodle_url($returnurl), get_string('no'), 'get');
126 echo $OUTPUT->confirm(get_string('deletefilterareyousuremessage', 'admin', $filtername), $linkcontinue, $formcancel);
127 echo $OUTPUT->footer();
128 exit;
131 // Do the deletion.
132 $title = get_string('deletingfilter', 'admin', $filtername);
133 echo $OUTPUT->header();
134 echo $OUTPUT->heading($title);
136 // Delete all data for this plugin.
137 filter_delete_all_for_filter($filterpath);
139 $a = new stdClass;
140 $a->filter = $filtername;
141 $a->directory = $filterpath;
142 echo $OUTPUT->box(get_string('deletefilterfiles', 'admin', $a), 'generalbox', 'notice');
143 echo $OUTPUT->continue_button($returnurl);
144 echo $OUTPUT->footer();
145 exit;
148 // Add any missing filters to the DB table.
149 foreach ($newfilters as $filter => $notused) {
150 filter_set_global_state($filter, TEXTFILTER_DISABLED);
153 // Reset caches and return
154 if ($action) {
155 reset_text_filters_cache();
156 redirect($returnurl);
159 /// End of process actions =====================================================
161 /// Print the page heading.
162 echo $OUTPUT->header();
163 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
165 $activechoices = array(
166 TEXTFILTER_DISABLED => get_string('disabled', 'filters'),
167 TEXTFILTER_OFF => get_string('offbutavailable', 'filters'),
168 TEXTFILTER_ON => get_string('on', 'filters'),
170 $applytochoices = array(
171 0 => get_string('content', 'filters'),
172 1 => get_string('contentandheadings', 'filters'),
175 $filters = filter_get_global_states();
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]);
183 $stringfilters = filter_get_string_filters();
185 $table = new html_table();
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();
192 $lastactive = null;
193 foreach ($filters as $filter => $filterinfo) {
194 if ($filterinfo->active != TEXTFILTER_DISABLED) {
195 $lastactive = $filter;
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) {
206 $table->rowclasses[] = 'dimmed_text';
207 } else {
208 $table->rowclasses[] = '';
210 $firstrow = false;
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;
218 $table->rowclasses[] = 'dimmed_text';
221 echo html_writer::table($table);
222 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
223 echo $OUTPUT->footer();
225 /// Display helper functions ===================================================
227 function filters_action_url($filterpath, $action) {
228 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
231 function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
232 global $CFG, $OUTPUT, $activechoices, $applytochoices, $filternames; //TODO: this is sloppy coding style!!
233 $row = array();
234 $filter = $filterinfo->filter;
236 // Filter name
237 if (!empty($filternames[$filter])) {
238 $row[] = $filternames[$filter];
239 } else {
240 $row[] = '<span class="error">' . get_string('filemissing', '', $filter) . '</span>';
243 // Disable/off/on
244 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $filterinfo->active, null, 'active' . basename($filter));
245 $row[] = $OUTPUT->render($select);
247 // Re-order
248 $updown = '';
249 $spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';
250 if ($filterinfo->active != TEXTFILTER_DISABLED) {
251 if (!$isfirstrow) {
252 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up')));
253 } else {
254 $updown .= $spacer;
256 if (!$islastactive) {
257 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down')));
258 } else {
259 $updown .= $spacer;
262 $row[] = $updown;
264 // Apply to strings.
265 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . basename($filter));
266 $select->disabled = $filterinfo->active == TEXTFILTER_DISABLED;
267 $row[] = $OUTPUT->render($select);
269 // Settings link, if required
270 if (filter_has_global_settings($filter)) {
271 $row[] = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' .
272 str_replace('/', '',$filter) . '">' . get_string('settings') . '</a>';
273 } else {
274 $row[] = '';
277 // Delete
278 if (substr($filter, 0, 4) != 'mod/') {
279 $row[] = '<a href="' . filters_action_url($filter, 'delete') . '">' . get_string('delete') . '</a>';
280 } else {
281 $row[] = '';
284 return $row;