#41 Add setting to display the position X and Y elements

This commit is contained in:
Mark Nelson 2016-10-21 21:33:59 +08:00
parent ceb2d5623f
commit 13fc5dfa4f
9 changed files with 115 additions and 18 deletions

View file

@ -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,"<div id='elementcontent'></div>",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)}}});
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,"<div id='elementcontent'></div>",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)}}});

View file

@ -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');

View file

@ -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');
}
$this->element = \mod_customcert\element::instance($element);
$this->element->render_form_elements($mform);

View file

@ -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,8 +65,11 @@ abstract class element {
// Render the common elements.
element_helper::render_form_element_font($mform);
element_helper::render_form_element_colour($mform);
if ($this->showposxy) {
element_helper::render_form_element_position($mform);
}
element_helper::render_form_element_width($mform);
}
/**
* Sets the data on the form when editing an element.
@ -91,7 +102,10 @@ abstract class element {
// Common validation methods.
$errors += element_helper::validate_form_element_colour($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();

View file

@ -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;
}

View file

@ -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.';

View file

@ -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();
}

View file

@ -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'), ''));

View file

@ -24,10 +24,10 @@
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2016120500; // Requires this Moodle version (3.2).
$plugin->cron = 0; // Period for cron to check this module (secs).
$plugin->component = 'mod_customcert';
$plugin->maturity = MATURITY_STABLE;
$plugin->release = "3.2 release (Build: 2016120500)"; // User-friendly version number.
$plugin->release = "3.2 release (Build: 2016120501)"; // User-friendly version number.