Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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