Merge branch 'MDL-42026-master' of git://github.com/damyon/moodle
[moodle.git] / filter / manage.php
blob550b0f0e60f494526f64c7aae0a6ce173a336d56
1 <?php
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.
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/>.
18 /**
19 * Lets users configure which filters are active in a sub-context.
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package core
23 * @subpackage filter
26 require_once(dirname(__FILE__) . '/../config.php');
27 require_once($CFG->libdir . '/adminlib.php');
29 $contextid = required_param('contextid',PARAM_INT);
30 $forfilter = optional_param('filter', '', PARAM_SAFEDIR);
31 $returnto = optional_param('return', null, PARAM_ALPHANUMEXT);
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);
46 if ($returnto !== null) {
47 $baseurl->param('return', $returnto);
50 // This is a policy decision, rather than something that would be impossible to implement.
51 if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
52 print_error('cannotcustomisefiltersblockuser', 'error');
55 $isfrontpage = ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID);
57 $contextname = $context->get_context_name();
59 if ($context->contextlevel == CONTEXT_COURSECAT) {
60 $heading = "$SITE->fullname: ".get_string("categories");
61 } else if ($context->contextlevel == CONTEXT_COURSE) {
62 $heading = $course->fullname;
63 } else if ($context->contextlevel == CONTEXT_MODULE) {
64 // Must be module context.
65 $heading = $PAGE->activityrecord->name;
66 } else {
67 $heading = '';
70 /// Check login and permissions.
71 require_login($course, false, $cm);
72 require_capability('moodle/filter:manage', $context);
74 $PAGE->set_context($context);
75 $PAGE->set_heading($heading);
77 /// Get the list of available filters.
78 $availablefilters = filter_get_available_in_context($context);
79 if (!$isfrontpage && empty($availablefilters)) {
80 print_error('nofiltersenabled', 'error');
83 // If we are handling local settings for a particular filter, start processing.
84 if ($forfilter) {
85 if (!filter_has_local_settings($forfilter)) {
86 print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
88 require_once($CFG->dirroot . '/filter/local_settings_form.php');
89 require_once($CFG->dirroot . '/filter/' . $forfilter . '/filterlocalsettings.php');
90 $formname = $forfilter . '_filter_local_settings_form';
91 $settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context);
92 if ($settingsform->is_cancelled()) {
93 redirect($baseurl);
94 } else if ($data = $settingsform->get_data()) {
95 $settingsform->save_changes($data);
96 redirect($baseurl);
100 /// Process any form submission.
101 if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
102 foreach ($availablefilters as $filter => $filterinfo) {
103 $newstate = optional_param($filter, false, PARAM_INT);
104 if ($newstate !== false && $newstate != $filterinfo->localstate) {
105 filter_set_local_state($filter, $context->id, $newstate);
108 redirect($baseurl, get_string('changessaved'), 1);
111 /// Work out an appropriate page title.
112 if ($forfilter) {
113 $a = new stdClass;
114 $a->filter = filter_get_name($forfilter);
115 $a->context = $contextname;
116 $title = get_string('filtersettingsforin', 'filters', $a);
117 } else {
118 $title = get_string('filtersettingsin', 'filters', $contextname);
120 $straction = get_string('filters', 'admin'); // Used by tabs.php
122 /// Print the header and tabs
123 if ($isfrontpage) {
124 admin_externalpage_setup('frontpagefilters');
125 echo $OUTPUT->header();
126 } else {
127 $PAGE->set_cacheable(false);
128 $PAGE->set_title($title);
129 $PAGE->set_pagelayout('admin');
130 echo $OUTPUT->header();
133 /// Print heading.
134 echo $OUTPUT->heading_with_help($title, 'filtersettings', 'filters');
136 if (empty($availablefilters)) {
137 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
138 } else if ($forfilter) {
139 $current = filter_get_local_config($forfilter, $contextid);
140 $settingsform->set_data((object) $current);
141 $settingsform->display();
142 } else {
143 $settingscol = false;
144 foreach ($availablefilters as $filter => $notused) {
145 $hassettings = filter_has_local_settings($filter);
146 $availablefilters[$filter]->hassettings = $hassettings;
147 $settingscol = $settingscol || $hassettings;
150 $strsettings = get_string('settings');
151 $stroff = get_string('off', 'filters');
152 $stron = get_string('on', 'filters');
153 $strdefaultoff = get_string('defaultx', 'filters', $stroff);
154 $strdefaulton = get_string('defaultx', 'filters', $stron);
155 $activechoices = array(
156 TEXTFILTER_INHERIT => '',
157 TEXTFILTER_OFF => $stroff,
158 TEXTFILTER_ON => $stron,
161 echo html_writer::start_tag('form', array('action'=>$baseurl->out_omit_querystring(), 'method'=>'post'));
162 echo html_writer::start_tag('div');
163 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
164 foreach ($baseurl->params() as $key => $value) {
165 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
168 $table = new html_table();
169 $table->head = array(get_string('filter'), get_string('isactive', 'filters'));
170 $table->colclasses = array('leftalign', 'leftalign');
171 if ($settingscol) {
172 $table->head[] = $strsettings;
173 $table->colclasses[] = 'leftalign';
175 $table->id = 'frontpagefiltersettings';
176 $table->attributes['class'] = 'admintable generaltable';
177 $table->data = array();
179 // iterate through filters adding to display table
180 foreach ($availablefilters as $filter => $filterinfo) {
181 $row = array();
183 // Filter name.
184 $row[] = filter_get_name($filter);
186 // Default/on/off choice.
187 if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
188 $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
189 } else {
190 $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
192 $select = html_writer::label($filterinfo->localstate, 'menu'. $filter, false, array('class' => 'accesshide'));
193 $select .= html_writer::select($activechoices, $filter, $filterinfo->localstate, false);
194 $row[] = $select;
196 // Settings link, if required
197 if ($settingscol) {
198 $settings = '';
199 if ($filterinfo->hassettings) {
200 $settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
202 $row[] = $settings;
205 $table->data[] = $row;
208 echo html_writer::table($table);
209 echo html_writer::start_tag('div', array('class'=>'buttons'));
210 echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
211 echo html_writer::end_tag('div');
212 echo html_writer::end_tag('div');
213 echo html_writer::end_tag('form');
216 /// Appropriate back link.
217 if (!$isfrontpage) {
219 if ($context->contextlevel === CONTEXT_COURSECAT && $returnto === 'management') {
220 $url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid));
221 } else {
222 $url = $context->get_url();
225 echo html_writer::start_tag('div', array('class'=>'backlink'));
226 echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
227 echo html_writer::end_tag('div');
230 echo $OUTPUT->footer();