30 '+' =>
array(
'precedence' => 1 ),
31 '-' =>
array(
'precedence' => 1 ),
32 '*' =>
array(
'precedence' => 2 ),
33 '/' =>
array(
'precedence' => 2 ),
34 '^' =>
array(
'precedence' => 3 ),
86 if (isset(self::$cache_tokens[$this->field->getId()])) {
87 $tokens = self::$cache_tokens[$this->field->getId()];
90 self::$cache_tokens[$this->field->getId()] = $tokens;
94 foreach ($tokens as $token) {
108 if (strpos($token,
'"') === 0) {
109 $parsed .= strip_tags(trim($token,
'"'));
110 } elseif (strpos($token,
'[[') === 0) {
111 $parsed .= trim(strip_tags($this->substituteFieldValue($token)));
113 throw new ilException(
"Unrecognized string token: '$token'");
129 if (abs($value) >= self::SCIENTIFIC_NOTATION_UPPER) {
130 return sprintf(
"%e", $value);
132 if (abs($value) <= self::SCIENTIFIC_NOTATION_LOWER && $value != 0) {
133 return sprintf(
"%e", $value);
135 if (is_float($value)) {
148 return self::$operators;
157 return self::$functions;
170 if (isset(self::$cache_math_tokens[$this->field->getId()][$token])) {
171 return self::$cache_math_tokens[$this->field->getId()][$token];
173 if (strpos($token,
'"') === 0) {
176 $operators = array_keys(self::getOperators());
179 self::$cache_math_tokens[$this->field->getId()][$token] =
$result;
195 if (isset(self::$cache_math_function_tokens[$this->field->getId()][$token])) {
196 $result = self::$cache_math_function_tokens[$this->field->getId()][$token];
202 foreach (self::getFunctions() as
$function) {
203 $pattern .=
"($function)\\(([^)]*)\\)|";
205 if (!preg_match_all(rtrim($pattern,
'|') .
'#', $token,
$result)) {
207 self::$cache_math_function_tokens[$this->field->getId()][$token] =
false;
213 foreach (
$result[0] as $k => $to_replace) {
238 for (
$i = 1;
$i < count($data);
$i++) {
242 $args = explode(
';', $data[
$i + 1][$index]);
244 $return[
'args'] = $args;
263 foreach ($tokens as $token) {
264 if (strpos($token,
'[[') === 0) {
265 $replaced[] = $this->substituteFieldValue($token);
267 $replaced[] = $token;
283 protected function substituteFieldValue($placeholder)
285 if (isset(self::$cache_fields[$placeholder])) {
286 $field = self::$cache_fields[$placeholder];
289 $field_title = preg_replace(
'#^\[\[(.*)\]\]#',
"$1", $placeholder);
300 $lng->loadLanguageModule(
'dcl');
302 throw new ilException(sprintf(
$lng->txt(
'dcl_err_formula_field_not_found'), $field_title));
305 self::$cache_fields[$placeholder] =
$field;
308 return $this->record->getRecordFieldHTML(
$field->getId());
327 foreach ($tokens as $token) {
328 if (empty($token) or is_null($token)) {
331 if (is_numeric($token) or $token ===
'(') {
332 $stack->push($token);
333 if ($token ===
'(') {
336 } elseif (in_array($token, array_keys(
$operators))) {
337 $new_precedence =
$operators[$token][
'precedence'];
338 if ($new_precedence > $precedence || $in_bracket) {
340 $stack->push($token);
341 $precedences->push($new_precedence);
342 $precedence = $new_precedence;
345 while ($new_precedence <= $precedence && $stack->count() > 1) {
346 $right = (float) $stack->pop();
347 $operator = $stack->pop();
348 $left = (float) $stack->pop();
351 $precedence = $precedences->pop();
353 $stack->push($token);
354 $precedence = $new_precedence;
355 $precedences->push($new_precedence);
357 } elseif ($token ===
')') {
360 $elem = $stack->pop();
361 while ($elem !==
'(' && !$stack->isEmpty()) {
363 $elem = $stack->pop();
366 $stack->push($this->
parseMath(array_reverse($_tokens)));
369 throw new ilException(
"Unrecognized token '$token'");
374 if ($stack->count() == 1) {
377 return (ctype_digit((
string)
$result)) ?
$result : number_format($result, self::N_DECIMALS,
'.',
"'");
379 while ($stack->count() >= 2) {
380 $right = $stack->pop();
381 $operator = $stack->pop();
382 $left = $stack->count() ? $stack->pop() : 0;
383 $stack->push($this->
calculate($operator, $left, $right));
405 $count = count($args);
407 return ($count) ? array_sum($args) / $count : 0;
409 return array_sum($args);
415 throw new ilException(
"Unrecognized function '$function'");
441 $result = ($right == 0) ? 0 : $left / $right;
447 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.
Create styles array
The data for the language used.
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