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
namespace tool_usertours\local\filter;
18
 
19
use tool_usertours\tour;
20
use context;
21
 
22
/**
23
 * Course filter.
24
 *
25
 * @package    tool_usertours
26
 * @copyright  2017 The Open University
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class course extends base {
1441 ariadna 30
    /** @var string Option to select all courses. */
31
    public const OPERATOR_ALL = 'all';
32
    /** @var string Option to select specific courses. */
33
    public const OPERATOR_SELECT = 'select';
34
    /** @var string Option to select all courses except specific courses. */
35
    public const OPERATOR_EXCEPT = 'except';
36
    /** @var string The filter operator key constant. */
37
    public const OPERATOR_KEY = 'course_operator';
38
    /** @var string The filter key constant. */
39
    public const FILTER_KEY = 'filter_course';
40
 
1 efrain 41
    /**
42
     * The name of the filter.
43
     *
44
     * @return  string
45
     */
1441 ariadna 46
    public static function get_filter_name(): string {
1 efrain 47
        return 'course';
48
    }
49
 
50
    /**
51
     * Overrides the base add form element with a course selector.
52
     *
53
     * @param \MoodleQuickForm $mform
54
     */
55
    public static function add_filter_to_form(\MoodleQuickForm &$mform) {
1441 ariadna 56
        // Add the operator selector.
57
        $operatorkey = 'filter_' . self::OPERATOR_KEY;
58
        $mform->addElement('select', $operatorkey, get_string($operatorkey, 'tool_usertours'), static::get_operator_options());
59
        $mform->setDefault($operatorkey, static::OPERATOR_ALL);
60
        $mform->addHelpButton($operatorkey, $operatorkey, 'tool_usertours');
61
 
62
        // Add the course selector.
63
        $key = self::FILTER_KEY;
1 efrain 64
        $options = ['multiple' => true];
1441 ariadna 65
        $mform->addElement("course", $key, get_string($key, 'tool_usertours'), $options);
1 efrain 66
        $mform->setDefault($key, '0');
67
        $mform->addHelpButton($key, $key, 'tool_usertours');
1441 ariadna 68
        $mform->hideIf($key, $operatorkey, 'eq', self::OPERATOR_ALL);
1 efrain 69
    }
70
 
71
    /**
1441 ariadna 72
     * Validate form data specific to the course filter.
73
     *
74
     * @param array $data The current form data.
75
     * @param array $files The current form files.
76
     * @return array Any validation errors for this filter.
77
     */
78
    public static function validate_form(array $data, array $files): array {
79
        $errors = [];
80
        $key = static::FILTER_KEY;
81
        $operatorkey = 'filter_' . self::OPERATOR_KEY;
82
        if ($data[$operatorkey] !== static::OPERATOR_ALL && empty($data[$key])) {
83
            $errors[$key] = get_string('filter_course_error_course_selection', 'tool_usertours');
84
        }
85
 
86
        return $errors;
87
    }
88
 
89
    /**
1 efrain 90
     * Check whether the filter matches the specified tour and/or context.
91
     *
92
     * @param   tour        $tour       The tour to check
93
     * @param   context     $context    The context to check
94
     * @return  boolean
95
     */
1441 ariadna 96
    public static function filter_matches(tour $tour, context $context): bool {
1 efrain 97
        global $COURSE;
1441 ariadna 98
        $values = $tour->get_filter_values(static::get_filter_name());
99
        $operator = $tour->get_filter_values(static::OPERATOR_KEY)[0] ?? static::OPERATOR_ALL;
100
 
1 efrain 101
        if (empty($values) || empty($values[0])) {
102
            return true;
103
        }
1441 ariadna 104
 
1 efrain 105
        if (empty($COURSE->id)) {
106
            return false;
107
        }
1441 ariadna 108
 
109
        return match ($operator) {
110
            static::OPERATOR_SELECT => in_array($COURSE->id, $values),
111
            static::OPERATOR_EXCEPT => !in_array($COURSE->id, $values),
112
            default => true,
113
        };
1 efrain 114
    }
115
 
116
    /**
117
     * Overrides the base prepare the filter values for the form with an integer value.
118
     *
119
     * @param   tour            $tour       The tour to prepare values from
120
     * @param   stdClass        $data       The data value
121
     * @return  stdClass
122
     */
123
    public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
1441 ariadna 124
        // Prepare the operator value.
125
        $operatorfiltername = static::OPERATOR_KEY;
126
        $operatorkey = 'filter_' . $operatorfiltername;
127
        $operator = $tour->get_filter_values($operatorfiltername)[0] ?? static::OPERATOR_ALL;
128
        $data->$operatorkey = $operator;
129
 
130
        // Prepare the course value.
1 efrain 131
        $filtername = static::get_filter_name();
1441 ariadna 132
        $key = 'filter_' . $filtername;
133
        $values = $tour->get_filter_values($filtername) ?: 0;
134
        $data->$key = $data->$operatorkey === static::OPERATOR_ALL ? 0 : $values;
135
 
1 efrain 136
        return $data;
137
    }
138
 
139
    /**
140
     * Overrides the base save the filter values from the form to the tour.
141
     *
142
     * @param   tour            $tour       The tour to save values to
143
     * @param   stdClass        $data       The data submitted in the form
144
     */
1441 ariadna 145
    public static function save_filter_values_from_form(
146
        tour $tour,
147
        \stdClass $data,
148
    ) {
149
        $operatorfiltername = static::OPERATOR_KEY;
150
        $operatorkey = 'filter_' . $operatorfiltername;
151
        $tour->set_filter_values($operatorfiltername, [$data->$operatorkey]);
1 efrain 152
        $filtername = static::get_filter_name();
1441 ariadna 153
        if ($data->$operatorkey === static::OPERATOR_ALL) {
1 efrain 154
            $newvalue = [];
1441 ariadna 155
        } else {
156
            $key = 'filter_' . $filtername;
157
            $newvalue = $data->$key;
158
            if (empty($data->$key)) {
159
                $newvalue = [];
160
            }
1 efrain 161
        }
162
        $tour->set_filter_values($filtername, $newvalue);
163
    }
1441 ariadna 164
 
165
    /**
166
     * Retrieve the available operator options.
167
     *
168
     * @return string[] The available operator options.
169
     */
170
    public static function get_operator_options(): array {
171
        $operatorkey = 'filter_' . self::OPERATOR_KEY;
172
        return [
173
            static::OPERATOR_ALL => get_string($operatorkey . '_' . static::OPERATOR_ALL, 'tool_usertours'),
174
            static::OPERATOR_SELECT => get_string($operatorkey . '_' . static::OPERATOR_SELECT, 'tool_usertours'),
175
            static::OPERATOR_EXCEPT => get_string($operatorkey . '_' . static::OPERATOR_EXCEPT, 'tool_usertours'),
176
        ];
177
    }
1 efrain 178
}