if ($error = qtype_calculated_find_formula_errors($expression)) {
throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '', $error);
}
- return $this->calculate_raw($this->substitute_values_for_eval($expression));
+ $expression = $this->substitute_values_for_eval($expression);
+ if ($datasets = question_bank::get_qtype('calculated')->find_dataset_names($expression)) {
+ // Some placeholders were not substituted.
+ throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '',
+ '{' . reset($datasets) . '}');
+ }
+ return $this->calculate_raw($expression);
}
/**
$this->assertEquals('= 3', $vs->replace_expressions_in_text('= {={a} + {b}}'));
}
+ public function test_expression_has_unmapped_placeholder() {
+ $this->expectException('moodle_exception');
+ $this->expectExceptionMessage(get_string('illegalformulasyntax', 'qtype_calculated', '{c}'));
+ $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.');
+ $vs->calculate('{c} - {a} + {b}');
+ }
+
public function test_replace_expressions_in_text_negative() {
$vs = new qtype_calculated_variable_substituter(array('a' => -1, 'b' => 2), '.');
$this->assertEquals('temperatures -1 and 2',