| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?PHP 2 ///////////////////////////////////////////////////////////////////////////// 3 // // 4 // NOTICE OF COPYRIGHT // 5 // // 6 // Moodle - Filter for converting simple calculator-type algebraic // 7 // expressions to cached gif images // 8 // // 9 // Copyright (C) 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu // 10 // Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca// 11 // This program is free software; you can redistribute it and/or modify // 12 // it under the terms of the GNU General Public License as published by // 13 // the Free Software Foundation; either version 2 of the License, or // 14 // (at your option) any later version. // 15 // // 16 // This program is distributed in the hope that it will be useful, // 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 19 // GNU General Public License for more details: // 20 // // 21 // http://www.gnu.org/copyleft/gpl.html // 22 // // 23 ///////////////////////////////////////////////////////////////////////////// 24 //------------------------------------------------------------------------- 25 // NOTE: This Moodle text filter converts algebraic expressions delimited 26 // by either @@...@@ or by <algebra...>...</algebra> tags 27 // first converts it to TeX using WeBWorK algebra parser Perl library 28 // AlgParser.pm, part of the WeBWorK distribution obtained from 29 // http://webhost.math.rochester.edu/downloadwebwork/ 30 // then converts the TeX to gif images using 31 // mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by 32 // John Forkosh john@forkosh.com. The mimetex.cgi ELF binary compiled for Linux i386 33 // as well as AlgParser.pm are included with this distribution. 34 // Note that there may be patent restrictions on the production of gif images 35 // in Canada and some parts of Western Europe and Japan until July 2004. 36 //------------------------------------------------------------------------- 37 // You will then need to edit your moodle/config.php to invoke mathml_filter.php 38 //------------------------------------------------------------------------- 39 40 function string_file_picture_algebra($imagefile, $tex= "", $height="", $width="", $align="middle") { 41 // Given the path to a picture file in a course, or a URL, 42 // this function includes the picture in the page. 43 global $CFG; 44 45 $output = ""; 46 $origtex = $tex; 47 $style = 'style="border:0px; vertical-align:'.$align.';'; 48 if ($tex) { 49 $tex = str_replace('&','&',$tex); 50 $tex = str_replace('<','<',$tex); 51 $tex = str_replace('>','>',$tex); 52 $tex = str_replace('"','"',$tex); 53 $tex = str_replace("\'",''',$tex); 54 $title = "title=\"$tex\""; 55 } 56 if ($height) { 57 $style .= " height:{$height}px;"; 58 } 59 if ($width) { 60 $style .= " width:{$width}px;"; 61 } 62 $style .= '"'; 63 if ($imagefile) { 64 if (!file_exists("$CFG->dataroot/filter/algebra/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { 65 $output .= "<a href=\"$CFG->wwwroot/filter/algebra/algebradebug.php\">"; 66 } else { 67 $output .= "<a target=\"popup\" title=\"TeX\" href="; 68 $output .= "\"$CFG->wwwroot/filter/algebra/displaytex.php?"; 69 $output .= urlencode($tex) . "\" onclick=\"return openpopup('/filter/algebra/displaytex.php?"; 70 $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,"; 71 $output .= "resizable,width=300,height=240', 0);\">"; 72 } 73 $output .= "<img $title alt=\"".s($origtex)."\" src=\""; 74 if ($CFG->slasharguments) { // Use this method if possible for better caching 75 $output .= "$CFG->wwwroot/filter/algebra/pix.php/$imagefile"; 76 } else { 77 $output .= "$CFG->wwwroot/filter/algebra/pix.php?file=$imagefile"; 78 } 79 $output .= "\" $style />"; 80 $output .= "</a>"; 81 } else { 82 $output .= "Error: must pass URL or course"; 83 } 84 return $output; 85 } 86 87 88 function algebra_filter ($courseid, $text) { 89 90 global $CFG; 91 92 /// Do a quick check using stripos to avoid unnecessary wor 93 if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) { 94 return $text; 95 } 96 97 # //restrict filtering to forum 130 (Maths Tools on moodle.org) 98 # $scriptname = $_SERVER['SCRIPT_NAME']; 99 # if (!strstr($scriptname,'/forum/')) { 100 # return $text; 101 # } 102 # if (strstr($scriptname,'post.php')) { 103 # $parent = forum_get_post_full($_GET['reply']); 104 # $discussion = get_record("forum_discussions","id",$parent->discussion); 105 # } else if (strstr($scriptname,'discuss.php')) { 106 # $discussion = get_record("forum_discussions","id",$_GET['d'] ); 107 # } else { 108 # return $text; 109 # } 110 # if ($discussion->forum != 130) { 111 # return $text; 112 # } 113 114 115 $text .= ' '; 116 117 preg_match_all('/@(@@+)([^@])/',$text,$matches); 118 for ($i=0;$i<count($matches[0]);$i++) { 119 $replacement = str_replace('@','@',$matches[1][$i]).$matches[2][$i]; 120 $text = str_replace($matches[0][$i],$replacement,$text); 121 } 122 123 // <algebra> some algebraic input expression </algebra> 124 // or @@ some algebraic input expression @@ 125 126 preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches); 127 for ($i=0; $i<count($matches[0]); $i++) { 128 $algebra = $matches[1][$i] . $matches[2][$i]; 129 $algebra = str_replace('<nolink>','',$algebra); 130 $algebra = str_replace('</nolink>','',$algebra); 131 $algebra = str_replace('<span class="nolink">','',$algebra); 132 $algebra = str_replace('</span>','',$algebra); 133 $align = "middle"; 134 if (preg_match('/^align=bottom /',$algebra)) { 135 $align = "text-bottom"; 136 $algebra = preg_replace('/^align=bottom /','',$algebra); 137 } else if (preg_match('/^align=top /',$algebra)) { 138 $align = "text-top"; 139 $algebra = preg_replace('/^align=top /','',$algebra); 140 } 141 $md5 = md5($algebra); 142 $filename = $md5 . ".gif"; 143 if (! $texcache = get_record("cache_filters","filter","algebra", "md5key", $md5)) { 144 $algebra = str_replace('<','<',$algebra); 145 $algebra = str_replace('>','>',$algebra); 146 $algebra = str_replace('<>','#',$algebra); 147 $algebra = str_replace('<=','%',$algebra); 148 $algebra = str_replace('>=','!',$algebra); 149 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra); 150 $algebra = str_replace('delta','zdelta',$algebra); 151 $algebra = str_replace('beta','bita',$algebra); 152 $algebra = str_replace('theta','thita',$algebra); 153 $algebra = str_replace('zeta','zita',$algebra); 154 $algebra = str_replace('eta','xeta',$algebra); 155 $algebra = str_replace('epsilon','zepslon',$algebra); 156 $algebra = str_replace('upsilon','zupslon',$algebra); 157 $algebra = preg_replace('!\r\n?!',' ',$algebra); 158 $algebra = escapeshellarg($algebra); 159 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) { 160 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra"; 161 } else { 162 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra"; 163 } 164 $texexp = `$cmd`; 165 if (preg_match('/parsehilight/',$texexp)) { 166 $text = str_replace( $matches[0][$i],"<b>Syntax error:</b> " . $texexp,$text); 167 } else if ($texexp) { 168 $texexp = str_replace('zeroplace','',$texexp); 169 $texexp = str_replace('#','\not= ',$texexp); 170 $texexp = str_replace('%','\leq ',$texexp); 171 $texexp = str_replace('!','\geq ',$texexp); 172 $texexp = str_replace('\left{','{',$texexp); 173 $texexp = str_replace('\right}','}',$texexp); 174 $texexp = str_replace('\fun',' ',$texexp); 175 $texexp = str_replace('infty','\infty',$texexp); 176 $texexp = str_replace('alpha','\alpha',$texexp); 177 $texexp = str_replace('gamma','\gamma',$texexp); 178 $texexp = str_replace('iota','\iota',$texexp); 179 $texexp = str_replace('kappa','\kappa',$texexp); 180 $texexp = str_replace('lambda','\lambda',$texexp); 181 $texexp = str_replace('mu','\mu',$texexp); 182 $texexp = str_replace('nu','\nu',$texexp); 183 $texexp = str_replace('xi','\xi',$texexp); 184 $texexp = str_replace('rho','\rho',$texexp); 185 $texexp = str_replace('sigma','\sigma',$texexp); 186 $texexp = str_replace('tau','\tau',$texexp); 187 $texexp = str_replace('phi','\phi',$texexp); 188 $texexp = str_replace('chi','\chi',$texexp); 189 $texexp = str_replace('psi','\psi',$texexp); 190 $texexp = str_replace('omega','\omega',$texexp); 191 $texexp = str_replace('zdelta','\delta',$texexp); 192 $texexp = str_replace('bita','\beta',$texexp); 193 $texexp = str_replace('thita','\theta',$texexp); 194 $texexp = str_replace('zita','\zeta',$texexp); 195 $texexp = str_replace('xeta','\eta',$texexp); 196 $texexp = str_replace('zepslon','\epsilon',$texexp); 197 $texexp = str_replace('zupslon','\upsilon',$texexp); 198 $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp); 199 $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp); 200 $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp); 201 $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp); 202 $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp); 203 $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp); 204 $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp); 205 $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp); 206 $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp); 207 $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp); 208 $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp); 209 $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp); 210 $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp); 211 //$texexp = preg_replace('/\\\frac{(.+?)}{\\\left\((.+?)\\\right\)}/s','\frac{'."\$1}{\$2}",$texexp); 212 $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp); 213 $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp); 214 $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp); 215 $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp); 216 $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp); 217 $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp); 218 $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp); 219 $texcache->filter = 'algebra'; 220 $texcache->version = 1; 221 $texcache->md5key = $md5; 222 $texcache->rawtext = addslashes($texexp); 223 $texcache->timemodified = time(); 224 insert_record("cache_filters",$texcache, false); 225 $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texexp, '', '', $align), $text); 226 } else { 227 $text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text); 228 } 229 } else { 230 $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texcache->rawtext), $text); 231 } 232 } 233 return $text; 234 } 235 236 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |