[Aimmath-commit] moodle/mod/math_assignment pdfgen.php,NONE,1.1
Brought to you by:
gustav_delius,
npstrick
From: <ma...@us...> - 2003-09-11 14:41:04
|
Update of /cvsroot/aimmath/moodle/mod/math_assignment In directory sc8-pr-cvs1:/tmp/cvs-serv12766/mod/math_assignment Added Files: pdfgen.php Log Message: Template for pdf generation file. --- NEW FILE: pdfgen.php --- <?PHP // $Id: pdfgen.php,v 1.1 2003/09/11 14:40:55 madalex Exp $ // This file produces the problem and solution sheets in pdf. require_once("../../config.php"); require_once("lib.php"); require_variable($id); // Course Module ID, or optional_variable($type, "problems"); // "problems" or "solutions" $solutions = (strcasecmp($type, "solutions") == 0); if (! $site = get_site()) { redirect("$CFG->wwwroot/$CFG->admin/index.php"); } if (! $cm = get_record("course_modules", "id", $id)) { error("Course Module ID was incorrect"); } if (! $course = get_record("course", "id", $cm->course)) { error("Course is misconfigured"); } if (! $math_assignment = get_record("math_assignment", "id", $cm->instance)) { error("Course module is incorrect"); } require_login($course->id); add_to_log($course->id, "math_assignment", "pdfgen", "pdfgen.php?id=$cm->id", "$math_assignment->id"); // check that the request is valid if ($solutions) { if (time() < $math_assignment->timedue) { error("You can not view the solutions until the assignment has closed!"); } } ///// ####### 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) { if (!$subject = get_record("math_assignment_subject", "id", $math_assignment->aimsubject)) { error(get_string("noaccess", "math_assignment")); } if (!$server = get_record("math_assignment_server", "id", $subject->server)) { error(get_string("noaccess", "math_assignment")); } $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 $data = math_assignment_get_aim_output($server->address, $postdata, "latex"); // check if we need to sooth any AiM grumbles $error = math_assignment_check_aim_output($data); if ($error == 1) { // user not registered if ($CFG->math_assignment_auto_register) { // attempt to register them automatically if (!math_assignment_register_with_aim($server, $subject)) { error(get_string("nooutput", "math_assignment"), "view.php?id=$cm->id"); } // retry $data = math_assignment_get_aim_output($server->address, $postdata, "latex"); } } $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 } else { error("It didn't work!"); // error message } ?> |