diff --git a/question/bank/viewquestionname/classes/plugin_feature.php b/question/bank/viewquestionname/classes/plugin_feature.php index 32a16be7064..d1d39dd1ec4 100644 --- a/question/bank/viewquestionname/classes/plugin_feature.php +++ b/question/bank/viewquestionname/classes/plugin_feature.php @@ -39,6 +39,7 @@ class plugin_feature extends plugin_features_base { public function get_question_filters(?view $qbank = null): array { return [ new question_name_condition($qbank), + new question_idnumber_condition($qbank), ]; } } diff --git a/question/bank/viewquestionname/classes/question_idnumber_condition.php b/question/bank/viewquestionname/classes/question_idnumber_condition.php new file mode 100644 index 00000000000..40b37884c5d --- /dev/null +++ b/question/bank/viewquestionname/classes/question_idnumber_condition.php @@ -0,0 +1,70 @@ +. + +namespace qbank_viewquestionname; + +use core\output\datafilter; +use core_question\local\bank\condition; + +/** + * Filter condition for filtering on the question idnumber + * + * @package qbank_viewquestionname + * @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net} + * @author Mark Johnson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class question_idnumber_condition extends condition { + #[\Override] + public function get_title() { + return get_string('questionidnumbercondition', 'qbank_viewquestionname'); + } + + #[\Override] + public static function get_condition_key() { + return 'questionidnumber'; + } + + #[\Override] + public function get_filter_class() { + return 'core/datafilter/filtertypes/keyword'; + } + + /** + * Return an SQL condition and parameters for filtering on q.idnumber. + * + * This will search for the terms provided anywhere in the name. + * + * @param array $filter + * @return array + */ + public static function build_query_from_filter(array $filter): array { + global $DB; + + $conditions = []; + $params = []; + $notlike = $filter['jointype'] === datafilter::JOINTYPE_NONE; + foreach ($filter['values'] as $key => $value) { + $params["idnumber{$key}"] = "%{$value}%"; + $conditions[] = $DB->sql_like('qbe.idnumber', ":idnumber{$key}", casesensitive: false, notlike: $notlike); + } + $delimiter = $filter['jointype'] === datafilter::JOINTYPE_ANY ? ' OR ' : ' AND '; + return [ + implode($delimiter, $conditions), + $params, + ]; + } +} diff --git a/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php b/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php index dcc30f8e7bc..f8d1f368ca0 100644 --- a/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php +++ b/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php @@ -25,6 +25,7 @@ $string['pluginname'] = 'View question name'; $string['privacy:metadata'] = 'The View question name question bank plugin does not store any personal data.'; +$string['questionidnumbercondition'] = 'Question ID number'; $string['questionnamecondition'] = 'Question name'; // In place editing. $string['edit_question_name_hint'] = 'Edit question name'; diff --git a/question/bank/viewquestionname/tests/behat/filter_condition_question_idnumber.feature b/question/bank/viewquestionname/tests/behat/filter_condition_question_idnumber.feature new file mode 100644 index 00000000000..a0620f1a507 --- /dev/null +++ b/question/bank/viewquestionname/tests/behat/filter_condition_question_idnumber.feature @@ -0,0 +1,56 @@ +@qbank @qbank_viewquestionnname @javascript +Feature: Filter questions by idnumber + As a teacher + In order to organise my questions + I want to filter the list of questions by idnumber + + 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 | idnumber | + | Test questions | truefalse | a | Answer the first question | q_01_ab | + | Test questions | numerical | b | Answer the second question | q_02_bc | + | Test questions | essay | c | Answer the third question | Q_21_ca | + And I am on the "Qbank 1" "core_question > question bank" page logged in as "admin" + And I should see "q_01_ab" + And I should see "q_02_bc" + And I should see "Q_21_ca" + + Scenario: Filter by a single term + When I apply question bank filter "Question ID number" with value "ab" + Then I should see "q_01_ab" + And I should not see "q_02_bc" + And I should not see "Q_21_ca" + + Scenario: Filter by any term + When I apply question bank filter "Question ID number" with value "ab, ca" + Then I should see "q_01_ab" + And I should not see "q_02_bc" + And I should see "Q_21_ca" + + Scenario: Filter by all terms + When I add question bank filter "Question ID number" + And I set the field "Question ID number" to "q, c" + And I set the field "Match" in the "Filter 3" "fieldset" to "All" + And I press "Apply filters" + Then I should not see "q_01_ab" + And I should see "q_02_bc" + # Filter should be case-insensitive. + And I should see "Q_21_ca" + + Scenario: Exclude idnumbers by filter + When I add question bank filter "Question ID number" + And I set the field "Question ID number" to "ab, ca" + And I set the field "Match" in the "Filter 3" "fieldset" to "None" + And I press "Apply filters" + Then I should not see "q_01_ab" + And I should see "q_02_bc" + And I should not see "Q_21_ca"