Skip to content

Commit

Permalink
issue #54: add support for suspended user field
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Mar 22, 2024
1 parent 70778fd commit 40eb841
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion classes/local/tool_dynamic_cohorts/condition/user_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/local/tool_dynamic_cohorts/condition/user_profile_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()));
}

/**
Expand Down

0 comments on commit 40eb841

Please sign in to comment.