Update of /cvsroot/aimmath/moodle/mod/math_assignment
In directory sc8-pr-cvs1:/tmp/cvs-serv15969/mod/math_assignment
Modified Files:
lib.php pdfgen.php
Log Message:
works on unix and windows but on windows temp files are in math_assignment directory rather than temp directory until we can find out how to change working directory for tex.
Index: lib.php
===================================================================
RCS file: /cvsroot/aimmath/moodle/mod/math_assignment/lib.php,v
retrieving revision 1.30
retrieving revision 1.31
diff -C2 -d -r1.30 -r1.31
*** lib.php 28 Sep 2003 23:15:32 -0000 1.30
--- lib.php 29 Sep 2003 19:21:59 -0000 1.31
***************
*** 1041,1043 ****
--- 1041,1062 ----
}
+ function copy_from_url($url, $file) {
+ if (!$handle = fopen($url, "rb"))
+ return false;
+ $contents = "";
+ do {
+ $data = fread($handle, 8192);
+ if (strlen($data) == 0) {
+ break;
+ }
+ $contents .= $data;
+ } while(true);
+ fclose($handle);
+ if (!$handle = fopen($file, "wb"))
+ return false;
+ fwrite($handle, $contents, strlen($contents));
+ fclose($handle);
+ return true;
+ }
+
?>
Index: pdfgen.php
===================================================================
RCS file: /cvsroot/aimmath/moodle/mod/math_assignment/pdfgen.php,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** pdfgen.php 28 Sep 2003 19:26:18 -0000 1.12
--- pdfgen.php 29 Sep 2003 19:21:59 -0000 1.13
***************
*** 37,45 ****
}
! // Create temp directory if it doesn't exist
! $tempdir = "$CFG->dataroot/temp/math_assignment/$math_assignment->id";
! @mkdir("$CFG->dataroot/temp", $CFG->directorypermissions);
! @mkdir("$CFG->dataroot/temp/math_assignment", $CFG->directorypermissions);
! @mkdir($tempdir, $CFG->directorypermissions);
// if there is an aim quiz load it into $data
--- 37,49 ----
}
! // Create temp file names
! if (strpos($_ENV["OS"], "Win") === false) {
! if (!$tempfile = tempnam("/tmp","pdf")) {
! error("Sorry, I can't produce the pdf output. Failed to open temporary file.");
! }
! }
! else {
! $tempfile = substr(md5(microtime()), 0, 6);
! }
// if there is an aim quiz load it into $data
***************
*** 83,90 ****
$imageurls = $imagematches[1];
foreach ($imageurls as $key => $imageurl) {
! if (!copy($imageurl,"$tempdir/$key.jpg"))
error("Couldn't copy image file to temp directory");
$data = preg_replace("/<img src=\"([^\"]+jpg)\"/",
! "<latex>\n\\begin\{center}\n\\pdfimageresolution=150\n\\pdfximage\{$tempdir/$key.jpg}\\pdfrefximage\\pdflastximage\n\\end\{center}\n</latex>",
$data,1
);
--- 87,94 ----
$imageurls = $imagematches[1];
foreach ($imageurls as $key => $imageurl) {
! if (!copy_from_url($imageurl,"$tempfile$key.jpg"))
error("Couldn't copy image file to temp directory");
$data = preg_replace("/<img src=\"([^\"]+jpg)\"/",
! "<latex>\n\\begin\{center}\n\\pdfimageresolution=150\n\\pdfximage\{$tempfile$key.jpg}\\pdfrefximage\\pdflastximage\n\\end\{center}\n</latex>",
$data,1
);
***************
*** 138,150 ****
// generate pdf file from $latex
- $tempfile = substr(md5(microtime()), 0, 8);
if (!$fp=fopen("$tempfile.tex","wb"))
error("Sorry, I can't produce the pdf output. Failed to open temporary file.");
fwrite($fp,$latex);
fclose($fp);
! exec("pdflatex $tempfile.tex");
! @unlink("$tempfile.tex");
! @unlink("$tempfile.aux");
! @unlink("$tempfile.log");
// output of pdf
--- 142,153 ----
// generate pdf file from $latex
if (!$fp=fopen("$tempfile.tex","wb"))
error("Sorry, I can't produce the pdf output. Failed to open temporary file.");
fwrite($fp,$latex);
fclose($fp);
! if (strpos($_ENV["OS"], "Win") === false)
! exec("cd ".dirname($tempfile)."; pdflatex $tempfile.tex");
! else
! exec("pdflatex $tempfile.tex");
// output of pdf
***************
*** 152,156 ****
/* We need to remove the temporary file so we had better output it to the browser directly rather than redirecting.
If we redirect we have no idea when the file is no longer needed. */
! redirect("$tempfile.pdf");
/* I have just noticed the following in the php documentation on this issue of using Content-disposition headers:
--- 155,159 ----
/* We need to remove the temporary file so we had better output it to the browser directly rather than redirecting.
If we redirect we have no idea when the file is no longer needed. */
! //redirect("$tempfile.pdf");
/* I have just noticed the following in the php documentation on this issue of using Content-disposition headers:
***************
*** 162,167 ****
problem sheet is required and 'solutions.pdf' when solutions are required. With luck this might fool IE which seems to
go by the extension when it ignores the headers. This would need testing in a broken version of IE. */
! /*
! if ($file = fopen("$tempfile.pdf", "rb")) { // the 'b' is for compatibility with Windoze
// tell the browser that it is pdf
--- 165,171 ----
problem sheet is required and 'solutions.pdf' when solutions are required. With luck this might fool IE which seems to
go by the extension when it ignores the headers. This would need testing in a broken version of IE. */
!
! $errlog = "";
! if ($file = @fopen("$tempfile.pdf", "rb")) { // the 'b' is for compatibility with Windoze
// tell the browser that it is pdf
***************
*** 171,183 ****
else
header("Content-disposition: inline; filename=problems.pdf");
!
fpassthru($file); // chuck all the data direct to the browser and close the handle
}
else {
! error("It didn't work!"); // error message
}
@unlink("$tempfile.pdf");
! */
function math_assignment_aim_to_latex($data,$type) {
--- 175,199 ----
else
header("Content-disposition: inline; filename=problems.pdf");
! header('Cache-Control: public');
fpassthru($file); // chuck all the data direct to the browser and close the handle
+ @fclose($file);
}
else {
! $errlog = htmlentities(implode('', file("$tempfile.log")));
}
@unlink("$tempfile.pdf");
! @unlink("$tempfile.tex");
! @unlink("$tempfile.aux");
! @unlink("$tempfile.log");
! foreach ($imageurls as $key => $imageurl) {
! @unlink("$tempfile$key.jpg");
! }
!
! if (strlen($errlog) > 0) {
! error("There was a problem while compiling the tex source!<br>
! pdflatex wrote the following log file:<br><br>
! <pre>$errlog</pre>"); // error message
! }
function math_assignment_aim_to_latex($data,$type) {
***************
*** 251,265 ****
}
-
- function str_replace_once($needle, $replace, $haystack) {
- // Looks for the first occurence of $needle in $haystack
- // and replaces it with $replace.
- $pos = strpos($haystack, $needle);
- if ($pos === false) {
- // Nothing found
- return $haystack;
- }
- return substr_replace($haystack, $replace, $pos, strlen($needle));
- }
?>
--- 267,270 ----
|