@@ -30,9 +30,10 @@ By default this will use the `core/datafilter/filtertype` "filter" class, althou
3030
3131### Basic example
3232
33- This outlines the bare minimum required to implement a new filter condition. This will allow you to filter based on a pre-defined
34- list of values, selected from an autocomplete field. This assumes that you already have the basic framework of a qbank
35- plugin in place. For real-world examples, look for classes that extend ` core_question\local\bank\condition ` .
33+ This outlines the bare minimum required to implement a new filter condition. This will give you a field that allows you to
34+ enter keywords and add them to a list of selected search terms, the filter the questions by that list of terms.
35+ This assumes that you already have the basic framework of a qbank plugin in place. For real-world examples,
36+ look for classes that extend ` core_question\local\bank\condition ` .
3637
3738Create a ` condition ` class within your plugin's namespace. For a plugin called ` qbank_myplugin ` this would look something like:
3839
@@ -63,32 +64,6 @@ public function get_condition_key(): string {
6364}
6465```
6566
66- To define the list of possible filter values, define ` get_initial_values() ` , which returns an array of ` ['value', 'title'] ` for each
67- option.
68-
69- ``` php title=question/bank/myplugin/classes/myfilter_condition.php
70- public function get_initial_values(): string {
71- return [
72- [
73- 'value' => 0,
74- 'title' => 'Option 1',
75- ],
76- [
77- 'value' => 1,
78- 'title' => 'Option 2',
79- ]
80- ];
81- }
82- ```
83-
84- To prevent additional values being added by typing them into the autocomplete, define ` allow_custom() ` and have it return ` false ` .
85-
86- ``` php title=question/bank/myplugin/classes/myfilter_condition.php
87- public function allow_custom(): bool {
88- return false;
89- }
90- ```
91-
9267To actually filter the results, define ` build_query_from_filter() ` which returns an SQL ` WHERE ` condition, and an array of parameters.
9368The ` $filter ` parameter receives an array with a ` 'values' ` key, containing an array of the selected values, and a ` 'jointype' ` key,
9469containing one of the ` JOINTTYPE_ANY ` , ` JOINTYPE_ALL ` or ` JOINTYPE_NONE ` constants. Use these to build your condition as required.
@@ -121,39 +96,47 @@ more complex functionality, which can be achieved through additional methods.
12196
12297### Additional options
12398
124- #### Restrict join types
99+ #### Pre-defined values
125100
126- Not all join types are relevant to all filters. If each question will only match one of the selected values, it does not make
127- sense to allow JOINTYPE_ALL. Define ` get_join_list() ` and return an array of the applicable jointypes .
101+ To define the list of possible filter values, define ` get_initial_values() ` , which returns an array of ` ['value', 'title'] ` for each
102+ option. These will then be searchable and selectable in the autocomplete field .
128103
129104``` php title=question/bank/myplugin/classes/myfilter_condition.php
130- public function get_join_list (): array {
105+ public function get_initial_values (): string {
131106 return [
132- datafilter::JOINTYPE_ANY,
133- datafilter::JOINTYPE_NONE,
107+ [
108+ 'value' => 0,
109+ 'title' => 'Option 1',
110+ ],
111+ [
112+ 'value' => 1,
113+ 'title' => 'Option 2',
114+ ]
134115 ];
135116}
136117```
137118
138- #### Custom filter class
119+ #### Restrict custom keywords
139120
140- By default, the filter will be displayed and processed using the ` core/datafilter/filtertype ` JavaScript class.
141- This will provide a single autocomplete field for selecting one or multiple numeric IDs with textual labels.
142- If this does not fit your filter's use case, you will need to define your own filter class.
121+ To restrict the possible filter terms to only those returned from ` get_initial_values() ` , define ` allow_custom() ` and have it return ` false ` .
143122
144- Create a new JavaScript file in your plugin under ` amd/src/datafilter/filtertypes/myfilter.js ` .
145- In this file, export a default class that extends ` core/datafilter/filtertype `
146- (or another core filter type from '/lib/amd/src/datafilter/filtertypes') and override the base methods as required.
147- For example, if your filter uses textual rather than numeric values, you can override ` get values() ` to return the raw values
148- without running ` parseInt() ` (see ` types ` filter). If you want a different UI for selecting your filter values instead of a
149- single autocomplete, you can override ` addValueSelector() ` .
123+ ``` php title=question/bank/myplugin/classes/myfilter_condition.php
124+ public function allow_custom(): bool {
125+ return false;
126+ }
127+ ```
150128
151- To tell your filter condition to use a custom filter class, override the ` get_filter_class() ` method to return the namespaced
152- path to your JavaScript class.
129+ #### Restrict join types
130+
131+ Not all join types are relevant to all filters. If each question will only match one of the selected values, it does not make
132+ sense to allow JOINTYPE_ALL. Define ` get_join_list() ` and return an array of the applicable jointypes.
153133
154134``` php title=question/bank/myplugin/classes/myfilter_condition.php
155- public function get_filter_class(): string {
156- return 'qbank_myplugin/datafilter/filtertype/myfilter';
135+ public function get_join_list(): array {
136+ return [
137+ datafilter::JOINTYPE_ANY,
138+ datafilter::JOINTYPE_NONE,
139+ ];
157140}
158141```
159142
@@ -189,6 +172,33 @@ public function is_required(): bool {
189172}
190173```
191174
175+ #### Custom filter class
176+
177+ By default, the filter will be displayed and processed using the ` core/datafilter/filtertype ` JavaScript class.
178+ This will provide a single autocomplete field for selecting one or multiple numeric IDs with textual labels.
179+ If this does not fit your filter's use case, you can tell your condition to use a different filter class.
180+
181+ You can either use a different core filter type from ` /lib/amd/src/datafilter/filtertypes ` , or define your own.
182+
183+ To tell your filter condition to use a different filter class, override the ` get_filter_class() ` method to return the namespaced
184+ path to your JavaScript class.
185+
186+ ``` php title=question/bank/myplugin/classes/myfilter_condition.php
187+ public function get_filter_class(): string {
188+ return 'qbank_myplugin/datafilter/filtertype/myfilter';
189+ }
190+ ```
191+
192+ To create your own filter class, a new JavaScript file in your plugin under ` amd/src/datafilter/filtertypes/myfilter.js ` .
193+ In this file, export a default class that extends ` core/datafilter/filtertype `
194+ (or another core filter type from ` /lib/amd/src/datafilter/filtertypes ` ) and override the base methods as required.
195+ For example, if your filter uses textual rather than numeric values, you can override ` get values() ` to return the raw values
196+ without running ` parseInt() ` (see
197+ [ ` qbank_viewquestiontype/datafilter/filtertypes/type ` ] ( https://github.com/moodle/moodle/blob/main/mod/quiz/tests/behat/editing_add_from_question_bank.feature ) ).
198+
199+ If you want a different UI for selecting your filter values instead of a single autocomplete, you can override ` addValueSelector() ` .
200+ This also provides flexibility over how the values provided by ` get_initial_values() ` are used by the UI.
201+
192202#### Filter options
193203
194204If your condition supports additional options as to how the selected values are applied to the query, such as whether child
@@ -202,7 +212,8 @@ as an example.
202212
203213You JavaScript filter class will also need to support your filter options. Override the constructor an add additional code
204214for the UI required to set your filter options, and override ` get filterOptions() ` to return the current value for any options set
205- in this UI. See [ ` qbank_managecategories/datafilter/filtertypes/categories ` ] (https://github.com/moodle/moodle/blob/main/question/bank/managecategories/amd/src/datafilter/filtertypes/categories.js
215+ in this UI. See
216+ [ ` qbank_managecategories/datafilter/filtertypes/categories ` ] ( https://github.com/moodle/moodle/blob/main/question/bank/managecategories/amd/src/datafilter/filtertypes/categories.js )
206217as an example.
207218
208219#### Context-sensitive configuration
0 commit comments