Added ability to print out a grid PDF so that the user knows where to specify the placement of elements

This commit is contained in:
Mark Nelson 2013-07-25 18:36:43 +08:00
parent 4ad8ec0495
commit 23d611de1b
4 changed files with 85 additions and 3 deletions

View file

@ -165,13 +165,18 @@ if ($data = $mform->get_data()) {
customcert_save_page_data($data);
// Check if we are adding a page.
if (!empty($data->addcertpage)) { // Check if they chose to add a page.
if (!empty($data->addcertpage)) {
customcert_add_page($data);
}
// Loop through the data.
foreach ($data as $key => $value) {
if (strpos($key, 'addelement_') !== false) { // Check if they chose to add an element to a page.
// Check if they wanted to download the grid PDF.
if (strpos($key, 'downloadgrid_') !== false) {
// Get the page id.
$pageid = str_replace('downloadgrid_', '', $key);
customcert_generate_grid_pdf($pageid);
} else if (strpos($key, 'addelement_') !== false) { // Check if they chose to add an element to a page.
// Get the page id.
$pageid = str_replace('addelement_', '', $key);
// Get the element.

View file

@ -162,7 +162,7 @@ class mod_customcert_edit_form extends moodleform {
* Adds the page elements to the form.
*
* @param stdClass $page the customcert page
**/
*/
private function add_customcert_page_elements($page) {
global $DB, $OUTPUT;
@ -201,6 +201,8 @@ class mod_customcert_edit_form extends moodleform {
$mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client');
$mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert');
$mform->addElement('submit', 'downloadgrid_' . $page->id, get_string('downloadgrid', 'customcert'));
$group = array();
$group[] = $mform->createElement('select', 'element_' . $page->id, '', customcert_get_elements());
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'));

View file

@ -40,6 +40,7 @@ $string['deleteelement'] = 'Delete element';
$string['deleteelementconfirm'] = 'Are you sure you want to delete this element?';
$string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?';
$string['description'] = 'Description';
$string['downloadgrid'] = 'Download grid';
$string['editcustomcert'] = 'Edit custom certificate';
$string['elementname'] = 'Element name';
$string['elementname_help'] = 'This will be the name used to identify this element when editing a custom certificate. For example, you may have multiple images on a
@ -55,6 +56,7 @@ $string['fontcolour_help'] = 'The colour of the font.';
$string['fontsize'] = 'Size';
$string['fontsize_help'] = 'The size of the font in points.';
$string['getcustomcert'] = 'Get your custom certificate';
$string['gridpdfname'] = 'grid_for_page_{$a}';
$string['height'] = 'Height';
$string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.';
$string['invalidcolour'] = 'Invalid colour chosen, please enter a valid HTML colour name, or a six-digit, or three-digit hexadecimal colour.';

73
lib.php
View file

@ -788,6 +788,77 @@ function customcert_generate_code() {
return $code;
}
/**
* Generate the grid PDF to show the layout of the PDF.
*
* @param int $pageid the customcert page
*/
function customcert_generate_grid_pdf($pageid) {
global $CFG, $DB;
require_once($CFG->libdir . '/pdflib.php');
// Get the page.
$page = $DB->get_record('customcert_pages', array('id' => $pageid), '*', MUST_EXIST);
// Set the PDF name.
$pdfname = get_string('gridpdfname', 'customcert', $page->id);
// Create the pdf object.
$pdf = new pdf();
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetTitle($pdfname);
$pdf->SetMargins(0, 0);
$pdf->SetAutoPageBreak(true, 0);
// Add the page to the PDF.
$pdf->AddPage($page->orientation, array($page->width, $page->height));
// The cell width.
$cellwidth = 10;
$cellheight = 10;
// The increments we want to go up in.
$inc = 20;
$decpos = 4;
// Draw first lines before we increment from here.
$pdf->Line(($inc / 2) + 2, 0, ($inc / 2) + 2, $page->height);
$pdf->Line(0, ($inc / 2), $page->width, ($inc / 2));
// Now we want to start drawing the lines to make the grid.
// Draw the horizontal lines.
for ($h = $inc; $h <= $page->height; $h += $inc) {
// Dirty hack cause the printer keeps cutting off characters
// near the end of the page for some BS reason.
if (strlen($h) <= 2) {
$lmargin = 5;
} else {
$lmargin = 4;
}
// Write the distance we are at.
$pdf->writeHTMLCell($cellwidth, $cellheight, $lmargin, $h - $decpos, $h);
// Get the location we are going to draw the line and then draw it.
$hline = $h + ($inc / 2);
$pdf->Line(0, $hline, $page->width, $hline);
}
// Draw the vertical lines.
for ($w = $inc; $w <= $page->width; $w += $inc) {
// Write the distance we are at.
$pdf->writeHTMLCell($cellwidth, $cellheight, $w - $decpos, 3, $w);
// Get the location we are going to draw the line and then draw it.
$wline = $w + ($inc / 2);
$pdf->Line($wline, 0, $wline, $page->height);
}
$pdf->Output($pdfname . '.pdf', 'D');
exit();
}
/**
* Generate the PDF for the specified customcert and user.
*
@ -810,6 +881,8 @@ function customcert_generate_pdf($customcert, $preview = false) {
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetTitle($customcert->name);
$pdf->SetMargins(0, 0);
$pdf->SetAutoPageBreak(true, 0);
// 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');