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

feat: 🔨 implement "IN" and "NOT IN" in text custom fields #128

Open
wants to merge 1 commit into
base: MOODLE_404_STABLE
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions classes/condition_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ abstract class condition_base {
*/
public const TEXT_IS_NOT_EQUAL_TO = 8;

/**
* Value for operator text is in array
*/
public const TEXT_IN = 9;

/**
* Value for operator text is not in array
*/
public const TEXT_NOT_IN = 10;

/**
* Value for operator date is after.
*/
Expand Down
29 changes: 29 additions & 0 deletions classes/local/tool_dynamic_cohorts/condition/fields_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ protected function get_text_operators(): array {
self::TEXT_ENDS_WITH => get_string('endswith', 'filters'),
self::TEXT_IS_EMPTY => get_string('isempty', 'filters'),
self::TEXT_IS_NOT_EMPTY => get_string('isnotempty', 'tool_dynamic_cohorts'),
self::TEXT_IN => get_string('textin', 'tool_dynamic_cohorts'),
self::TEXT_NOT_IN => get_string('textnotin', 'tool_dynamic_cohorts'),
];
}

Expand Down Expand Up @@ -340,13 +342,40 @@ protected function get_text_sql(string $tablealias, string $fieldname): conditio
$where = $DB->sql_compare_text("$tablealias.$fieldname") . " != " . $DB->sql_compare_text(":$param");
$params[$param] = '';
break;
case self::TEXT_IN:
[$insql, $inparams] = $DB->get_in_or_equal(explode(', ', $fieldvalue), SQL_PARAMS_QM);

foreach($inparams as $key => $val) {
$insql = $this->str_replace_first('?', ':'.$param, $insql);
$params[$param] = $val;
$param = condition_sql::generate_param_alias();
}
$where = $DB->sql_compare_text("$tablealias.$fieldname") . " " . $insql;
break;
case self::TEXT_NOT_IN:
[$insql, $inparams] = $DB->get_in_or_equal(explode(', ', $fieldvalue), SQL_PARAMS_QM, 'param', false);

foreach ($inparams as $key => $val) {
$insql = $this->str_replace_first('?', ':' . $param, $insql);
$params[$param] = $val;
$param = condition_sql::generate_param_alias();
}
$where = $DB->sql_compare_text("$tablealias.$fieldname") . " " . $insql;
break;
default:
return new condition_sql('', '', []);
}


return new condition_sql('', $where, $params);
}

protected function str_replace_first($search, $replace, $subject)
{
$search = '/' . preg_quote($search, '/') . '/';
return preg_replace($search, $replace, $subject, 1);
}

/**
* Get SQL data for menu type fields.
*
Expand Down
2 changes: 2 additions & 0 deletions lang/en/tool_dynamic_cohorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,5 @@
$string['realtime_help'] = 'If enabled, the rule will be processed synchronously as part of the event (if conditions support triggering on the event). Use caution when enabling as long running rule processing will block the user interface.';
$string['rule_entity.realtime'] = 'Realtime processing';
$string['realtimedisabledglobally'] = 'Realtime processing disabled globally';
$string['textin'] = ' set to one of the values';
$string['textnotin'] = 'not set to one of the values';
Loading