diff --git a/classes/condition_base.php b/classes/condition_base.php index 7fac373..3753fb4 100644 --- a/classes/condition_base.php +++ b/classes/condition_base.php @@ -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. */ diff --git a/classes/local/tool_dynamic_cohorts/condition/fields_trait.php b/classes/local/tool_dynamic_cohorts/condition/fields_trait.php index cdbe679..995a280 100644 --- a/classes/local/tool_dynamic_cohorts/condition/fields_trait.php +++ b/classes/local/tool_dynamic_cohorts/condition/fields_trait.php @@ -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'), ]; } @@ -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. * diff --git a/lang/en/tool_dynamic_cohorts.php b/lang/en/tool_dynamic_cohorts.php index 9ad2c1b..dccea9b 100644 --- a/lang/en/tool_dynamic_cohorts.php +++ b/lang/en/tool_dynamic_cohorts.php @@ -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'; \ No newline at end of file