From 40eb841166719bf30b8620e651b90c73abfbb4ca Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 22 Mar 2024 13:37:46 +1100 Subject: [PATCH] issue #54: add support for suspended user field --- .../condition/user_profile.php | 11 +++++++++- .../condition/user_profile_test.php | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/classes/local/tool_dynamic_cohorts/condition/user_profile.php b/classes/local/tool_dynamic_cohorts/condition/user_profile.php index a4451f5..6ce49ef 100644 --- a/classes/local/tool_dynamic_cohorts/condition/user_profile.php +++ b/classes/local/tool_dynamic_cohorts/condition/user_profile.php @@ -37,7 +37,7 @@ class user_profile extends condition_base { * A list of supported default fields. */ protected const SUPPORTED_STANDARD_FIELDS = ['auth', 'firstname', 'lastname', 'username', 'email', 'idnumber', - 'city', 'country', 'institution', 'department']; + 'city', 'country', 'institution', 'department', 'suspended']; /** * Return field name in the condition config form. @@ -81,6 +81,9 @@ public function config_form_add(\MoodleQuickForm $mform): void { case self::FIELD_DATA_TYPE_MENU: $this->add_menu_field($mform, $group, $field, $shortname); break; + case self::FIELD_DATA_TYPE_CHECKBOX: + $this->add_checkbox_field($mform, $group, $field, $shortname); + break; default: throw new coding_exception('Invalid field type ' . $field->datatype); } @@ -163,6 +166,11 @@ protected function get_fields_info(): array { $fields[$field]->datatype = self::FIELD_DATA_TYPE_MENU; $fields[$field]->param1 = $options; break; + case 'suspended': + $fields[$field]->name = get_string($field); + $fields[$field]->datatype = self::FIELD_DATA_TYPE_CHECKBOX; + $fields[$field]->param1 = array_combine([0, 1], [get_string('no'), get_string('yes')]);; + break; default: $fields[$field]->name = get_string($field); $fields[$field]->datatype = self::FIELD_DATA_TYPE_TEXT; @@ -215,6 +223,7 @@ public function get_sql(): condition_sql { $result = $this->get_text_sql('u', $this->get_field_name()); break; case self::FIELD_DATA_TYPE_MENU: + case self::FIELD_DATA_TYPE_CHECKBOX: $result = $this->get_menu_sql('u', $this->get_field_name()); break; } diff --git a/tests/local/tool_dynamic_cohorts/condition/user_profile_test.php b/tests/local/tool_dynamic_cohorts/condition/user_profile_test.php index 9cf0c88..5f07a8e 100644 --- a/tests/local/tool_dynamic_cohorts/condition/user_profile_test.php +++ b/tests/local/tool_dynamic_cohorts/condition/user_profile_test.php @@ -141,6 +141,7 @@ public function test_get_sql_data() { $this->getDataGenerator()->create_user(['username' => 'user1username']); $this->getDataGenerator()->create_user(['username' => 'user2username']); + $this->getDataGenerator()->create_user(['username' => 'different', 'suspended' => '1']); $condition = $this->get_condition([ 'profilefield' => 'username', @@ -182,6 +183,26 @@ public function test_get_sql_data() { $sql = "SELECT u.id FROM {user} u {$result->get_join()} WHERE {$result->get_where()}"; $totalusers = $DB->count_records('user'); $this->assertCount($totalusers - 1, $DB->get_records_sql($sql, $result->get_params())); + + $condition = $this->get_condition([ + 'profilefield' => 'suspended', + 'suspended_operator' => condition_base::TEXT_IS_EQUAL_TO, + 'suspended_value' => 1, + ]); + + $result = $condition->get_sql(); + $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())); + + $condition = $this->get_condition([ + 'profilefield' => 'suspended', + 'suspended_operator' => condition_base::TEXT_IS_NOT_EQUAL_TO, + 'suspended_value' => 1, + ]); + + $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())); } /**