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 core_question\local\bank;
18
 
19
use core\output\datafilter;
20
 
21
/**
22
 * An abstract class for filtering/searching questions.
23
 *
24
 * @package    core_question
25
 * @copyright  2013 Ray Morris
26
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
abstract class condition {
30
    /** @var int The default filter type */
31
    const JOINTYPE_DEFAULT = datafilter::JOINTYPE_ANY;
32
 
33
    /** @var ?array Filter properties for this condition. */
34
    public ?array $filter;
35
 
36
    /** @var string SQL fragment to add to the where clause. */
37
    protected string $where = '';
38
 
39
    /** @var array query param used in where. */
40
    protected array $params = [];
41
 
42
    /**
43
     * Return title of the condition
44
     *
45
     * @return string title of the condition
46
     */
47
    abstract public function get_title();
48
 
49
    /**
1441 ariadna 50
     * Return the Javascript filter class to provide the UI for this condition.
1 efrain 51
     *
1441 ariadna 52
     * If left as null, this will use the default core/datafilter/filtertype class. Otherwise, override it to return
53
     * the full path to the Javascript module path for the class.
54
     *
55
     * @return ?string filter class
1 efrain 56
     */
1441 ariadna 57
    public function get_filter_class() {
58
        return null;
59
    }
1 efrain 60
 
61
    /**
1441 ariadna 62
     * Extract the required filter from the provided question bank view and set the initial values.
1 efrain 63
     *
1441 ariadna 64
     * This will look for the filter matching {@see get_condition_key()} in the view's current filter parameter.
65
     * If the filter is not being initialised to display the question bank UI (for example, to resolve a list of questions matching
66
     * a set of filters), then the `$qbank` argument may be null, and any usage of it to set the initial filter state is skipped.
1 efrain 67
     *
1441 ariadna 68
     * @param ?view $qbank The question bank view the filter is being rendered for. This may only be used for setting the
69
     *     initial state of the filter.
1 efrain 70
     */
1441 ariadna 71
    public function __construct(?view $qbank = null) {
1 efrain 72
        if (is_null($qbank)) {
73
            return;
74
        }
75
        $this->filter = static::get_filter_from_list($qbank->get_pagevars('filter'));
76
        // Build where and params.
77
        [$this->where, $this->params] = $this->filter ? static::build_query_from_filter($this->filter) : ['', []];
78
    }
79
 
80
    /**
81
     * Whether customisation is allowed.
82
     *
83
     * @return bool
84
     */
85
    public function allow_custom() {
86
        return true;
87
    }
88
 
89
    /**
90
     * Whether multiple values are allowed .
91
     *
92
     * @return bool
93
     */
94
    public function allow_multiple() {
95
        return true;
96
    }
97
 
98
    /**
99
     * Initial values of the condition
100
     *
101
     * @return array
102
     */
103
    public function get_initial_values() {
104
        return [];
105
    }
106
 
107
    /**
108
     * Extra data specific to this condition.
109
     *
110
     * @return \stdClass
111
     */
112
    public function get_filteroptions(): \stdClass {
113
        return (object)[];
114
    }
115
 
116
    /**
117
     * Whether empty value is allowed
118
     *
119
     * @return bool
120
     */
121
    public function allow_empty() {
122
        return true;
123
    }
124
 
125
    /**
126
     * Whether this filter is required - if so it cannot be removed from the list of filters.
127
     *
128
     * @return bool
129
     */
130
    public function is_required(): bool {
131
        return false;
132
    }
133
 
134
    /**
135
     * Return this condition class
136
     *
137
     * @return string
138
     */
139
    public function get_condition_class() {
140
        return get_class($this);
141
    }
142
 
143
    /**
144
     * Each condition will need a unique key to be identified and sequenced by the api.
145
     * Use a unique string for the condition identifier, use string directly, dont need to use language pack.
146
     * Using language pack might break the filter object for multilingual support.
147
     *
148
     * @return string
149
     */
1441 ariadna 150
    abstract public static function get_condition_key();
1 efrain 151
 
152
    /**
153
     * Return an SQL fragment to be ANDed into the WHERE clause to filter which questions are shown.
154
     *
155
     * @return string SQL fragment. Must use named parameters.
156
     */
157
    public function where() {
158
        return $this->where;
159
    }
160
 
161
    /**
162
     * Return parameters to be bound to the above WHERE clause fragment.
163
     * @return array parameter name => value.
164
     */
165
    public function params() {
166
        return $this->params;
167
    }
168
 
169
    /**
170
     * Display GUI for selecting criteria for this condition. Displayed when Show More is open.
171
     *
172
     * Compare display_options(), which displays always, whether Show More is open or not.
173
     * @return bool|string HTML form fragment
174
     * @deprecated since Moodle 4.0 MDL-72321 - please do not use this function any more.
175
     * @todo Final deprecation on Moodle 4.1 MDL-72572
176
     */
177
    public function display_options_adv() {
178
        debugging('Function display_options_adv() is deprecated, please use filtering objects', DEBUG_DEVELOPER);
179
        return false;
180
    }
181
 
182
    /**
183
     * Display GUI for selecting criteria for this condition. Displayed always, whether Show More is open or not.
184
     *
185
     * Compare display_options_adv(), which displays when Show More is open.
186
     * @return bool|string HTML form fragment
187
     * @deprecated since Moodle 4.0 MDL-72321 - please do not use this function any more.
188
     * @todo Final deprecation on Moodle 4.1 MDL-72572
189
     */
190
    public function display_options() {
191
        debugging('Function display_options() is deprecated, please use filtering objects', DEBUG_DEVELOPER);
192
        return false;
193
    }
194
 
195
    /**
196
     * Get the list of available joins for the filter.
197
     *
198
     * @return array
199
     */
200
    public function get_join_list(): array {
201
        return [
202
            datafilter::JOINTYPE_NONE,
203
            datafilter::JOINTYPE_ANY,
204
            datafilter::JOINTYPE_ALL,
205
        ];
206
    }
207
 
208
    /**
1441 ariadna 209
     * Method to be overridden in condition classes to filter out anything invalid from the filterconditions array.
210
     *
211
     * This can be applied anywhere where the $filterconditions array exists, to let condition plugins remove elements
212
     *  from the array, based on their own internal logic/validation. For example, this is used on the
213
     *  /mod/quiz/editrandom.php page to filter out question categories which no longer exist, which previously
214
     *  broke the editrandom page.
215
     *
216
     * @param array $filterconditions
217
     * @return array
218
     */
219
    public function filter_invalid_values(array $filterconditions): array {
220
        return $filterconditions;
221
    }
222
 
223
    /**
1 efrain 224
     * Given an array of filters, pick the entry that matches the condition key and return it.
225
     *
226
     * @param array $filters Array of filters, keyed by condition.
227
     * @return ?array The filter that matches this condition
228
     */
229
    public static function get_filter_from_list(array $filters): ?array {
230
        return $filters[static::get_condition_key()] ?? null;
231
    }
232
 
233
    /**
1441 ariadna 234
     * Return the SQL WHERE condition and parameters to be ANDed with other filter conditions.
1 efrain 235
     *
1441 ariadna 236
     * The $filter parameter recieves an array with a 'values' key, containing an array of the filter values selected,
237
     * and a 'jointype' key containing the selected join type.
238
     *
1 efrain 239
     * @param array $filter filter properties
1441 ariadna 240
     * @return array ['SQL where condition', ['param1' => 'value1', 'param2' => 'value2', ...]]
1 efrain 241
     */
1441 ariadna 242
    abstract public static function build_query_from_filter(array $filter): array;
1 efrain 243
}