Added a 'leftmargin' field

Also renamed the 'margin' field to 'rightmargin'
to easily distinguish between the two.
This commit is contained in:
Mark Nelson 2015-12-08 17:41:38 +08:00
parent 9413d65ee0
commit 355de679e8
10 changed files with 118 additions and 32 deletions

View file

@ -49,7 +49,7 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
// The pages.
$pages = new backup_nested_element('pages');
$page = new backup_nested_element('page', array('id'), array(
'customcertid', 'width', 'height', 'margin',
'customcertid', 'width', 'height', 'leftmargin', 'rightmargin',
'pagenumber', 'timecreated', 'timemodified'));
// The elements.

View file

@ -39,7 +39,8 @@
<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="leftmargin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="rightmargin" 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"/>
@ -89,7 +90,8 @@
<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="leftmargin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="rightmargin" 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"/>

View file

@ -100,5 +100,34 @@ function xmldb_customcert_upgrade($oldversion=0) {
upgrade_mod_savepoint(true, 2015120800, 'customcert');
}
if ($oldversion < 2015120801) {
// Rename the 'margin' field to 'rightmargin' in the 'customcert_pages' and 'customcert_template_pages' tables.
$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->rename_field($table, $field, 'rightmargin');
}
$table = new xmldb_table('customcert_template_pages');
if ($dbman->field_exists($table, $field)) {
$dbman->rename_field($table, $field, 'rightmargin');
}
// Add 'leftmargin' fields to the 'customcert_pages' and 'customcert_template_pages' tables.
$table = new xmldb_table('customcert_pages');
$field = new xmldb_field('leftmargin', XMLDB_TYPE_INTEGER, 10, null, null, null, 0, 'height');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$table = new xmldb_table('customcert_template_pages');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Customcert savepoint reached.
upgrade_mod_savepoint(true, 2015120801, 'customcert');
}
return true;
}

View file

@ -99,9 +99,12 @@ 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);
// Set the left margin.
$element = $mform->getElement('pageleftmargin_' . $p->id);
$element->setValue($p->leftmargin);
// Set the right margin.
$element = $mform->getElement('pagerightmargin_' . $p->id);
$element->setValue($p->rightmargin);
}
}
}
@ -135,8 +138,14 @@ 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 (strpos($key, 'pageleftmargin_') !== false) {
// Validate that the left margin is a valid value.
if (isset($data[$key]) && ($data[$key] < 0)) {
$errors[$key] = get_string('invalidmargin', 'customcert');
}
}
if (strpos($key, 'pagerightmargin_') !== false) {
// Validate that the right margin is a valid value.
if (isset($data[$key]) && ($data[$key] < 0)) {
$errors[$key] = get_string('invalidmargin', 'customcert');
}
@ -183,9 +192,13 @@ 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('text', 'pageleftmargin_' . $page->id, get_string('leftmargin', 'customcert'));
$mform->setType('pageleftmargin_' . $page->id, PARAM_INT);
$mform->addHelpButton('pageleftmargin_' . $page->id, 'leftmargin', 'customcert');
$mform->addElement('text', 'pagerightmargin_' . $page->id, get_string('rightmargin', 'customcert'));
$mform->setType('pagerightmargin_' . $page->id, PARAM_INT);
$mform->addHelpButton('pagerightmargin_' . $page->id, 'rightmargin', 'customcert');
$group = array();
$group[] = $mform->createElement('select', 'element_' . $page->id, '', customcert_get_elements());

View file

@ -68,11 +68,11 @@ $string['invalidmargin'] = 'The margin has to be a valid number greater than 0.'
$string['invalidwidth'] = 'The width has to be a valid number greater than 0.';
$string['issued'] = 'Issued';
$string['landscape'] = 'Landscape';
$string['leftmargin'] = 'Left margin';
$string['leftmargin_help'] = 'This is the left margin of the certificate PDF in mm.';
$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';
@ -97,6 +97,8 @@ $string['refpoint'] = 'Reference point location';
$string['refpoint_help'] = 'This specifies which location of the element to be located at position X and position Y.';
$string['replacetemplate'] = 'Replace';
$string['report'] = 'Report';
$string['rightmargin'] = 'Right margin';
$string['rightmargin_help'] = 'This is the right margin of the certificate PDF in mm.';
$string['save'] = 'Save';
$string['saveandclose'] = 'Save and close';
$string['saveandcontinue'] = 'Save and continue';

View file

@ -222,13 +222,15 @@ 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;
$leftmargin = 'pageleftmargin_' . $page->id;
$rightmargin = 'pagerightmargin_' . $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->leftmargin = $data->$leftmargin;
$p->rightmargin = $data->$rightmargin;
$p->timemodified = $time;
// Update the page.
$DB->update_record('customcert_pages', $p);
@ -622,7 +624,7 @@ function customcert_generate_pdf($customcert, $preview = false) {
$orientation = 'P';
}
$pdf->AddPage($orientation, array($page->width, $page->height));
$pdf->SetMargins(0, 0, $page->margin);
$pdf->SetMargins($page->leftmargin, 0, $page->rightmargin);
// 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

@ -50,7 +50,7 @@ $module = array(
'fullpath' => '/mod/customcert/yui/src/rearrange.js',
'requires' => array('dd-delegate', 'dd-drag')
);
$PAGE->requires->js_init_call('M.mod_customcert.rearrange.init', array($cm->id, $elements), false, $module);
$PAGE->requires->js_init_call('M.mod_customcert.rearrange.init', array($cm->id, $page, $elements), false, $module);
// Create the buttons to save the position of the elements.
$html = html_writer::start_tag('div', array('class' => 'buttons'));
@ -62,19 +62,14 @@ $html .= $OUTPUT->single_button(new moodle_url('/mod/customcert/edit.php', array
get_string('cancel'), 'get', array('class' => 'cancelbtn'));
$html .= html_writer::end_tag('div');
$style = 'height: ' . $page->height . 'mm; line-height: normal;';
if ($page->margin) {
$style .= 'width: ' . ($page->width - $page->margin) . 'mm;';
$style .= 'background-image: url(' . new moodle_url('/mod/customcert/pix/dash') . ');';
$style .= 'background-repeat: repeat-y;';
$style .= 'background-position-x: ' . ($page->width - $page->margin) . 'mm;';
$style .= 'padding-right: ' . $page->margin . 'mm;';
} else {
$style .= 'width: ' . $page->width . 'mm;';
}
// Create the div that represents the PDF.
$style = 'height: ' . $page->height . 'mm; line-height: normal; width: ' . $page->width . 'mm;';
$marginstyle = 'height: ' . $page->height . 'mm; width:1px; float:left; position:relative;';
$html .= html_writer::start_tag('div', array('id' => 'pdf', 'style' => $style));
if ($page->leftmargin) {
$position = 'left:' . $page->leftmargin . 'mm;';
$html .= "<div id='leftmargin' style='$position $marginstyle'></div>";
}
if ($elements) {
foreach ($elements as $element) {
// Get an instance of the element class.
@ -94,6 +89,10 @@ if ($elements) {
}
}
}
if ($page->rightmargin) {
$position = 'left:' . ($page->width - $page->rightmargin) . 'mm;';
$html .= "<div id='rightmargin' style='$position $marginstyle'></div>";
}
$html .= html_writer::end_tag('div');
echo $OUTPUT->header();

View file

@ -56,4 +56,12 @@
clear:both;
border-style:solid;
border-width:1px;
}
}
#page-mod-customcert-position div#leftmargin {
border-left: 1px dotted black;
}
#page-mod-customcert-position div#rightmargin {
border-right: 1px dotted black;
}

View file

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

35
yui/src/rearrange.js vendored
View file

@ -8,6 +8,11 @@ M.mod_customcert.rearrange = {
*/
cmid : 0,
/**
* The customcert page we are displaying.
*/
page : Array(),
/**
* The custom certificate elements to display.
*/
@ -38,6 +43,17 @@ M.mod_customcert.rearrange = {
*/
elementxy : 0,
/**
* Store the left boundary of the pdf div.
*/
pdfleftboundary : 0,
/**
* Store the right boundary of the pdf div.
*/
pdfrightboundary : 0,
/**
* The number of pixels in a mm.
*/
@ -47,11 +63,15 @@ M.mod_customcert.rearrange = {
* Initialise.
*
* @param Y
* @param cmid
* @param page
* @param elements
*/
init : function(Y, cmid, elements) {
init : function(Y, cmid, page, elements) {
// Set the course module id.
this.cmid = cmid;
// Set the page.
this.page = page;
// Set the elements.
this.elements = elements;
@ -61,6 +81,17 @@ M.mod_customcert.rearrange = {
this.pdfwidth = parseFloat(Y.one('#pdf').getComputedStyle('width'), 10);
this.pdfheight = parseFloat(Y.one('#pdf').getComputedStyle('height'), 10);
// Set the boundaries.
this.pdfleftboundary = this.pdfx;
if (this.page['leftmargin']) {
this.pdfleftboundary += parseInt(this.page['leftmargin'] * this.pixelsinmm);
}
this.pdfrightboundary = this.pdfx + this.pdfwidth;
if (this.page['rightmargin']) {
this.pdfrightboundary -= parseInt(this.page['rightmargin'] * this.pixelsinmm);
}
this.set_data(Y);
this.set_positions(Y);
this.create_events(Y);
@ -248,7 +279,7 @@ M.mod_customcert.rearrange = {
var bottom = top + nodeheight;
// Check if it is out of bounds horizontally.
if ((left < this.pdfx) || (right > (this.pdfx + this.pdfwidth))) {
if ((left < this.pdfleftboundary) || (right > this.pdfrightboundary)) {
return true;
}