MDL-42091 atto - fixing on hover and active states of buttons
[moodle.git] / admin / filters.php
blob05c463ab419193e9942c670740374e06521fc958
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');
36 require_once($CFG->libdir . '/pluginlib.php');
38 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
39 $filterpath = optional_param('filterpath', '', PARAM_SAFEDIR);
41 require_login();
42 $systemcontext = context_system::instance();
43 require_capability('moodle/site:config', $systemcontext);
45 $returnurl = "$CFG->wwwroot/$CFG->admin/filters.php";
46 admin_externalpage_setup('managefilters');
48 $filters = filter_get_global_states();
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]);
57 /// Process actions ============================================================
59 if ($action) {
60 if ($action !== 'delete' and !isset($filters[$filterpath]) and !isset($newfilters[$filterpath])) {
61 throw new moodle_exception('filternotinstalled', 'error', $returnurl, $filterpath);
64 if (!confirm_sesskey()) {
65 redirect($returnurl);
69 switch ($action) {
71 case 'setstate':
72 if ($newstate = optional_param('newstate', '', PARAM_INT)) {
73 filter_set_global_state($filterpath, $newstate);
74 if ($newstate == TEXTFILTER_DISABLED) {
75 filter_set_applies_to_strings($filterpath, false);
77 unset($newfilters[$filterpath]);
79 break;
81 case 'setapplyto':
82 $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
83 filter_set_applies_to_strings($filterpath, $applytostrings);
84 break;
86 case 'down':
87 if (isset($filters[$filterpath])) {
88 filter_set_global_state($filterpath, $filters[$filterpath]->active, 1);
90 break;
92 case 'up':
93 if (isset($filters[$filterpath])) {
94 $oldpos = $filters[$filterpath]->sortorder;
95 filter_set_global_state($filterpath, $filters[$filterpath]->active, -1);
97 break;
100 // Add any missing filters to the DB table.
101 foreach ($newfilters as $filter => $notused) {
102 filter_set_global_state($filter, TEXTFILTER_DISABLED);
105 // Reset caches and return
106 if ($action) {
107 plugin_manager::reset_caches();
108 reset_text_filters_cache();
109 redirect($returnurl);
112 /// End of process actions =====================================================
114 /// Print the page heading.
115 echo $OUTPUT->header();
116 echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
118 $activechoices = array(
119 TEXTFILTER_DISABLED => get_string('disabled', 'filters'),
120 TEXTFILTER_OFF => get_string('offbutavailable', 'filters'),
121 TEXTFILTER_ON => get_string('on', 'filters'),
123 $applytochoices = array(
124 0 => get_string('content', 'filters'),
125 1 => get_string('contentandheadings', 'filters'),
128 $filters = filter_get_global_states();
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]);
136 $stringfilters = filter_get_string_filters();
138 $table = new html_table();
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'));
141 $table->colclasses = array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
142 $table->attributes['class'] = 'admintable generaltable';
143 $table->id = 'filterssetting';
144 $table->data = array();
146 $lastactive = null;
147 foreach ($filters as $filter => $filterinfo) {
148 if ($filterinfo->active != TEXTFILTER_DISABLED) {
149 $lastactive = $filter;
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) {
160 $table->rowclasses[] = 'dimmed_text';
161 } else {
162 $table->rowclasses[] = '';
164 $firstrow = false;
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;
172 $table->rowclasses[] = 'dimmed_text';
175 echo html_writer::table($table);
176 echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
177 echo $OUTPUT->footer();
179 /// Display helper functions ===================================================
181 function filters_action_url($filterpath, $action) {
182 if ($action === 'delete') {
183 return new moodle_url('/admin/plugins.php', array('sesskey'=>sesskey(), 'uninstall'=>'filter_'.$filterpath));
185 return new moodle_url('/admin/filters.php', array('sesskey'=>sesskey(), 'filterpath'=>$filterpath, 'action'=>$action));
188 function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
189 global $CFG, $OUTPUT, $activechoices, $applytochoices, $filternames; //TODO: this is sloppy coding style!!
190 $row = array();
191 $filter = $filterinfo->filter;
193 // Filter name
194 if (!empty($filternames[$filter])) {
195 $row[] = $filternames[$filter];
196 } else {
197 $row[] = '<span class="error">' . get_string('filemissing', '', $filter) . '</span>';
200 // Disable/off/on
201 $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $filterinfo->active, null, 'active' . $filter);
202 $select->set_label(get_string('isactive', 'filters'), array('class' => 'accesshide'));
203 $row[] = $OUTPUT->render($select);
205 // Re-order
206 $updown = '';
207 $spacer = '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" />';
208 if ($filterinfo->active != TEXTFILTER_DISABLED) {
209 if (!$isfirstrow) {
210 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'), new pix_icon('t/up', get_string('up'), '', array('class' => 'iconsmall')));
211 } else {
212 $updown .= $spacer;
214 if (!$islastactive) {
215 $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'), new pix_icon('t/down', get_string('down'), '', array('class' => 'iconsmall')));
216 } else {
217 $updown .= $spacer;
220 $row[] = $updown;
222 // Apply to strings.
223 $select = new single_select(filters_action_url($filter, 'setapplyto'), 'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
224 $select->set_label(get_string('applyto', 'filters'), array('class' => 'accesshide'));
225 $select->disabled = $filterinfo->active == TEXTFILTER_DISABLED;
226 $row[] = $OUTPUT->render($select);
228 // Settings link, if required
229 if (filter_has_global_settings($filter)) {
230 $row[] = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' . $filter . '">' . get_string('settings') . '</a>';
231 } else {
232 $row[] = '';
235 // Delete
236 $row[] = '<a href="' . filters_action_url($filter, 'delete') . '">' . get_string('delete') . '</a>';
238 return $row;