Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/mathslib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00025 defined('MOODLE_INTERNAL') || die();
00026 
00028 require_once $CFG->dirroot.'/lib/evalmath/evalmath.class.php';
00029 
00038 class calc_formula {
00039 
00040     // private properties
00041     var $_em;
00042     var $_nfx   = false;   // postfix notation
00043     var $_error = false; // last error
00044 
00051     function calc_formula($formula, $params=false) {
00052         $this->_em = new EvalMath();
00053         $this->_em->suppress_errors = !debugging('', DEBUG_DEVELOPER);
00054         if (strpos($formula, '=') !== 0) {
00055             $this->_error = "missing leading '='";
00056             return;
00057         }
00058         $formula = substr($formula, 1);
00059         if (strpos($formula, '=') !== false) {
00060             $this->_error = "too many '='";
00061             return;
00062         }
00063         $this->_nfx = $this->_em->nfx($formula);
00064         if ($this->_nfx == false) {
00065             $this->_error = $this->_em->last_error;
00066             return;
00067         }
00068         if ($params != false) {
00069             $this->set_params($params);
00070         }
00071     }
00072 
00079     function set_params($params) {
00080         $this->_em->v = $params;
00081     }
00082 
00088     function evaluate() {
00089         if ($this->_nfx == false) {
00090             return false;
00091         }
00092         $res = $this->_em->pfx($this->_nfx);
00093         if ($res === false) {
00094             $this->_error = $this->_em->last_error;
00095             return false;
00096         } else {
00097             $this->_error = false;
00098             return $res;
00099         }
00100     }
00101 
00108     function get_error() {
00109         return $this->_error;
00110     }
00111 
00118     function localize($formula) {
00119         $formula = str_replace('.', '$', $formula); // temp placeholder
00120         $formula = str_replace(',', get_string('listsep', 'langconfig'), $formula);
00121         $formula = str_replace('$', get_string('decsep', 'langconfig'), $formula);
00122         return $formula;
00123     }
00124 
00130     function unlocalize($formula) {
00131         $formula = str_replace(get_string('decsep', 'langconfig'), '$', $formula);
00132         $formula = str_replace(get_string('listsep', 'langconfig'), ',', $formula);
00133         $formula = str_replace('$', '.', $formula); // temp placeholder
00134         return $formula;
00135     }
00136 }
 All Data Structures Namespaces Files Functions Variables Enumerations