3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
21 * @copyright Petr Skoda (skodak)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 /** @see evalmath/evalmath.class.php */
28 require_once $CFG->dirroot
.'/lib/evalmath/evalmath.class.php';
31 * This class abstracts evaluation of spreadsheet formulas.
32 * See unit tests in lib/simpletest/testmathslib.php for sample usage.
35 * @copyright Petr Skoda (skodak)
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 var $_nfx = false; // postfix notation
43 var $_error = false; // last error
46 * Constructor for spreadsheet formula with optional parameters
48 * @param string $formula with leading =
49 * @param array $params associative array of parameters used in formula. All parameter names must be lowercase!
51 function calc_formula($formula, $params=false) {
52 $this->_em
= new EvalMath();
53 $this->_em
->suppress_errors
= !debugging('', DEBUG_DEVELOPER
);
54 if (strpos($formula, '=') !== 0) {
55 $this->_error
= "missing leading '='";
58 $formula = substr($formula, 1);
59 if (strpos($formula, '=') !== false) {
60 $this->_error
= "too many '='";
63 $this->_nfx
= $this->_em
->nfx($formula);
64 if ($this->_nfx
== false) {
65 $this->_error
= $this->_em
->last_error
;
68 if ($params != false) {
69 $this->set_params($params);
74 * Raplace parameters used in existing formula,
75 * parameter names must contain only lowercase [a-z] letters, no other characters are allowed!
77 * @param array $params associative array of parameters used in formula
79 function set_params($params) {
80 $this->_em
->v
= $params;
86 * @return mixed number if ok, false if error
89 if ($this->_nfx
== false) {
92 $res = $this->_em
->pfx($this->_nfx
);
94 $this->_error
= $this->_em
->last_error
;
97 $this->_error
= false;
104 * TODO: localize the strings from contructor and EvalMath library
106 * @return mixed string with last error description or false if ok
108 function get_error() {
109 return $this->_error
;
113 * Similar to format_float, formats the numbers and list separators
114 * according to locale specifics.
115 * @param string $formula
116 * @return string localised formula
118 function localize($formula) {
119 $formula = str_replace('.', '$', $formula); // temp placeholder
120 $formula = str_replace(',', get_string('listsep', 'langconfig'), $formula);
121 $formula = str_replace('$', get_string('decsep', 'langconfig'), $formula);
126 * Similar to unformat_float, converts floats and lists to PHP standards.
127 * @param string $formula localised formula
130 function unlocalize($formula) {
131 $formula = str_replace(get_string('decsep', 'langconfig'), '$', $formula);
132 $formula = str_replace(get_string('listsep', 'langconfig'), ',', $formula);
133 $formula = str_replace('$', '.', $formula); // temp placeholder