Updated the HEAD build version to 20100421
[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
e4ca0c85
SH
43$args = array('contextid'=>$contextid);
44$baseurl = new moodle_url('/filter/manage.php', $args);
45if (!empty($forfilter)) {
46 $args['filter'] = $forfilter;
47}
48$PAGE->set_url($baseurl, $args);
49
0f74bb01 50// This is a policy decision, rather than something that would be impossible to implement.
51if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
52 print_error('cannotcustomisefiltersblockuser', 'error');
53}
54
55$isfrontpage = $context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID;
56$contextname = print_context_name($context);
57
58if ($context->contextlevel == CONTEXT_COURSECAT) {
59 $course = clone($SITE);
60} else if ($context->contextlevel == CONTEXT_COURSE) {
61 $course = $DB->get_record('course', array('id' => $context->instanceid));
62} else {
63 // Must be module context.
64 $course = $DB->get_record_sql('SELECT c.* FROM {course} c JOIN {context} ctx ON c.id = ctx.instanceid WHERE ctx.id = ?',
65 array(get_parent_contextid($context)));
66}
67if (!$course) {
68 print_error('invalidcourse', 'error');
69}
70
71/// Check login and permissions.
72require_login($course);
73require_capability('moodle/filter:manage', $context);
74
75/// Get the list of available filters.
76$availablefilters = filter_get_available_in_context($context);
77if (!$isfrontpage && empty($availablefilters)) {
78 print_error('nofiltersenabled', 'error');
79}
80
604eb7be 81// If we are handling local settings for a particular filter, start processing.
82if ($forfilter) {
83 if (!filter_has_local_settings($forfilter)) {
84 print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
85 }
86 require_once($CFG->dirroot . '/filter/local_settings_form.php');
87 require_once($CFG->dirroot . '/' . $forfilter . '/filterlocalsettings.php');
88 $formname = basename($forfilter) . '_filter_local_settings_form';
89 $settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context);
90 if ($settingsform->is_cancelled()) {
91 redirect($baseurl);
92 } else if ($data = $settingsform->get_data()) {
93 $settingsform->save_changes($data);
94 redirect($baseurl);
95 }
96}
97
0f74bb01 98/// Process any form submission.
604eb7be 99if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
0f74bb01 100 foreach ($availablefilters as $filter => $filterinfo) {
101 $newstate = optional_param(str_replace('/', '_', $filter), false, PARAM_INT);
102 if ($newstate !== false && $newstate != $filterinfo->localstate) {
103 filter_set_local_state($filter, $context->id, $newstate);
104 }
105 }
106 redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
107}
108
109/// These are needed early because of tabs.php
110$assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
111$overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
112
113/// Work out an appropriate page title.
604eb7be 114if ($forfilter) {
115 $a = new stdClass;
116 $a->filter = filter_get_name($forfilter);
117 $a->context = $contextname;
118 $title = get_string('filtersettingsforin', 'filters', $a);
119} else {
120 $title = get_string('filtersettingsin', 'filters', $contextname);
121}
0f74bb01 122$straction = get_string('filters', 'admin'); // Used by tabs.php
123
124/// Print the header and tabs
125if ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID) {
126 admin_externalpage_setup('frontpagefilters');
61ef8f9f 127 echo $OUTPUT->header();
0f74bb01 128} else {
129 $currenttab = 'filters';
130 include_once($CFG->dirroot . '/' . $CFG->admin . '/roles/tabs.php');
131}
132
133/// Print heading.
4bcc5118 134echo $OUTPUT->heading_with_help($title, 'localfiltersettings');
0f74bb01 135
136if (empty($availablefilters)) {
137 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
604eb7be 138} else if ($forfilter) {
139 $current = filter_get_local_config($forfilter, $contextid);
140 $settingsform->set_data((object) $current);
141 $settingsform->display();
0f74bb01 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;
148 }
149
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,
159 );
160
e4ca0c85 161 echo '<form action="'.$baseurl->out().'" method="post">';
0f74bb01 162 echo "\n<div>\n";
163 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
164
16fa7456 165 $table = new html_table();
0f74bb01 166 $table->head = array(get_string('filter'), get_string('isactive', 'filters'));
167 $table->align = array('left', 'left');
168 if ($settingscol) {
169 $table->head[] = $strsettings;
604eb7be 170 $table->align[] = 'left';
0f74bb01 171 }
172 $table->width = ' ';
173 $table->data = array();
174
175 // iterate through filters adding to display table
176 foreach ($availablefilters as $filter => $filterinfo) {
177 $row = array();
178
179 // Filter name.
180 $row[] = filter_get_name($filter);
181
182 // Default/on/off choice.
183 if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
184 $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
185 } else {
186 $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
187 }
d776d59e 188 $row[] = html_writer::select($activechoices, str_replace('/', '_', $filter), $filterinfo->localstate, false);
0f74bb01 189
190 // Settings link, if required
191 if ($settingscol) {
192 $settings = '';
193 if ($filterinfo->hassettings) {
e4ca0c85 194 $settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
0f74bb01 195 }
196 $row[] = $settings;
197 }
198
199 $table->data[] = $row;
200 }
201
16be8974 202 echo html_writer::table($table);
0f74bb01 203 echo '<div class="buttons">' . "\n";
204 echo '<input type="submit" name="savechanges" value="' . get_string('savechanges') . '" />';
205 echo "\n</div>\n";
206 echo "</div>\n";
207 echo "</form>\n";
208
209}
210
211/// Appropriate back link.
212if (!$isfrontpage && ($url = get_context_url($context))) {
213 echo '<div class="backlink"><a href="' . $url . '">' .
214 get_string('backto', '', $contextname) . '</a></div>';
215}
216
948b2713 217echo $OUTPUT->footer();
4317f92f 218