From 81ee3ec88048eb59924189afd82e81dee194d361 Mon Sep 17 00:00:00 2001 From: Kevin Pham Date: Tue, 25 Jul 2023 15:43:44 +1000 Subject: [PATCH] feat: get dynamic vars with expression get_var() Leverages the root variable tree. --- classes/local/provider/expression_provider.php | 15 +++++++++++++++ classes/local/step/flow_transformer_filter.php | 6 ++---- classes/local/variables/var_value.php | 3 ++- lang/en/tool_dataflows.php | 2 +- version.php | 4 ++-- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/classes/local/provider/expression_provider.php b/classes/local/provider/expression_provider.php index 097c268f..bcd64048 100644 --- a/classes/local/provider/expression_provider.php +++ b/classes/local/provider/expression_provider.php @@ -18,6 +18,7 @@ use Symfony\Component\ExpressionLanguage\ExpressionFunction; use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; +use tool_dataflows\parser; /** * Expression Provider @@ -55,9 +56,23 @@ public function getFunctions(): array { // phpcs:ignore return isset($var); }); + // Get variable (to handle dynamic variable names). + $getvar = new ExpressionFunction('get_var', function ($str) + { + return sprintf('get_var(%1$s)', $str); + }, function ($arguments, $var) + { + if (isset($arguments['root'])) { + $result = $arguments['root']->get($var); + return $result; + } + return $var; + }); + return [ $fromjson, $isset, + $getvar, ExpressionFunction::fromPhp('count', 'count'), ExpressionFunction::fromPhp('format_time', 'format_time'), ExpressionFunction::fromPhp('round', 'round'), diff --git a/classes/local/step/flow_transformer_filter.php b/classes/local/step/flow_transformer_filter.php index 944cfb5b..88178405 100644 --- a/classes/local/step/flow_transformer_filter.php +++ b/classes/local/step/flow_transformer_filter.php @@ -16,10 +16,6 @@ namespace tool_dataflows\local\step; -use tool_dataflows\local\execution\flow_engine_step; -use tool_dataflows\local\execution\iterators\dataflow_iterator; -use tool_dataflows\local\execution\iterators\iterator; - /** * Flow filter (transformer step) class * @@ -80,10 +76,12 @@ public function form_add_custom_inputs(\MoodleQuickForm &$mform) { */ public function execute($input = null) { $expr = $this->stepdef->config->filter; + $result = (bool) $this->get_variables()->evaluate('${{ ' . $expr . ' }}'); if (!$result) { return false; }; + return $input; } } diff --git a/classes/local/variables/var_value.php b/classes/local/variables/var_value.php index d5d819cb..d241ddb1 100644 --- a/classes/local/variables/var_value.php +++ b/classes/local/variables/var_value.php @@ -150,6 +150,7 @@ protected function get_resolved() { */ public function evaluate(?callable $errorhandler = null) { $tree = $this->make_reference_tree(); + $tree->root = $this->root; if (is_null($errorhandler)) { $this->resolved = parser::get_parser()->evaluate($this->raw, (array) $tree); } else { @@ -191,7 +192,7 @@ private function make_reference_tree(): \stdClass { foreach ($this->references as $name => $obj) { $value = $obj->get_resolved(false); - // Do not add the value if it is not set. This shoudl result in the expression remaining unresolved in the evalutaion. + // Do not add the value if it is not set. This should result in the expression remaining unresolved in the evalutaion. if (is_null($value)) { continue; } diff --git a/lang/en/tool_dataflows.php b/lang/en/tool_dataflows.php index dc231fc4..ccfe7b68 100644 --- a/lang/en/tool_dataflows.php +++ b/lang/en/tool_dataflows.php @@ -492,7 +492,7 @@ // Flow transformer filter. $string['flow_transformer_filter:filter'] = 'Filter expression'; -$string['flow_transformer_filter:filter_help'] = "This expression must evaluate to a boolean. When the expression evaluates to false, the current value for the flow will not be passed on to later steps in the flow. There is no need for '\${{' and '}}'."; +$string['flow_transformer_filter:filter_help'] = "This expression must evaluate to a boolean. When false, the current value will not be passed on to later flow steps. There is no need for '\${{' and '}}'."; // Flow tranformer regex. $string['flow_transformer_regex:pattern'] = 'Regex pattern'; diff --git a/version.php b/version.php index d4882ea0..b05d18eb 100644 --- a/version.php +++ b/version.php @@ -25,8 +25,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023072600; -$plugin->release = 2023072600; +$plugin->version = 2023072601; +$plugin->release = 2023072601; $plugin->requires = 2017051500; // Our lowest supported Moodle (3.3.0). $plugin->supported = [35, 401]; // Available as of Moodle 3.9.0 or later. // TODO $plugin->incompatible = ; // Available as of Moodle 3.9.0 or later.