From 527d453950acfc19ff3ac1d9d0492d1fd151dd42 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 21 Oct 2016 21:33:59 +0800 Subject: [PATCH] #41 Add setting to display the position X and Y elements --- amd/build/rearrange-area.min.js | 2 +- amd/src/rearrange-area.js | 34 ++++++++++++++++++++++++++-- classes/edit_element_form.php | 15 +++++-------- classes/element.php | 22 +++++++++++++++++-- classes/element_helper.php | 39 +++++++++++++++++++++++++++++++++ lang/en/customcert.php | 10 ++++++++- lib.php | 2 +- settings.php | 5 +++++ version.php | 4 ++-- 9 files changed, 115 insertions(+), 18 deletions(-) diff --git a/amd/build/rearrange-area.min.js b/amd/build/rearrange-area.min.js index b23172e..d3acec3 100644 --- a/amd/build/rearrange-area.min.js +++ b/amd/build/rearrange-area.min.js @@ -1 +1 @@ -define(["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax"],function(a,b,c,d,e,f,g,h){var i=function(b){this._node=a(b),this._setEvents()};return i.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,i.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,i.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,i.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},i.prototype._editElement=function(a){var g=a.currentTarget.id.substr(8),h=this._node.attr("data-contextid"),i={elementid:g};c.loadFragment("mod_customcert","editelement",h,i).done(function(a,c){f.get_string("editelement","mod_customcert").done(function(e){b.use("moodle-core-formchangechecker",function(){new d(e,"
",this._editElementDialogueConfig.bind(this,g,a,c),(void 0),(!0))}.bind(this))}.bind(this))}.bind(this)).fail(e.exception)},i.prototype._editElementDialogueConfig=function(b,c,d,e){g.replaceNode("#elementcontent",c,d);var f=a(e.getContent());f.on("click","#id_submitbutton",function(c){M.core_formchangechecker.reset_form_dirty_state(),this._saveElement(b).always(function(){this._getElementHTML(b).always(function(c){var d=this._node.find("#element-"+b),f=a("#id_refpoint").val(),g="";f==this.CUSTOMCERT_REF_POINT_TOPLEFT?g="refpoint-left":f==this.CUSTOMCERT_REF_POINT_TOPCENTER?g="refpoint-center":f==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(g="refpoint-right"),d.empty().append(c),d.removeClass(),d.addClass("element "+g),e.close()}.bind(this))}.bind(this)),c.preventDefault()}.bind(this)),f.on("click","#id_cancel",function(a){e.close(),a.preventDefault()}.bind(this))},i.prototype._getElementHTML=function(a){var b=this._node.attr("data-templateid"),c=h.call([{methodname:"mod_customcert_get_element_html",args:{templateid:b,elementid:a}}]);return c[0]},i.prototype._saveElement=function(b){var c=this._node.attr("data-templateid"),d=a("#editelementform").serializeArray(),e=h.call([{methodname:"mod_customcert_save_element",args:{templateid:c,elementid:b,values:d}}]);return e[0]},{init:function(a){new i(a)}}}); \ No newline at end of file +define(["jquery","core/yui","core/fragment","mod_customcert/dialogue","core/notification","core/str","core/templates","core/ajax"],function(a,b,c,d,e,f,g,h){var i=function(b){this._node=a(b),this._setEvents()};return i.prototype.CUSTOMCERT_REF_POINT_TOPLEFT=0,i.prototype.CUSTOMCERT_REF_POINT_TOPCENTER=1,i.prototype.CUSTOMCERT_REF_POINT_TOPRIGHT=2,i.prototype._setEvents=function(){this._node.on("click",".element",this._editElement.bind(this))},i.prototype._editElement=function(a){var g=a.currentTarget.id.substr(8),h=this._node.attr("data-contextid"),i={elementid:g};c.loadFragment("mod_customcert","editelement",h,i).done(function(a,c){f.get_string("editelement","mod_customcert").done(function(e){b.use("moodle-core-formchangechecker",function(){new d(e,"
",this._editElementDialogueConfig.bind(this,g,a,c),(void 0),(!0))}.bind(this))}.bind(this))}.bind(this)).fail(e.exception)},i.prototype._editElementDialogueConfig=function(b,c,d,e){g.replaceNode("#elementcontent",c,d);var f=a(e.getContent());f.on("click","#id_submitbutton",function(c){M.core_formchangechecker.reset_form_dirty_state(),this._saveElement(b).then(function(){this._getElementHTML(b).done(function(c){var d=this._node.find("#element-"+b),f=a("#id_refpoint").val(),g="";f==this.CUSTOMCERT_REF_POINT_TOPLEFT?g="refpoint-left":f==this.CUSTOMCERT_REF_POINT_TOPCENTER?g="refpoint-center":f==this.CUSTOMCERT_REF_POINT_TOPRIGHT&&(g="refpoint-right"),d.empty().append(c),d.removeClass(),d.addClass("element "+g),this._setPosition(b,f),e.close()}.bind(this))}.bind(this)),c.preventDefault()}.bind(this)),f.on("click","#id_cancel",function(a){e.close(),a.preventDefault()}.bind(this))},i.prototype._setPosition=function(c,d){var e=b.one("#element-"+c),f=3.779527559055,g=b.one("#pdf").getX(),h=b.one("#pdf").getY(),i=g+a("#editelementform #id_posx").val()*f,j=h+a("#editelementform #id_posy").val()*f,k=parseFloat(e.getComputedStyle("width")),l=e.width*this.pixelsinmm;switch(l&&k>l&&(k=l),d){case"1":i-=k/2;break;case"2":i=i-k+2}e.setX(i),e.setY(j)},i.prototype._getElementHTML=function(a){var b=this._node.attr("data-templateid"),c=h.call([{methodname:"mod_customcert_get_element_html",args:{templateid:b,elementid:a}}]);return c[0]},i.prototype._saveElement=function(b){var c=this._node.attr("data-templateid"),d=a("#editelementform").serializeArray(),e=h.call([{methodname:"mod_customcert_save_element",args:{templateid:c,elementid:b,values:d}}]);return e[0]},{init:function(a){new i(a)}}}); \ No newline at end of file diff --git a/amd/src/rearrange-area.js b/amd/src/rearrange-area.js index 034c2d7..dbb072a 100644 --- a/amd/src/rearrange-area.js +++ b/amd/src/rearrange-area.js @@ -75,9 +75,9 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/ // Do not want to ask the user if they wish to stay on page after saving. M.core_formchangechecker.reset_form_dirty_state(); // Save the data. - this._saveElement(elementid).always(function() { + this._saveElement(elementid).then(function() { // Update the DOM to reflect the adjusted value. - this._getElementHTML(elementid).always(function(html) { + this._getElementHTML(elementid).done(function(html) { var elementNode = this._node.find('#element-' + elementid); var refpoint = $('#id_refpoint').val(); var refpointClass = ''; @@ -92,6 +92,8 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/ // Update the ref point. elementNode.removeClass(); elementNode.addClass('element ' + refpointClass); + // Move the element if we need to. + this._setPosition(elementid, refpoint); popup.close(); }.bind(this)); }.bind(this)); @@ -104,6 +106,34 @@ define(['jquery', 'core/yui', 'core/fragment', 'mod_customcert/dialogue', 'core/ }.bind(this)); }; + RearrangeArea.prototype._setPosition = function(elementid, refpoint) { + var element = Y.one('#element-' + elementid); + + var pixelsinmm = 3.779527559055; + var pdfx = Y.one('#pdf').getX(); + var pdfy = Y.one('#pdf').getY(); + var posx = pdfx + $('#editelementform #id_posx').val() * pixelsinmm; + var posy = pdfy + $('#editelementform #id_posy').val() * pixelsinmm; + var nodewidth = parseFloat(element.getComputedStyle('width')); + var maxwidth = element.width * this.pixelsinmm; + + if (maxwidth && (nodewidth > maxwidth)) { + nodewidth = maxwidth; + } + + switch (refpoint) { + case '1': // Top-center. + posx -= nodewidth / 2; + break; + case '2': // Top-right. + posx = posx - nodewidth + 2; + break; + } + + element.setX(posx); + element.setY(posy); + }; + RearrangeArea.prototype._getElementHTML = function(elementid) { // Get the variables we need. var templateid = this._node.attr('data-templateid'); diff --git a/classes/edit_element_form.php b/classes/edit_element_form.php index 7a38033..2322267 100644 --- a/classes/edit_element_form.php +++ b/classes/edit_element_form.php @@ -48,15 +48,12 @@ class edit_element_form extends \moodleform { $element = $this->_customdata['element']; - // Do not display the name if we are on the rearrange page. - if (!isset($this->_customdata['rearrange'])) { - // Add the field for the name of the element, this is required for all elements. - $mform->addElement('text', 'name', get_string('elementname', 'customcert')); - $mform->setType('name', PARAM_TEXT); - $mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element)); - $mform->addRule('name', get_string('required'), 'required', null, 'client'); - $mform->addHelpButton('name', 'elementname', 'customcert'); - } + // Add the field for the name of the element, this is required for all elements. + $mform->addElement('text', 'name', get_string('elementname', 'customcert')); + $mform->setType('name', PARAM_TEXT); + $mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element)); + $mform->addRule('name', get_string('required'), 'required', null, 'client'); + $mform->addHelpButton('name', 'elementname', 'customcert'); $this->element = \mod_customcert\element::instance($element); $this->element->render_form_elements($mform); diff --git a/classes/element.php b/classes/element.php index e1e5e0e..5faeef6 100644 --- a/classes/element.php +++ b/classes/element.php @@ -38,13 +38,21 @@ abstract class element { */ public $element; + /** + * @var bool $showposxy Show position XY form elements? + */ + public $showposxy; + /** * Constructor. * * @param \stdClass $element the element data */ public function __construct($element) { + $showposxy = get_config('customcert', 'showposxy'); + $this->element = clone($element); + $this->showposxy = isset($showposxy) && $showposxy; } /** @@ -57,7 +65,10 @@ abstract class element { // Render the common elements. element_helper::render_form_element_font($mform); element_helper::render_form_element_colour($mform); - element_helper::render_form_element_position($mform); + if ($this->showposxy) { + element_helper::render_form_element_position($mform); + } + element_helper::render_form_element_width($mform); } /** @@ -91,7 +102,10 @@ abstract class element { // Common validation methods. $errors += element_helper::validate_form_element_colour($data); - $errors += element_helper::validate_form_element_position($data); + if ($this->showposxy) { + $errors += element_helper::validate_form_element_position($data); + } + $errors += element_helper::validate_form_element_width($data); return $errors; } @@ -113,6 +127,10 @@ abstract class element { $element->font = (isset($data->font)) ? $data->font : null; $element->size = (isset($data->size)) ? $data->size : null; $element->colour = (isset($data->colour)) ? $data->colour : null; + if ($this->showposxy) { + $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->refpoint = (isset($data->refpoint)) ? $data->refpoint : null; $element->timemodified = time(); diff --git a/classes/element_helper.php b/classes/element_helper.php index 30bfb02..00fbc71 100644 --- a/classes/element_helper.php +++ b/classes/element_helper.php @@ -157,6 +157,22 @@ class element_helper { * @param \mod_customcert\edit_element_form $mform the edit_form instance. */ public static function render_form_element_position($mform) { + $mform->addElement('text', 'posx', get_string('posx', 'customcert'), array('size' => 10)); + $mform->setType('posx', PARAM_INT); + $mform->setDefault('posx', 0); + $mform->addHelpButton('posx', 'posx', 'customcert'); + $mform->addElement('text', 'posy', get_string('posy', 'customcert'), array('size' => 10)); + $mform->setType('posy', PARAM_INT); + $mform->setDefault('posy', 0); + $mform->addHelpButton('posy', 'posy', 'customcert'); + } + + /** + * Helper function to render the width element. + * + * @param \mod_customcert\edit_element_form $mform the edit_form instance. + */ + public static function render_form_element_width($mform) { $mform->addElement('text', 'width', get_string('elementwidth', 'customcert'), array('size' => 10)); $mform->setType('width', PARAM_INT); $mform->setDefault('width', 0); @@ -194,10 +210,33 @@ class element_helper { */ public static function validate_form_element_position($data) { $errors = array(); + + // Check if posx is not set, or not numeric or less than 0. + if ((!isset($data['posx'])) || (!is_numeric($data['posx'])) || ($data['posx'] < 0)) { + $errors['posx'] = get_string('invalidposition', 'customcert', 'X'); + } + // Check if posy is not set, or not numeric or less than 0. + if ((!isset($data['posy'])) || (!is_numeric($data['posy'])) || ($data['posy'] < 0)) { + $errors['posy'] = get_string('invalidposition', 'customcert', 'Y'); + } + + return $errors; + } + + /** + * Helper function to perform validation on the width element. + * + * @param array $data the submitted data + * @return array the validation errors + */ + public static function validate_form_element_width($data) { + $errors = array(); + // 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 5cf3084..b8811a0 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -92,8 +92,12 @@ $string['options'] = 'Options'; $string['page'] = 'Page {$a}'; $string['pluginadministration'] = 'Custom certificate administration'; $string['pluginname'] = 'Custom certificate'; -$string['print'] = 'Print'; $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['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['print'] = 'Print'; $string['rearrangeelements'] = 'Reposition elements'; $string['rearrangeelementsheading'] = 'Drag and drop elements to change where they are positioned on the certificate.'; $string['receiveddate'] = 'Received date'; @@ -110,6 +114,10 @@ $string['savetemplate'] = 'Save template'; $string['search:activity'] = 'Custom certificate - activity information'; $string['setprotection'] = 'Set protection'; $string['setprotection_help'] = 'Choose the actions you wish to prevent users from performing on this certificate.'; +$string['showposxy'] = 'Show position X and Y'; +$string['showposxy_desc'] = 'This will show the X and Y position when editing of an element, allowing the user to accurately specify the location. + +This isn\'t required if you plan on solely using the drag and drop interface for this purpose.'; $string['summaryofissue'] = 'Summary of issue'; $string['templatename'] = 'Template name'; $string['templatenameexists'] = 'That template name is currently in use, please choose another.'; diff --git a/lib.php b/lib.php index 9f51ab5..2c747f2 100644 --- a/lib.php +++ b/lib.php @@ -316,7 +316,7 @@ function mod_customcert_output_fragment_editelement($args) { $element = $DB->get_record('customcert_elements', array('id' => $args['elementid']), '*', MUST_EXIST); $pageurl = new moodle_url('/mod/customcert/rearrange.php', array('pid' => $element->pageid)); - $form = new \mod_customcert\edit_element_form($pageurl, array('element' => $element, 'rearrange' => true)); + $form = new \mod_customcert\edit_element_form($pageurl, array('element' => $element)); return $form->render(); } diff --git a/settings.php b/settings.php index 7c36d87..01a998e 100644 --- a/settings.php +++ b/settings.php @@ -24,6 +24,11 @@ defined('MOODLE_INTERNAL') || die; +$settings->add(new admin_setting_configcheckbox('customcert/showposxy', + get_string('showposxy', 'customcert'), + get_string('showposxy_desc', 'customcert'), + 0)); + $settings->add(new \mod_customcert\admin_setting_link('customcert/managetemplates', get_string('managetemplates', 'customcert'), get_string('managetemplatesdesc', 'customcert'), get_string('managetemplates', 'customcert'), new moodle_url('/mod/customcert/manage_templates.php'), '')); diff --git a/version.php b/version.php index aefab0b..81aad54 100644 --- a/version.php +++ b/version.php @@ -24,10 +24,10 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2016052303; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2016052304; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2016052300; // Requires this Moodle version (3.1). $plugin->cron = 0; // Period for cron to check this module (secs). $plugin->component = 'mod_customcert'; $plugin->maturity = MATURITY_STABLE; -$plugin->release = "3.1 release (Build: 2016052302)"; // User-friendly version number. +$plugin->release = "3.1 release (Build: 2016052304)"; // User-friendly version number.