filters: MDL-7336 settings page for setting the local enabled/disabled state
[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);
37
38if (!$context = get_context_instance_by_id($contextid)) {
39 print_error('wrongcontextid', 'error');
40}
41
42// This is a policy decision, rather than something that would be impossible to implement.
43if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
44 print_error('cannotcustomisefiltersblockuser', 'error');
45}
46
47$isfrontpage = $context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID;
48$contextname = print_context_name($context);
49
50if ($context->contextlevel == CONTEXT_COURSECAT) {
51 $course = clone($SITE);
52} else if ($context->contextlevel == CONTEXT_COURSE) {
53 $course = $DB->get_record('course', array('id' => $context->instanceid));
54} else {
55 // Must be module context.
56 $course = $DB->get_record_sql('SELECT c.* FROM {course} c JOIN {context} ctx ON c.id = ctx.instanceid WHERE ctx.id = ?',
57 array(get_parent_contextid($context)));
58}
59if (!$course) {
60 print_error('invalidcourse', 'error');
61}
62
63/// Check login and permissions.
64require_login($course);
65require_capability('moodle/filter:manage', $context);
66
67/// Get the list of available filters.
68$availablefilters = filter_get_available_in_context($context);
69if (!$isfrontpage && empty($availablefilters)) {
70 print_error('nofiltersenabled', 'error');
71}
72
73/// Process any form submission.
74if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
75 foreach ($availablefilters as $filter => $filterinfo) {
76 $newstate = optional_param(str_replace('/', '_', $filter), false, PARAM_INT);
77 if ($newstate !== false && $newstate != $filterinfo->localstate) {
78 filter_set_local_state($filter, $context->id, $newstate);
79 }
80 }
81 redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
82}
83
84/// These are needed early because of tabs.php
85$assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
86$overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
87
88/// Work out an appropriate page title.
89$title = get_string('filtersettingsfor', 'filters', $contextname);
90$straction = get_string('filters', 'admin'); // Used by tabs.php
91
92/// Print the header and tabs
93if ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID) {
94 admin_externalpage_setup('frontpagefilters');
95 admin_externalpage_print_header();
96} else {
97 $currenttab = 'filters';
98 include_once($CFG->dirroot . '/' . $CFG->admin . '/roles/tabs.php');
99}
100
101/// Print heading.
102print_heading_with_help($title, 'localfiltersettings');
103
104if (empty($availablefilters)) {
105 echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
106} else {
107 $settingscol = false;
108 foreach ($availablefilters as $filter => $notused) {
109 $hassettings = filter_has_local_settings($filter);
110 $availablefilters[$filter]->hassettings = $hassettings;
111 $settingscol = $settingscol || $hassettings;
112 }
113
114 $strsettings = get_string('settings');
115 $stroff = get_string('off', 'filters');
116 $stron = get_string('on', 'filters');
117 $strdefaultoff = get_string('defaultx', 'filters', $stroff);
118 $strdefaulton = get_string('defaultx', 'filters', $stron);
119 $activechoices = array(
120 TEXTFILTER_INHERIT => '',
121 TEXTFILTER_OFF => $stroff,
122 TEXTFILTER_ON => $stron,
123 );
124
125 echo '<form action="' . $CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id . '" method="post">';
126 echo "\n<div>\n";
127 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
128
129 $table = new stdClass;
130 $table->head = array(get_string('filter'), get_string('isactive', 'filters'));
131 $table->align = array('left', 'left');
132 if ($settingscol) {
133 $table->head[] = $strsettings;
134 $table->align = 'left';
135 }
136 $table->width = ' ';
137 $table->data = array();
138
139 // iterate through filters adding to display table
140 foreach ($availablefilters as $filter => $filterinfo) {
141 $row = array();
142
143 // Filter name.
144 $row[] = filter_get_name($filter);
145
146 // Default/on/off choice.
147 if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
148 $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
149 } else {
150 $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
151 }
152 $row[] = choose_from_menu($activechoices, str_replace('/', '_', $filter),
153 $filterinfo->localstate, '', '', '', true);
154
155 // Settings link, if required
156 if ($settingscol) {
157 $settings = '';
158 if ($filterinfo->hassettings) {
159 $settings = '<a href="' . $CFG->wwwroot . '/filter/settings.php?contextid=' .
160 $context->id . '&amp;filter=' . $filter . '">' . $strsettings . '</a>';
161 }
162 $row[] = $settings;
163 }
164
165 $table->data[] = $row;
166 }
167
168 print_table($table);
169 echo '<div class="buttons">' . "\n";
170 echo '<input type="submit" name="savechanges" value="' . get_string('savechanges') . '" />';
171 echo "\n</div>\n";
172 echo "</div>\n";
173 echo "</form>\n";
174
175}
176
177/// Appropriate back link.
178if (!$isfrontpage && ($url = get_context_url($context))) {
179 echo '<div class="backlink"><a href="' . $url . '">' .
180 get_string('backto', '', $contextname) . '</a></div>';
181}
182
183print_footer($course);
184?>