Skip to content

Commit

Permalink
issue #97: implement past and future for date related custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Jun 24, 2024
1 parent 152cc5a commit 0cb4514
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
10 changes: 10 additions & 0 deletions classes/condition_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ abstract class condition_base {
*/
public const DATE_IS_BEFORE = 2;

/**
* Value for operator date is in the past.
*/
public const DATE_IN_THE_PAST = 30;

/**
* Value for operator date is in the future.
*/
public const DATE_IN_THE_FUTURE = 40;

/**
* Condition persistent object.
*
Expand Down
25 changes: 22 additions & 3 deletions classes/local/tool_dynamic_cohorts/condition/fields_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ protected function get_date_operators(): array {
self::DATE_IS_BEFORE => get_string('isbefore', 'tool_dynamic_cohorts'),
self::TEXT_IS_EMPTY => get_string('isempty', 'filters'),
self::TEXT_IS_NOT_EMPTY => get_string('isnotempty', 'tool_dynamic_cohorts'),
self::DATE_IN_THE_PAST => get_string('inthepast', 'tool_dynamic_cohorts'),
self::DATE_IN_THE_FUTURE => get_string('inthefuture', 'tool_dynamic_cohorts'),
];
}

Expand Down Expand Up @@ -129,7 +131,8 @@ protected function get_field_value_text(): ?string {
}
}

if (in_array($this->get_operator_value(), [self::TEXT_IS_EMPTY, self::TEXT_IS_NOT_EMPTY])) {
$nullvalue = [self::TEXT_IS_EMPTY, self::TEXT_IS_NOT_EMPTY, self::DATE_IN_THE_PAST, self::DATE_IN_THE_FUTURE];
if (in_array($this->get_operator_value(), $nullvalue)) {
$fieldvalue = null;
}

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

$elements[] = $mform->createElement('date_time_selector', $shortname . '_value');
$mform->setDefault($shortname . '_value', usergetmidnight(time()));
$mform->hideIf($shortname . '_value', $shortname . '_operator', 'in', self::TEXT_IS_EMPTY . '|' . self::TEXT_IS_NOT_EMPTY);
$mform->hideIf(
$shortname . '_value',
$shortname . '_operator',
'in',
self::TEXT_IS_EMPTY . '|' .
self::TEXT_IS_NOT_EMPTY . '|' .
self::DATE_IN_THE_PAST . '|' .
self::DATE_IN_THE_FUTURE
);

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

Expand Down Expand Up @@ -403,7 +414,15 @@ protected function get_date_sql(string $tablealias, string $fieldname): conditio
break;
case self::DATE_IS_AFTER:
$where = "$tablealias.$fieldname >= :$param";
$params[$param] = (int) $fieldvalue;
$params[$param] = (int) $fieldvalue;
break;
case self::DATE_IN_THE_FUTURE:
$where = "$tablealias.$fieldname >= :$param";
$params[$param] = time();
break;
case self::DATE_IN_THE_PAST:
$where = "$tablealias.$fieldname <= :$param";
$params[$param] = time();
break;
default:
return new condition_sql('', '', []);
Expand Down
2 changes: 2 additions & 0 deletions lang/en/tool_dynamic_cohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
$string['isnotempty'] = 'is not empty';
$string['isafter'] = 'is after';
$string['isbefore'] = 'is before';
$string['inthepast'] = 'is in the past';
$string['inthefuture'] = 'is in the future';
$string['loggedintime'] = 'Users who logged in {$a->operator} {$a->time}';
$string['logical_operator'] = 'Logical operator';
$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.';
Expand Down
24 changes: 24 additions & 0 deletions tests/local/tool_dynamic_cohorts/condition/cohort_field_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,30 @@ public function test_get_sql_data_custom_fields() {
$result = $condition->get_sql();
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
$this->assertCount($totalusers - 1, $DB->get_records_sql($sql, $result->get_params()));

// User 1 and user 2 as they are members of cohort 1.
$condition = $this->get_condition([
'cohort_field_operator' => cohort_field::OPERATOR_IS_MEMBER_OF,
'cohort_field_field' => $datefieldname,
$datefieldname . '_operator' => condition_base::DATE_IN_THE_PAST,
$datefieldname . '_value' => $now,
]);

$result = $condition->get_sql();
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
$this->assertCount(2, $DB->get_records_sql($sql, $result->get_params()));

// No cohort with a date is in the future. No users should be returned.
$condition = $this->get_condition([
'cohort_field_operator' => cohort_field::OPERATOR_IS_MEMBER_OF,
'cohort_field_field' => $datefieldname,
$datefieldname . '_operator' => condition_base::DATE_IN_THE_FUTURE,
$datefieldname . '_value' => $now,
]);

$result = $condition->get_sql();
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
$this->assertCount(0, $DB->get_records_sql($sql, $result->get_params()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ public function test_get_sql_data() {
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
$this->assertCount(1, $DB->get_records_sql($sql, $result->get_params()));

$test = $DB->get_records('user_info_data');

$fieldname = 'profile_field_' . $fielddate->shortname;
$condition->set_config_data([
'profilefield' => $fieldname,
Expand All @@ -315,6 +313,33 @@ public function test_get_sql_data() {
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
$this->assertCount(1, $DB->get_records_sql($sql, $result->get_params()));

$fieldname = 'profile_field_' . $fielddate->shortname;
$condition->set_config_data([
'profilefield' => $fieldname,
$fieldname . '_operator' => condition_base::DATE_IN_THE_FUTURE,
$fieldname . '_value' => $now,
'include_missing_data' => 0,
]);

$result = $condition->get_sql();
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
$users = $DB->get_records_sql($sql, $result->get_params());
$this->assertCount(1, $users);
$this->assertArrayHasKey($user2->id, $users);

$fieldname = 'profile_field_' . $fielddate->shortname;
$condition->set_config_data([
'profilefield' => $fieldname,
$fieldname . '_operator' => condition_base::DATE_IN_THE_PAST,
$fieldname . '_value' => $now,
'include_missing_data' => 0,
]);

$result = $condition->get_sql();
$sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}";
$users = $DB->get_records_sql($sql, $result->get_params());
$this->assertCount(1, $users);
$this->assertArrayHasKey($user1->id, $users);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'tool_dynamic_cohorts';
$plugin->release = 2024062200;
$plugin->version = 2024062200;
$plugin->release = 2024062400;
$plugin->version = 2024062400;
$plugin->requires = 2022112800;
$plugin->supported = [404, 404];
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 0cb4514

Please sign in to comment.