From 7451ab20ba4d70f97cd936bd3d7424c1c2557cd2 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 11 Apr 2013 15:57:39 +0800 Subject: [PATCH] Introduced the function responsible for generating the PDF --- lib.php | 42 ++++++++++++++++++++++++++++++++++++++++++ view.php | 7 ++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib.php b/lib.php index 772fd38..f3e56da 100644 --- a/lib.php +++ b/lib.php @@ -735,3 +735,45 @@ function customcert_generate_code() { return $code; } + +/** + * Generate the PDF for the specified customcert and user. + * + * @param stdClass $customcert + * @param int $userid + */ +function customcert_generate_pdf($customcert, $userid) { + global $CFG, $DB; + + // Get the pages for the customcert, there should always be at least one page for each customcert. + if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $customcert->id), 'pagenumber ASC')) { + // Create the pdf object. + $pdf = new pdf(); + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + $pdf->SetTitle($customcert->name); + // Remove full-stop at the end, if it exists, to avoid "..pdf" being created and being filtered by clean_filename. + $filename = rtrim($customcert->name, '.'); + $filename = clean_filename($filename . '.pdf'); + // Loop through the pages and display their content. + foreach ($pages as $page) { + // Add the page to the PDF. + $pdf->AddPage($page->orientation, array($page->width, $page->height)); + // Get the elements for the page. + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { + // Loop through and display. + foreach ($elements as $element) { + // Check that the standard class file exists. + $classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; + if (file_exists($classfile)) { + require_once($classfile); + $classname = "customcert_element_{$element->element}"; + $e = new $classname($element); + $e->render($pdf, $userid); + } + } + } + } + $pdf->Output($filename, 'D'); + } +} \ No newline at end of file diff --git a/view.php b/view.php index d750dcc..1a62e6f 100644 --- a/view.php +++ b/view.php @@ -24,8 +24,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -require_once("../../config.php"); -require_once("$CFG->dirroot/mod/customcert/lib.php"); +require_once('../../config.php'); +require_once($CFG->dirroot . '/mod/customcert/lib.php'); +require_once($CFG->libdir . '/pdflib.php'); $id = required_param('id', PARAM_INT); $action = optional_param('action', '', PARAM_ALPHA); @@ -119,5 +120,5 @@ if (empty($action)) { $DB->insert_record('customcert_issues', $customcertissue); } // Now we want to generate the PDF. - customcert_generate_pdf(); + customcert_generate_pdf($customcert, $USER->id); }