Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/filter/algebra/filter.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 
00029 defined('MOODLE_INTERNAL') || die();
00030 
00031 //-------------------------------------------------------------------------
00032 // NOTE: This Moodle text filter converts algebraic expressions delimited
00033 // by either @@...@@ or by <algebra...>...</algebra> tags
00034 // first converts it to TeX using WeBWorK algebra parser Perl library
00035 // AlgParser.pm, part of the WeBWorK distribution obtained from
00036 // http://webhost.math.rochester.edu/downloadwebwork/
00037 // then converts the TeX to gif images using
00038 // mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by
00039 // John Forkosh john@forkosh.com. The mimetex.cgi ELF binary compiled for Linux i386
00040 // as well as AlgParser.pm are included with this distribution.
00041 // Note that there may be patent restrictions on the production of gif images
00042 // in Canada and some parts of Western Europe and Japan until July 2004.
00043 //-------------------------------------------------------------------------
00044 // You will then need to edit your moodle/config.php to invoke mathml_filter.php
00045 //-------------------------------------------------------------------------
00046 
00047 function filter_algebra_image($imagefile, $tex= "", $height="", $width="", $align="middle") {
00048   // Given the path to a picture file in a course, or a URL,
00049   // this function includes the picture in the page.
00050   global $CFG, $OUTPUT;
00051 
00052   $output = "";
00053   $style = 'style="border:0px; vertical-align:'.$align.';';
00054   $title = '';
00055   if ($tex) {
00056     $tex = html_entity_decode($tex, ENT_QUOTES, 'UTF-8');
00057     $title = 'title="'.s($tex).'"';
00058   }
00059   if ($height) {
00060     $style .= " height:{$height}px;";
00061   }
00062   if ($width) {
00063     $style .= " width:{$width}px;";
00064   }
00065   $style .= '"';
00066   $anchorcontents = '';
00067   if ($imagefile) {
00068     $anchorcontents .= "<img $title alt=\"".s($tex)."\" src=\"";
00069     if ($CFG->slasharguments) {        // Use this method if possible for better caching
00070       $anchorcontents .= "$CFG->wwwroot/filter/algebra/pix.php/$imagefile";
00071     } else {
00072       $anchorcontents .= "$CFG->wwwroot/filter/algebra/pix.php?file=$imagefile";
00073     }
00074     $anchorcontents .= "\" $style />";
00075 
00076     if (!file_exists("$CFG->dataroot/filter/algebra/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
00077         $link = '/filter/algebra/algebradebug.php';
00078         $action = null;
00079     } else {
00080         $link = new moodle_url('/filter/tex/displaytex.php', array('texexp'=>$tex));
00081         $action = new popup_action('click', $link, 'popup', array('width'=>320,'height'=>240)); //TODO: the popups do not work when text caching is enabled!!
00082     }
00083     $output .= $OUTPUT->action_link($link, $anchorcontents, $action, array('title'=>'TeX'));
00084 
00085   } else {
00086     $output .= "Error: must pass URL or course";
00087   }
00088   return $output;
00089 }
00090 
00091 class filter_algebra extends moodle_text_filter {
00092     function filter($text, array $options = array()){
00093         global $CFG, $DB;
00094 
00096         if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) {
00097             return $text;
00098         }
00099 
00100 //restrict filtering to forum 130 (Maths Tools on moodle.org)
00101 #    $scriptname = $_SERVER['SCRIPT_NAME'];
00102 #    if (!strstr($scriptname,'/forum/')) {
00103 #        return $text;
00104 #    }
00105 #    if (strstr($scriptname,'post.php')) {
00106 #        $parent = forum_get_post_full($_GET['reply']);
00107 #        $discussion = $DB->get_record("forum_discussions",array("id"=>$parent->discussion));
00108 #    } else if (strstr($scriptname,'discuss.php')) {
00109 #        $discussion = $DB->get_record("forum_discussions",array("id"=>$_GET['d']));
00110 #    } else {
00111 #        return $text;
00112 #    }
00113 #    if ($discussion->forum != 130) {
00114 #        return $text;
00115 #    }
00116 
00117 
00118         $text .= ' ';
00119 
00120         preg_match_all('/@(@@+)([^@])/',$text,$matches);
00121         for ($i=0;$i<count($matches[0]);$i++) {
00122             $replacement = str_replace('@','&#x00040;',$matches[1][$i]).$matches[2][$i];
00123             $text = str_replace($matches[0][$i],$replacement,$text);
00124         }
00125 
00126         // <algebra> some algebraic input expression </algebra>
00127         // or @@ some algebraic input expression @@
00128 
00129         preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
00130         for ($i=0; $i<count($matches[0]); $i++) {
00131             $algebra = $matches[1][$i] . $matches[2][$i];
00132             $algebra = str_replace('<nolink>','',$algebra);
00133             $algebra = str_replace('</nolink>','',$algebra);
00134             $algebra = str_replace('<span class="nolink">','',$algebra);
00135             $algebra = str_replace('</span>','',$algebra);
00136             $align = "middle";
00137             if (preg_match('/^align=bottom /',$algebra)) {
00138               $align = "text-bottom";
00139               $algebra = preg_replace('/^align=bottom /','',$algebra);
00140             } else if (preg_match('/^align=top /',$algebra)) {
00141               $align = "text-top";
00142               $algebra = preg_replace('/^align=top /','',$algebra);
00143             }
00144             $md5 =  md5($algebra);
00145             $filename =  $md5  . ".gif";
00146             if (! $texcache = $DB->get_record("cache_filters",array("filter"=>"algebra", "md5key"=>$md5))) {
00147                $algebra = str_replace('&lt;','<',$algebra);
00148                $algebra = str_replace('&gt;','>',$algebra);
00149                $algebra = str_replace('<>','#',$algebra);
00150                $algebra = str_replace('<=','%',$algebra);
00151                $algebra = str_replace('>=','!',$algebra);
00152                $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
00153                $algebra = str_replace('delta','zdelta',$algebra);
00154                $algebra = str_replace('beta','bita',$algebra);
00155                $algebra = str_replace('theta','thita',$algebra);
00156                $algebra = str_replace('zeta','zita',$algebra);
00157                $algebra = str_replace('eta','xeta',$algebra);
00158                $algebra = str_replace('epsilon','zepslon',$algebra);
00159                $algebra = str_replace('upsilon','zupslon',$algebra);
00160                $algebra = preg_replace('!\r\n?!',' ',$algebra);
00161                $algebra = escapeshellarg($algebra);
00162                if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
00163                   $cmd  = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
00164                } else {
00165                   $cmd  = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
00166                }
00167                $texexp = `$cmd`;
00168                if (preg_match('/parsehilight/',$texexp)) {
00169                  $text = str_replace( $matches[0][$i],"<b>Syntax error:</b> " . $texexp,$text);
00170                } else if ($texexp) {
00171                   $texexp = str_replace('zeroplace','',$texexp);
00172                   $texexp = str_replace('#','\not= ',$texexp);
00173                   $texexp = str_replace('%','\leq ',$texexp);
00174                   $texexp = str_replace('!','\geq ',$texexp);
00175                   $texexp = str_replace('\left{','{',$texexp);
00176                   $texexp = str_replace('\right}','}',$texexp);
00177                   $texexp = str_replace('\fun',' ',$texexp);
00178                   $texexp = str_replace('infty','\infty',$texexp);
00179                   $texexp = str_replace('alpha','\alpha',$texexp);
00180                   $texexp = str_replace('gamma','\gamma',$texexp);
00181                   $texexp = str_replace('iota','\iota',$texexp);
00182                   $texexp = str_replace('kappa','\kappa',$texexp);
00183                   $texexp = str_replace('lambda','\lambda',$texexp);
00184                   $texexp = str_replace('mu','\mu',$texexp);
00185                   $texexp = str_replace('nu','\nu',$texexp);
00186                   $texexp = str_replace('xi','\xi',$texexp);
00187                   $texexp = str_replace('rho','\rho',$texexp);
00188                   $texexp = str_replace('sigma','\sigma',$texexp);
00189                   $texexp = str_replace('tau','\tau',$texexp);
00190                   $texexp = str_replace('phi','\phi',$texexp);
00191                   $texexp = str_replace('chi','\chi',$texexp);
00192                   $texexp = str_replace('psi','\psi',$texexp);
00193                   $texexp = str_replace('omega','\omega',$texexp);
00194                   $texexp = str_replace('zdelta','\delta',$texexp);
00195                   $texexp = str_replace('bita','\beta',$texexp);
00196                   $texexp = str_replace('thita','\theta',$texexp);
00197                   $texexp = str_replace('zita','\zeta',$texexp);
00198                   $texexp = str_replace('xeta','\eta',$texexp);
00199                   $texexp = str_replace('zepslon','\epsilon',$texexp);
00200                   $texexp = str_replace('zupslon','\upsilon',$texexp);
00201                   $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
00202                   $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
00203                   $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
00204                   $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
00205                   $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
00206                   $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
00207                   $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
00208                   $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
00209                   $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
00210                   $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
00211                   $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
00212                   $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
00213                   $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
00214                   //$texexp = preg_replace('/\\\frac{(.+?)}{\\\left\((.+?)\\\right\)}/s','\frac{'."\$1}{\$2}",$texexp);
00215                   $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
00216                   $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
00217                   $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
00218                   $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
00219                   $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
00220                   $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
00221                   $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
00222                   $texexp = str_replace('\mbox', '', $texexp); // now blacklisted in tex, sorry
00223                   $texcache->filter = 'algebra';
00224                   $texcache->version = 1;
00225                   $texcache->md5key = $md5;
00226                   $texcache->rawtext = $texexp;
00227                   $texcache->timemodified = time();
00228                   $DB->insert_record("cache_filters", $texcache, false);
00229                   $text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texexp, '', '', $align), $text);
00230                } else {
00231                   $text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
00232                }
00233             } else {
00234                $text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texcache->rawtext), $text);
00235             }
00236         }
00237         return $text;
00238     }
00239 }
00240 
00241 
 All Data Structures Namespaces Files Functions Variables Enumerations