diff --git a/db/install.xml b/db/install.xml index bed795f..ad17256 100644 --- a/db/install.xml +++ b/db/install.xml @@ -39,6 +39,7 @@ + @@ -60,6 +61,7 @@ + @@ -86,6 +88,7 @@ + @@ -107,6 +110,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php new file mode 100644 index 0000000..a488429 --- /dev/null +++ b/db/upgrade.php @@ -0,0 +1,65 @@ +. + +/** + * This file keeps track of upgrades to the customcert module + * + * @package mod_customcert + * @copyright 2015 Shamim Rezaie + * @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; +} diff --git a/edit_form.php b/edit_form.php index f5baae3..769758c 100644 --- a/edit_form.php +++ b/edit_form.php @@ -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(); diff --git a/element/element.class.php b/element/element.class.php index 62929d6..dd71b62 100644 --- a/element/element.class.php +++ b/element/element.class.php @@ -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; } diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 484667f..d681fb6 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -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'; diff --git a/locallib.php b/locallib.php index 8e07982..8bbef73 100644 --- a/locallib.php +++ b/locallib.php @@ -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. diff --git a/version.php b/version.php index ece81da..bf15d40 100644 --- a/version.php +++ b/version.php @@ -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';