Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / mathslib.php
blob07112d34d04a0d13d6a0e0298035cd70cbc36d86
1 <?php
3 require_once $CFG->dirroot.'/lib/evalmath/evalmath.class.php';
5 /**
6 * This class abstracts evaluation of spreadsheet formulas.
7 * See unit tests in lib/simpletest/testmathslib.php for sample usage.
9 * @author Petr Skoda (skodak)
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 class calc_formula {
14 // private properties
15 var $_em;
16 var $_nfx = false; // postfix notation
17 var $_error = false; // last error
19 /**
20 * Constructor for spreadsheet formula with optional parameters
22 * @param string $formula with leading =
23 * @param array $params associative array of parameters used in formula. All parameter names must be lowercase!
25 function calc_formula($formula, $params=false) {
26 $this->_em = new EvalMath();
27 $this->_em->suppress_errors = true;
28 if (strpos($formula, '=') !== 0) {
29 $this->_error = "missing leading '='";
30 return;
32 $formula = substr($formula, 1);
33 if (strpos($formula, '=') !== false) {
34 $this->_error = "too many '='";
35 return;
37 $this->_nfx = $this->_em->nfx($formula);
38 if ($this->_nfx == false) {
39 $this->_error = $this->_em->last_error;
40 return;
42 if ($params != false) {
43 $this->set_params($params);
47 /**
48 * Raplace parameters used in existing formula,
49 * parameter names must contain only lowercase [a-z] letters, no other characters are allowed!
51 * @param array $params associative array of parameters used in formula
53 function set_params($params) {
54 $this->_em->v = $params;
57 /**
58 * Evaluate formula
60 * @return mixed number if ok, false if error
62 function evaluate() {
63 if ($this->_nfx == false) {
64 return false;
66 $res = $this->_em->pfx($this->_nfx);
67 if ($res === false) {
68 $this->_error = $this->_em->last_error;
69 return false;
70 } else {
71 $this->_error = false;
72 return $res;
76 /**
77 * Get last error.
78 * TODO: localize the strings from contructor and EvalMath library
80 * @return mixed string with last error description or false if ok
82 function get_error() {
83 return $this->_error;