Skip to content

Commit 686f62c

Browse files
committed
MDL-83860 qbank_viewquestionname: Add question name filter condition
1 parent a798b1d commit 686f62c

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

question/bank/viewquestionname/classes/plugin_feature.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace qbank_viewquestionname;
1818

1919
use core_question\local\bank\plugin_features_base;
20+
use core_question\local\bank\view;
2021

2122
/**
2223
* Plugin entrypoint for columns.
@@ -27,10 +28,17 @@
2728
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2829
*/
2930
class plugin_feature extends plugin_features_base {
30-
31+
#[\Override]
3132
public function get_question_columns($qbank): array {
3233
return [
3334
new question_name_idnumber_tags_column($qbank)
3435
];
3536
}
37+
38+
#[\Override]
39+
public function get_question_filters(?view $qbank = null): array {
40+
return [
41+
new question_name_condition($qbank),
42+
];
43+
}
3644
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 qbank_viewquestionname;
18+
19+
use core\output\datafilter;
20+
21+
/**
22+
* Filter condition for filtering on the question name
23+
*
24+
* @package qbank_viewquestionname
25+
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
26+
* @author Mark Johnson <[email protected]>
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class question_name_condition extends \core_question\local\bank\condition {
30+
#[\Override]
31+
public function get_title() {
32+
return get_string('questionnamecondition', 'qbank_viewquestionname');
33+
}
34+
35+
#[\Override]
36+
public static function get_condition_key() {
37+
return 'questionname';
38+
}
39+
40+
#[\Override]
41+
public function get_filter_class() {
42+
return 'core/datafilter/filtertypes/keyword';
43+
}
44+
45+
/**
46+
* Return an SQL condition and parameters for filtering on q.name.
47+
*
48+
* @return array
49+
*/
50+
public static function build_query_from_filter(array $filter): array {
51+
global $DB;
52+
53+
$conditions = [];
54+
$params = [];
55+
$notlike = $filter['jointype'] === datafilter::JOINTYPE_NONE;
56+
foreach ($filter['values'] as $key => $value) {
57+
$params["questionname{$key}"] = "%$value%";
58+
$conditions[] = $DB->sql_like('q.name', ":questionname{$key}", notlike: $notlike);
59+
}
60+
$delimiter = $filter['jointype'] === datafilter::JOINTYPE_ANY ? ' OR ' : ' AND ';
61+
return [
62+
implode($delimiter, $conditions),
63+
$params,
64+
];
65+
}
66+
}

question/bank/viewquestionname/lang/en/qbank_viewquestionname.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
$string['pluginname'] = 'View question name';
2727
$string['privacy:metadata'] = 'The View question name question bank plugin does not store any personal data.';
28+
$string['questionnamecondition'] = 'Question name';
2829
// In place editing.
2930
$string['edit_question_name_hint'] = 'Edit question name';
3031
$string['edit_question_name_label'] = 'New value for {$a->name}';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
@qbank @qbank_viewquestionnname @javascript
2+
Feature: Filter questions by name
3+
As a teacher
4+
In order to organise my questions
5+
I want to filter the list of questions by name
6+
7+
Background:
8+
Given the following "courses" exist:
9+
| fullname | shortname | category |
10+
| Course 1 | C1 | 0 |
11+
And the following "activities" exist:
12+
| activity | name | intro | course | idnumber |
13+
| qbank | Qbank 1 | Question bank 1 | C1 | qbank1 |
14+
And the following "question categories" exist:
15+
| contextlevel | reference | name |
16+
| Activity module | qbank1 | Test questions |
17+
And the following "questions" exist:
18+
| questioncategory | qtype | name | questiontext |
19+
| Test questions | truefalse | First question | Answer the first question |
20+
| Test questions | numerical | Second question | Answer the second question |
21+
| Test questions | essay | Third question | Answer the third question |
22+
23+
Scenario: Filter by a single word
24+
Given I am on the "Qbank 1" "core_question > question bank" page logged in as "admin"
25+
When I apply question bank filter "Question name" with value "First"
26+
Then I should see "First question"
27+
And I should not see "Second question"
28+
And I should not see "Third question"
29+
30+
Scenario: Filter by any word
31+
Given I am on the "Qbank 1" "core_question > question bank" page logged in as "admin"
32+
When I apply question bank filter "Question name" with value "First, Third"
33+
Then I should see "First question"
34+
And I should not see "Second question"
35+
And I should see "Third question"
36+
37+
Scenario: Filter by all words
38+
Given I am on the "Qbank 1" "core_question > question bank" page logged in as "admin"
39+
When I add question bank filter "Question name"
40+
And I set the field "Question name" to "question, d"
41+
And I set the field "Match" in the "Filter 3" "fieldset" to "All"
42+
And I press "Apply filters"
43+
Then I should not see "First question"
44+
And I should see "Second question"
45+
And I should see "Third question"
46+
47+
Scenario: Exclude names by filter
48+
Given I am on the "Qbank 1" "core_question > question bank" page logged in as "admin"
49+
When I add question bank filter "Question name"
50+
And I set the field "Question name" to "First, Third"
51+
And I set the field "Match" in the "Filter 3" "fieldset" to "None"
52+
And I press "Apply filters"
53+
Then I should not see "First question"
54+
And I should see "Second question"
55+
And I should not see "Third question"

0 commit comments

Comments
 (0)