MDL-83881 qbank_viewcreator: Creator and modifier name filters

Add additional question filters for the "Created by" name and "Modified
by" name.

These filters will operate on all name fields that are selected for
these columns, as the exact text displayed is determined by the
fullname() function and not he query.
This commit is contained in:
Mark Johnson
2025-03-06 11:42:30 +00:00
parent d5397fee96
commit 086f6ba6db
7 changed files with 325 additions and 1 deletions
@@ -0,0 +1,45 @@
<?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_viewcreator;
use core\output\datafilter;
use core_question\local\bank\condition;
/**
* Filter condition for filtering on creator name
*
* @package qbank_viewcreator
* @copyright 2025 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 createdby_condition extends user_condition {
#[\Override]
public function get_title() {
return get_string('createdby', 'question');
}
#[\Override]
public static function get_condition_key() {
return 'createdby';
}
#[\Override]
protected static function get_table_alias(): string {
return 'uc';
}
}
@@ -0,0 +1,42 @@
<?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_viewcreator;
/**
* Filter condition for filtering on modifier name
*
* @package qbank_viewcreator
* @copyright 2025 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 modifiedby_condition extends user_condition {
#[\Override]
public function get_title() {
return get_string('modifiedby', 'qbank_viewcreator');
}
#[\Override]
public static function get_condition_key() {
return 'modifiername';
}
#[\Override]
protected static function get_table_alias(): string {
return 'um';
}
}
@@ -40,6 +40,8 @@ class plugin_feature extends plugin_features_base {
public function get_question_filters(?view $qbank = null): array {
return [
new timemodified_condition($qbank),
new createdby_condition($qbank),
new modifiedby_condition($qbank),
];
}
}
@@ -0,0 +1,65 @@
<?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_viewcreator;
use core\output\datafilter;
use core_question\local\bank\condition;
/**
* Abstract class for conditions filtering by user.
*
* @package qbank_viewcreator
* @copyright 2025 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
*/
abstract class user_condition extends condition {
/**
* Return the alias for the instance of the user table to filter on.
*
* @return string
*/
abstract protected static function get_table_alias(): string;
#[\Override]
public function get_filter_class() {
return 'core/datafilter/filtertypes/keyword';
}
#[\Override]
public static function build_query_from_filter(array $filter): array {
global $DB;
$conditions = [];
$params = [];
$notlike = $filter['jointype'] === datafilter::JOINTYPE_NONE;
$tablealias = static::get_table_alias();
$allnames = array_map(fn($field) => "{$tablealias}.{$field}", \core_user\fields::get_name_fields());
$allnames = $DB->sql_concat(...$allnames);
$conditionkey = static::get_condition_key();
foreach ($filter['values'] as $key => $value) {
$params["{$conditionkey}{$key}"] = "%$value%";
$conditions[] = $DB->sql_like($allnames, ":{$conditionkey}{$key}", casesensitive: false, notlike: $notlike);
}
$delimiter = $filter['jointype'] === datafilter::JOINTYPE_ANY ? ' OR ' : ' AND ';
return [
implode($delimiter, $conditions),
$params,
];
}
}
@@ -22,7 +22,6 @@
* @author Ghaly Marc-Alexandre <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['history'] = 'History';
$string['modifiedby'] = 'Modified by';
$string['timemodified'] = 'Time modified';
@@ -0,0 +1,83 @@
@qbank @qbank_viewcreator @javascript
Feature: Filter questions by creator name
As a teacher
In order to organise my questions
I want to filter the list of questions by creator name
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | firstnamephonetic | lastnamephonetic | middlename | alternatename |
| aa | Aaron | Aaronson | Aron | Aronsun | Andrew | Andy |
| bb | Bob | Bobson | | | | |
| cc | Clare | Clareson | | | | |
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 "course enrolments" exist:
| course | user | role |
| C1 | aa | editingteacher |
| C1 | bb | editingteacher |
| C1 | cc | editingteacher |
And the following "questions" exist:
| qtype | questioncategory | name | user |
| truefalse | Test questions | First question | aa |
| truefalse | Test questions | Second question | bb |
| truefalse | Test questions | Third question | cc |
And I am on the "Qbank 1" "core_question > question bank" page logged in as "admin"
And I should see "First question"
And I should see "Second question"
And I should see "Third question"
Scenario: Filter by a single word
When I apply question bank filter "Created by" with value "Aaron"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
Scenario: Filter by any word
When I apply question bank filter "Created by" with value "Aaron, Clare"
Then I should see "First question"
And I should not see "Second question"
And I should see "Third question"
Scenario: Filter by all words
When I add question bank filter "Created by"
And I set the field "Created by" to "son, Aar"
And I set the field "Match" in the "Filter 3" "fieldset" to "All"
And I press "Apply filters"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
Scenario: Filter by additional name fields
When I apply question bank filter "Created by" with value "Aron"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
When I apply question bank filter "Created by" with value "sun"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
When I apply question bank filter "Created by" with value "drew"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
When I apply question bank filter "Created by" with value "Andy"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
Scenario: Exclude names by filter
When I add question bank filter "Created by"
And I set the field "Created by" to "Aron, Clare"
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,0 +1,88 @@
@qbank @qbank_viewcreator @javascript
Feature: Filter questions by modifier name
As a teacher
In order to organise my questions
I want to filter the list of questions by modifier name
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | firstnamephonetic | lastnamephonetic | middlename | alternatename |
| aa | Aaron | Aaronson | Aron | Aronsun | Andrew | Andy |
| bb | Bob | Bobson | | | | |
| cc | Clare | Clareson | | | | |
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 "course enrolments" exist:
| course | user | role |
| C1 | aa | editingteacher |
| C1 | bb | editingteacher |
| C1 | cc | editingteacher |
And the following "questions" exist:
| qtype | questioncategory | name |
| truefalse | Test questions | First question |
| truefalse | Test questions | Second question |
| truefalse | Test questions | Third question |
And the following "core_question > updated questions" exist:
| questioncategory | question | name | modifiedbyuser |
| Test questions | First question | First question | aa |
| Test questions | Second question | Second question | bb |
| Test questions | Third question | Third question | cc |
And I am on the "Qbank 1" "core_question > question bank" page logged in as "admin"
And I should see "First question"
And I should see "Second question"
And I should see "Third question"
Scenario: Filter by a single word
When I apply question bank filter "Modified by" with value "Aaron"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
Scenario: Filter by any word
When I apply question bank filter "Modified by" with value "Aaron, Clare"
Then I should see "First question"
And I should not see "Second question"
And I should see "Third question"
Scenario: Filter by all words
When I add question bank filter "Modified by"
And I set the field "Modified by" to "son, Aar"
And I set the field "Match" in the "Filter 3" "fieldset" to "All"
And I press "Apply filters"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
Scenario: Filter by additional name fields
When I apply question bank filter "Modified by" with value "Aron"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
When I apply question bank filter "Modified by" with value "sun"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
When I apply question bank filter "Modified by" with value "drew"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
When I apply question bank filter "Modified by" with value "Andy"
Then I should see "First question"
And I should not see "Second question"
And I should not see "Third question"
Scenario: Exclude names by filter
When I add question bank filter "Modified by"
And I set the field "Modified by" to "Aron, Clare"
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"