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