MDL-77056 reportbuilder: relative date filter for before given period.

This commit is contained in:
Paul Holden 2023-01-26 17:19:39 +00:00
parent 556208417d
commit 198db552b0
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
3 changed files with 23 additions and 0 deletions

View File

@ -120,6 +120,7 @@ $string['errorsourceinvalid'] = 'Could not find valid report source';
$string['errorsourceunavailable'] = 'Report source is not available';
$string['filteradded'] = 'Added filter \'{$a}\'';
$string['filtercontains'] = 'Contains';
$string['filterdatebefore'] = 'Before';
$string['filterdatecurrent'] = 'Current';
$string['filterdatedays'] = 'day(s)';
$string['filterdatefrom'] = 'Date from';

View File

@ -64,6 +64,9 @@ class date extends base {
/** @var int Date in the future */
public const DATE_FUTURE = 8;
/** @var int Date before [X relative date unit(s)] */
public const DATE_BEFORE = 9;
/** @var int Relative date unit for an hour */
public const DATE_UNIT_HOUR = 0;
@ -90,6 +93,7 @@ class date extends base {
self::DATE_NOT_EMPTY => new lang_string('filterisnotempty', 'core_reportbuilder'),
self::DATE_EMPTY => new lang_string('filterisempty', 'core_reportbuilder'),
self::DATE_RANGE => new lang_string('filterrange', 'core_reportbuilder'),
self::DATE_BEFORE => new lang_string('filterdatebefore', 'core_reportbuilder'),
self::DATE_LAST => new lang_string('filterdatelast', 'core_reportbuilder'),
self::DATE_CURRENT => new lang_string('filterdatecurrent', 'core_reportbuilder'),
self::DATE_NEXT => new lang_string('filterdatenext', 'core_reportbuilder'),
@ -195,6 +199,13 @@ class date extends base {
$sql = implode(' AND ', $clauses);
break;
case self::DATE_BEFORE:
$param = database::generate_param_name();
// We can use the start date of the "Last" operator as the end date here.
$sql = "{$fieldsql} < :{$param}";
$params[$param] = self::get_relative_timeframe(self::DATE_LAST, $dateunitvalue, $dateunit)[0];
break;
// Relative helper method can handle these three cases.
case self::DATE_LAST:

View File

@ -221,6 +221,17 @@ class date_test extends advanced_testcase {
*/
public function get_sql_filter_relative_provider(): array {
return [
'Before hour' => [date::DATE_BEFORE, 1, date::DATE_UNIT_HOUR, '-90 minute'],
'Before day' => [date::DATE_BEFORE, 1, date::DATE_UNIT_DAY, '-25 hour'],
'Before week' => [date::DATE_BEFORE, 1, date::DATE_UNIT_WEEK, '-10 day'],
'Before month' => [date::DATE_BEFORE, 1, date::DATE_UNIT_MONTH, '-7 week'],
'Before year' => [date::DATE_BEFORE, 1, date::DATE_UNIT_YEAR, '-15 month'],
'Before two hours' => [date::DATE_BEFORE, 2, date::DATE_UNIT_HOUR, '-150 minute'],
'Before two days' => [date::DATE_BEFORE, 2, date::DATE_UNIT_DAY, '-50 hour'],
'Before two weeks' => [date::DATE_BEFORE, 2, date::DATE_UNIT_WEEK, '-20 day'],
'Before two months' => [date::DATE_BEFORE, 2, date::DATE_UNIT_MONTH, '-15 week'],
'Before two years' => [date::DATE_BEFORE, 2, date::DATE_UNIT_YEAR, '-30 month'],
'Last hour' => [date::DATE_LAST, 1, date::DATE_UNIT_HOUR, '-30 minute'],
'Last day' => [date::DATE_LAST, 1, date::DATE_UNIT_DAY, '-6 hour'],
'Last week' => [date::DATE_LAST, 1, date::DATE_UNIT_WEEK, '-3 day'],