Skip to content

Commit

Permalink
issue #36: add support for checkbox custom field
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Mar 8, 2024
1 parent 3182e6c commit 4eb5cfb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
112 changes: 86 additions & 26 deletions classes/local/tool_dynamic_cohorts/condition/user_custom_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace tool_dynamic_cohorts\local\tool_dynamic_cohorts\condition;

use coding_exception;
use tool_dynamic_cohorts\condition_sql;

/**
Expand All @@ -26,6 +27,7 @@
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_custom_profile extends user_profile {

/**
* Field name to store include missing data option,
*/
Expand All @@ -34,7 +36,12 @@ class user_custom_profile extends user_profile {
/**
* A list of supported custom profile fields.
*/
protected const SUPPORTED_CUSTOM_FIELDS = ['text', 'menu'];
protected const SUPPORTED_CUSTOM_FIELDS = ['text', 'menu', 'checkbox'];

/**
* Value for checkbox field types.
*/
public const FIELD_DATA_TYPE_CHECKBOX = 'checkbox';

/**
* Custom field prefix.
Expand All @@ -51,7 +58,7 @@ public function get_name(): string {
}

/**
* Returns a list of all fields with extra data (shortname, name, datatype, param1 and type).
* Returns a list of all fields with extra data (shortname, name, datatype and param1).
*
* @return \stdClass[]
*/
Expand All @@ -70,11 +77,19 @@ protected function get_fields_info(): array {
$field = (object)array_intersect_key((array)$customfield->field,
['shortname' => 1, 'name' => 1, 'datatype' => 1, 'param1' => 1]);

if ($field->datatype == self::FIELD_DATA_TYPE_MENU) {
$options = explode("\n", $field->param1);
$field->param1 = array_combine($options, $options);
} else if ($field->datatype == 'text') {
$field->paramtype = PARAM_TEXT;
switch ($field->datatype) {
case self::FIELD_DATA_TYPE_MENU:
$options = explode("\n", $field->param1);
$field->param1 = array_combine($options, $options);
break;
case self::FIELD_DATA_TYPE_TEXT:
$field->paramtype = PARAM_TEXT;
break;
case self::FIELD_DATA_TYPE_CHECKBOX:
$field->param1 = array_combine([0, 1], [get_string('no'), get_string('yes')]);
break;
default:
throw new coding_exception('Invalid field type ' . $field->datatype);
}

$shortname = self::FIELD_PREFIX . $field->shortname;
Expand All @@ -90,7 +105,31 @@ protected function get_fields_info(): array {
* @param \MoodleQuickForm $mform
*/
public function config_form_add(\MoodleQuickForm $mform): void {
parent::config_form_add($mform);
$options = [0 => get_string('select')];

$fields = $this->get_fields_info();
foreach ($fields as $shortname => $field) {
$options[$shortname] = $field->name;
}

$group = [];
$group[] = $mform->createElement('select', self::FIELD_NAME, '', $options);

foreach ($fields as $shortname => $field) {
switch ($field->datatype) {
case self::FIELD_DATA_TYPE_TEXT:
$this->add_text_field($mform, $group, $field, $shortname);
break;
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;
}
}

$mform->addGroup($group, 'profilefieldgroup', get_string('profilefield', 'tool_dynamic_cohorts'), '', false);

$mform->addElement(
'checkbox',
Expand All @@ -102,6 +141,23 @@ public function config_form_add(\MoodleQuickForm $mform): void {
$mform->addHelpButton(self::INCLUDE_MISSING_DATA_FIELD_NAME, self::INCLUDE_MISSING_DATA_FIELD_NAME, 'tool_dynamic_cohorts');
}

/**
* Adds a check box field to the form.
*
* @param \MoodleQuickForm $mform Form to add the field to.
* @param array $group A group to add the field to.
* @param \stdClass $field Field info.
* @param string $shortname A field shortname.
*/
protected function add_checkbox_field(\MoodleQuickForm $mform, array &$group, \stdClass $field, string $shortname): void {
$options = (array) $field->param1;

$elements = [];
$elements[] = $mform->createElement('hidden', $shortname . '_operator', self::TEXT_IS_EQUAL_TO);
$elements[] = $mform->createElement('select', $shortname . '_value', $field->name, $options);
$group[] = $mform->createElement('group', $shortname, '', $elements, '', false);
$mform->hideIf($shortname, self::FIELD_NAME, 'neq', $shortname);
}
/**
* Gets required config data from submitted condition form data.
*
Expand All @@ -121,24 +177,7 @@ public static function retrieve_config_data(\stdClass $formdata): array {
* @return bool
*/
protected function should_include_missing_data(): bool {
if (in_array($this->get_operator_value(), $this->missing_data_operators())) {
return !empty($this->get_config_data()[self::INCLUDE_MISSING_DATA_FIELD_NAME]);
}

return false;
}

/**
* A list of operators that getting missing data does make sense for.
*
* @return array
*/
protected function missing_data_operators(): array {
return [
self::TEXT_DOES_NOT_CONTAIN,
self::TEXT_IS_NOT_EQUAL_TO,
self::TEXT_IS_EMPTY,
];
return !empty($this->get_config_data()[self::INCLUDE_MISSING_DATA_FIELD_NAME]);
}

/**
Expand All @@ -157,6 +196,7 @@ public function get_sql(): condition_sql {
case self::FIELD_DATA_TYPE_TEXT:
$result = $this->get_text_sql_data($ud, 'data');
break;
case self::FIELD_DATA_TYPE_CHECKBOX:
case self::FIELD_DATA_TYPE_MENU:
$result = $this->get_menu_sql_data($ud, 'data');
break;
Expand Down Expand Up @@ -191,6 +231,26 @@ public function get_sql(): condition_sql {
return $result;
}

/**
* Returns a value of the configured field.
*
* @return string|null
*/
protected function get_field_value(): ?string {
$fieldvalue = parent::get_field_value();

// A special case for checkbox field.
$fieldname = $this->get_field_name();
if (!empty($fieldname) && !empty($this->get_fields_info()[$fieldname])) {
$datatype = $this->get_fields_info()[$fieldname]->datatype;
if ($datatype == self::FIELD_DATA_TYPE_CHECKBOX) {
$fieldvalue = empty($fieldvalue) ? get_string('no') : get_string('yes');
}
}

return $fieldvalue;
}

/**
* Human-readable description of the configured condition.
*
Expand Down
9 changes: 6 additions & 3 deletions classes/local/tool_dynamic_cohorts/condition/user_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use tool_dynamic_cohorts\condition_sql;
use core_user;
use core_plugin_manager;
use coding_exception;

/**
* Condition using standard user profile.
Expand All @@ -31,7 +32,7 @@
class user_profile extends condition_base {

/**
* Base field nname.
* Base field name.
*/
public const FIELD_NAME = 'profilefield';

Expand Down Expand Up @@ -121,9 +122,11 @@ public function config_form_add(\MoodleQuickForm $mform): void {
case self::FIELD_DATA_TYPE_TEXT:
$this->add_text_field($mform, $group, $field, $shortname);
break;
case self::FIELD_DATA_TYPE_MENU:
case self::FIELD_DATA_TYPE_MENU:
$this->add_menu_field($mform, $group, $field, $shortname);
break;
default:
throw new coding_exception('Invalid field type ' . $field->datatype);
}
}

Expand Down Expand Up @@ -349,7 +352,7 @@ protected function get_operator_text(string $fielddatatype): string {
}

/**
* Human readable description of the configured condition.
* Human-readable description of the configured condition.
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$editurl = new moodle_url('/admin/tool/dynamic_cohorts/edit.php');

foreach (rule::get_records() as $rule) {
if ($rule->is_broken(true)) {
if ($rule->is_broken()) {
notification::warning(get_string('brokenruleswarning', 'tool_dynamic_cohorts'));
break;
}
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_dynamic_cohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
$string['event:ruledeleted'] = 'Rule deleted';
$string['includingmissingdatadesc'] = '(including users with missing data)';
$string['includeusersmissingdata'] = 'include users with missing data';
$string['include_missing_data'] = 'Include users with missing data.';
$string['include_missing_data_help'] = 'Some users may not have a custom field data set yet. This option includes those user in the final result.';
$string['invalidfieldvalue'] = 'Invalid field value';
$string['ismemberof'] = 'is member of';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ public function test_set_and_get_configdata() {
*/
public function config_description_data_provider(): array {
return [
[user_profile::TEXT_CONTAINS, 'Test field1 contains 123', false],
[user_profile::TEXT_CONTAINS, 'Test field1 contains 123', true],
[user_profile::TEXT_DOES_NOT_CONTAIN, 'Test field1 doesn\'t contain 123', true],
[user_profile::TEXT_IS_EQUAL_TO, 'Test field1 is equal to 123', false],
[user_profile::TEXT_IS_EQUAL_TO, 'Test field1 is equal to 123', true],
[user_profile::TEXT_IS_NOT_EQUAL_TO, 'Test field1 isn\'t equal to 123', true],
[user_profile::TEXT_STARTS_WITH, 'Test field1 starts with 123', false],
[user_profile::TEXT_ENDS_WITH, 'Test field1 ends with 123', false],
[user_profile::TEXT_STARTS_WITH, 'Test field1 starts with 123', true],
[user_profile::TEXT_ENDS_WITH, 'Test field1 ends with 123', true],
[user_profile::TEXT_IS_EMPTY, 'Test field1 is empty', true],
[user_profile::TEXT_IS_NOT_EMPTY, 'Test field1 is not empty', false],
[user_profile::TEXT_IS_NOT_EMPTY, 'Test field1 is not empty', true],
];
}

Expand Down

0 comments on commit 4eb5cfb

Please sign in to comment.