2007-05-25 06:50:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once $CFG->dirroot.'/lib/evalmath/evalmath.class.php';
|
|
|
|
|
2007-05-26 12:59:13 +00:00
|
|
|
/**
|
|
|
|
* This class abstracts evaluation of spreadsheet formulas.
|
|
|
|
* See unit tests in lib/simpletest/testmathslib.php for sample usage.
|
|
|
|
*
|
|
|
|
* @author Petr Skoda (skodak)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
*/
|
2007-05-25 06:50:09 +00:00
|
|
|
class calc_formula {
|
|
|
|
|
2007-05-26 12:59:13 +00:00
|
|
|
// private properties
|
|
|
|
var $_em;
|
2007-05-26 13:11:54 +00:00
|
|
|
var $_nfx = false; // postfix notation
|
2007-05-26 12:59:13 +00:00
|
|
|
var $_error = false; // last error
|
2007-05-25 06:50:09 +00:00
|
|
|
|
2007-05-26 12:59:13 +00:00
|
|
|
/**
|
|
|
|
* Constructor for spreadsheet formula with optional parameters
|
|
|
|
*
|
|
|
|
* @param string $formula with leading =
|
|
|
|
* @param array $params associative array of parameters used in formula. All parameter names must be lowercase!
|
|
|
|
*/
|
2007-05-25 06:50:09 +00:00
|
|
|
function calc_formula($formula, $params=false) {
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->_em = new EvalMath();
|
2007-12-10 16:53:17 +00:00
|
|
|
$this->_em->suppress_errors = !debugging('', DEBUG_DEVELOPER);
|
2007-05-25 06:50:09 +00:00
|
|
|
if (strpos($formula, '=') !== 0) {
|
2007-05-26 13:11:54 +00:00
|
|
|
$this->_error = "missing leading '='";
|
2007-05-25 06:50:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$formula = substr($formula, 1);
|
|
|
|
if (strpos($formula, '=') !== false) {
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->_error = "too many '='";
|
2007-05-25 06:50:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->_nfx = $this->_em->nfx($formula);
|
|
|
|
if ($this->_nfx == false) {
|
|
|
|
$this->_error = $this->_em->last_error;
|
2007-05-25 06:50:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($params != false) {
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->set_params($params);
|
2007-05-25 06:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-26 12:59:13 +00:00
|
|
|
/**
|
2007-05-26 13:11:54 +00:00
|
|
|
* Raplace parameters used in existing formula,
|
|
|
|
* parameter names must contain only lowercase [a-z] letters, no other characters are allowed!
|
2007-05-26 12:59:13 +00:00
|
|
|
*
|
2007-05-26 13:11:54 +00:00
|
|
|
* @param array $params associative array of parameters used in formula
|
2007-05-26 12:59:13 +00:00
|
|
|
*/
|
2007-05-25 06:50:09 +00:00
|
|
|
function set_params($params) {
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->_em->v = $params;
|
2007-05-25 06:50:09 +00:00
|
|
|
}
|
|
|
|
|
2007-05-26 12:59:13 +00:00
|
|
|
/**
|
|
|
|
* Evaluate formula
|
|
|
|
*
|
|
|
|
* @return mixed number if ok, false if error
|
|
|
|
*/
|
2007-05-25 06:50:09 +00:00
|
|
|
function evaluate() {
|
2007-05-26 12:59:13 +00:00
|
|
|
if ($this->_nfx == false) {
|
2007-05-25 06:50:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-05-26 12:59:13 +00:00
|
|
|
$res = $this->_em->pfx($this->_nfx);
|
2007-05-25 06:50:09 +00:00
|
|
|
if ($res === false) {
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->_error = $this->_em->last_error;
|
2007-05-25 06:50:09 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2007-05-26 12:59:13 +00:00
|
|
|
$this->_error = false;
|
2007-05-25 06:50:09 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-26 12:59:13 +00:00
|
|
|
/**
|
|
|
|
* Get last error.
|
2007-05-26 13:11:54 +00:00
|
|
|
* TODO: localize the strings from contructor and EvalMath library
|
2007-05-26 12:59:13 +00:00
|
|
|
*
|
|
|
|
* @return mixed string with last error description or false if ok
|
|
|
|
*/
|
2007-05-25 06:50:09 +00:00
|
|
|
function get_error() {
|
2007-05-26 12:59:13 +00:00
|
|
|
return $this->_error;
|
2007-05-25 06:50:09 +00:00
|
|
|
}
|
2007-08-10 15:00:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to format_float, formats the numbers and list separators
|
|
|
|
* according to locale specifics.
|
|
|
|
* @param string $formula
|
|
|
|
* @return string localised formula
|
|
|
|
*/
|
|
|
|
function localize($formula) {
|
|
|
|
$formula = str_replace('.', '$', $formula); // temp placeholder
|
|
|
|
$formula = str_replace(',', get_string('listsep'), $formula);
|
|
|
|
$formula = str_replace('$', get_string('decsep'), $formula);
|
|
|
|
return $formula;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to unformat_float, converts floats and lists to PHP standards.
|
|
|
|
* @param string $formula localised formula
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function unlocalize($formula) {
|
|
|
|
$formula = str_replace(get_string('decsep'), '$', $formula);
|
|
|
|
$formula = str_replace(get_string('listsep'), ',', $formula);
|
|
|
|
$formula = str_replace('$', '.', $formula); // temp placeholder
|
|
|
|
return $formula;
|
|
|
|
}
|
2007-05-25 06:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|