From 0cb45146f8a13757a8e1f894388d590a48f03833 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Mon, 24 Jun 2024 17:38:17 +1000 Subject: [PATCH] issue #97: implement past and future for date related custom fields --- classes/condition_base.php | 10 +++++++ .../condition/fields_trait.php | 25 ++++++++++++++-- lang/en/tool_dynamic_cohorts.php | 2 ++ .../condition/cohort_field_test.php | 24 +++++++++++++++ .../condition/user_custom_profile_test.php | 29 +++++++++++++++++-- version.php | 4 +-- 6 files changed, 87 insertions(+), 7 deletions(-) diff --git a/classes/condition_base.php b/classes/condition_base.php index 65ecda9..7fac373 100644 --- a/classes/condition_base.php +++ b/classes/condition_base.php @@ -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. * diff --git a/classes/local/tool_dynamic_cohorts/condition/fields_trait.php b/classes/local/tool_dynamic_cohorts/condition/fields_trait.php index 442a7e3..cdbe679 100644 --- a/classes/local/tool_dynamic_cohorts/condition/fields_trait.php +++ b/classes/local/tool_dynamic_cohorts/condition/fields_trait.php @@ -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'), ]; } @@ -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; } @@ -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); @@ -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('', '', []); diff --git a/lang/en/tool_dynamic_cohorts.php b/lang/en/tool_dynamic_cohorts.php index c5267ae..048d930 100644 --- a/lang/en/tool_dynamic_cohorts.php +++ b/lang/en/tool_dynamic_cohorts.php @@ -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.'; diff --git a/tests/local/tool_dynamic_cohorts/condition/cohort_field_test.php b/tests/local/tool_dynamic_cohorts/condition/cohort_field_test.php index ac49617..7fbe3ac 100644 --- a/tests/local/tool_dynamic_cohorts/condition/cohort_field_test.php +++ b/tests/local/tool_dynamic_cohorts/condition/cohort_field_test.php @@ -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())); } /** diff --git a/tests/local/tool_dynamic_cohorts/condition/user_custom_profile_test.php b/tests/local/tool_dynamic_cohorts/condition/user_custom_profile_test.php index ceda0a2..29007c6 100644 --- a/tests/local/tool_dynamic_cohorts/condition/user_custom_profile_test.php +++ b/tests/local/tool_dynamic_cohorts/condition/user_custom_profile_test.php @@ -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, @@ -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); } /** diff --git a/version.php b/version.php index 10cf3db..73dc6bd 100644 --- a/version.php +++ b/version.php @@ -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;