Skip to content

Commit 0cb4514

Browse files
committed
issue #97: implement past and future for date related custom fields
1 parent 152cc5a commit 0cb4514

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

classes/condition_base.php

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ abstract class condition_base {
110110
*/
111111
public const DATE_IS_BEFORE = 2;
112112

113+
/**
114+
* Value for operator date is in the past.
115+
*/
116+
public const DATE_IN_THE_PAST = 30;
117+
118+
/**
119+
* Value for operator date is in the future.
120+
*/
121+
public const DATE_IN_THE_FUTURE = 40;
122+
113123
/**
114124
* Condition persistent object.
115125
*

classes/local/tool_dynamic_cohorts/condition/fields_trait.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ protected function get_date_operators(): array {
6868
self::DATE_IS_BEFORE => get_string('isbefore', 'tool_dynamic_cohorts'),
6969
self::TEXT_IS_EMPTY => get_string('isempty', 'filters'),
7070
self::TEXT_IS_NOT_EMPTY => get_string('isnotempty', 'tool_dynamic_cohorts'),
71+
self::DATE_IN_THE_PAST => get_string('inthepast', 'tool_dynamic_cohorts'),
72+
self::DATE_IN_THE_FUTURE => get_string('inthefuture', 'tool_dynamic_cohorts'),
7173
];
7274
}
7375

@@ -129,7 +131,8 @@ protected function get_field_value_text(): ?string {
129131
}
130132
}
131133

132-
if (in_array($this->get_operator_value(), [self::TEXT_IS_EMPTY, self::TEXT_IS_NOT_EMPTY])) {
134+
$nullvalue = [self::TEXT_IS_EMPTY, self::TEXT_IS_NOT_EMPTY, self::DATE_IN_THE_PAST, self::DATE_IN_THE_FUTURE];
135+
if (in_array($this->get_operator_value(), $nullvalue)) {
133136
$fieldvalue = null;
134137
}
135138

@@ -236,7 +239,15 @@ protected function add_date_field(\MoodleQuickForm $mform, array &$group, \stdCl
236239

237240
$elements[] = $mform->createElement('date_time_selector', $shortname . '_value');
238241
$mform->setDefault($shortname . '_value', usergetmidnight(time()));
239-
$mform->hideIf($shortname . '_value', $shortname . '_operator', 'in', self::TEXT_IS_EMPTY . '|' . self::TEXT_IS_NOT_EMPTY);
242+
$mform->hideIf(
243+
$shortname . '_value',
244+
$shortname . '_operator',
245+
'in',
246+
self::TEXT_IS_EMPTY . '|' .
247+
self::TEXT_IS_NOT_EMPTY . '|' .
248+
self::DATE_IN_THE_PAST . '|' .
249+
self::DATE_IN_THE_FUTURE
250+
);
240251

241252
$group[] = $mform->createElement('group', $shortname, '', $elements, '', false);
242253

@@ -403,7 +414,15 @@ protected function get_date_sql(string $tablealias, string $fieldname): conditio
403414
break;
404415
case self::DATE_IS_AFTER:
405416
$where = "$tablealias.$fieldname >= :$param";
406-
$params[$param] = (int) $fieldvalue;
417+
$params[$param] = (int) $fieldvalue;
418+
break;
419+
case self::DATE_IN_THE_FUTURE:
420+
$where = "$tablealias.$fieldname >= :$param";
421+
$params[$param] = time();
422+
break;
423+
case self::DATE_IN_THE_PAST:
424+
$where = "$tablealias.$fieldname <= :$param";
425+
$params[$param] = time();
407426
break;
408427
default:
409428
return new condition_sql('', '', []);

lang/en/tool_dynamic_cohorts.php

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@
108108
$string['isnotempty'] = 'is not empty';
109109
$string['isafter'] = 'is after';
110110
$string['isbefore'] = 'is before';
111+
$string['inthepast'] = 'is in the past';
112+
$string['inthefuture'] = 'is in the future';
111113
$string['loggedintime'] = 'Users who logged in {$a->operator} {$a->time}';
112114
$string['logical_operator'] = 'Logical operator';
113115
$string['logical_operator_help'] = 'A logical operator to be applied to conditions for this rule. Operator "AND" means a user has to match all conditions to be added to a cohort. "OR" means a user has to match any of conditions to be added to a cohort.';

tests/local/tool_dynamic_cohorts/condition/cohort_field_test.php

+24
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,30 @@ public function test_get_sql_data_custom_fields() {
501501
$result = $condition->get_sql();
502502
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
503503
$this->assertCount($totalusers - 1, $DB->get_records_sql($sql, $result->get_params()));
504+
505+
// User 1 and user 2 as they are members of cohort 1.
506+
$condition = $this->get_condition([
507+
'cohort_field_operator' => cohort_field::OPERATOR_IS_MEMBER_OF,
508+
'cohort_field_field' => $datefieldname,
509+
$datefieldname . '_operator' => condition_base::DATE_IN_THE_PAST,
510+
$datefieldname . '_value' => $now,
511+
]);
512+
513+
$result = $condition->get_sql();
514+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
515+
$this->assertCount(2, $DB->get_records_sql($sql, $result->get_params()));
516+
517+
// No cohort with a date is in the future. No users should be returned.
518+
$condition = $this->get_condition([
519+
'cohort_field_operator' => cohort_field::OPERATOR_IS_MEMBER_OF,
520+
'cohort_field_field' => $datefieldname,
521+
$datefieldname . '_operator' => condition_base::DATE_IN_THE_FUTURE,
522+
$datefieldname . '_value' => $now,
523+
]);
524+
525+
$result = $condition->get_sql();
526+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
527+
$this->assertCount(0, $DB->get_records_sql($sql, $result->get_params()));
504528
}
505529

506530
/**

tests/local/tool_dynamic_cohorts/condition/user_custom_profile_test.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ public function test_get_sql_data() {
289289
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
290290
$this->assertCount(1, $DB->get_records_sql($sql, $result->get_params()));
291291

292-
$test = $DB->get_records('user_info_data');
293-
294292
$fieldname = 'profile_field_' . $fielddate->shortname;
295293
$condition->set_config_data([
296294
'profilefield' => $fieldname,
@@ -315,6 +313,33 @@ public function test_get_sql_data() {
315313
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
316314
$this->assertCount(1, $DB->get_records_sql($sql, $result->get_params()));
317315

316+
$fieldname = 'profile_field_' . $fielddate->shortname;
317+
$condition->set_config_data([
318+
'profilefield' => $fieldname,
319+
$fieldname . '_operator' => condition_base::DATE_IN_THE_FUTURE,
320+
$fieldname . '_value' => $now,
321+
'include_missing_data' => 0,
322+
]);
323+
324+
$result = $condition->get_sql();
325+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
326+
$users = $DB->get_records_sql($sql, $result->get_params());
327+
$this->assertCount(1, $users);
328+
$this->assertArrayHasKey($user2->id, $users);
329+
330+
$fieldname = 'profile_field_' . $fielddate->shortname;
331+
$condition->set_config_data([
332+
'profilefield' => $fieldname,
333+
$fieldname . '_operator' => condition_base::DATE_IN_THE_PAST,
334+
$fieldname . '_value' => $now,
335+
'include_missing_data' => 0,
336+
]);
337+
338+
$result = $condition->get_sql();
339+
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
340+
$users = $DB->get_records_sql($sql, $result->get_params());
341+
$this->assertCount(1, $users);
342+
$this->assertArrayHasKey($user1->id, $users);
318343
}
319344

320345
/**

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'tool_dynamic_cohorts';
28-
$plugin->release = 2024062200;
29-
$plugin->version = 2024062200;
28+
$plugin->release = 2024062400;
29+
$plugin->version = 2024062400;
3030
$plugin->requires = 2022112800;
3131
$plugin->supported = [404, 404];
3232
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)