[Aimmath-commit] moodle/mod/math_assignment pdfgen.php,1.7,1.8
Brought to you by:
gustav_delius,
npstrick
|
From: <gus...@us...> - 2003-09-24 23:57:32
|
Update of /cvsroot/aimmath/moodle/mod/math_assignment
In directory sc8-pr-cvs1:/tmp/cvs-serv1217/mod/math_assignment
Modified Files:
pdfgen.php
Log Message:
Now problem and solution sheets contain images.
Index: pdfgen.php
===================================================================
RCS file: /cvsroot/aimmath/moodle/mod/math_assignment/pdfgen.php,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** pdfgen.php 24 Sep 2003 11:53:42 -0000 1.7
--- pdfgen.php 24 Sep 2003 23:57:28 -0000 1.8
***************
*** 9,15 ****
optional_variable($type, "problems"); // "problems" or "solutions"
-
- $solutions = (boolean)($type == "solutions");
-
if (! $site = get_site()) {
redirect("$CFG->wwwroot/$CFG->admin/index.php");
--- 9,12 ----
***************
*** 40,63 ****
}
// Load lecturer's tex files or use dummy and store in $latex
! math_assignment_get_file_urls($math_assignment,$probfile,$solfile);
! if ($solutions) $texfile = $solfile; else $texfile = $probfile;
if (math_assignment_is_tex_file($texfile)) {
$latex = file_get_contents($texfile);
}
else {
! $latex = "
! \\documentclass[12pt]{article}\n
! \\usepackage{amsmath}\n
! \\begin{document}\n
! \\begin{center}
! {\\bf " . $course->fullname . ": " . $math_assignment->name . "}
! \\end{center}
! ";
}
// if there is an aim quiz load it into $data
!
if ($math_assignment->aimquiz) {
--- 37,78 ----
}
+ // 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);
+
// Load lecturer's tex files or use dummy and store in $latex
! if ($type == "problems") {
! if ($math_assignment->problemsisurl) {
! $texfile = $math_assignment->problems;
! }
! else {
! $texfile = "$CFG->dataroot/$course->id/$math_assignment->problems";
! }
! }
! else if ($type == solutions) {
! if ($math_assignment->solutionsisurl) {
! $texfile = $math_assignment->solutions;
! }
! else {
! $texfile = "$CFG->dataroot/$course->id/$math_assignment->solutions";
! }
! }
!
if (math_assignment_is_tex_file($texfile)) {
$latex = file_get_contents($texfile);
}
else {
! $latex = file_get_contents("$CFG->dirroot/mod/math_assignment/$type.tex");
}
+ // Insert course and assignment name if required
+ $latex = str_replace("\coursename","$course->fullname",$latex);
+ $latex = str_replace("\assignmentname","$math_assignment->name",$latex);
+
// if there is an aim quiz load it into $data
! $data = "";
if ($math_assignment->aimquiz) {
***************
*** 70,87 ****
$postdata = "";
! if (isstudent($course->id)) {
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Command", "ShowQuizPage");
! $postdata = math_assignment_add_arg_to_url_data($postdata, "SubjectName", $subject->name);
! $postdata = math_assignment_add_arg_to_url_data($postdata, "QuizName", $math_assignment->aimquiz);
! $postdata = math_assignment_add_arg_to_url_data($postdata, "StudentID", $USER->username);
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Password", $subject->passwd);
! }
! else {
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Command","try/Quiz");
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Action", "TryQuiz");
! $postdata = math_assignment_add_arg_to_url_data($postdata, "SubjectName", $subject->name);
! $postdata = math_assignment_add_arg_to_url_data($postdata, "QuizName", $math_assignment->aimquiz);
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Password", $subject->password);
! }
// load the AiM page and grab the result
--- 85,93 ----
$postdata = "";
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Command","try/Quiz");
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Action", "TryQuiz");
! $postdata = math_assignment_add_arg_to_url_data($postdata, "SubjectName", $subject->name);
! $postdata = math_assignment_add_arg_to_url_data($postdata, "QuizName", $math_assignment->aimquiz);
! $postdata = math_assignment_add_arg_to_url_data($postdata, "Password", $subject->password);
// load the AiM page and grab the result
***************
*** 102,117 ****
}
// convert AiM questions or solutions in $data and add to $latex
! // remove everything before the first question
! $data = preg_replace("/^[\s\S]*?(?=id=\"question)/",
! "",$data);
! if (!$solutions) {
// remove all answers and solutions
$data = preg_replace("/class=\"lastanswer\"[\s\S]*?((?=id=\"question)|$)/",
"",$data);
}
! else {
// remove answers but keep solution
--- 108,177 ----
}
+
+ // convert images
+
+ if (preg_match_all("/<img src=\"([^\"]+jpg)\"/",$data,$imagematches)) {
+ $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
+ );
+ }
+ }
+
// convert AiM questions or solutions in $data and add to $latex
! $questions = preg_split("/id=\"question(\d+)\"/",$data);
! array_shift($questions);
! foreach ($questions as $question) {
! //$latex = preg_replace("/(?<!newcommand\{)\\\aimquestion/",addslashes(math_assignment_aim_to_latex($question,$type)),$latex,1);
! $latex = str_replace_once("\aimquestion",math_assignment_aim_to_latex($question,$type),$latex);
! }
! // generate pdf file from $latex
!
! //$tempfile = tempnam();
! $tempfile = "temp.tex";
! if (!$fp=fopen($tempfile,"w"))
! error("Sorry, I can't produce the pdf output. Failed to open temporary file.");
! fwrite($fp,$latex);
! fclose($fp);
! exec("pdflatex -interaction=batchmode temp.tex");
!
! // output of pdf
!
! redirect("temp.pdf");
!
! /*
! if ($file = fopen("temp.pdf", "rb")) { // the 'b' is for compatibility with Windoze
!
! // tell the browser that it is pdf
! header("Content-type: application/pdf");
! if ($solutions)
! header("Content-disposition: inline; filename=solutions.pdf");
! 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
! }
!
! */
!
! function math_assignment_aim_to_latex($data,$type) {
! // This function is given a chunk of the aimpage corresponding to one question
! // and returns a latex string
!
! if ($type == "problems") {
// remove all answers and solutions
$data = preg_replace("/class=\"lastanswer\"[\s\S]*?((?=id=\"question)|$)/",
"",$data);
}
! else if ($type == "solutions") {
// remove answers but keep solution
***************
*** 119,139 ****
// The "class=\"solu is there to save this from being erased in the next step below
$data = preg_replace("/The correct answer is ([A-Z])/",
! "class=\"solu<latex>\n\n\\noindent{\\bf Right Answer: } $1</latex>",$data);
// Remove anything else up to the solution or the next question or the end of file
! $data = preg_replace("/class=\"lastanswer\"[\s\S]*?((?=class=\"solu)|(?=id=\"quest)|$)/",
! "",$data);
!
! $data = preg_replace("/class=\"solution/",
! "<latex>\n\n\\noindent{\\bf Solution:}\n\n</latex>",$data);
! }
!
// remove questionheaders including descriptions
$data = preg_replace("/class=\"questionheader\"[\s\S]*?class=\"questionbody/",
"",$data);
-
- // Questions
- $data = preg_replace("/id=\"question(\d+)\"/i",
- "<latex>\n\n\vspace{5mm}\\noindent{\\bf Question \$1:}\n\n</latex>",$data);
// Parts
--- 179,192 ----
// The "class=\"solu is there to save this from being erased in the next step below
$data = preg_replace("/The correct answer is ([A-Z])/",
! "class=\"solu<latex>\n\n\\noindent{ Right Answer: } $1. </latex>",$data);
// Remove anything else up to the solution or the next question or the end of file
! $data = preg_replace("/(?<=class=\"questionbody)[\s\S]*?((?=class=\"solu)|(?=id=\"quest)|$)/",
! "",$data);
! }
!
// remove questionheaders including descriptions
$data = preg_replace("/class=\"questionheader\"[\s\S]*?class=\"questionbody/",
"",$data);
// Parts
***************
*** 155,197 ****
// We might at some point want to replace input box by ....
//preg_match_all("/<latex>([\s\S]*)?<\/latex>/", $data, $matches);
! //$latex .= implode(" ", $matches[0]);
!
! // extract anything inside <latex> ..</latex> and append it to $latex
// this is not done elegantly, but who cares.
$chunks = explode("</latex>",$data);
array_pop($chunks);
foreach ($chunks as $chunk) {
! preg_match("/(?<=<latex>)[\s\S]*$/","$chunk",$piece);
! $latex .= $piece[0];
}
! $latex .= "\n\n\\end{document}";
!
! // generate pdf file from $latex
!
! //$tempfile = tempnam();
! $tempfile = "temp.tex";
! if (!$fp=fopen($tempfile,"w"))
! error("Sorry, I can't produce the pdf output. Failed to open temporary file.");
! fwrite($fp,$latex);
! fclose($fp);
! exec("pdflatex -interaction=batchmode temp.tex");
!
! // output of pdf
!
! if ($file = fopen("temp.pdf", "rb")) { // the 'b' is for compatibility with Windoze
!
! // tell the browser that it is pdf
! header("Content-type: application/pdf");
! if ($solutions)
! header("Content-disposition: inline; filename=solutions.pdf");
! 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
}
!
?>
--- 208,238 ----
// We might at some point want to replace input box by ....
//preg_match_all("/<latex>([\s\S]*)?<\/latex>/", $data, $matches);
!
! // extract anything inside <latex> ..</latex> and append it to $questiontex
// this is not done elegantly, but who cares.
+ $questiontex = "";
$chunks = explode("</latex>",$data);
array_pop($chunks);
foreach ($chunks as $chunk) {
! // preg_match("/(?<=<latex>)[\s\S]*$/","$chunk",$piece);
! // $questiontex .= $piece[0];
! $pieces = explode("<latex>",$chunk);
! $questiontex .= $pieces[1];
}
! return($questiontex);
!
! }
! 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));
! }
!
?>
|