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