moodle/lib/mathslib.php

112 lines
3.3 KiB
PHP
Raw Normal View History

<?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
*/
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-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!
*/
function calc_formula($formula, $params=false) {
2007-05-26 12:59:13 +00:00
$this->_em = new EvalMath();
$this->_em->suppress_errors = !debugging('', DEBUG_DEVELOPER);
if (strpos($formula, '=') !== 0) {
2007-05-26 13:11:54 +00:00
$this->_error = "missing leading '='";
return;
}
$formula = substr($formula, 1);
if (strpos($formula, '=') !== false) {
2007-05-26 12:59:13 +00:00
$this->_error = "too many '='";
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;
return;
}
if ($params != false) {
2007-05-26 12:59:13 +00:00
$this->set_params($params);
}
}
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
*/
function set_params($params) {
2007-05-26 12:59:13 +00:00
$this->_em->v = $params;
}
2007-05-26 12:59:13 +00:00
/**
* Evaluate formula
*
* @return mixed number if ok, false if error
*/
function evaluate() {
2007-05-26 12:59:13 +00:00
if ($this->_nfx == false) {
return false;
}
2007-05-26 12:59:13 +00:00
$res = $this->_em->pfx($this->_nfx);
if ($res === false) {
2007-05-26 12:59:13 +00:00
$this->_error = $this->_em->last_error;
return false;
} else {
2007-05-26 12:59:13 +00:00
$this->_error = false;
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
*/
function get_error() {
2007-05-26 12:59:13 +00:00
return $this->_error;
}
/**
* 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;
}
}
?>