31 '+' => array(
'precedence' => 1),
32 '-' => array(
'precedence' => 1),
33 '*' => array(
'precedence' => 2),
34 '/' => array(
'precedence' => 2),
35 '^' => array(
'precedence' => 3),
88 if (isset(self::$cache_tokens[$this->field->getId()])) {
89 $tokens = self::$cache_tokens[$this->field->getId()];
92 self::$cache_tokens[$this->field->getId()] = $tokens;
96 foreach ($tokens as
$token) {
110 if (strpos($token,
'"') === 0) {
111 $parsed .= strip_tags(trim($token,
'"'));
112 } elseif (strpos($token,
'[[') === 0) {
113 $parsed .= trim(strip_tags($this->substituteFieldValue($token)));
115 throw new ilException(
"Unrecognized string token: '$token'");
131 if (abs($value) >= self::SCIENTIFIC_NOTATION_UPPER) {
132 return sprintf(
"%e", $value);
134 if (abs($value) <= self::SCIENTIFIC_NOTATION_LOWER && $value != 0) {
135 return sprintf(
"%e", $value);
137 if (is_float($value)) {
150 return self::$operators;
159 return self::$functions;
172 if (isset(self::$cache_math_tokens[$this->field->getId()][
$token])) {
173 return self::$cache_math_tokens[$this->field->getId()][
$token];
175 if (strpos(
$token,
'"') === 0) {
178 $operators = array_keys(self::getOperators());
181 self::$cache_math_tokens[$this->field->getId()][
$token] =
$result;
197 if (isset(self::$cache_math_function_tokens[$this->field->getId()][
$token])) {
198 $result = self::$cache_math_function_tokens[$this->field->getId()][
$token];
204 foreach (self::getFunctions() as $function) {
205 $pattern .=
"($function)\\(([^)]*)\\)|";
207 if (!preg_match_all(rtrim($pattern,
'|') .
'#',
$token,
$result)) {
209 self::$cache_math_function_tokens[$this->field->getId()][
$token] =
false;
215 foreach (
$result[0] as $k => $to_replace) {
217 $function = $function_args[
'function'];
240 for (
$i = 1;
$i < count($data);
$i++) {
243 $function = $_data[
$index];
244 $args = explode(
';', $data[
$i + 1][$index]);
245 $return[
'function'] = $function;
246 $return[
'args'] = $args;
265 foreach ($tokens as
$token) {
266 if (strpos($token,
'[[') === 0) {
267 $replaced[] = $this->substituteFieldValue($token);
285 protected function substituteFieldValue($placeholder)
287 if (isset(self::$cache_fields[$placeholder])) {
288 $field = self::$cache_fields[$placeholder];
291 $field_title = preg_replace(
'#^\[\[(.*)\]\]#',
"$1", $placeholder);
302 $lng->loadLanguageModule(
'dcl');
304 throw new ilException(sprintf(
$lng->txt(
'dcl_err_formula_field_not_found'), $field_title));
307 self::$cache_fields[$placeholder] =
$field;
310 return $this->record->getRecordFieldFormulaValue(
$field->getId());
329 foreach ($tokens as
$token) {
330 if (empty($token)
or is_null($token)) {
333 if (is_numeric($token)
or $token ===
'(') {
334 $stack->push($token);
335 if ($token ===
'(') {
338 } elseif (in_array($token, array_keys(
$operators))) {
340 if ($new_precedence > $precedence || $in_bracket) {
342 $stack->push($token);
343 $precedences->push($new_precedence);
344 $precedence = $new_precedence;
347 while ($new_precedence <= $precedence && $stack->count() > 1) {
348 $right = (float) $stack->pop();
349 $operator = $stack->pop();
350 $left = (float) $stack->pop();
353 $precedence = $precedences->pop();
355 $stack->push($token);
356 $precedence = $new_precedence;
357 $precedences->push($new_precedence);
359 } elseif ($token ===
')') {
362 $elem = $stack->pop();
363 while ($elem !==
'(' && !$stack->isEmpty()) {
365 $elem = $stack->pop();
368 $stack->push($this->
parseMath(array_reverse($_tokens)));
371 throw new ilException(
"Unrecognized token '$token'");
376 if ($stack->count() == 1) {
379 return (ctype_digit((
string)
$result)) ?
$result : number_format($result, self::N_DECIMALS,
'.',
"'");
381 while ($stack->count() >= 2) {
382 $right = $stack->pop();
383 $operator = $stack->pop();
384 $left = $stack->count() ? $stack->pop() : 0;
385 $stack->push($this->
calculate($operator, $left, $right));
407 $count = count($args);
409 return ($count) ? array_sum($args) / $count : 0;
411 return array_sum($args);
417 throw new ilException(
"Unrecognized function '$function'");
443 $result = ($right == 0) ? 0 : $left / $right;
449 throw new ilException(
"Unrecognized operator '$operator'");
Class ilDclBaseFieldModel.
static $cache_math_function_tokens
Class ilDclExpressionParser.
parse()
Parse expression and return result.
calculate($operator, $left, $right)
substituteFieldValues(array $tokens)
Given an array of tokens, replace each token that is a placeholder (e.g.
getFunctionArgs($index, array $data)
Helper method to return the function and its arguments from a preg_replace_all $result array...
__construct($expression, ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
static getTableCache($table_id=0)
parseMath(array $tokens)
Parse a math expression.
isMathToken($token)
Check if a given token is a math expression.
calculateFunctions($token)
Execute any math functions inside a token.
static $cache_math_tokens
static getTokens($expression)
Split expression by & (ignore escaped &-symbols with backslash)
static getMathTokens($math_expression)
Generate tokens for a math expression.
const SCIENTIFIC_NOTATION_UPPER
Class ilDclBaseRecordModel.
if(empty($password)) $table
calculateFunction($function, array $args=array())
Calculate a function with its arguments.
const SCIENTIFIC_NOTATION_LOWER