MDL-10689:
[moodle-linuxchix.git] / filter / algebra / filter.php
blob3bb480df46ea31d87fbef6d513bb1ed30843f4d1
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 //-------------------------------------------------------------------------
41 /// Edit these lines to correspond to your installation
42 // File path to the directory where mathml_filter.php resides
43 $CFG->algebrafilterdir = "filter/algebra";
44 $CFG->texfilterdir = "filter/tex";
45 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
46 $CFG->algebrafilterdirwin = "filter\\algebra";
49 function string_file_picture_algebra($imagefile, $tex= "", $height="", $width="", $align="middle") {
50 // Given the path to a picture file in a course, or a URL,
51 // this function includes the picture in the page.
52 global $CFG;
54 $output = "";
55 $origtex = $tex;
56 $style = 'style="border:0px; vertical-align:'.$align.';';
57 if ($tex) {
58 $tex = str_replace('&','&amp;',$tex);
59 $tex = str_replace('<','&lt;',$tex);
60 $tex = str_replace('>','&gt;',$tex);
61 $tex = str_replace('"','&quot;',$tex);
62 $tex = str_replace("\'",'&#39;',$tex);
63 $title = "title=\"$tex\"";
65 if ($height) {
66 $style .= " height:{$height}px;";
68 if ($width) {
69 $style .= " width:{$width}px;";
71 $style .= '"';
72 if ($imagefile) {
73 if (!file_exists("$CFG->dataroot/$CFG->algebrafilterdir/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
74 $output .= "<a href=\"$CFG->wwwroot/$CFG->algebrafilterdir/algebradebug.php\">";
75 } else {
76 $output .= "<a target=\"popup\" title=\"TeX\" href=";
77 $output .= "\"$CFG->wwwroot/$CFG->texfilterdir/displaytex.php?";
78 $output .= urlencode($tex) . "\" onclick=\"return openpopup('/$CFG->texfilterdir/displaytex.php?";
79 $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,";
80 $output .= "resizable,width=300,height=240', 0);\">";
82 $output .= "<img $title alt=\"".s($origtex)."\" src=\"";
83 if ($CFG->slasharguments) { // Use this method if possible for better caching
84 $output .= "$CFG->wwwroot/$CFG->algebrafilterdir/pix.php/$imagefile";
85 } else {
86 $output .= "$CFG->wwwroot/$CFG->algebrafilterdir/pix.php?file=$imagefile";
88 $output .= "\" $style />";
89 $output .= "</a>";
90 } else {
91 $output .= "Error: must pass URL or course";
93 return $output;
97 function algebra_filter ($courseid, $text) {
99 global $CFG;
101 /// Do a quick check using stripos to avoid unnecessary wor
102 if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) {
103 return $text;
106 # //restrict filtering to forum 130 (Maths Tools on moodle.org)
107 # $scriptname = $_SERVER['SCRIPT_NAME'];
108 # if (!strstr($scriptname,'/forum/')) {
109 # return $text;
111 # if (strstr($scriptname,'post.php')) {
112 # $parent = forum_get_post_full($_GET['reply']);
113 # $discussion = get_record("forum_discussions","id",$parent->discussion);
114 # } else if (strstr($scriptname,'discuss.php')) {
115 # $discussion = get_record("forum_discussions","id",$_GET['d'] );
116 # } else {
117 # return $text;
119 # if ($discussion->forum != 130) {
120 # return $text;
124 $text .= ' ';
126 preg_match_all('/@(@@+)([^@])/',$text,$matches);
127 for ($i=0;$i<count($matches[0]);$i++) {
128 $replacement = str_replace('@','&#x00040;',$matches[1][$i]).$matches[2][$i];
129 $text = str_replace($matches[0][$i],$replacement,$text);
132 // <algebra> some algebraic input expression </algebra>
133 // or @@ some algebraic input expression @@
135 preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
136 for ($i=0; $i<count($matches[0]); $i++) {
137 $algebra = $matches[1][$i] . $matches[2][$i];
138 $algebra = str_replace('<nolink>','',$algebra);
139 $algebra = str_replace('</nolink>','',$algebra);
140 $algebra = str_replace('<span class="nolink">','',$algebra);
141 $algebra = str_replace('</span>','',$algebra);
142 $align = "middle";
143 if (preg_match('/^align=bottom /',$algebra)) {
144 $align = "text-bottom";
145 $algebra = preg_replace('/^align=bottom /','',$algebra);
146 } else if (preg_match('/^align=top /',$algebra)) {
147 $align = "text-top";
148 $algebra = preg_replace('/^align=top /','',$algebra);
150 $md5 = md5($algebra);
151 $filename = $md5 . ".gif";
152 if (! $texcache = get_record("cache_filters","filter","algebra", "md5key", $md5)) {
153 $algebra = str_replace('&lt;','<',$algebra);
154 $algebra = str_replace('&gt;','>',$algebra);
155 $algebra = str_replace('<>','#',$algebra);
156 $algebra = str_replace('<=','%',$algebra);
157 $algebra = str_replace('>=','!',$algebra);
158 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
159 $algebra = str_replace('delta','zdelta',$algebra);
160 $algebra = str_replace('beta','bita',$algebra);
161 $algebra = str_replace('theta','thita',$algebra);
162 $algebra = str_replace('zeta','zita',$algebra);
163 $algebra = str_replace('eta','xeta',$algebra);
164 $algebra = str_replace('epsilon','zepslon',$algebra);
165 $algebra = str_replace('upsilon','zupslon',$algebra);
166 $algebra = preg_replace('!\r\n?!',' ',$algebra);
167 $algebra = escapeshellarg($algebra);
168 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
169 $cmd = "cd $CFG->dirroot\\$CFG->algebrafilterdirwin & algebra2tex.pl $algebra";
170 } else {
171 $cmd = "cd $CFG->dirroot/$CFG->algebrafilterdir; ./algebra2tex.pl $algebra";
173 $texexp = `$cmd`;
174 if (preg_match('/parsehilight/',$texexp)) {
175 $text = str_replace( $matches[0][$i],"<b>Syntax error:</b> " . $texexp,$text);
176 } else if ($texexp) {
177 $texexp = str_replace('zeroplace','',$texexp);
178 $texexp = str_replace('#','\not= ',$texexp);
179 $texexp = str_replace('%','\leq ',$texexp);
180 $texexp = str_replace('!','\geq ',$texexp);
181 $texexp = str_replace('\left{','{',$texexp);
182 $texexp = str_replace('\right}','}',$texexp);
183 $texexp = str_replace('\fun',' ',$texexp);
184 $texexp = str_replace('infty','\infty',$texexp);
185 $texexp = str_replace('alpha','\alpha',$texexp);
186 $texexp = str_replace('gamma','\gamma',$texexp);
187 $texexp = str_replace('iota','\iota',$texexp);
188 $texexp = str_replace('kappa','\kappa',$texexp);
189 $texexp = str_replace('lambda','\lambda',$texexp);
190 $texexp = str_replace('mu','\mu',$texexp);
191 $texexp = str_replace('nu','\nu',$texexp);
192 $texexp = str_replace('xi','\xi',$texexp);
193 $texexp = str_replace('rho','\rho',$texexp);
194 $texexp = str_replace('sigma','\sigma',$texexp);
195 $texexp = str_replace('tau','\tau',$texexp);
196 $texexp = str_replace('phi','\phi',$texexp);
197 $texexp = str_replace('chi','\chi',$texexp);
198 $texexp = str_replace('psi','\psi',$texexp);
199 $texexp = str_replace('omega','\omega',$texexp);
200 $texexp = str_replace('zdelta','\delta',$texexp);
201 $texexp = str_replace('bita','\beta',$texexp);
202 $texexp = str_replace('thita','\theta',$texexp);
203 $texexp = str_replace('zita','\zeta',$texexp);
204 $texexp = str_replace('xeta','\eta',$texexp);
205 $texexp = str_replace('zepslon','\epsilon',$texexp);
206 $texexp = str_replace('zupslon','\upsilon',$texexp);
207 $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
208 $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
209 $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
210 $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
211 $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
212 $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
213 $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
214 $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
215 $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
216 $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
217 $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
218 $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
219 $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
220 //$texexp = preg_replace('/\\\frac{(.+?)}{\\\left\((.+?)\\\right\)}/s','\frac{'."\$1}{\$2}",$texexp);
221 $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
222 $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
223 $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
224 $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
225 $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
226 $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
227 $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
228 $texcache->filter = 'algebra';
229 $texcache->version = 1;
230 $texcache->md5key = $md5;
231 $texcache->rawtext = addslashes($texexp);
232 $texcache->timemodified = time();
233 insert_record("cache_filters",$texcache, false);
234 $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texexp, '', '', $align), $text);
235 } else {
236 $text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
238 } else {
239 $text = str_replace( $matches[0][$i], string_file_picture_algebra($filename, $texcache->rawtext), $text);
242 return $text;