Added support for having right margin and width limit for each element

In addition to width and height, each page can have an
optional (right) margin attribute. Also, each element
can have an optional width attribute.
This commit is contained in:
Shamim Rezaie 2015-07-29 11:29:16 +10:00 committed by Mark Nelson
parent 3774a6323c
commit 6400e587d8
7 changed files with 105 additions and 4 deletions

View file

@ -39,6 +39,7 @@
<FIELD NAME="customcertid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="width" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="margin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="pagenumber" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
@ -60,6 +61,7 @@
<FIELD NAME="colour" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="posx" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="posy" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="width" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="sequence" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
@ -86,6 +88,7 @@
<FIELD NAME="templateid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="width" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="margin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="pagenumber" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
@ -107,6 +110,7 @@
<FIELD NAME="colour" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="posx" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="posy" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="width" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="sequence" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>

65
db/upgrade.php Normal file
View file

@ -0,0 +1,65 @@
<?php
// This file is part of the Certificate module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file keeps track of upgrades to the customcert module
*
* @package mod_customcert
* @copyright 2015 Shamim Rezaie <rezaie@foodle.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function xmldb_customcert_upgrade($oldversion=0) {
global $CFG, $DB;
$dbman = $DB->get_manager();
if ($oldversion < 2015072700) {
// Add the margin fields to customcert_pages table
$table = new xmldb_table('customcert_pages');
$field = new xmldb_field('margin', XMLDB_TYPE_INTEGER, 10, null, null, null, 0, 'height');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Add the margin fields to customcert_template_pages table
$table = new xmldb_table('customcert_template_pages');
$field = new xmldb_field('margin', XMLDB_TYPE_INTEGER, 10, null, null, null, 0, 'height');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Add the width fields to customcert_elements table
$table = new xmldb_table('customcert_elements');
$field = new xmldb_field('width', XMLDB_TYPE_INTEGER, 10, null, null, null, 0, 'posy');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Add the width fields to customcert_template_elements table
$table = new xmldb_table('customcert_template_elements');
$field = new xmldb_field('width', XMLDB_TYPE_INTEGER, 10, null, null, null, 0, 'posy');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Customcert savepoint reached.
upgrade_mod_savepoint(true, 2015072700, 'customcert');
}
return true;
}

View file

@ -99,6 +99,9 @@ class mod_customcert_edit_form extends moodleform {
// Set the height.
$element = $mform->getElement('pageheight_' . $p->id);
$element->setValue($p->height);
// Set the margin.
$element = $mform->getElement('pagemargin_' . $p->id);
$element->setValue($p->margin);
}
}
}
@ -114,7 +117,7 @@ class mod_customcert_edit_form extends moodleform {
public function validation($data, $files) {
$errors = parent::validation($data, $files);
// Go through the data and check any width or height values.
// Go through the data and check any width, height or margin values.
foreach ($data as $key => $value) {
if (strpos($key, 'pagewidth_') !== false) {
$page = str_replace('pagewidth_', '', $key);
@ -132,6 +135,12 @@ class mod_customcert_edit_form extends moodleform {
$errors[$heightid] = get_string('invalidheight', 'customcert');
}
}
if (strpos($key, 'pagemargin_') !== false) {
// Validate that the margin is a valid value.
if (isset($data[$key]) && ($data[$key] < 0)) {
$errors[$key] = get_string('invalidmargin', 'customcert');
}
}
}
return $errors;
@ -174,6 +183,10 @@ 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('text', 'pagemargin_' . $page->id, get_string('margin', 'customcert'));
$mform->setType('pagemargin_' . $page->id, PARAM_INT);
$mform->addHelpButton('pagemargin_' . $page->id, 'margin', 'customcert');
$mform->addElement('submit', 'downloadgrid_' . $page->id, get_string('downloadgrid', 'customcert'));
$group = array();

View file

@ -113,6 +113,7 @@ abstract class customcert_element_base {
$element->colour = (isset($data->colour)) ? $data->colour : null;
$element->posx = (isset($data->posx)) ? $data->posx : null;
$element->posy = (isset($data->posy)) ? $data->posy : null;
$element->width = (isset($data->width)) ? $data->width : null;
$element->timemodified = time();
// Check if we are updating, or inserting a new element.
@ -183,7 +184,7 @@ abstract class customcert_element_base {
$this->set_font($pdf);
$fontcolour = TCPDF_COLORS::convertHTMLColorToDec($this->element->colour, $fontcolour);
$pdf->SetTextColor($fontcolour['R'], $fontcolour['G'], $fontcolour['B']);
$pdf->writeHTMLCell(0, 0, $this->element->posx, $this->element->posy, $content);
$pdf->writeHTMLCell($this->element->width, 0, $this->element->posx, $this->element->posy, $content);
}
/**
@ -266,6 +267,11 @@ abstract class customcert_element_base {
$mform->setType('posy', PARAM_INT);
$mform->setDefault('posy', '0');
$mform->addHelpButton('posy', 'posy', 'customcert');
$mform->addElement('text', 'width', get_string('elementwidth', 'customcert'), array('size' => 10));
$mform->setType('width', PARAM_INT);
$mform->setDefault('width', '');
$mform->addHelpButton('width', 'elementwidth', 'customcert');
}
/**
@ -304,6 +310,11 @@ abstract class customcert_element_base {
$errors['posy'] = get_string('invalidposition', 'customcert', 'Y');
}
// Check if width is less than 0.
if (isset($data['width']) && $data['width'] < 0) {
$errors['width'] = get_string('invalidelementwidth', 'customcert');
}
return $errors;
}

View file

@ -47,6 +47,8 @@ $string['elementname_help'] = 'This will be the name used to identify this eleme
page and will want to distinguish between them quickly when editing the certificate. Note: this will not displayed on the PDF.';
$string['elements'] = 'Elements';
$string['elements_help'] = 'These are the list of elements that will be displayed on this PDF page.';
$string['elementwidth'] = 'Max width';
$string['elementwidth_help'] = 'Specify the maximum width of the element. 0 means no limit.';
$string['errorloadingelement'] = 'Error loading the element "{$a}"';
$string['errorsavingelement'] = 'Error saving the element "{$a}"';
$string['font'] = 'Font';
@ -60,14 +62,18 @@ $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.';
$string['invalidelementwidth'] = 'Please enter a positive number.';
$string['invalidposition'] = 'Please select a positive number for position {$a}.';
$string['invalidheight'] = 'The height has to be a valid number greater than 0.';
$string['invalidmargin'] = 'The margin cannot be a negative number.';
$string['invalidwidth'] = 'The width has to be a valid number greater than 0.';
$string['issued'] = 'Issued';
$string['landscape'] = 'Landscape';
$string['load'] = 'Load';
$string['loadtemplate'] = 'Load template';
$string['loadtemplatemsg'] = 'Are you sure you wish to load this template? This will remove any existing pages and elements for this certificate.';
$string['margin'] = 'Right margin';
$string['margin_help'] = 'This is the right margin of the certificate PDF in mm.';
$string['modify'] = 'Modify';
$string['modulename'] = 'Custom Certificate';
$string['modulenameplural'] = 'Custom Certificates';

View file

@ -207,11 +207,13 @@ function customcert_save_page_data($data) {
// Get the name of the fields we want from the form.
$width = 'pagewidth_' . $page->id;
$height = 'pageheight_' . $page->id;
$margin = 'pagemargin_' . $page->id;
// Create the page data to update the DB with.
$p = new stdClass();
$p->id = $page->id;
$p->width = $data->$width;
$p->height = $data->$height;
$p->margin = $data->$margin;
$p->timemodified = $time;
// Update the page.
$DB->update_record('customcert_pages', $p);
@ -668,7 +670,6 @@ 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, '.');
@ -682,6 +683,7 @@ function customcert_generate_pdf($customcert, $preview = false) {
$orientation = 'P';
}
$pdf->AddPage($orientation, array($page->width, $page->height));
$pdf->SetMargins(0, 0, $page->margin);
// Get the elements for the page.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
// Loop through and display.

View file

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2015031300; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2015072700; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2014051200; // Requires this Moodle version (2.7).
$plugin->cron = 0; // Period for cron to check this module (secs).
$plugin->component = 'mod_customcert';