Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #54: add support for suspended user field #57

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading