forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-83859 qbank_viewquestiontype: Add type filter
This adds a new question bank filter for filtering by question type. The filter can be applied with one or multiple question types, and can either include or exclude the selected types.
- Loading branch information
1 parent
1613a98
commit e46a0c3
Showing
7 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
question/bank/viewquestiontype/amd/build/datafilter/filtertype/type.min.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
question/bank/viewquestiontype/amd/build/datafilter/filtertype/type.min.js.map
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
question/bank/viewquestiontype/amd/src/datafilter/filtertype/type.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Filter managing hidden questions. | ||
* | ||
* @module qbank_viewquestiontype/datafilter/filtertypes/type | ||
* @author Mark Johnson <[email protected]> | ||
* @copyright 2024 Catalyst IT Europe Ltd | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
import Filter from 'core/datafilter/filtertype'; | ||
|
||
export default class extends Filter { | ||
/** | ||
* Get the list of values for this filter type. | ||
* | ||
* Overrides the default behaviour of running parseInt on the raw values, since we have textual | ||
* plugin identifiers and not numeric IDs. | ||
* | ||
* @returns {Array} | ||
*/ | ||
get values() { | ||
return this.rawValues; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
question/bank/viewquestiontype/classes/type_condition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace qbank_viewquestiontype; | ||
|
||
use question_bank; | ||
use core\output\datafilter; | ||
use core_question\local\bank\condition; | ||
|
||
/** | ||
* Filter condition for question type | ||
* | ||
* @package qbank_viewquestiontype | ||
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net} | ||
* @author Mark Johnson <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class type_condition extends condition { | ||
#[\Override] | ||
public static function get_condition_key() { | ||
return 'qtype'; | ||
} | ||
|
||
#[\Override] | ||
public function get_title() { | ||
return get_string('typefilter', 'qbank_viewquestiontype'); | ||
} | ||
|
||
#[\Override] | ||
public function get_filter_class() { | ||
return 'qbank_viewquestiontype/datafilter/filtertype/type'; | ||
} | ||
|
||
#[\Override] | ||
public function allow_custom() { | ||
return false; | ||
} | ||
|
||
/** | ||
* Get the list of available joins for the filter. | ||
* | ||
* Questions can be ANY or NONE of the selected types. Since questions cannot have multiple types, | ||
* allowing "ALL" does not make sense here. | ||
* | ||
* @return array | ||
*/ | ||
public function get_join_list(): array { | ||
return [ | ||
datafilter::JOINTYPE_NONE, | ||
datafilter::JOINTYPE_ANY, | ||
]; | ||
} | ||
|
||
/** | ||
* Build a list of the available question types. | ||
* | ||
* @return array | ||
*/ | ||
public function get_initial_values(): array { | ||
$types = question_bank::get_all_qtypes(); | ||
$values = []; | ||
foreach ($types as $plugin => $type) { | ||
$values[] = [ | ||
'value' => $plugin, | ||
'title' => get_string('pluginname', 'qtype_' . $plugin), | ||
]; | ||
} | ||
usort($values, fn($a, $b) => $a['title'] <=> $b['title']); | ||
return $values; | ||
} | ||
|
||
/** | ||
* Build a WHERE condition to filter the q.qtype column by the selected question types. | ||
* | ||
* @param array $filter | ||
* @return array | ||
* @throws \coding_exception | ||
* @throws \dml_exception | ||
*/ | ||
public static function build_query_from_filter(array $filter): array { | ||
global $DB; | ||
|
||
// Remove empty string. | ||
$filter['values'] = array_filter($filter['values']); | ||
|
||
$selectedtypes = $filter['values'] ?? []; | ||
|
||
$params = []; | ||
$where = ''; | ||
$jointype = $filter['jointype'] ?? self::JOINTYPE_DEFAULT; | ||
if ($selectedtypes) { | ||
// If we are matching NONE rather than ANY, exclude the selected types instead. | ||
$equal = !($jointype === datafilter::JOINTYPE_NONE); | ||
[$typesql, $params] = $DB->get_in_or_equal($selectedtypes, SQL_PARAMS_NAMED, 'param', $equal); | ||
$where = "q.qtype $typesql"; | ||
} | ||
return [$where, $params]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
question/bank/viewquestiontype/tests/behat/filter_condition_question_type.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
@qbank @qbank_viewquestiontype @javascript | ||
Feature: Filter questions by type | ||
As a teacher | ||
In order to organise my questions | ||
I want to filter the list of questions by type | ||
|
||
Background: | ||
Given the following "courses" exist: | ||
| fullname | shortname | category | | ||
| Course 1 | C1 | 0 | | ||
And the following "activities" exist: | ||
| activity | name | intro | course | idnumber | | ||
| qbank | Qbank 1 | Question bank 1 | C1 | qbank1 | | ||
And the following "question categories" exist: | ||
| contextlevel | reference | name | | ||
| Activity module | qbank1 | Test questions | | ||
And the following "questions" exist: | ||
| questioncategory | qtype | name | questiontext | | ||
| Test questions | truefalse | First question | Answer the first question | | ||
| Test questions | numerical | Second question | Answer the second question | | ||
| Test questions | essay | Third question | Answer the third question | | ||
|
||
Scenario: Filter by a single type | ||
Given I am on the "Qbank 1" "core_question > question bank" page logged in as "admin" | ||
When I apply question bank filter "Type" with value "True/False" | ||
Then I should see "First question" | ||
And I should not see "Second question" | ||
And I should not see "Third question" | ||
|
||
Scenario: Filter by multiple types | ||
Given I am on the "Qbank 1" "core_question > question bank" page logged in as "admin" | ||
When I apply question bank filter "Type" with value "True/False, Essay" | ||
Then I should see "First question" | ||
And I should not see "Second question" | ||
And I should see "Third question" | ||
|
||
Scenario: Exclude types by filter | ||
Given I am on the "Qbank 1" "core_question > question bank" page logged in as "admin" | ||
When I add question bank filter "Type" | ||
And I set the field "Type" to "True/False, Essay" | ||
And I set the field "Match" in the "Filter 3" "fieldset" to "None" | ||
And I press "Apply filters" | ||
Then I should not see "First question" | ||
And I should see "Second question" | ||
And I should not see "Third question" |