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-83862 qbank_viewcreator: Add timemodified filter
This adds a new filter for the timemodified, displayed in the "Modified by" column. It uses the new core datetime filter for the UI.
- Loading branch information
1 parent
ce8ce17
commit 1e6dd8e
Showing
4 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
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
119 changes: 119 additions & 0 deletions
119
question/bank/viewcreator/classes/timemodified_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,119 @@ | ||
<?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\exception\moodle_exception; | ||
use core\plugininfo\filter; | ||
use core_question\local\bank\condition; | ||
|
||
/** | ||
* Filter condition for date/time modified | ||
* | ||
* @package qbank_viewcreator | ||
* @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 timemodified_condition extends condition { | ||
|
||
const MODE_BEFORE = 'before'; | ||
|
||
const MODE_AFTER = 'after'; | ||
|
||
const MODE_BETWEEN = 'between'; | ||
|
||
#[\Override] | ||
public function get_title() { | ||
return get_string('timemodified', 'qbank_viewcreator'); | ||
} | ||
|
||
#[\Override] | ||
public static function get_condition_key() { | ||
return 'timemodified'; | ||
} | ||
|
||
#[\Override] | ||
public function get_filter_class() { | ||
return 'core/datafilter/filtertypes/datetime'; | ||
} | ||
|
||
public function get_join_list(): array { | ||
return [ | ||
self::JOINTYPE_DEFAULT, | ||
]; | ||
} | ||
|
||
/** | ||
* @param array $filter | ||
* @return array | ||
*/ | ||
public static function build_query_from_filter(array $filter): array { | ||
if (!isset($filter['filteroptions']['mode']) || empty($filter['values'])) { | ||
return ['', []]; | ||
} | ||
$mode = $filter['filteroptions']['mode']; | ||
if (!in_array($mode, [self::MODE_AFTER, self::MODE_BEFORE, self::MODE_BETWEEN])) { | ||
throw new moodle_exception('invaliddatetimemode', 'error', a: $filter['filteroptions']['mode']); | ||
} | ||
$tz = new \DateTimeZone(\core_date::get_user_timezone(99)); | ||
$datetimeafter = new \DateTime($filter['values'][0], $tz); | ||
$datetimebefore = new \DateTime($filter['values'][1], $tz); | ||
if ($mode === self::MODE_AFTER) { | ||
$conditions = 'q.timemodified > :timemodifiedafter'; | ||
$params['timemodifiedafter'] = $datetimeafter->getTimestamp(); | ||
} else if ($mode === self::MODE_BEFORE) { | ||
$conditions = 'q.timemodified < :timemodifiedbefore'; | ||
$params['timemodifiedbefore'] = $datetimebefore->getTimestamp(); | ||
} else { | ||
if ($datetimeafter > $datetimebefore) { | ||
throw new moodle_exception( | ||
'invaliddatetimebetween', | ||
'error', | ||
a: (object) [ | ||
'before' => $datetimebefore->format('Y-m-d H:i'), | ||
'after' => $datetimeafter->format('Y-m-d H:i'), | ||
], | ||
); | ||
} | ||
$conditions = 'q.timemodified > :timemodifiedafter AND q.timemodified < :timemodifiedbefore'; | ||
$params = [ | ||
'timemodifiedafter' => $datetimeafter->getTimestamp(), | ||
'timemodifiedbefore' => $datetimebefore->getTimestamp(), | ||
]; | ||
} | ||
|
||
return [$conditions, $params]; | ||
} | ||
|
||
public function get_initial_values(): array { | ||
$tz = new \DateTimeZone(\core_date::get_user_timezone()); | ||
// Datetime format used by the <input type="datetime-local"> field. | ||
$format = 'Y-m-d\TH:i'; | ||
$now = (new \DateTime('now', $tz))->format($format); | ||
$oneweek = (new \DateTime('midnight 1 week ago', $tz))->format($format); | ||
return [ | ||
[ | ||
'value' => $now, | ||
'title' => $now, | ||
], | ||
[ | ||
'value' => $oneweek, | ||
'title' => $oneweek, | ||
], | ||
]; | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
question/bank/viewcreator/tests/behat/filter_condition_timemodified.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,74 @@ | ||
@qbank @qbank_viewcreator @javascript | ||
Feature: Time modified filter condition | ||
As a teacher | ||
In order to organise my questions | ||
I want to filter the list of questions by the time and date of last modification | ||
|
||
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 | truefalse | Second question | Answer the second question | | ||
| Test questions | truefalse | Third question | Answer the third question | | ||
And the following "core_question > updated questions" exist: | ||
| questioncategory | question | name | timemodified | | ||
| Test questions | First question | First question | ## 2024-01-10 10:00 ## | | ||
| Test questions | Second question | Second question | ## 2024-01-10 11:00 ## | | ||
| Test questions | Third question | Third question | ## 2024-01-10 12:00 ## | | ||
Given 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 questions modified before time | ||
When I add question bank filter "Time modified" | ||
And the field "Time modified 2" matches value "## now ##%FT%R##" | ||
And I set the field "Time modified 2" to "2024-01-10T10:59" | ||
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 questions modified after time | ||
When I add question bank filter "Time modified" | ||
And I set the field "Select dates" to "After" | ||
And the field "Time modified 1" matches value "## midnight 1 week ago ##%FT%R##" | ||
And I set the field "Time modified 1" to "2024-01-10T10:59" | ||
And I press "Apply filters" | ||
Then I should not see "First question" | ||
And I should see "Second question" | ||
And I should see "Third question" | ||
|
||
Scenario: Filter by questions modified between times | ||
When I add question bank filter "Time modified" | ||
And I set the field "Select dates" to "Between" | ||
And I set the field "Time modified 1" to "2024-01-10T10:59" | ||
And I set the field "Time modified 2" to "2024-01-10T11:01" | ||
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" | ||
|
||
Scenario: Apply filter between invalid times | ||
When I add question bank filter "Time modified" | ||
And I set the field "Select dates" to "Between" | ||
And I set the field "Time modified 1" to "2024-01-10T10:59" | ||
And I set the field "Time modified 2" to "2024-01-10T11:01" | ||
And I press "Apply filters" | ||
And I set the field "Time modified 1" to "2024-01-10T11:01" | ||
And I set the field "Time modified 2" to "2024-01-10T10:59" | ||
And I press "Apply filters" | ||
Then I should see "Invalid datetime filter. \"After\" value 2024-01-10 11:01 was later than \"before\" value 2024-01-10 10:59." | ||
# Invalid values should not be set in the URL. | ||
When I reload the page | ||
Then the field "Time modified 1" matches value "2024-01-10T10:59" | ||
And the field "Time modified 2" matches value "2024-01-10T11:01" |