public function calculate($expression) {
        // Make sure no malicious code is present in the expression. Refer MDL-46148 for details.
        if ($error = qtype_calculated_find_formula_errors($expression)) {
            throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '', $error);
        }
        $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);
    }