MDL-23479 backup - fix files/tags/comments/links backup & restore
[moodle.git] / filter / manage.php
CommitLineData
4317f92f 1<?php
0f74bb01 2
35716b86
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
0f74bb01 17
18/**
19 * Lets users configure which filters are active in a sub-context.
20 *
35716b86
PS
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package core
23 * @subpackage filter
24 */
0f74bb01 25
26require_once(dirname(__FILE__) . '/../config.php');
27require_once($CFG->libdir . '/adminlib.php');
28
29$contextid = required_param('contextid',PARAM_INT);
604eb7be 30$forfilter = optional_param('filter', '', PARAM_SAFEPATH);
0f74bb01 31
ba9dc077 32$context = get_context_instance_by_id($contextid, MUST_EXIST);
0f74bb01 33
e4ca0c85
SH
34$args = array('contextid'=>$contextid);
35$baseurl = new moodle_url('/filter/manage.php', $args);
36if (!empty($forfilter)) {
37 $args['filter'] = $forfilter;
38}
39$PAGE->set_url($baseurl, $args);
40
0f74bb01 41// This is a policy decision, rather than something that would be impossible to implement.
42if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
43 print_error('cannotcustomisefiltersblockuser', 'error');
44}
45
ba9dc077 46$isfrontpage = ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID);
0f74bb01 47$contextname = print_context_name($context);
48
ba9dc077 49$cm = null;
0f74bb01 50if ($context->contextlevel == CONTEXT_COURSECAT) {
51 $course = clone($SITE);
ba9dc077
SH
52 $heading = "$SITE->fullname: ".get_string("categories");
53 $PAGE->set_heading();
0f74bb01 54} else if ($context->contextlevel == CONTEXT_COURSE) {
ba9dc077
SH
55 $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST);
56 $heading = $course->fullname;
0f74bb01 57} else {
58 // Must be module context.
ba9dc077
SH
59 $cm = get_coursemodule_from_id(null, $context->instanceid, false, 0, MUST_EXIST);
60 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
0f74bb01 61}
62
63/// Check login and permissions.
ba9dc077 64require_login($course, false, $cm);
0f74bb01 65require_capability('moodle/filter:manage', $context);
66
ba9dc077
SH
67if ($cm) {
68 $heading = $PAGE->activityrecord->name;
69}
70
71$PAGE->set_context($context);
72$PAGE->set_heading($heading);
73
0f74bb01 74/// Get the list of available filters.
75$availablefilters = filter_get_available_in_context($context);
76if (!$isfrontpage && empty($availablefilters)) {
77 print_error('nofiltersenabled', 'error');
78}
79
604eb7be 80// If we are handling local settings for a particular filter, start processing.
81if ($forfilter) {
82 if (!filter_has_local_settings($forfilter)) {
83 print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
84 }
85 require_once($CFG->dirroot . '/filter/local_settings_form.php');
86 require_once($CFG->dirroot . '/' . $forfilter . '/filterlocalsettings.php');
87 $formname = basename($forfilter) . '_filter_local_settings_form';
88 $settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context);
89 if ($settingsform->is_cancelled()) {
90 redirect($baseurl);
91 } else if ($data = $settingsform->get_data()) {
92 $settingsform->save_changes($data);
93 redirect($baseurl);
94 }
95}
96
0f74bb01 97/// Process any form submission.
604eb7be 98if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
0f74bb01 99 foreach ($availablefilters as $filter => $filterinfo) {
100 $newstate = optional_param(str_replace('/', '_', $filter), false, PARAM_INT);
101 if ($newstate !== false && $newstate != $filterinfo->localstate) {
102 filter_set_local_state($filter, $context->id, $newstate);
103 }
104 }
105 redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
106}
107
0f74bb01 108/// Work out an appropriate page title.
604eb7be 109if ($forfilter) {
110 $a = new stdClass;
111 $a->filter = filter_get_name($forfilter);
112 $a->context = $contextname;
113 $title = get_string('filtersettingsforin', 'filters', $a);
114} else {
115 $title = get_string('filtersettingsin', 'filters', $contextname);
116}
0f74bb01 117$straction = get_string('filters', 'admin'); // Used by tabs.php
118
119/// Print the header and tabs
ba9dc077 120if ($isfrontpage) {
0f74bb01 121 admin_externalpage_setup('frontpagefilters');
61ef8f9f 122 echo $OUTPUT->header();
0f74bb01 123} else {
ba9dc077
SH
124 $PAGE->set_cacheable(false);
125 $PAGE->set_title($title);
126 $PAGE->set_pagelayout('admin');
127 echo $OUTPUT->header();
0f74bb01 128}
129
130/// Print heading.
8e0b6fb1 131echo $OUTPUT->heading_with_help($title, 'filtersettings', 'filters');
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
ba9dc077
SH
158 echo html_writer::start_tag('form', array('action'=>$baseurl->out(), 'method'=>'post'));
159 echo html_writer::start_tag('div');
160 echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
0f74bb01 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 }
d776d59e 185 $row[] = html_writer::select($activechoices, str_replace('/', '_', $filter), $filterinfo->localstate, false);
0f74bb01 186
187 // Settings link, if required
188 if ($settingscol) {
189 $settings = '';
190 if ($filterinfo->hassettings) {
e4ca0c85 191 $settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
0f74bb01 192 }
193 $row[] = $settings;
194 }
195
196 $table->data[] = $row;
197 }
198
16be8974 199 echo html_writer::table($table);
ba9dc077
SH
200 echo html_writer::start_tag('div', array('class'=>'buttons'));
201 echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
202 echo html_writer::end_tag('div');
203 echo html_writer::end_tag('div');
204 echo html_writer::end_tag('form');
0f74bb01 205}
206
207/// Appropriate back link.
ba9dc077
SH
208if (!$isfrontpage) {
209 echo html_writer::start_tag('div', array('class'=>'backlink'));
210 echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
211 echo html_writer::end_tag('div');
0f74bb01 212}
213
948b2713 214echo $OUTPUT->footer();
4317f92f 215