[Aimmath-commit] moodle/mod/math_assignment pdfgen.php,1.2,1.3
                
                Brought to you by:
                
                    gustav_delius,
                    
                
                    npstrick
                    
                
            
            
        
        
        
    | 
      
      
      From: <gus...@us...> - 2003-09-17 17:13:57
      
     | 
| Update of /cvsroot/aimmath/moodle/mod/math_assignment
In directory sc8-pr-cvs1:/tmp/cvs-serv32512/mod/math_assignment
Modified Files:
	pdfgen.php 
Log Message:
moved the pdf creation onto this page. Still preliminary
Index: pdfgen.php
===================================================================
RCS file: /cvsroot/aimmath/moodle/mod/math_assignment/pdfgen.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** pdfgen.php	13 Sep 2003 13:01:16 -0000	1.2
--- pdfgen.php	17 Sep 2003 17:13:09 -0000	1.3
***************
*** 9,13 ****
      optional_variable($type, "problems");  // "problems" or "solutions"
  
!     $solutions = (strcasecmp($type, "solutions") == 0);
  
      if (! $site = get_site()) {
--- 9,14 ----
      optional_variable($type, "problems");  // "problems" or "solutions"
  
! 
!     $solutions = (boolean)($type == "solutions");
  
      if (! $site = get_site()) {
***************
*** 24,27 ****
--- 25,32 ----
  
      require_login($course->id);
+     
+     if (!isstudent($course->id) and !isteacher) {
+         error("Sorry, this assignment is not for you!");
+     }
  
      add_to_log($course->id, "math_assignment", "pdfgen", "pdfgen.php?id=$math_assignment->id", $type);
***************
*** 29,53 ****
  // check that the request is valid
  
-     if ($problems) {
-         if (strcasecmp(substr($math_assignment->problems, -4), ".tex")) {
-             error("Not a dynamic problem sheet."); // this should never happen as we only link to this page if needed
-         }
-     }
      if ($solutions) {
!         if (time() < $math_assignment->timedue) {
              error("You can not view the solutions until the assignment has closed!");
          }
-         if (strcasecmp(substr($math_assignment->solutions, -4), ".tex")) {
-             error("Not a dynamic solution sheet."); // this should never happen as we only link to this page if needed
-         }
      }
  
! ///// #######  YOUR CODE HERE! #######
! /*
! - Examine $math_assignment->problems or $math_assignment->solutions
! - If URL or filesystem path points to a non-tex file then fail with an error message (since my code should produce links directly to static files)
! - load tex file
! - load aim page
! */
      if ($math_assignment->aimquiz) {
  
--- 34,63 ----
  // check that the request is valid
  
      if ($solutions) {
!         if (time() < $math_assignment->timedue and isstudent($course->id)) {
              error("You can not view the solutions until the assignment has closed!");
          }
      }
  
! // 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) {
  
***************
*** 60,75 ****
  
          $postdata = "";
!         // #####    I wasn't sure which aim page you actually wanted. Use this for the student's view of the quiz:
!         $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", $server->student_passwd);
!         // #####    Or this for the lecturer's 'try quiz' view of the quiz:
!         $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
--- 70,86 ----
  
          $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", $server->student_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
***************
*** 88,121 ****
              }
          }
  
!         $data = math_assignment_convert_aim_output($math_assignment, $subject, $data, "", "", false, $grade, $max_grade);
  
!         // ####    $data now contains the same data that was being passed to your function
  
      }
! /*
! - generate pdf file
! */
  
! // output of pdf
  
!     /*
!     Is it possible to generate the pdf direct to stdout? If so you could call the program with the passthru function
!     and not generate any files which would be good.
!     If you do have to create a file, remeber to use the tempnam function so that you don't get collisions.
  
!     I assume here that you have created a pdf file, for the sake of argument in "/tmp/problems.pdf"
!     Whilst you could use the readfile function to open and output the file in one go it is best to do it this way
!     because we only set the output type to pdf after successfully opening the file. If it fails we can still output
!     an error message in html.
!     */
  
!     if ($file = fopen("/tmp/problems.pdf", "rb")) { // the 'b' is for compatibility with Windoze
  
          // tell the browser that it is pdf
          header("Content-type: application/pdf");
  
-         /* more headers controlling caching, etc if required */
- 
          fpassthru($file); // chuck all the data direct to the browser and close the handle
      }
--- 99,187 ----
              }
          }
+     }
  
! // 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
!         
!         // Keep the answer to multiple choice questions
!         // 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    
!     $data = preg_replace("/id=\"question(\d+).(\d+)\"/i", 
!         "<latex>\n\n\vspace{2mm}\\noindent{\\bf Part \$2}\n\n</latex>",$data);
!     
!     // replace radio buttons 
!     $data = preg_replace("/(type=\"radio\"[^\/]*value=\")([A-Z])\"/",
!         "<latex>\n\n {\bf $2 } </latex>",$data);
!     
!     // replace checkboxes by diamonds    
!     $data = preg_replace("/(type=\"checkbox\"[^\/]*value=\")([A-Z])\"/",
!         "<latex>\n\n {\bf $2 } </latex>",$data);
!         
!     // delete AiM syntax hints (identified by \small)
!     $data = preg_replace("/\\\\small[^}]*./",
!         "",$data);
!         
!     // 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");
  
          fpassthru($file); // chuck all the data direct to the browser and close the handle
      }
***************
*** 123,125 ****
          error("It didn't work!"); // error message
      }
! ?>
\ No newline at end of file
--- 189,195 ----
          error("It didn't work!"); // error message
      }
! */
!     
!         redirect("temp.pdf");
!         
! ?>
 |