filters.php 11 KB
Newer Older
thepurpleblob's avatar
thepurpleblob committed
1
<?php // $Id$
moodler's avatar
moodler committed
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
///////////////////////////////////////////////////////////////////////////
//                                                                       //
// NOTICE OF COPYRIGHT                                                   //
//                                                                       //
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
//          http://moodle.org                                            //
//                                                                       //
// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
//                                                                       //
// This program is free software; you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by  //
// the Free Software Foundation; either version 2 of the License, or     //
// (at your option) any later version.                                   //
//                                                                       //
// This program is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
// GNU General Public License for more details:                          //
//                                                                       //
//          http://www.gnu.org/copyleft/gpl.html                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
moodler's avatar
moodler committed
25

26
27
28
29
30
31
32
33
34
/**
 * Processes actions from the admin_setting_managefilters object (defined in
 * adminlib.php).
 *
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package administration
 *//** */

    require_once(dirname(__FILE__) . '/../config.php');
35
    require_once($CFG->libdir . '/adminlib.php');
36
37

    $action = optional_param('action', '', PARAM_ACTION);
38
    $filterpath = optional_param('filterpath', '', PARAM_PATH);
moodler's avatar
moodler committed
39

40
    require_login();
41
42
    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
    require_capability('moodle/site:config', $systemcontext);
moodler's avatar
moodler committed
43

44
45
    $returnurl = "$CFG->wwwroot/$CFG->admin/filters.php";
    admin_externalpage_setup('managefilters');
46

47
    $filters = filter_get_global_states();
moodler's avatar
moodler committed
48

49
50
51
52
53
    // In case any new filters have been installed, but not put in the table yet.
    $fitlernames = filter_get_all_installed();
    $newfilters = $fitlernames;
    foreach ($filters as $filter => $notused) {
        unset($newfilters[$filter]);
moodler's avatar
moodler committed
54
    }
55

56
57
58
59
60
61
62
63
64
65
/// Process actions ============================================================

    if ($action) {
        if (!isset($filters[$filterpath]) && !isset($newfilters[$filterpath])) {
            throw new moodle_exception('filternotinstalled', 'error', $returnurl, $filterpath);
        }

        if (!confirm_sesskey()) {
            redirect($returnurl);
        }
66
    }
67

68
69
    switch ($action) {

70
71
72
    case 'setstate':
        if ($newstate = optional_param('newstate', '', PARAM_INTEGER)) {
            filter_set_global_state($filterpath, $newstate);
73
74
75
            if ($newstate == TEXTFILTER_DISABLED) {
                filter_set_applies_to_strings($filterpath, false);
            }
76
            unset($newfilters[$filterpath]);
skodak's avatar
skodak committed
77
        }
78
79
        break;

80
81
82
83
84
    case 'setapplyto':
        $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
        filter_set_applies_to_strings($filterpath, $applytostrings);
        break;

85
    case 'down':
86
87
88
89
90
        if (isset($filters[$filterpath])) {
            $oldpos = $filters[$filterpath]->sortorder;
            if ($oldpos <= count($filters)) {
                filter_set_global_state($filterpath, $filters[$filterpath]->active, $oldpos + 1);
            }
skodak's avatar
skodak committed
91
        }
92
93
94
        break;

    case 'up':
95
96
97
98
99
        if (isset($filters[$filterpath])) {
            $oldpos = $filters[$filterpath]->sortorder;
            if ($oldpos >= 1) {
                filter_set_global_state($filterpath, $filters[$filterpath]->active, $oldpos - 1);
            }
moodler's avatar
moodler committed
100
        }
101
        break;
102
103

    case 'delete':
104
105
106
107
108
109
        if (!empty($filternames[$filterpath])) {
            $filtername = $filternames[$filterpath];
        } else {
            $filtername = $filterpath;
        }

110
111
112
113
114
        if (substr($filterpath, 0, 4) == 'mod/') {
            $mod = basename($filterpath);
            $a = new stdClass;
            $a->filter = $filtername;
            $a->module = get_string('modulename', $mod);
115
            print_error('cannotdeletemodfilter', 'admin', $returnurl, $a);
116
117
118
119
120
        }

        // If not yet confirmed, display a confirmation message.
        if (!optional_param('confirm', '', PARAM_BOOL)) {
            $title = get_string('deletefilterareyousure', 'admin', $filtername);
121
            admin_externalpage_print_header();
122
            echo $OUTPUT->heading($title);
tjhunt's avatar
tjhunt committed
123
            notice_yesno(get_string('deletefilterareyousuremessage', 'admin', $filtername), $returnurl .
124
                    '?action=delete&amp;filterpath=' . $filterpath . '&amp;confirm=1&amp;sesskey=' . sesskey(),
125
                    $returnurl, NULL, NULL, 'post', 'get');
126
            echo $OUTPUT->footer();
127
128
129
130
131
            exit;
        }

        // Do the deletion.
        $title = get_string('deletingfilter', 'admin', $filtername);
132
        admin_externalpage_print_header();
133
        echo $OUTPUT->heading($title);
134
135

        // Delete all data for this plugin.
136
        filter_delete_all_for_filter($filterpath);
137
138

        $a = new stdClass;
139
140
        $a->filter = $filtername;
        $a->directory = $filterpath;
141
142
        print_box(get_string('deletefilterfiles', 'admin', $a), 'generalbox', 'notice');
        print_continue($returnurl);
143
        echo $OUTPUT->footer();
144
        exit;
moodler's avatar
moodler committed
145
146
    }

147
148
149
150
151
152
    // Add any missing filters to the DB table.
    foreach ($newfilters as $filter => $notused) {
        filter_set_global_state($filter, TEXTFILTER_DISABLED);
    }

    // Reset caches and return
153
154
155
156
157
158
159
160
161
    if ($action) {
        reset_text_filters_cache();
        redirect($returnurl);
    }

/// End of process actions =====================================================

/// Print the page heading.
    admin_externalpage_print_header();
162
    echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

    $activechoices = array(
        TEXTFILTER_DISABLED => get_string('disabled', 'filters'),
        TEXTFILTER_OFF => get_string('offbutavailable', 'filters'),
        TEXTFILTER_ON => get_string('on', 'filters'),
    );
    $applytochoices = array(
        0 => get_string('content', 'filters'),
        1 => get_string('contentandheadings', 'filters'),
    );

    $filters = filter_get_global_states();

    // In case any new filters have been installed, but not put in the table yet.
    $filternames = filter_get_all_installed();
    $newfilters = $filternames;
    foreach ($filters as $filter => $notused) {
        unset($newfilters[$filter]);
    }
    $stringfilters = filter_get_string_filters();

    $table = new object();
    $table->head  = array(get_string('filter'), get_string('isactive', 'filters'),
            get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('delete'));
    $table->align = array('left', 'left', 'center', 'left', 'left');
    $table->width = '100%';
    $table->data  = array();

    $lastactive = null;
    foreach ($filters as $filter => $filterinfo) {
        if ($filterinfo->active != TEXTFILTER_DISABLED) {
            $lastactive = $filter;
        }
    }

    // iterate through filters adding to display table
    $firstrow = true;
    foreach ($filters as $filter => $filterinfo) {
        $applytostrings = isset($stringfilters[$filter]) && $filterinfo->active != TEXTFILTER_DISABLED;
        $row = get_table_row($filterinfo, $firstrow, $filter == $lastactive, $applytostrings);
        $table->data[] = $row;
        if ($filterinfo->active == TEXTFILTER_DISABLED) {
205
            $table->rowclasses[] = 'dimmed_text';
206
        } else {
207
            $table->rowclasses[] = '';
208
209
210
211
212
213
214
215
216
        }
        $firstrow = false;
    }
    foreach ($newfilters as $filter => $filtername) {
        $filterinfo = new stdClass;
        $filterinfo->filter = $filter;
        $filterinfo->active = TEXTFILTER_DISABLED;
        $row = get_table_row($filterinfo, false, false, false);
        $table->data[] = $row;
217
        $table->rowclasses[] = 'dimmed_text';
218
219
220
221
    }

    print_table($table);
    echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
222
    echo $OUTPUT->footer();
223
224
225
226
227
228
229
230
231
232

/// Display helper functions ===================================================

function action_url($filterpath, $action) {
    global $returnurl;
    return $returnurl . '?sesskey=' . sesskey() . '&amp;filterpath=' .
            urlencode($filterpath) . '&amp;action=' . $action;
}

function action_icon($url, $icon, $straction) {
233
    global $CFG, $OUTPUT;
234
    return '<a href="' . $url . '" title="' . $straction . '">' .
235
            '<img src="' . $OUTPUT->old_icon_url('t/' . $icon) . '" alt="' . $straction . '" /></a> ';
236
237
238
}

function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
239
    global $CFG, $OUTPUT, $activechoices, $applytochoices, $filternames;
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    $row = array();
    $filter = $filterinfo->filter;

    // Filter name
    if (!empty($filternames[$filter])) {
        $row[] = $filternames[$filter];
    } else {
        $row[] = '<span class="error">' . get_string('filemissing', '', $filter) . '</span>';
    }

    // Disable/off/on
    $row[] = popup_form(action_url($filter, 'setstate') . '&amp;newstate=', $activechoices,
            'active' . basename($filter), $filterinfo->active, '', '', '', true, 'self', '', NULL, get_string('save', 'admin'));

    // Re-order
    $updown = '';
256
    $spacer = '<img src="' . $OUTPUT->old_icon_url('spacer') . '" class="iconsmall" alt="" /> ';
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    if ($filterinfo->active != TEXTFILTER_DISABLED) {
        if (!$isfirstrow) {
            $updown .= action_icon(action_url($filter, 'up'), 'up', get_string('up'));
        } else {
            $updown .= $spacer;
        }
        if (!$islastactive) {
            $updown .= action_icon(action_url($filter, 'down'), 'down', get_string('down'));
        } else {
            $updown .= $spacer;
        }
    }
    $row[] = $updown;

    // Apply to strings.
    $row[] = popup_form(action_url($filter, 'setapplyto') . '&amp;stringstoo=', $applytochoices,
            'applyto' . basename($filter), $applytostrings, '', '', '', true, 'self', '', NULL, get_string('save', 'admin'),
            $filterinfo->active == TEXTFILTER_DISABLED);

    // Settings link, if required
    if (filter_has_global_settings($filter)) {
278
        $row[] = '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=filtersetting' .
279
                str_replace('/', '',$filter) . '">' . get_string('settings') . '</a>';
280
281
    } else {
        $row[] = '';
282
283
284
    }

    // Delete
285
286
287
288
289
    if (substr($filter, 0, 4) != 'mod/') {
        $row[] = '<a href="' . action_url($filter, 'delete') . '">' . get_string('delete') . '</a>';
    } else {
        $row[] = '';
    }
290
291
292

    return $row;
}