MDL-29912 course search - oracle inabilities with concats, empties, nulls and type...
[moodle.git] / filter / manage.php
blob943c7fcaea1137a8240bb44db9d9bb4fd8aeb4e2
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_SAFEPATH);
32 list($context, $course, $cm) = get_context_info_array($contextid);
34 /// Check login and permissions.
35 require_login($course, false, $cm);
36 require_capability('moodle/filter:manage', $context);
37 $PAGE->set_context($context);
39 $args = array('contextid'=>$contextid);
40 $baseurl = new moodle_url('/filter/manage.php', $args);
41 if (!empty($forfilter)) {
42 $args['filter'] = $forfilter;
44 $PAGE->set_url($baseurl, $args);
46 // This is a policy decision, rather than something that would be impossible to implement.
47 if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
48 print_error('cannotcustomisefiltersblockuser', 'error');
51 $isfrontpage = ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID);
53 $contextname = print_context_name($context);
55 if ($context->contextlevel == CONTEXT_COURSECAT) {
56 $heading = "$SITE->fullname: ".get_string("categories");
57 } else if ($context->contextlevel == CONTEXT_COURSE) {
58 $heading = $course->fullname;
59 } else if ($context->contextlevel == CONTEXT_MODULE) {
60 // Must be module context.
61 $heading = $PAGE->activityrecord->name;
62 } else {
63 $heading = '';
66 /// Check login and permissions.
67 require_login($course, false, $cm);
68 require_capability('moodle/filter:manage', $context);
70 $PAGE->set_context($context);
71 $PAGE->set_heading($heading);
73 /// Get the list of available filters.
74 $availablefilters = filter_get_available_in_context($context);
75 if (!$isfrontpage && empty($availablefilters)) {
76 print_error('nofiltersenabled', 'error');
79 // If we are handling local settings for a particular filter, start processing.
80 if ($forfilter) {
81 if (!filter_has_local_settings($forfilter)) {
82 print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
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);
96 /// Process any form submission.
97 if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
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);
104 redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
107 /// Work out an appropriate page title.
108 if ($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);
116 $straction = get_string('filters', 'admin'); // Used by tabs.php
118 /// Print the header and tabs
119 if ($isfrontpage) {
120 admin_externalpage_setup('frontpagefilters');
121 echo $OUTPUT->header();
122 } else {
123 $PAGE->set_cacheable(false);
124 $PAGE->set_title($title);
125 $PAGE->set_pagelayout('admin');
126 echo $OUTPUT->header();
129 /// Print heading.
130 echo $OUTPUT->heading_with_help($title, 'filtersettings', 'filters');
132 if (empty($availablefilters)) {
133 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
134 } else if ($forfilter) {
135 $current = filter_get_local_config($forfilter, $contextid);
136 $settingsform->set_data((object) $current);
137 $settingsform->display();
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;
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,
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()));
161 $table = new html_table();
162 $table->head = array(get_string('filter'), get_string('isactive', 'filters'));
163 $table->align = array('left', 'left');
164 if ($settingscol) {
165 $table->head[] = $strsettings;
166 $table->align[] = 'left';
168 $table->width = ' ';
169 $table->data = array();
171 // iterate through filters adding to display table
172 foreach ($availablefilters as $filter => $filterinfo) {
173 $row = array();
175 // Filter name.
176 $row[] = filter_get_name($filter);
178 // Default/on/off choice.
179 if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
180 $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
181 } else {
182 $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
184 $row[] = html_writer::select($activechoices, str_replace('/', '_', $filter), $filterinfo->localstate, false);
186 // Settings link, if required
187 if ($settingscol) {
188 $settings = '';
189 if ($filterinfo->hassettings) {
190 $settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
192 $row[] = $settings;
195 $table->data[] = $row;
198 echo html_writer::table($table);
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');
206 /// Appropriate back link.
207 if (!$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');
213 echo $OUTPUT->footer();