More logical wording on button to import from local file
[moodle-linuxchix.git] / filter / algebra / filter.php
blobf4dd3bd7971da11dabc77524120e2489b721e49e
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 if ($tex) {
56 $tex = str_replace('&','&amp;',$tex);
57 $tex = str_replace('<','&lt;',$tex);
58 $tex = str_replace('>','&gt;',$tex);
59 $tex = str_replace('"','&quot;',$tex);
60 $tex = str_replace("\'",'&#39;',$tex);
61 $title = "title=\"$tex\"";
63 if ($height) {
64 $height = "height=\"$height\"";
66 if ($width) {
67 $width = "width=\"$width\"";
69 if ($imagefile) {
70 if (!file_exists("$CFG->dataroot/$CFG->algebrafilterdir/$imagefile") && isadmin()) {
71 $output .= "<a href=\"$CFG->wwwroot/$CFG->algebrafilterdir/algebradebug.php\">";
72 } else {
73 $output .= "<a target=\"popup\" title=\"TeX\" href=";
74 $output .= "\"$CFG->wwwroot/$CFG->texfilterdir/displaytex.php?";
75 $output .= urlencode($tex) . "\" onclick=\"return openpopup('/$CFG->texfilterdir/displaytex.php?";
76 $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,";
77 $output .= "resizable,width=300,height=240', 0);\">";
79 $output .= "<img border=\"0\" $title $height $width alt=\"\" src=\"";
80 if ($CFG->slasharguments) { // Use this method if possible for better caching
81 $output .= "$CFG->wwwroot/$CFG->algebrafilterdir/pix.php/$imagefile";
82 } else {
83 $output .= "$CFG->wwwroot/$CFG->algebrafilterdir/pix.php?file=$imagefile";
85 $output .= "\" style=\"vertical-align:$align\" />";
86 $output .= "</a>";
87 } else {
88 $output .= "Error: must pass URL or course";
90 return $output;
94 function algebra_filter ($courseid, $text) {
96 global $CFG;
98 /// Do a quick check using stripos to avoid unnecessary wor
99 if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) {
100 return $text;
103 # //restrict filtering to forum 130 (Maths Tools on moodle.org)
104 # $scriptname = $_SERVER['SCRIPT_NAME'];
105 # if (!strstr($scriptname,'/forum/')) {
106 # return $text;
108 # if (strstr($scriptname,'post.php')) {
109 # $parent = forum_get_post_full($_GET['reply']);
110 # $discussion = get_record("forum_discussions","id",$parent->discussion);
111 # } else if (strstr($scriptname,'discuss.php')) {
112 # $discussion = get_record("forum_discussions","id",$_GET['d'] );
113 # } else {
114 # return $text;
116 # if ($discussion->forum != 130) {
117 # return $text;
121 $text .= ' ';
123 preg_match_all('/@(@@+)([^@])/',$text,$matches);
124 for ($i=0;$i<count($matches[0]);$i++) {
125 $replacement = str_replace('@','&#x00040;',$matches[1][$i]).$matches[2][$i];
126 $text = str_replace($matches[0][$i],$replacement,$text);
129 //if (isadmin()) { error_reporting (E_ALL); }; //for debugging
131 // <algebra> some algebraic input expression </algebra>
132 // or @@ some algebraic input expression @@
134 preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
135 for ($i=0; $i<count($matches[0]); $i++) {
136 $algebra = $matches[1][$i] . $matches[2][$i];
137 $algebra = str_replace('<nolink>','',$algebra);
138 $algebra = str_replace('</nolink>','',$algebra);
139 $algebra = str_replace('<span class="nolink">','',$algebra);
140 $algebra = str_replace('</span>','',$algebra);
141 $align = "middle";
142 if (preg_match('/^align=bottom /',$algebra)) {
143 $align = "text-bottom";
144 $algebra = preg_replace('/^align=bottom /','',$algebra);
145 } else if (preg_match('/^align=top /',$algebra)) {
146 $align = "text-top";
147 $algebra = preg_replace('/^align=top /','',$algebra);
149 $md5 = md5($algebra);
150 $filename = $md5 . ".gif";
151 if (! $texcache = get_record("cache_filters","filter","algebra", "md5key", $md5)) {
152 $algebra = str_replace('&lt;','<',$algebra);
153 $algebra = str_replace('&gt;','>',$algebra);
154 $algebra = str_replace('<>','#',$algebra);
155 $algebra = str_replace('<=','%',$algebra);
156 $algebra = str_replace('>=','!',$algebra);
157 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
158 $algebra = str_replace('delta','zdelta',$algebra);
159 $algebra = str_replace('beta','bita',$algebra);
160 $algebra = str_replace('theta','thita',$algebra);
161 $algebra = str_replace('zeta','zita',$algebra);
162 $algebra = str_replace('eta','xeta',$algebra);
163 $algebra = str_replace('epsilon','zepslon',$algebra);
164 $algebra = str_replace('upsilon','zupslon',$algebra);
165 $algebra = preg_replace('!\r\n?!',' ',$algebra);
166 if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
167 $algebra = "\"". str_replace('"','\"',$algebra) . "\"";
168 $cmd = "cd $CFG->dirroot\\$CFG->algebrafilterdirwin & algebra2tex.pl $algebra";
169 } else {
170 $algebra = escapeshellarg($algebra);
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;