3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Lets users configure which filters are active in a sub-context.
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 require_once(dirname(__FILE__
) . '/../config.php');
27 require_once($CFG->libdir
. '/adminlib.php');
28 require_once($CFG->libdir
. '/pluginlib.php');
30 $contextid = required_param('contextid',PARAM_INT
);
31 $forfilter = optional_param('filter', '', PARAM_SAFEDIR
);
33 list($context, $course, $cm) = get_context_info_array($contextid);
35 /// Check login and permissions.
36 require_login($course, false, $cm);
37 require_capability('moodle/filter:manage', $context);
38 $PAGE->set_context($context);
40 $args = array('contextid'=>$contextid);
41 $baseurl = new moodle_url('/filter/manage.php', $args);
42 if (!empty($forfilter)) {
43 $args['filter'] = $forfilter;
45 $PAGE->set_url($baseurl, $args);
47 // This is a policy decision, rather than something that would be impossible to implement.
48 if (!in_array($context->contextlevel
, array(CONTEXT_COURSECAT
, CONTEXT_COURSE
, CONTEXT_MODULE
))) {
49 print_error('cannotcustomisefiltersblockuser', 'error');
52 $isfrontpage = ($context->contextlevel
== CONTEXT_COURSE
&& $context->instanceid
== SITEID
);
54 $contextname = $context->get_context_name();
56 if ($context->contextlevel
== CONTEXT_COURSECAT
) {
57 $heading = "$SITE->fullname: ".get_string("categories");
58 } else if ($context->contextlevel
== CONTEXT_COURSE
) {
59 $heading = $course->fullname
;
60 } else if ($context->contextlevel
== CONTEXT_MODULE
) {
61 // Must be module context.
62 $heading = $PAGE->activityrecord
->name
;
67 /// Check login and permissions.
68 require_login($course, false, $cm);
69 require_capability('moodle/filter:manage', $context);
71 $PAGE->set_context($context);
72 $PAGE->set_heading($heading);
74 /// Get the list of available filters.
75 $availablefilters = filter_get_available_in_context($context);
76 if (!$isfrontpage && empty($availablefilters)) {
77 print_error('nofiltersenabled', 'error');
80 // If we are handling local settings for a particular filter, start processing.
82 if (!filter_has_local_settings($forfilter)) {
83 print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
85 require_once($CFG->dirroot
. '/filter/local_settings_form.php');
86 require_once($CFG->dirroot
. '/filter/' . $forfilter . '/filterlocalsettings.php');
87 $formname = $forfilter . '_filter_local_settings_form';
88 $settingsform = new $formname($CFG->wwwroot
. '/filter/manage.php', $forfilter, $context);
89 if ($settingsform->is_cancelled()) {
91 } else if ($data = $settingsform->get_data()) {
92 $settingsform->save_changes($data);
97 /// Process any form submission.
98 if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL
) && confirm_sesskey()) {
99 foreach ($availablefilters as $filter => $filterinfo) {
100 $newstate = optional_param($filter, false, PARAM_INT
);
101 if ($newstate !== false && $newstate != $filterinfo->localstate
) {
102 filter_set_local_state($filter, $context->id
, $newstate);
105 redirect($CFG->wwwroot
. '/filter/manage.php?contextid=' . $context->id
, get_string('changessaved'), 1);
108 /// Work out an appropriate page title.
111 $a->filter
= filter_get_name($forfilter);
112 $a->context
= $contextname;
113 $title = get_string('filtersettingsforin', 'filters', $a);
115 $title = get_string('filtersettingsin', 'filters', $contextname);
117 $straction = get_string('filters', 'admin'); // Used by tabs.php
119 /// Print the header and tabs
121 admin_externalpage_setup('frontpagefilters');
122 echo $OUTPUT->header();
124 $PAGE->set_cacheable(false);
125 $PAGE->set_title($title);
126 $PAGE->set_pagelayout('admin');
127 echo $OUTPUT->header();
131 echo $OUTPUT->heading_with_help($title, 'filtersettings', 'filters');
133 if (empty($availablefilters)) {
134 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
135 } else if ($forfilter) {
136 $current = filter_get_local_config($forfilter, $contextid);
137 $settingsform->set_data((object) $current);
138 $settingsform->display();
140 $settingscol = false;
141 foreach ($availablefilters as $filter => $notused) {
142 $hassettings = filter_has_local_settings($filter);
143 $availablefilters[$filter]->hassettings
= $hassettings;
144 $settingscol = $settingscol ||
$hassettings;
147 $strsettings = get_string('settings');
148 $stroff = get_string('off', 'filters');
149 $stron = get_string('on', 'filters');
150 $strdefaultoff = get_string('defaultx', 'filters', $stroff);
151 $strdefaulton = get_string('defaultx', 'filters', $stron);
152 $activechoices = array(
153 TEXTFILTER_INHERIT
=> '',
154 TEXTFILTER_OFF
=> $stroff,
155 TEXTFILTER_ON
=> $stron,
158 echo html_writer
::start_tag('form', array('action'=>$baseurl->out(), 'method'=>'post'));
159 echo html_writer
::start_tag('div');
160 echo html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
162 $table = new html_table();
163 $table->head
= array(get_string('filter'), get_string('isactive', 'filters'));
164 $table->colclasses
= array('leftalign', 'leftalign');
166 $table->head
[] = $strsettings;
167 $table->colclasses
[] = 'leftalign';
169 $table->id
= 'frontpagefiltersettings';
170 $table->attributes
['class'] = 'admintable generaltable';
171 $table->data
= array();
173 // iterate through filters adding to display table
174 foreach ($availablefilters as $filter => $filterinfo) {
178 $row[] = filter_get_name($filter);
180 // Default/on/off choice.
181 if ($filterinfo->inheritedstate
== TEXTFILTER_ON
) {
182 $activechoices[TEXTFILTER_INHERIT
] = $strdefaulton;
184 $activechoices[TEXTFILTER_INHERIT
] = $strdefaultoff;
186 $select = html_writer
::label($filterinfo->localstate
, 'menu'. $filter, false, array('class' => 'accesshide'));
187 $select .= html_writer
::select($activechoices, $filter, $filterinfo->localstate
, false);
190 // Settings link, if required
193 if ($filterinfo->hassettings
) {
194 $settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
199 $table->data
[] = $row;
202 echo html_writer
::table($table);
203 echo html_writer
::start_tag('div', array('class'=>'buttons'));
204 echo html_writer
::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
205 echo html_writer
::end_tag('div');
206 echo html_writer
::end_tag('div');
207 echo html_writer
::end_tag('form');
210 /// Appropriate back link.
212 echo html_writer
::start_tag('div', array('class'=>'backlink'));
213 echo html_writer
::tag('a', get_string('backto', '', $contextname), array('href' => $context->get_url()));
214 echo html_writer
::end_tag('div');
217 echo $OUTPUT->footer();