forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.php
213 lines (181 loc) · 7.54 KB
/
manage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Lets users configure which filters are active in a sub-context.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package core
* @subpackage filter
*/
require_once(dirname(__FILE__) . '/../config.php');
require_once($CFG->libdir . '/adminlib.php');
$contextid = required_param('contextid',PARAM_INT);
$forfilter = optional_param('filter', '', PARAM_SAFEPATH);
list($context, $course, $cm) = get_context_info_array($contextid);
/// Check login and permissions.
require_login($course, false, $cm);
require_capability('moodle/filter:manage', $context);
$args = array('contextid'=>$contextid);
$baseurl = new moodle_url('/filter/manage.php', $args);
if (!empty($forfilter)) {
$args['filter'] = $forfilter;
}
$PAGE->set_url($baseurl, $args);
// This is a policy decision, rather than something that would be impossible to implement.
if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
print_error('cannotcustomisefiltersblockuser', 'error');
}
$isfrontpage = ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID);
$contextname = print_context_name($context);
if ($context->contextlevel == CONTEXT_COURSECAT) {
$heading = "$SITE->fullname: ".get_string("categories");
} else if ($context->contextlevel == CONTEXT_COURSE) {
$heading = $course->fullname;
} else if ($context->contextlevel == CONTEXT_MODULE) {
// Must be module context.
$heading = $PAGE->activityrecord->name;
} else {
$heading = '';
}
/// Check login and permissions.
require_login($course, false, $cm);
require_capability('moodle/filter:manage', $context);
$PAGE->set_context($context);
$PAGE->set_heading($heading);
/// Get the list of available filters.
$availablefilters = filter_get_available_in_context($context);
if (!$isfrontpage && empty($availablefilters)) {
print_error('nofiltersenabled', 'error');
}
// If we are handling local settings for a particular filter, start processing.
if ($forfilter) {
if (!filter_has_local_settings($forfilter)) {
print_error('filterdoesnothavelocalconfig', 'error', $forfilter);
}
require_once($CFG->dirroot . '/filter/local_settings_form.php');
require_once($CFG->dirroot . '/' . $forfilter . '/filterlocalsettings.php');
$formname = basename($forfilter) . '_filter_local_settings_form';
$settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context);
if ($settingsform->is_cancelled()) {
redirect($baseurl);
} else if ($data = $settingsform->get_data()) {
$settingsform->save_changes($data);
redirect($baseurl);
}
}
/// Process any form submission.
if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
foreach ($availablefilters as $filter => $filterinfo) {
$newstate = optional_param(str_replace('/', '_', $filter), false, PARAM_INT);
if ($newstate !== false && $newstate != $filterinfo->localstate) {
filter_set_local_state($filter, $context->id, $newstate);
}
}
redirect($CFG->wwwroot . '/filter/manage.php?contextid=' . $context->id, get_string('changessaved'), 1);
}
/// Work out an appropriate page title.
if ($forfilter) {
$a = new stdClass;
$a->filter = filter_get_name($forfilter);
$a->context = $contextname;
$title = get_string('filtersettingsforin', 'filters', $a);
} else {
$title = get_string('filtersettingsin', 'filters', $contextname);
}
$straction = get_string('filters', 'admin'); // Used by tabs.php
/// Print the header and tabs
if ($isfrontpage) {
admin_externalpage_setup('frontpagefilters');
echo $OUTPUT->header();
} else {
$PAGE->set_cacheable(false);
$PAGE->set_title($title);
$PAGE->set_pagelayout('admin');
echo $OUTPUT->header();
}
/// Print heading.
echo $OUTPUT->heading_with_help($title, 'filtersettings', 'filters');
if (empty($availablefilters)) {
echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
} else if ($forfilter) {
$current = filter_get_local_config($forfilter, $contextid);
$settingsform->set_data((object) $current);
$settingsform->display();
} else {
$settingscol = false;
foreach ($availablefilters as $filter => $notused) {
$hassettings = filter_has_local_settings($filter);
$availablefilters[$filter]->hassettings = $hassettings;
$settingscol = $settingscol || $hassettings;
}
$strsettings = get_string('settings');
$stroff = get_string('off', 'filters');
$stron = get_string('on', 'filters');
$strdefaultoff = get_string('defaultx', 'filters', $stroff);
$strdefaulton = get_string('defaultx', 'filters', $stron);
$activechoices = array(
TEXTFILTER_INHERIT => '',
TEXTFILTER_OFF => $stroff,
TEXTFILTER_ON => $stron,
);
echo html_writer::start_tag('form', array('action'=>$baseurl->out(), 'method'=>'post'));
echo html_writer::start_tag('div');
echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
$table = new html_table();
$table->head = array(get_string('filter'), get_string('isactive', 'filters'));
$table->align = array('left', 'left');
if ($settingscol) {
$table->head[] = $strsettings;
$table->align[] = 'left';
}
$table->width = ' ';
$table->data = array();
// iterate through filters adding to display table
foreach ($availablefilters as $filter => $filterinfo) {
$row = array();
// Filter name.
$row[] = filter_get_name($filter);
// Default/on/off choice.
if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
$activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
} else {
$activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
}
$row[] = html_writer::select($activechoices, str_replace('/', '_', $filter), $filterinfo->localstate, false);
// Settings link, if required
if ($settingscol) {
$settings = '';
if ($filterinfo->hassettings) {
$settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
}
$row[] = $settings;
}
$table->data[] = $row;
}
echo html_writer::table($table);
echo html_writer::start_tag('div', array('class'=>'buttons'));
echo html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'savechanges', 'value'=>get_string('savechanges')));
echo html_writer::end_tag('div');
echo html_writer::end_tag('div');
echo html_writer::end_tag('form');
}
/// Appropriate back link.
if (!$isfrontpage) {
echo html_writer::start_tag('div', array('class'=>'backlink'));
echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
echo html_writer::end_tag('div');
}
echo $OUTPUT->footer();