MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / filter / manage.php
CommitLineData
0f74bb01 1<?php // $Id$
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.
e25b7ded 128$helpicon = new moodle_help_icon();
129$helpicon->text = $title;
130$helpicon->page = 'localfiltersettings';
131echo $OUTPUT->heading_with_help($helpicon);
0f74bb01 132
133if (empty($availablefilters)) {
134 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
604eb7be 135} else if ($forfilter) {
136 $current = filter_get_local_config($forfilter, $contextid);
137 $settingsform->set_data((object) $current);
138 $settingsform->display();
0f74bb01 139} else {
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;
145 }
146
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,
156 );
157
604eb7be 158 echo '<form action="' . $baseurl . '" method="post">';
0f74bb01 159 echo "\n<div>\n";
160 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
161
16fa7456 162 $table = new html_table();
0f74bb01 163 $table->head = array(get_string('filter'), get_string('isactive', 'filters'));
164 $table->align = array('left', 'left');
165 if ($settingscol) {
166 $table->head[] = $strsettings;
604eb7be 167 $table->align[] = 'left';
0f74bb01 168 }
169 $table->width = ' ';
170 $table->data = array();
171
172 // iterate through filters adding to display table
173 foreach ($availablefilters as $filter => $filterinfo) {
174 $row = array();
175
176 // Filter name.
177 $row[] = filter_get_name($filter);
178
179 // Default/on/off choice.
180 if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
181 $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
182 } else {
183 $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
184 }
2c5c7029 185 $select = html_select::make($activechoices, str_replace('/', '_', $filter), $filterinfo->localstate, false);
186 $select->nothingvalue = '';
187 $row[] = $OUTPUT->select($select);
0f74bb01 188
189 // Settings link, if required
190 if ($settingscol) {
191 $settings = '';
192 if ($filterinfo->hassettings) {
604eb7be 193 $settings = '<a href="' . $baseurl . '&amp;filter=' . $filter . '">' . $strsettings . '</a>';
0f74bb01 194 }
195 $row[] = $settings;
196 }
197
198 $table->data[] = $row;
199 }
200
16fa7456 201 echo $OUTPUT->table($table);
0f74bb01 202 echo '<div class="buttons">' . "\n";
203 echo '<input type="submit" name="savechanges" value="' . get_string('savechanges') . '" />';
204 echo "\n</div>\n";
205 echo "</div>\n";
206 echo "</form>\n";
207
208}
209
210/// Appropriate back link.
211if (!$isfrontpage && ($url = get_context_url($context))) {
212 echo '<div class="backlink"><a href="' . $url . '">' .
213 get_string('backto', '', $contextname) . '</a></div>';
214}
215
948b2713 216echo $OUTPUT->footer();
0f74bb01 217?>