Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/lib/ -> mathslib.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    core
  20   * @subpackage lib
  21   * @copyright  Petr Skoda (skodak)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /** @see evalmath/evalmath.class.php */
  28  require_once $CFG->dirroot.'/lib/evalmath/evalmath.class.php';
  29  
  30  /**
  31   * This class abstracts evaluation of spreadsheet formulas.
  32   * See unit tests in lib/tests/mathslib_test.php for sample usage.
  33   *
  34   * @package moodlecore
  35   * @copyright Petr Skoda (skodak)
  36    * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class calc_formula {
  39  
  40      // private properties
  41      var $_em;
  42      var $_nfx   = false;   // postfix notation
  43      var $_error = false; // last error
  44  
  45      /**
  46       * Constructor for spreadsheet formula with optional parameters
  47       *
  48       * @param string $formula with leading =
  49       * @param array $params associative array of parameters used in formula. All parameter names must be lowercase!
  50       */
  51      public function __construct($formula, $params=false) {
  52          $this->_em = new EvalMath();
  53          $this->_em->suppress_errors = true; // no PHP errors!
  54          if (strpos($formula, '=') !== 0) {
  55              $this->_error = "missing leading '='";
  56              return;
  57          }
  58          $formula = substr($formula, 1);
  59  
  60          $this->_nfx = $this->_em->nfx($formula);
  61          if ($this->_nfx == false) {
  62              $this->_error = $this->_em->last_error;
  63              return;
  64          }
  65          if ($params != false) {
  66              $this->set_params($params);
  67          }
  68      }
  69  
  70      /**
  71       * Old syntax of class constructor. Deprecated in PHP7.
  72       *
  73       * @deprecated since Moodle 3.1
  74       */
  75      public function calc_formula($formula, $params=false) {
  76          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  77          self::__construct($formula, $params);
  78      }
  79  
  80      /**
  81       * Raplace parameters used in existing formula,
  82       * parameter names must contain only lowercase [a-z] letters, no other characters are allowed!
  83       *
  84       * @param array $params associative array of parameters used in formula
  85       */
  86      function set_params($params) {
  87          $this->_em->v = $params;
  88      }
  89  
  90      /**
  91       * Evaluate formula
  92       *
  93       * @return mixed number if ok, false if error
  94       */
  95      function evaluate() {
  96          if ($this->_nfx == false) {
  97              return false;
  98          }
  99          $res = $this->_em->pfx($this->_nfx);
 100          if ($res === false) {
 101              $this->_error = $this->_em->last_error;
 102              return false;
 103          } else {
 104              $this->_error = false;
 105              return $res;
 106          }
 107      }
 108  
 109      /**
 110       * Get last error.
 111       * TODO: localize the strings from contructor and EvalMath library
 112       *
 113       * @return mixed string with last error description or false if ok
 114       */
 115      function get_error() {
 116          return $this->_error;
 117      }
 118  
 119      /**
 120       * Similar to format_float, formats the numbers and list separators
 121       * according to locale specifics.
 122       * @param string $formula
 123       * @return string localised formula
 124       */
 125      public static function localize($formula) {
 126          $formula = str_replace('.', '$', $formula ?? ''); // Temp placeholder.
 127          $formula = str_replace(',', get_string('listsep', 'langconfig'), $formula);
 128          $formula = str_replace('$', get_string('decsep', 'langconfig'), $formula);
 129          return $formula;
 130      }
 131  
 132      /**
 133       * Similar to unformat_float, converts floats and lists to PHP standards.
 134       * @param string $formula localised formula
 135       * @return string
 136       */
 137      public static function unlocalize($formula) {
 138          $formula = str_replace(get_string('decsep', 'langconfig'), '$', $formula);
 139          $formula = str_replace(get_string('listsep', 'langconfig'), ',', $formula);
 140          $formula = str_replace('$', '.', $formula); // temp placeholder
 141          return $formula;
 142      }
 143  }