Skip to content

Commit e60fb0c

Browse files
committed
feat: 🔨 implement "IN" and "NOT IN" in text custom fields
1 parent 7a285f6 commit e60fb0c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

classes/condition_base.php

+10
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ abstract class condition_base {
100100
*/
101101
public const TEXT_IS_NOT_EQUAL_TO = 8;
102102

103+
/**
104+
* Value for operator text is in array
105+
*/
106+
public const TEXT_IN = 9;
107+
108+
/**
109+
* Value for operator text is not in array
110+
*/
111+
public const TEXT_NOT_IN = 10;
112+
103113
/**
104114
* Value for operator date is after.
105115
*/

classes/local/tool_dynamic_cohorts/condition/fields_trait.php

+29
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ protected function get_text_operators(): array {
4242
self::TEXT_ENDS_WITH => get_string('endswith', 'filters'),
4343
self::TEXT_IS_EMPTY => get_string('isempty', 'filters'),
4444
self::TEXT_IS_NOT_EMPTY => get_string('isnotempty', 'tool_dynamic_cohorts'),
45+
self::TEXT_IN => get_string('textin', 'tool_dynamic_cohorts'),
46+
self::TEXT_NOT_IN => get_string('textnotin', 'tool_dynamic_cohorts'),
4547
];
4648
}
4749

@@ -340,13 +342,40 @@ protected function get_text_sql(string $tablealias, string $fieldname): conditio
340342
$where = $DB->sql_compare_text("$tablealias.$fieldname") . " != " . $DB->sql_compare_text(":$param");
341343
$params[$param] = '';
342344
break;
345+
case self::TEXT_IN:
346+
[$insql, $inparams] = $DB->get_in_or_equal(explode(', ', $fieldvalue), SQL_PARAMS_QM);
347+
348+
foreach($inparams as $key => $val) {
349+
$insql = $this->str_replace_first('?', ':'.$param, $insql);
350+
$params[$param] = $val;
351+
$param = condition_sql::generate_param_alias();
352+
}
353+
$where = $DB->sql_compare_text("$tablealias.$fieldname") . " " . $insql;
354+
break;
355+
case self::TEXT_NOT_IN:
356+
[$insql, $inparams] = $DB->get_in_or_equal(explode(', ', $fieldvalue), SQL_PARAMS_QM, 'param', false);
357+
358+
foreach ($inparams as $key => $val) {
359+
$insql = $this->str_replace_first('?', ':' . $param, $insql);
360+
$params[$param] = $val;
361+
$param = condition_sql::generate_param_alias();
362+
}
363+
$where = $DB->sql_compare_text("$tablealias.$fieldname") . " " . $insql;
364+
break;
343365
default:
344366
return new condition_sql('', '', []);
345367
}
346368

369+
347370
return new condition_sql('', $where, $params);
348371
}
349372

373+
protected function str_replace_first($search, $replace, $subject)
374+
{
375+
$search = '/' . preg_quote($search, '/') . '/';
376+
return preg_replace($search, $replace, $subject, 1);
377+
}
378+
350379
/**
351380
* Get SQL data for menu type fields.
352381
*

lang/en/tool_dynamic_cohorts.php

+2
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,5 @@
165165
$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.';
166166
$string['rule_entity.realtime'] = 'Realtime processing';
167167
$string['realtimedisabledglobally'] = 'Realtime processing disabled globally';
168+
$string['textin'] = ' set to one of the values';
169+
$string['textnotin'] = 'not set to one of the values';

0 commit comments

Comments
 (0)