Skip to content

Commit 24264ad

Browse files
committed
[docs] Backport quesiton bank filter docs to 4.3/4.4/4.5
1 parent feac6c1 commit 24264ad

6 files changed

Lines changed: 659 additions & 4 deletions

File tree

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
---
2+
title: Question bank filters
3+
tags:
4+
- Plugins
5+
- Question
6+
- qbank
7+
description: Question bank plugins allow you to define new filters for the question bank view and random question sets.
8+
documentationDraft: true
9+
---
10+
11+
<Since
12+
version="4.3"
13+
issueNumber="MDL-72321"
14+
/>
15+
16+
Question bank plugins allow you define additional filters. These can be used when viewing the question bank, and are included in the URL so that a filtered view of the question bank can be shared. They are also used when defining the criteria for adding random questions to a quiz.
17+
18+
## Creating a new filter condition
19+
20+
A filter condition consists of two parts - the backend "condition" PHP class, and the frontend "filter" JavaScript class.
21+
22+
The "condition" class defines the general properties of the filter - its name, various options, and how it is applied to the question bank query.
23+
The "filter" class defines how the filter is displayed in the UI, and how values selected in the UI are passed back to the condition.
24+
25+
Each new filter condition must define a new "condition" class in the qbank plugin based on `core_question\local\bank\condition`.
26+
By default this will use the `core/datafilter/filtertype` "filter" class, although this can be overridden too if required.
27+
28+
### Basic example
29+
30+
This outlines the bare minimum required to implement a new filter condition. This will give you a field that allows you to enter keywords and add them to a list of selected search terms, the filter the questions by that list of terms.
31+
This assumes that you already have the basic framework of a qbank plugin in place. For real-world examples, look for classes that extend `core_question\local\bank\condition`.
32+
33+
Create a `condition` class within your plugin's namespace. For a plugin called `qbank_myplugin` this would look something like:
34+
35+
```php title="question/bank/myplugin/classes/myfilter_condition.php"
36+
namespace qbank_myplugin;
37+
38+
use core_question\local\bank\condition;
39+
40+
class myfilter_condition extends condition {
41+
42+
}
43+
```
44+
45+
Modify your `plugin_feature` class to return an instance of your condition from the `get_question_filters()` method:
46+
47+
```php title="question/bank/myplugin/classes/plugin_feature.php"
48+
namespace qbank_myplugin;
49+
50+
class plugin_feature extends core_question\local\bank\plugin_features_base {
51+
public function get_question_filters(?core_question\local\bank\view $qbank = null): array {
52+
return [
53+
new myfilter_condition($qbank),
54+
];
55+
}
56+
}
57+
```
58+
59+
Back in your `condition` class, define the `get_name()` method, which returns the label displayed in the filter UI.
60+
61+
```php title="Define the condition name"
62+
public function get_name(): string {
63+
return get_string('myfilter_name', 'myplugin');
64+
}
65+
```
66+
67+
Define `get_condition_key()`, which returns a unique machine-readable ID for this filter condition, used when passing the filter as a parameter.
68+
69+
```php title="Define the condition key"
70+
public function get_condition_key(): string {
71+
return 'myfilter';
72+
}
73+
```
74+
75+
To actually filter the results, define `build_query_from_filter()` which returns an SQL `WHERE` condition, and an array of parameters.
76+
The `$filter` parameter receives an array with a `'values'` key, containing an array of the selected values, and a `'jointype'` key, containing one of the `JOINTTYPE_ANY`, `JOINTYPE_ALL` or `JOINTYPE_NONE` constants. Use these to build your condition as required.
77+
78+
The conditions from each filter are combined with the query in [`core_question\local\bank\view::build_query()`](https://github.com/moodle/moodle/blob/c741492c38b9945abbfc7e90dfe8f943279f8265/question/classes/local/bank/view.php#L733)
79+
80+
```php title="Filter questions"
81+
public function build_query_from_filter(array $filter): array {
82+
$andor = ' AND ';
83+
$equal = '=';
84+
if ($filter['jointype'] === self::JOINTYPE_ANY) {
85+
$andor = ' OR ';
86+
} else if ($filter['jointype'] === self::JOINTYPE_NONE) {
87+
$equal = '!=';
88+
}
89+
$conditions = [];
90+
$params = [];
91+
// In real life we'd probably use $DB->get_in_or_equal here.
92+
foreach ($filter['values'] as $key => $value) {
93+
$conditions[] = 'q.fieldname ' . $equal . ' :myfilter' . $key;
94+
$params['myfilter' . $key] = $value;
95+
}
96+
return [
97+
'(' . implode($andor, $conditions) . ')',
98+
$params,
99+
];
100+
}
101+
```
102+
103+
Following this pattern with your own fields and options will give you a basic functional filter. Most filters will require more complex functionality, which can be achieved through additional methods.
104+
105+
### Additional options
106+
107+
#### Pre-defined values
108+
109+
To define the list of possible filter values, define `get_initial_values()`, which returns an array of `['value', 'title']` for each option. These will then be searchable and selectable in the autocomplete field.
110+
111+
```php title="Define initial filter values"
112+
public function get_initial_values(): string {
113+
return [
114+
[
115+
'value' => 0,
116+
'title' => 'Option 1',
117+
],
118+
[
119+
'value' => 1,
120+
'title' => 'Option 2',
121+
]
122+
];
123+
}
124+
```
125+
126+
#### Restrict custom keywords
127+
128+
To restrict the possible filter terms to only those returned from `get_initial_values()`, define `allow_custom()` and have it return `false`.
129+
130+
```php title="Disable custom terms"
131+
public function allow_custom(): bool {
132+
return false;
133+
}
134+
```
135+
136+
#### Restrict join types
137+
138+
Not all join types are relevant to all filters. If each question will only match one of the selected values, it does not make sense to allow `JOINTYPE_ALL`. Define `get_join_list()` and return an array of the applicable join types.
139+
140+
```php title="Define a restricted list of join types"
141+
public function get_join_list(): array {
142+
return [
143+
datafilter::JOINTYPE_ANY,
144+
datafilter::JOINTYPE_NONE,
145+
];
146+
}
147+
```
148+
149+
#### Allow multiple values?
150+
151+
By default, conditions allow multiple values to be selected and use the selected join type to decide how they are applied.
152+
If your condition should only allow a single value at a time, override `allow_multiple()` to return false.
153+
154+
```php title="Disable selection of multiple values"
155+
public function allow_multiple(): bool {
156+
return false;
157+
}
158+
```
159+
160+
#### Allow empty values?
161+
162+
By default, conditions can be left empty, and therefore will not be included in the filter. To make it compulsory to select a value for this condition when it is added, override `allow_empty()` to return false.
163+
164+
```php title="Disable empty values"
165+
public function allow_empty(): bool {
166+
return false;
167+
}
168+
```
169+
170+
#### Is the condition required?
171+
172+
If it is compulsory that your condition is always displayed, override `is_required()` to return true.
173+
174+
```php title="Make the condition compulsory"
175+
public function is_required(): bool {
176+
return true;
177+
}
178+
```
179+
180+
#### Custom filter class
181+
182+
By default, the filter will be displayed and processed using the `core/datafilter/filtertype` JavaScript class.
183+
This will provide a single autocomplete field for selecting one or multiple numeric IDs with textual labels.
184+
If this does not fit your filter's use case, you can tell your condition to use a different filter class.
185+
186+
You can either use a different core filter type from `/lib/amd/src/datafilter/filtertypes`, or define your own.
187+
188+
To tell your filter condition to use a different filter class, override the `get_filter_class()` method to return the namespaced path to your JavaScript class.
189+
190+
```php title="Override the default filter class"
191+
public function get_filter_class(): string {
192+
return 'qbank_myplugin/datafilter/filtertype/myfilter';
193+
}
194+
```
195+
196+
To create your own filter class, a new JavaScript file in your plugin under `amd/src/datafilter/filtertypes/myfilter.js`.
197+
In this file, export a default class that extends `core/datafilter/filtertype` (or another core filter type from `/lib/amd/src/datafilter/filtertypes`) and override the base methods as required.
198+
For example, if your filter uses textual rather than numeric values, you can override `get values()` to return the raw values without running `parseInt()` (see [`qbank_viewquestiontype/datafilter/filtertypes/type`](https://github.com/moodle/moodle/blob/main/mod/quiz/tests/behat/editing_add_from_question_bank.feature)).
199+
200+
If you want a different UI for selecting your filter values instead of a single autocomplete, you can override `addValueSelector()`.
201+
This also provides flexibility over how the values provided by `get_initial_values()` are used by the UI.
202+
203+
#### Filter options
204+
205+
If your condition supports additional options as to how the selected values are applied to the query, such as whether child categories are included when parent categories are selected, you can define "Filter options".
206+
207+
In your condition class, define `get_filteroptions()` which returns an object containing the current filter options. You will probably want to add some code to the constructor to read in the current filter options, and some code the `build_query_from_filter()` to use the option.
208+
See [`qbank_managecategories\category_condition`](https://github.com/moodle/moodle/blob/main/question/bank/managecategories/classes/category_condition.php) as an example.
209+
210+
You JavaScript filter class will also need to support your filter options. Override the constructor an add additional code for the UI required to set your filter options, and override `get filterOptions()` to return the current value for any options set in this UI.
211+
See [`qbank_managecategories/datafilter/filtertypes/categories`](https://github.com/moodle/moodle/blob/main/question/bank/managecategories/amd/src/datafilter/filtertypes/categories.js) as an example.
212+
213+
#### Context-sensitive configuration
214+
215+
You may want your filter to behave differently depending on where it is being displayed. In this case you can override the constructor which receives the current `$qbank` view object, and extract some data that is used later on by your other methods.
216+
217+
For example, the [tag condition](https://github.com/moodle/moodle/blob/main/question/bank/tagquestion/classes/tag_condition.php) will find the context of the current page, and use that to control which tags are available in the filter.
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
---
2-
title: Question Bank plugins
2+
title: Question bank plugins
33
tags:
44
- Plugins
55
- Question
66
- qbank
7-
- Quiz
8-
description: Question type plugins allow you to extend the functionality of the Moodle Question bank.
7+
description: Question bank plugins allow you to extend the functionality of the Moodle Question bank.
98
documentationDraft: true
109
---
1110

@@ -14,10 +13,13 @@ documentationDraft: true
1413
issueNumber="MDL-70329"
1514
/>
1615

17-
Question type plugins allow you to extend the functionality of the Moodle Question bank, and support features including:
16+
Question bank plugins allow you to extend the functionality of the Moodle Question bank. They just one of the plugin types used by core_question. To see how they fit in, please read [this overview of the question subsystems](../subsystems/question/).
17+
18+
Question bank plugins can extend the question bank in many ways, including:
1819

1920
- Table columns
2021
- Action menu items
2122
- Bulk actions
2223
- Navigation node (tabs)
2324
- Question preview additions (via callback)
25+
- [Question filters](./filters.md)

0 commit comments

Comments
 (0)