Skip to content

Commit

Permalink
Issue #370: Support right to left certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Nov 20, 2020
1 parent 6a19ec8 commit 9ea484c
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 24 deletions.
2 changes: 1 addition & 1 deletion amd/build/rearrange-area.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/rearrange-area.min.js.map

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions amd/src/rearrange-area.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/

RearrangeArea.prototype._setPosition = function(elementid, refpoint, posx, posy) {
var element = Y.one('#element-' + elementid);
var isRtl = Y.one('#pdf').getComputedStyle('direction') === 'rtl';

posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM;
if (isRtl) {
posx = Y.one('#pdf').getX() + parseFloat(Y.one('#pdf').getComputedStyle('width')) - posx * this.PIXELSINMM;
} else {
posx = Y.one('#pdf').getX() + posx * this.PIXELSINMM;
}
posy = Y.one('#pdf').getY() + posy * this.PIXELSINMM;
var nodewidth = parseFloat(element.getComputedStyle('width'));
var maxwidth = element.width * this.PIXELSINMM;
Expand All @@ -132,7 +137,7 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/
posx -= nodewidth / 2;
break;
case this.CUSTOMCERT_REF_POINT_TOPRIGHT:
posx = posx - nodewidth + 2;
posx = posx - nodewidth;
break;
}

Expand All @@ -143,6 +148,7 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/
RearrangeArea.prototype._setPositionInForm = function(elementid) {
var posxelement = $('#editelementform #id_posx');
var posyelement = $('#editelementform #id_posy');
var isRtl = Y.one('#pdf').getComputedStyle('direction') === 'rtl';

if (posxelement.length && posyelement.length) {
var element = Y.one('#element-' + elementid);
Expand All @@ -151,12 +157,25 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/
var refpoint = parseInt(element.getData('refpoint'));
var nodewidth = parseFloat(element.getComputedStyle('width'));

// Recalculate posx if the page's direction is right-to-left.
if (isRtl) {
posx = Y.one('#pdf').getX() + parseFloat(Y.one('#pdf').getComputedStyle('width')) - element.getX();
}

switch (refpoint) {
case this.CUSTOMCERT_REF_POINT_TOPCENTER:
posx += nodewidth / 2;
if (isRtl) {
posx -= nodewidth / 2;
} else {
posx += nodewidth / 2;
}
break;
case this.CUSTOMCERT_REF_POINT_TOPRIGHT:
posx += nodewidth;
if (isRtl) {
posx -= nodewidth;
} else {
posx += nodewidth;
}
break;
}

Expand Down
2 changes: 1 addition & 1 deletion backup/moodle2/backup_customcert_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function define_structure() {
// The pages.
$pages = new backup_nested_element('pages');
$page = new backup_nested_element('page', array('id'), array(
'templateid', 'width', 'height', 'leftmargin', 'rightmargin',
'templateid', 'width', 'height', 'direction', 'leftmargin', 'rightmargin',
'sequence', 'timecreated', 'timemodified'));

// The elements.
Expand Down
12 changes: 12 additions & 0 deletions classes/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public function definition_after_data() {
// Set the height.
$element = $mform->getElement('pageheight_' . $p->id);
$element->setValue($p->height);
// Set the direction.
$element = $mform->getElement('pagedirection_' . $p->id);
$element->setValue($p->direction);
// Set the left margin.
$element = $mform->getElement('pageleftmargin_' . $p->id);
$element->setValue($p->leftmargin);
Expand Down Expand Up @@ -230,6 +233,15 @@ protected function add_customcert_page_elements($page) {
$mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client');
$mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert');

$directions = [
0 => get_string('ltr', 'customcert'),
1 => get_string('rtl', 'customcert'),
];
$mform->addElement('select', 'pagedirection_' . $page->id, get_string('direction', 'customcert'), $directions);
$mform->setDefault('pagedirection_' . $page->id, 'ltr');
$mform->addRule('pagedirection_' . $page->id, null, 'required', null, 'client');
$mform->addHelpButton('pagedirection_' . $page->id, 'direction', 'customcert');

$mform->addElement('text', 'pageleftmargin_' . $page->id, get_string('leftmargin', 'customcert'));
$mform->setType('pageleftmargin_' . $page->id, PARAM_INT);
$mform->setDefault('pageleftmargin_' . $page->id, 0);
Expand Down
3 changes: 3 additions & 0 deletions classes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ public function save_page($data) {
// Get the name of the fields we want from the form.
$width = 'pagewidth_' . $page->id;
$height = 'pageheight_' . $page->id;
$direction = 'pagedirection_' . $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->direction = $data->$direction;
$p->leftmargin = $data->$leftmargin;
$p->rightmargin = $data->$rightmargin;
$p->timemodified = $time;
Expand Down Expand Up @@ -293,6 +295,7 @@ public function generate_pdf($preview = false, $userid = null, $return = false)
$orientation = 'P';
}
$pdf->AddPage($orientation, array($page->width, $page->height));
$pdf->setRTL($page->direction);
$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')) {
Expand Down
9 changes: 5 additions & 4 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/customcert/db" VERSION="20170530" COMMENT="XMLDB file for Moodle mod/customcert"
<XMLDB PATH="mod/customcert/db" VERSION="20200820" COMMENT="XMLDB file for Moodle mod/customcert"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -10,7 +10,7 @@
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="templateid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="intro" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="intro" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="requiredtime" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="verifyany" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
Expand Down Expand Up @@ -59,6 +59,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="direction" TYPE="int" LENGTH="4" 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="sequence" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
Expand All @@ -76,7 +77,7 @@
<FIELD NAME="pageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="element" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="data" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="fontsize" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="colour" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false"/>
Expand All @@ -94,4 +95,4 @@
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
15 changes: 15 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,20 @@ function xmldb_customcert_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2019111803, 'customcert');
}

if ($oldversion < 2020082000) {

// Define field direction to be added to customcert_pages.
$table = new xmldb_table('customcert_pages');
$field = new xmldb_field('direction', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'height');

// Conditionally launch add field direction.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Customcert savepoint reached.
upgrade_mod_savepoint(true, 2020082000, 'customcert');
}

return true;
}
2 changes: 2 additions & 0 deletions edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@
// Associate all the data from the form to the newly created page.
$width = 'pagewidth_' . $pageid;
$height = 'pageheight_' . $pageid;
$direction = 'pagedirection_' . $pageid;
$leftmargin = 'pageleftmargin_' . $pageid;
$rightmargin = 'pagerightmargin_' . $pageid;
$rightmargin = 'pagerightmargin_' . $pageid;

$data->$width = $data->pagewidth_0;
$data->$height = $data->pageheight_0;
$data->$direction = $data->pagedirection_0;
$data->$leftmargin = $data->pageleftmargin_0;
$data->$rightmargin = $data->pagerightmargin_0;

Expand Down
8 changes: 6 additions & 2 deletions lang/en/customcert.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
$string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?';
$string['deletetemplateconfirm'] = 'Are you sure you want to delete this certificate template?';
$string['description'] = 'Description';
$string['direction'] = 'Direction';
$string['direction_help'] = 'This is the direction of the certificate PDF.';
$string['ltr'] = 'Left to right';
$string['rtl'] = 'Right to left';
$string['duplicate'] = 'Duplicate';
$string['duplicateconfirm'] = 'Duplicate confirmation';
$string['duplicatetemplateconfirm'] = 'Are you sure you want to duplicate this certificate template?';
Expand Down Expand Up @@ -137,9 +141,9 @@
$string['pluginname'] = 'Custom certificate';
$string['portrait'] = 'Portrait';
$string['posx'] = 'Position X';
$string['posx_help'] = 'This is the position in mm from the top left corner you wish the element\'s reference point to locate in the x direction.';
$string['posx_help'] = 'This is the position in mm from the left side (or the right side, if the page\'s direction is right-to-left) of the page you wish the element\'s reference point to locate in the x direction.';
$string['posy'] = 'Position Y';
$string['posy_help'] = 'This is the position in mm from the top left corner you wish the element\'s reference point to locate in the y direction.';
$string['posy_help'] = 'This is the position in mm from the top of the page you wish the element\'s reference point to locate in the y direction.';
$string['preventcopy'] = 'Prevent copy';
$string['preventcopy_desc'] = 'Enable protection from copy action.';
$string['preventprint'] = 'Prevent print';
Expand Down
8 changes: 5 additions & 3 deletions rearrange.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@

// Create the div that represents the PDF.
$style = 'height: ' . $page->height . 'mm; line-height: normal; width: ' . $page->width . 'mm;';
$class = $page->direction == 1 ? 'page-rtl' : 'page-ltr';
$marginstyle = 'height: ' . $page->height . 'mm; width:1px; float:left; position:relative;';
$html .= html_writer::start_tag('div', array(
$html .= html_writer::start_tag('div', [
'data-templateid' => $template->get_id(),
'data-contextid' => $template->get_contextid(),
'id' => 'pdf',
'style' => $style)
);
'style' => $style,
'class' => $class,
]);
if ($page->leftmargin) {
$position = 'left:' . $page->leftmargin . 'mm;';
$html .= "<div id='leftmargin' style='$position $marginstyle'></div>";
Expand Down
Loading

0 comments on commit 9ea484c

Please sign in to comment.