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 |
|
|
|
31 |
/** @var int The default filter type */
|
|
|
32 |
const JOINTYPE_DEFAULT = datafilter::JOINTYPE_ANY;
|
|
|
33 |
|
|
|
34 |
/** @var ?array Filter properties for this condition. */
|
|
|
35 |
public ?array $filter;
|
|
|
36 |
|
|
|
37 |
/** @var string SQL fragment to add to the where clause. */
|
|
|
38 |
protected string $where = '';
|
|
|
39 |
|
|
|
40 |
/** @var array query param used in where. */
|
|
|
41 |
protected array $params = [];
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Return title of the condition
|
|
|
45 |
*
|
|
|
46 |
* @return string title of the condition
|
|
|
47 |
*/
|
|
|
48 |
abstract public function get_title();
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Return filter class associated with this condition
|
|
|
52 |
*
|
|
|
53 |
* @return string filter class
|
|
|
54 |
*/
|
|
|
55 |
abstract public function get_filter_class();
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Extract the required filter from the provided question bank view.
|
|
|
59 |
*
|
|
|
60 |
* This will look for the filter matching {@see get_condition_key()}
|
|
|
61 |
*
|
|
|
62 |
* @param view|null $qbank
|
|
|
63 |
*/
|
|
|
64 |
public function __construct(view $qbank = null) {
|
|
|
65 |
if (is_null($qbank)) {
|
|
|
66 |
return;
|
|
|
67 |
}
|
|
|
68 |
$this->filter = static::get_filter_from_list($qbank->get_pagevars('filter'));
|
|
|
69 |
// Build where and params.
|
|
|
70 |
[$this->where, $this->params] = $this->filter ? static::build_query_from_filter($this->filter) : ['', []];
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Whether customisation is allowed.
|
|
|
75 |
*
|
|
|
76 |
* @return bool
|
|
|
77 |
*/
|
|
|
78 |
public function allow_custom() {
|
|
|
79 |
return true;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Whether multiple values are allowed .
|
|
|
84 |
*
|
|
|
85 |
* @return bool
|
|
|
86 |
*/
|
|
|
87 |
public function allow_multiple() {
|
|
|
88 |
return true;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Initial values of the condition
|
|
|
93 |
*
|
|
|
94 |
* @return array
|
|
|
95 |
*/
|
|
|
96 |
public function get_initial_values() {
|
|
|
97 |
return [];
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Extra data specific to this condition.
|
|
|
102 |
*
|
|
|
103 |
* @return \stdClass
|
|
|
104 |
*/
|
|
|
105 |
public function get_filteroptions(): \stdClass {
|
|
|
106 |
return (object)[];
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Whether empty value is allowed
|
|
|
111 |
*
|
|
|
112 |
* @return bool
|
|
|
113 |
*/
|
|
|
114 |
public function allow_empty() {
|
|
|
115 |
return true;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Whether this filter is required - if so it cannot be removed from the list of filters.
|
|
|
120 |
*
|
|
|
121 |
* @return bool
|
|
|
122 |
*/
|
|
|
123 |
public function is_required(): bool {
|
|
|
124 |
return false;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Return this condition class
|
|
|
129 |
*
|
|
|
130 |
* @return string
|
|
|
131 |
*/
|
|
|
132 |
public function get_condition_class() {
|
|
|
133 |
return get_class($this);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Each condition will need a unique key to be identified and sequenced by the api.
|
|
|
138 |
* Use a unique string for the condition identifier, use string directly, dont need to use language pack.
|
|
|
139 |
* Using language pack might break the filter object for multilingual support.
|
|
|
140 |
*
|
|
|
141 |
* @return string
|
|
|
142 |
*/
|
|
|
143 |
public static function get_condition_key() {
|
|
|
144 |
return '';
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Return an SQL fragment to be ANDed into the WHERE clause to filter which questions are shown.
|
|
|
149 |
*
|
|
|
150 |
* @return string SQL fragment. Must use named parameters.
|
|
|
151 |
*/
|
|
|
152 |
public function where() {
|
|
|
153 |
return $this->where;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Return parameters to be bound to the above WHERE clause fragment.
|
|
|
158 |
* @return array parameter name => value.
|
|
|
159 |
*/
|
|
|
160 |
public function params() {
|
|
|
161 |
return $this->params;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Display GUI for selecting criteria for this condition. Displayed when Show More is open.
|
|
|
166 |
*
|
|
|
167 |
* Compare display_options(), which displays always, whether Show More is open or not.
|
|
|
168 |
* @return bool|string HTML form fragment
|
|
|
169 |
* @deprecated since Moodle 4.0 MDL-72321 - please do not use this function any more.
|
|
|
170 |
* @todo Final deprecation on Moodle 4.1 MDL-72572
|
|
|
171 |
*/
|
|
|
172 |
public function display_options_adv() {
|
|
|
173 |
debugging('Function display_options_adv() is deprecated, please use filtering objects', DEBUG_DEVELOPER);
|
|
|
174 |
return false;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Display GUI for selecting criteria for this condition. Displayed always, whether Show More is open or not.
|
|
|
179 |
*
|
|
|
180 |
* Compare display_options_adv(), which displays when Show More is open.
|
|
|
181 |
* @return bool|string HTML form fragment
|
|
|
182 |
* @deprecated since Moodle 4.0 MDL-72321 - please do not use this function any more.
|
|
|
183 |
* @todo Final deprecation on Moodle 4.1 MDL-72572
|
|
|
184 |
*/
|
|
|
185 |
public function display_options() {
|
|
|
186 |
debugging('Function display_options() is deprecated, please use filtering objects', DEBUG_DEVELOPER);
|
|
|
187 |
return false;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Get the list of available joins for the filter.
|
|
|
192 |
*
|
|
|
193 |
* @return array
|
|
|
194 |
*/
|
|
|
195 |
public function get_join_list(): array {
|
|
|
196 |
return [
|
|
|
197 |
datafilter::JOINTYPE_NONE,
|
|
|
198 |
datafilter::JOINTYPE_ANY,
|
|
|
199 |
datafilter::JOINTYPE_ALL,
|
|
|
200 |
];
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Given an array of filters, pick the entry that matches the condition key and return it.
|
|
|
205 |
*
|
|
|
206 |
* @param array $filters Array of filters, keyed by condition.
|
|
|
207 |
* @return ?array The filter that matches this condition
|
|
|
208 |
*/
|
|
|
209 |
public static function get_filter_from_list(array $filters): ?array {
|
|
|
210 |
return $filters[static::get_condition_key()] ?? null;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Build query from filter value
|
|
|
215 |
*
|
|
|
216 |
* @param array $filter filter properties
|
|
|
217 |
* @return array where sql and params
|
|
|
218 |
*/
|
|
|
219 |
public static function build_query_from_filter(array $filter): array {
|
|
|
220 |
return ['', []];
|
|
|
221 |
}
|
|
|
222 |
}
|