webservice MDL-17135 remove old webserviceuse capability. It is now controlled by...
[moodle.git] / filter / manage.php
CommitLineData
4317f92f 1<?php
0f74bb01 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * Lets users configure which filters are active in a sub-context.
28 *
29 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30 * @package moodlecore
31 *//** */
32
33require_once(dirname(__FILE__) . '/../config.php');
34require_once($CFG->libdir . '/adminlib.php');
35
36$contextid = required_param('contextid',PARAM_INT);
604eb7be 37$forfilter = optional_param('filter', '', PARAM_SAFEPATH);
0f74bb01 38
39if (!$context = get_context_instance_by_id($contextid)) {
40 print_error('wrongcontextid', 'error');
41}
42
43// This is a policy decision, rather than something that would be impossible to implement.
44if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
45 print_error('cannotcustomisefiltersblockuser', 'error');
46}
47
48$isfrontpage = $context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID;
49$contextname = print_context_name($context);
604eb7be 50$baseurl = $CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id;
0f74bb01 51
52if ($context->contextlevel == CONTEXT_COURSECAT) {
53 $course = clone($SITE);
54} else if ($context->contextlevel == CONTEXT_COURSE) {
55 $course = $DB->get_record('course', array('id' => $context->instanceid));
56} else {
57 // Must be module context.
58 $course = $DB->get_record_sql('SELECT c.* FROM {course} c JOIN {context} ctx ON c.id = ctx.instanceid WHERE ctx.id = ?',
59 array(get_parent_contextid($context)));
60}
61if (!$course) {
62 print_error('invalidcourse', 'error');
63}
64
65/// Check login and permissions.
66require_login($course);
67require_capability('moodle/filter:manage', $context);
68
69/// Get the list of available filters.
70$availablefilters = filter_get_available_in_context($context);
71if (!$isfrontpage && empty($availablefilters)) {
72 print_error('nofiltersenabled', 'error');
73}
74
604eb7be 75// If we are handling local settings for a particular filter, start processing.
76if ($forfilter) {
77 if (!filter_has_local_settings($forfilter)) {
78 print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
79 }
80 require_once($CFG->dirroot . '/filter/local_settings_form.php');
81 require_once($CFG->dirroot . '/' . $forfilter . '/filterlocalsettings.php');
82 $formname = basename($forfilter) . '_filter_local_settings_form';
83 $settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context);
84 if ($settingsform->is_cancelled()) {
85 redirect($baseurl);
86 } else if ($data = $settingsform->get_data()) {
87 $settingsform->save_changes($data);
88 redirect($baseurl);
89 }
90}
91
0f74bb01 92/// Process any form submission.
604eb7be 93if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
0f74bb01 94 foreach ($availablefilters as $filter => $filterinfo) {
95 $newstate = optional_param(str_replace('/', '_', $filter), false, PARAM_INT);
96 if ($newstate !== false && $newstate != $filterinfo->localstate) {
97 filter_set_local_state($filter, $context->id, $newstate);
98 }
99 }
100 redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
101}
102
103/// These are needed early because of tabs.php
104$assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
105$overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
106
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
119if ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID) {
120 admin_externalpage_setup('frontpagefilters');
121 admin_externalpage_print_header();
122} else {
123 $currenttab = 'filters';
124 include_once($CFG->dirroot . '/' . $CFG->admin . '/roles/tabs.php');
125}
126
127/// Print heading.
4bcc5118 128echo $OUTPUT->heading_with_help($title, 'localfiltersettings');
0f74bb01 129
130if (empty($availablefilters)) {
131 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
604eb7be 132} else if ($forfilter) {
133 $current = filter_get_local_config($forfilter, $contextid);
134 $settingsform->set_data((object) $current);
135 $settingsform->display();
0f74bb01 136} else {
137 $settingscol = false;
138 foreach ($availablefilters as $filter => $notused) {
139 $hassettings = filter_has_local_settings($filter);
140 $availablefilters[$filter]->hassettings = $hassettings;
141 $settingscol = $settingscol || $hassettings;
142 }
143
144 $strsettings = get_string('settings');
145 $stroff = get_string('off', 'filters');
146 $stron = get_string('on', 'filters');
147 $strdefaultoff = get_string('defaultx', 'filters', $stroff);
148 $strdefaulton = get_string('defaultx', 'filters', $stron);
149 $activechoices = array(
150 TEXTFILTER_INHERIT => '',
151 TEXTFILTER_OFF => $stroff,
152 TEXTFILTER_ON => $stron,
153 );
154
604eb7be 155 echo '<form action="' . $baseurl . '" method="post">';
0f74bb01 156 echo "\n<div>\n";
157 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
158
16fa7456 159 $table = new html_table();
0f74bb01 160 $table->head = array(get_string('filter'), get_string('isactive', 'filters'));
161 $table->align = array('left', 'left');
162 if ($settingscol) {
163 $table->head[] = $strsettings;
604eb7be 164 $table->align[] = 'left';
0f74bb01 165 }
166 $table->width = ' ';
167 $table->data = array();
168
169 // iterate through filters adding to display table
170 foreach ($availablefilters as $filter => $filterinfo) {
171 $row = array();
172
173 // Filter name.
174 $row[] = filter_get_name($filter);
175
176 // Default/on/off choice.
177 if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
178 $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
179 } else {
180 $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
181 }
d776d59e 182 $row[] = html_writer::select($activechoices, str_replace('/', '_', $filter), $filterinfo->localstate, false);
0f74bb01 183
184 // Settings link, if required
185 if ($settingscol) {
186 $settings = '';
187 if ($filterinfo->hassettings) {
604eb7be 188 $settings = '<a href="' . $baseurl . '&amp;filter=' . $filter . '">' . $strsettings . '</a>';
0f74bb01 189 }
190 $row[] = $settings;
191 }
192
193 $table->data[] = $row;
194 }
195
16fa7456 196 echo $OUTPUT->table($table);
0f74bb01 197 echo '<div class="buttons">' . "\n";
198 echo '<input type="submit" name="savechanges" value="' . get_string('savechanges') . '" />';
199 echo "\n</div>\n";
200 echo "</div>\n";
201 echo "</form>\n";
202
203}
204
205/// Appropriate back link.
206if (!$isfrontpage && ($url = get_context_url($context))) {
207 echo '<div class="backlink"><a href="' . $url . '">' .
208 get_string('backto', '', $contextname) . '</a></div>';
209}
210
948b2713 211echo $OUTPUT->footer();
4317f92f 212