Skip to content

Commit

Permalink
feat: get dynamic vars with expression get_var()
Browse files Browse the repository at this point in the history
Leverages the root variable tree.
  • Loading branch information
keevan committed Jul 26, 2023
1 parent b628864 commit 81ee3ec
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
15 changes: 15 additions & 0 deletions classes/local/provider/expression_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use tool_dataflows\parser;

/**
* Expression Provider
Expand Down Expand Up @@ -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'),
Expand Down
6 changes: 2 additions & 4 deletions classes/local/step/flow_transformer_filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion classes/local/variables/var_value.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 81ee3ec

Please sign in to comment.