weekly release 2.3.1+
[moodle.git] / filter / manage.php
CommitLineData
4317f92f 1<?php
0f74bb01 2
35716b86
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
0f74bb01 17
18/**
19 * Lets users configure which filters are active in a sub-context.
20 *
35716b86
PS
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package core
23 * @subpackage filter
24 */
0f74bb01 25
26require_once(dirname(__FILE__) . '/../config.php');
27require_once($CFG->libdir . '/adminlib.php');
28
29$contextid = required_param('contextid',PARAM_INT);
604eb7be 30$forfilter = optional_param('filter', '', PARAM_SAFEPATH);
0f74bb01 31
daf0f79a
PS
32list($context, $course, $cm) = get_context_info_array($contextid);
33
34/// Check login and permissions.
35require_login($course, false, $cm);
36require_capability('moodle/filter:manage', $context);
7313ac6c 37$PAGE->set_context($context);
0f74bb01 38
e4ca0c85
SH
39$args = array('contextid'=>$contextid);
40$baseurl = new moodle_url('/filter/manage.php', $args);
41if (!empty($forfilter)) {
42 $args['filter'] = $forfilter;
43}
44$PAGE->set_url($baseurl, $args);
45
0f74bb01 46// This is a policy decision, rather than something that would be impossible to implement.
47if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
48 print_error('cannotcustomisefiltersblockuser', 'error');
49}
50
ba9dc077 51$isfrontpage = ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID);
daf0f79a 52
0f74bb01 53$contextname = print_context_name($context);
54
55if ($context->contextlevel == CONTEXT_COURSECAT) {
ba9dc077 56 $heading = "$SITE->fullname: ".get_string("categories");
0f74bb01 57} else if ($context->contextlevel == CONTEXT_COURSE) {
ba9dc077 58 $heading = $course->fullname;
daf0f79a 59} else if ($context->contextlevel == CONTEXT_MODULE) {
0f74bb01 60 // Must be module context.
daf0f79a
PS
61 $heading = $PAGE->activityrecord->name;
62} else {
63 $heading = '';
0f74bb01 64}
65
66/// Check login and permissions.
ba9dc077 67require_login($course, false, $cm);
0f74bb01 68require_capability('moodle/filter:manage', $context);
69
ba9dc077
SH
70$PAGE->set_context($context);
71$PAGE->set_heading($heading);
72
0f74bb01 73/// Get the list of available filters.
74$availablefilters = filter_get_available_in_context($context);
75if (!$isfrontpage && empty($availablefilters)) {
76 print_error('nofiltersenabled', 'error');
77}
78
604eb7be 79// If we are handling local settings for a particular filter, start processing.
80if ($forfilter) {
81 if (!filter_has_local_settings($forfilter)) {
82 print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
83 }
84 require_once($CFG->dirroot . '/filter/local_settings_form.php');
85 require_once($CFG->dirroot . '/' . $forfilter . '/filterlocalsettings.php');
86 $formname = basename($forfilter) . '_filter_local_settings_form';
87 $settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context);
88 if ($settingsform->is_cancelled()) {
89 redirect($baseurl);
90 } else if ($data = $settingsform->get_data()) {
91 $settingsform->save_changes($data);
92 redirect($baseurl);
93 }
94}
95
0f74bb01 96/// Process any form submission.
604eb7be 97if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
0f74bb01 98 foreach ($availablefilters as $filter => $filterinfo) {
99 $newstate = optional_param(str_replace('/', '_', $filter), false, PARAM_INT);
100 if ($newstate !== false && $newstate != $filterinfo->localstate) {
101 filter_set_local_state($filter, $context->id, $newstate);
102 }
103 }
104 redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
105}
106
0f74bb01 107/// Work out an appropriate page title.
604eb7be 108if ($forfilter) {
109 $a = new stdClass;
110 $a->filter = filter_get_name($forfilter);
111 $a->context = $contextname;
112 $title = get_string('filtersettingsforin', 'filters', $a);
113} else {
114 $title = get_string('filtersettingsin', 'filters', $contextname);
115}
0f74bb01 116$straction = get_string('filters', 'admin'); // Used by tabs.php
117
118/// Print the header and tabs
ba9dc077 119if ($isfrontpage) {
0f74bb01 120 admin_externalpage_setup('frontpagefilters');
61ef8f9f 121 echo $OUTPUT->header();
0f74bb01 122} else {
ba9dc077
SH
123 $PAGE->set_cacheable(false);
124 $PAGE->set_title($title);
125 $PAGE->set_pagelayout('admin');
126 echo $OUTPUT->header();
0f74bb01 127}
128
129/// Print heading.
8e0b6fb1 130echo $OUTPUT->heading_with_help($title, 'filtersettings', 'filters');
0f74bb01 131
132if (empty($availablefilters)) {
133 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
604eb7be 134} else if ($forfilter) {
135 $current = filter_get_local_config($forfilter, $contextid);
136 $settingsform->set_data((object) $current);
137 $settingsform->display();
0f74bb01 138} else {
139 $settingscol = false;
140 foreach ($availablefilters as $filter => $notused) {
141 $hassettings = filter_has_local_settings($filter);
142 $availablefilters[$filter]->hassettings = $hassettings;
143 $settingscol = $settingscol || $hassettings;
144 }
145
146 $strsettings = get_string('settings');
147 $stroff = get_string('off', 'filters');
148 $stron = get_string('on', 'filters');
149 $strdefaultoff = get_string('defaultx', 'filters', $stroff);
150 $strdefaulton = get_string('defaultx', 'filters', $stron);
151 $activechoices = array(
152 TEXTFILTER_INHERIT => '',
153 TEXTFILTER_OFF => $stroff,
154 TEXTFILTER_ON => $stron,
155 );
156
ba9dc077
SH
157 echo html_writer::start_tag('form', array('action'=>$baseurl->out(), 'method'=>'post'));
158 echo html_writer::start_tag('div');
159 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
0f74bb01 160
16fa7456 161 $table = new html_table();
0f74bb01 162 $table->head = array(get_string('filter'), get_string('isactive', 'filters'));
163 $table->align = array('left', 'left');
164 if ($settingscol) {
165 $table->head[] = $strsettings;
604eb7be 166 $table->align[] = 'left';
0f74bb01 167 }
168 $table->width = ' ';
169 $table->data = array();
170
171 // iterate through filters adding to display table
172 foreach ($availablefilters as $filter => $filterinfo) {
173 $row = array();
174
175 // Filter name.
176 $row[] = filter_get_name($filter);
177
178 // Default/on/off choice.
179 if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
180 $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
181 } else {
182 $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
183 }
3211569a 184 $row[] = html_writer::select($activechoices, str_replace('/', '_', $filter), $filterinfo->localstate, false);
0f74bb01 185
186 // Settings link, if required
187 if ($settingscol) {
188 $settings = '';
189 if ($filterinfo->hassettings) {
e4ca0c85 190 $settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
0f74bb01 191 }
192 $row[] = $settings;
193 }
194
195 $table->data[] = $row;
196 }
197
16be8974 198 echo html_writer::table($table);
ba9dc077
SH
199 echo html_writer::start_tag('div', array('class'=>'buttons'));
200 echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
201 echo html_writer::end_tag('div');
202 echo html_writer::end_tag('div');
203 echo html_writer::end_tag('form');
0f74bb01 204}
205
206/// Appropriate back link.
ba9dc077
SH
207if (!$isfrontpage) {
208 echo html_writer::start_tag('div', array('class'=>'backlink'));
209 echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
210 echo html_writer::end_tag('div');
0f74bb01 211}
212
948b2713 213echo $OUTPUT->footer();
4317f92f 214