Skip to content

Commit

Permalink
MDL-83859 qbank_viewquestiontype: Add type filter
Browse files Browse the repository at this point in the history
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
marxjohnson committed Dec 5, 2024
1 parent fa4c9e8 commit 97443e9
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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;
}
}
12 changes: 10 additions & 2 deletions question/bank/viewquestiontype/classes/plugin_feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace qbank_viewquestiontype;

use core_question\local\bank\plugin_features_base;
use core_question\local\bank\view;

/**
* Class plugin_feature is the entrypoint for the columns.
Expand All @@ -27,10 +28,17 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_feature extends plugin_features_base {

#[\Override]
public function get_question_columns($qbank): array {
return [
new question_type_column($qbank)
new question_type_column($qbank),
];
}

#[\Override]
public function get_question_filters(?view $qbank = null): array {
return [
new type_condition($qbank),
];
}
}
112 changes: 112 additions & 0 deletions question/bank/viewquestiontype/classes/type_condition.php
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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['typefilter'] = 'Type';
$string['pluginname'] = 'View question type';
$string['privacy:metadata'] = 'The View question type question bank plugin does not store any personal data.';
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"

0 comments on commit 97443e9

Please sign in to comment.