Proyectos de Subversion Moodle

Rev

| 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
 * Actions controller
19
 *
20
 * Perform a synchronous action to modify the question bank UI and redirect back to the previous page.
21
 * These features are mostly progressively enhanced by actions.js and web services, but this remains as a fallback.
22
 *
23
 * @package   qbank_columnsortorder
24
 * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
25
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
26
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
 
29
require_once(__DIR__ . '/../../../config.php');
30
 
31
$action = required_param('action', PARAM_TEXT);
32
$global = optional_param('global', false, PARAM_BOOL);
33
$returnurl = optional_param('returnurl', '/question/bank/columnsortorder/sortcolumns.php', PARAM_LOCALURL);
34
 
35
require_login();
36
 
37
if ($global) {
38
    require_capability('moodle/site:config', context_system::instance());
39
}
40
 
41
if ($action === 'debugreset' && $CFG->debug === DEBUG_DEVELOPER) {
42
    $columnmanager = new \qbank_columnsortorder\column_manager($global);
43
    $columnmanager::set_hidden_columns([], $global);
44
    $columnmanager::set_column_order([], $global);
45
    $columnmanager::set_column_size('', $global);
46
    redirect(new moodle_url($returnurl));
47
}
48
 
49
require_sesskey();
50
$columnmanager = new \qbank_columnsortorder\column_manager($global);
51
switch ($action) {
52
    case 'add':
53
    case 'remove':
54
        $column = required_param('column', PARAM_RAW);
55
        [$columnclass, ] = explode(\core_question\local\bank\column_base::ID_SEPARATOR, $column);
56
        if (!class_exists($columnclass)) {
57
            throw new invalid_parameter_exception("'{$columnclass}' is not a valid column class.");
58
        }
59
        $hiddencolumns = $columnmanager->hiddencolumns;
60
        if ($action === 'add') {
61
            $key = array_search($column, $hiddencolumns);
62
            if ($key !== false) {
63
                unset($hiddencolumns[$key]);
64
            }
65
        } else {
66
            if (!in_array($column, $hiddencolumns)) {
67
                $hiddencolumns[] = $column;
68
            }
69
        }
70
        $columnmanager::set_hidden_columns($hiddencolumns, $global);
71
        break;
72
 
73
    case 'savewidths':
74
        $rawwidths = optional_param_array('width', [], PARAM_INT);
75
        $widths = [];
76
        foreach (array_filter($rawwidths) as $escapedclass => $width) {
77
            $class = str_replace('__', '\\', $escapedclass);
78
            // Validate that the class exists and the width is valid.
79
            // Since the browser uses Constraint Validation to prevent the form being submitted with an invalid width,
80
            // the only way we'll get one here is if someone is messing around, so don't worry about re-displaying the
81
            // form with an error message, just ignore the invalid value.
82
            if (class_exists($class) && $width >= 10) {
83
                $widths[] = (object)[
84
                    'column' => $class,
85
                    'width' => $width,
86
                ];
87
            }
88
        }
89
        $columnmanager::set_column_size(json_encode($widths), $global);
90
        break;
91
 
92
    case 'reset':
93
        $columnmanager::set_hidden_columns(null, $global);
94
        $columnmanager::set_column_order(null, $global);
95
        $columnmanager::set_column_size(null, $global);
96
        break;
97
}
98
redirect(new moodle_url($returnurl));