Skip to content

Commit

Permalink
MDL-77056 reportbuilder: relative date filter for before given period.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Mar 7, 2023
1 parent 5562084 commit 198db55
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions lang/en/reportbuilder.php
Expand Up @@ -120,6 +120,7 @@
$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';
Expand Down
11 changes: 11 additions & 0 deletions reportbuilder/classes/local/filters/date.php
Expand Up @@ -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;

Expand All @@ -90,6 +93,7 @@ private function get_operators(): array {
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'),
Expand Down Expand Up @@ -195,6 +199,13 @@ public function get_sql_filter(array $values): array {

$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:
Expand Down
11 changes: 11 additions & 0 deletions reportbuilder/tests/local/filters/date_test.php
Expand Up @@ -221,6 +221,17 @@ public function test_get_sql_filter_current_week_no_match(int $startweekday, str
*/
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'],
Expand Down

0 comments on commit 198db55

Please sign in to comment.