Changed how the elements are rendered when editing the customcert

Rather than using grouped elements which can span across multiple lines on a small screen, the elements are now generated within a header element, keeping them separated and making the form more manageable. The base class was also changed to accomodate this by making it easier for elements to utilise common features.
This commit is contained in:
Mark Nelson 2013-02-22 17:25:14 +08:00
parent b36c0b58dd
commit 5945171a9d
6 changed files with 94 additions and 100 deletions

View file

@ -221,6 +221,11 @@ class mod_customcert_edit_form extends moodleform {
$mform->addRule('height_' . $pageid, null, 'required', null, 'client'); $mform->addRule('height_' . $pageid, null, 'required', null, 'client');
$mform->addHelpButton('height_' . $pageid, 'height', 'customcert'); $mform->addHelpButton('height_' . $pageid, 'height', 'customcert');
$group = array();
$group[] = $mform->createElement('select', 'element_' . $pageid, '', $elementsavailable);
$group[] = $mform->createElement('submit', 'addelement_' . $pageid, get_string('addelement', 'customcert'));
$mform->addElement('group', 'elementgroup', '', $group, '', false);
// Check if there are elements to add. // Check if there are elements to add.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'id ASC')) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'id ASC')) {
// Loop through and add the ones present. // Loop through and add the ones present.
@ -229,6 +234,9 @@ class mod_customcert_edit_form extends moodleform {
// It's possible this element was added to the database then the folder was deleted, if // It's possible this element was added to the database then the folder was deleted, if
// this is the case we do not want to render these elements as an error will occur. // this is the case we do not want to render these elements as an error will occur.
if (file_exists($classfile)) { if (file_exists($classfile)) {
// Add the page number to the element so we can use within the element.
$element->pagenum = $pagenum;
// Get the classname.
$classname = "customcert_element_{$element->element}"; $classname = "customcert_element_{$element->element}";
$e = new $classname($element); $e = new $classname($element);
$e->render_form_elements($mform); $e->render_form_elements($mform);
@ -240,11 +248,6 @@ class mod_customcert_edit_form extends moodleform {
} }
} }
$group = array();
$group[] = $mform->createElement('select', 'element_' . $pageid, '', $elementsavailable);
$group[] = $mform->createElement('submit', 'addelement_' . $pageid, get_string('addelement', 'customcert'));
$mform->addElement('group', 'elementgroup', '', $group, '', false);
// Add option to delete this page if it is not the first page. // Add option to delete this page if it is not the first page.
if ($pagenum > 1) { if ($pagenum > 1) {
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletecertpage'))); $mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletecertpage')));

View file

@ -70,39 +70,61 @@ class customcert_element_base {
/** /**
* This function renders the form elements when adding a customcert element. * This function renders the form elements when adding a customcert element.
* Can be overridden if more functionality is needed. * Must be overriden.
* *
* @param stdClass $mform the edit_form instance. * @param stdClass $mform the edit_form instance.
* @return array the form elements * @return array the form elements
*/ */
public function render_form_elements($mform) { public function render_form_elements($mform) {
// Keep track of the number of times these elements have been // Must be overriden.
// added, so we only add the help icon once. return false;
static $numtimesadded = 0; }
/**
* This function renders the common form elements when adding a customcert element.
*
* @param stdClass $mform the edit_form instance.
* @return array the form elements
*/
public function render_common_form_elements($mform) {
// The identifier. // The identifier.
$id = $this->element->id; $id = $this->element->id;
// Commonly used string.
$strrequired = get_string('required');
// The common group of elements. // The common group of elements.
$group = array(); $mform->addElement('select', 'font_' . $id, get_string('font', 'customcert'), customcert_get_fonts());
$group[] = $mform->createElement('select', 'font_' . $id, '', customcert_get_fonts()); $mform->addElement('select', 'size_' . $id, get_string('fontsize', 'customcert'), customcert_get_font_sizes());
$group[] = $mform->createElement('select', 'size_' . $id, '', customcert_get_font_sizes()); $mform->addELement('text', 'colour_' . $id, get_string('fontcolour', 'customcert'), array('size' => 10, 'maxlength' => 6));
$group[] = $mform->createELement('text', 'colour_' . $id, '', array('size' => 10, 'maxlength' => 6)); $mform->addElement('text', 'posx_' . $id, get_string('posx', 'customcert'), array('size' => 10));
$group[] = $mform->createElement('text', 'posx_' . $id, '', array('size' => 10)); $mform->addElement('text', 'posy_' . $id, get_string('posy', 'customcert'), array('size' => 10));
$group[] = $mform->createElement('text', 'posy_' . $id, '', array('size' => 10));
// Add this group. // Set the types of these elements.
$mform->addElement('group', 'elementfieldgroup_' . $id, get_string('pluginname', 'customcertelement_' . $this->element->element), $mform->setType('font_' . $id, PARAM_TEXT);
$group, array(' ' . get_string('fontsize', 'customcert') . ' ', ' ' . get_string('colour', 'customcert') . ' ', $mform->setType('size_' . $id, PARAM_INT);
' ' . get_string('posx', 'customcert') . ' ', ' ' . get_string('posy', 'customcert') . ' '), false); $mform->setType('colour_' . $id, PARAM_RAW); // Need to validate this is a hexadecimal value.
$mform->setType('posx_' . $id, PARAM_INT);
$mform->setType('posy_' . $id, PARAM_INT);
$this->set_form_element_types($mform); // Add some rules.
$mform->addRule('colour_' . $id, $strrequired, 'required', null, 'client');
$mform->addRule('posx_' . $id, $strrequired, 'required', null, 'client');
$mform->addRule('posy_' . $id, $strrequired, 'required', null, 'client');
if ($numtimesadded == 0) { // Set the values of these elements.
$mform->addHelpButton('elementfieldgroup_' . $id, 'commonformelements', 'customcert'); $mform->setDefault('font_' . $id, $this->element->font);
} $mform->setDefault('size_' . $id, $this->element->size);
$mform->setDefault('colour_' . $id, $this->element->colour);
$mform->setDefault('posx_' . $id, $this->element->posx);
$mform->setDefault('posy_' . $id, $this->element->posy);
$numtimesadded++; // Add help buttons.
$mform->addHelpButton('font_' . $id, 'font', 'customcert');
$mform->addHelpButton('size_' . $id, 'fontsize', 'customcert');
$mform->addHelpButton('colour_' . $id, 'fontcolour', 'customcert');
$mform->addHelpButton('posx_' . $id, 'posx', 'customcert');
$mform->addHelpButton('posy_' . $id, 'posy', 'customcert');
} }
/** /**
@ -226,38 +248,4 @@ class customcert_element_base {
return $DB->delete_records('customcert_elements', array('id' => $this->element->id)); return $DB->delete_records('customcert_elements', array('id' => $this->element->id));
} }
/**
* Helper function that sets the types, defaults and rules for the common elements.
*
* @param stdClass $mform the edit_form instance.
* @return array the form elements
*/
public function set_form_element_types($mform) {
// The identifier.
$id = $this->element->id;
// Set the types of these elements.
$mform->setType('font_' . $id, PARAM_TEXT);
$mform->setType('size_' . $id, PARAM_INT);
$mform->setType('colour_' . $id, PARAM_RAW); // Need to validate this is a hexadecimal value.
$mform->setType('posx_' . $id, PARAM_INT);
$mform->setType('posy_' . $id, PARAM_INT);
// Add some rules.
$grouprule = array();
$grouprule['colour_' . $id][] = array(null, 'required', null, 'client');
$grouprule['posx_' . $id][] = array(null, 'required', null, 'client');
$grouprule['posy_' . $id][] = array(null, 'required', null, 'client');
$mform->addGroupRule('elementfieldgroup_' . $id, $grouprule);
// Set the values of these elements.
$mform->setDefault('font_' . $id, $this->element->font);
$mform->setDefault('size_' . $id, $this->element->size);
$mform->setDefault('colour_' . $id, $this->element->colour);
$mform->setDefault('posx_' . $id, $this->element->posx);
$mform->setDefault('posy_' . $id, $this->element->posy);
}
} }

View file

@ -23,15 +23,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
$string['gradeformelements'] = 'Grade form elements'; $string['gradeitem'] = 'Grade item';
$string['gradeformelements_help'] = 'These are the most common attributes shared between multiple customcert elements.<br /><br /> $string['gradeitem_help'] = 'The grade item you wish to display the grade of.';
<strong>Grade:</strong> The grade item you wish to display the grade of.<br />
<strong>Grade Format:</strong> The format you wish to use when displaying the grade.<br />
<strong>Font Size:</strong> This is the size of the font in points.<br />
<strong>Colour:</strong> This is the colour of the font.<br />
<strong>Position x:</strong> This is the position in pixels from the top left corner you wish the element to display in the x direction.<br />
<strong>Position Y:</strong> This is the position in pixels from the top left corner you wish the element to display in the y direction.<br />';
$string['gradeformat'] = 'Grade format'; $string['gradeformat'] = 'Grade format';
$string['gradeformat_help'] = 'The format you wish to use when displaying the grade.';
$string['gradeitem'] = 'Grade item'; $string['gradeitem'] = 'Grade item';
$string['gradepercent'] = 'Percentage Grade'; $string['gradepercent'] = 'Percentage Grade';
$string['gradepoints'] = 'Points Grade'; $string['gradepoints'] = 'Points Grade';

View file

@ -47,43 +47,35 @@ class customcert_element_grade extends customcert_element_base {
* @return array the form elements * @return array the form elements
*/ */
public function render_form_elements($mform) { public function render_form_elements($mform) {
// Keep track of the number of times these elements have been
// added, so we only add the help icon once.
static $numtimesadded = 0;
// The identifier. // The identifier.
$id = $this->element->id; $id = $this->element->id;
$gradeinfo = json_decode($this->element->data); $gradeitem = '';
$gradeitem = $gradeinfo->gradeitem; $gradeformat = '';
$gradeformat = $gradeinfo->gradeformat;
// The common group of elements. // Check if there is any data for this element.
$group = array(); if (!empty($this->element->data)) {
$group[] = $mform->createElement('select', 'gradeitem_' . $id, '', $this->get_grade_items()); $gradeinfo = json_decode($this->element->data);
$group[] = $mform->createElement('select', 'gradeformat_' . $id, '', $this->get_grade_format_options()); $gradeitem = $gradeinfo->gradeitem;
$group[] = $mform->createElement('select', 'font_' . $id, '', customcert_get_fonts()); $gradeformat = $gradeinfo->gradeformat;
$group[] = $mform->createElement('select', 'size_' . $id, '', customcert_get_font_sizes()); }
$group[] = $mform->createELement('text', 'colour_' . $id, '', array('size' => 10, 'maxlength' => 6));
$group[] = $mform->createElement('text', 'posx_' . $id, '', array('size' => 10));
$group[] = $mform->createElement('text', 'posy_' . $id, '', array('size' => 10));
// Add this group. // Add element header.
$mform->addElement('group', 'elementfieldgroup_' . $id, get_string('pluginname', 'customcertelement_grade'), $group, $mform->addElement('header', 'headerelement_' . $id, get_string('page', 'customcert', $this->element->pagenum) . " - " .
array(' ' . get_string('gradeformat', 'customcertelement_grade') . ' ', ' ' . get_string('font', 'customcert') . ' ', get_string('pluginname', 'customcertelement_grade'));
' ' . get_string('fontsize', 'customcert') . ' ', ' ' . get_string('colour', 'customcert') . ' ',
' ' . get_string('posx', 'customcert') . ' ', ' ' . get_string('posy', 'customcert') . ' '), false);
$this->set_form_element_types($mform); // The elements unique to this field.
$mform->addElement('select', 'gradeitem_' . $id, get_string('gradeitem', 'customcertelement_grade'), $this->get_grade_items());
$mform->addElement('select', 'gradeformat_' . $id, get_string('gradeformat', 'customcertelement_grade'), $this->get_grade_format_options());
parent::render_common_form_elements($mform);
$mform->setDefault('gradeitem_' . $id, $gradeitem); $mform->setDefault('gradeitem_' . $id, $gradeitem);
$mform->setDefault('gradeformat_' . $id, $gradeformat); $mform->setDefault('gradeformat_' . $id, $gradeformat);
if ($numtimesadded == 0) { // Add help buttons.
$mform->addHelpButton('elementfieldgroup_' . $id, 'gradeformelements', 'customcertelement_grade'); $mform->addHelpButton('gradeitem_' . $id, 'gradeitem', 'customcertelement_grade');
} $mform->addHelpButton('gradeformat_' . $id, 'gradeformat', 'customcertelement_grade');
$numtimesadded++;
} }
/** /**

View file

@ -38,6 +38,23 @@ class customcert_element_studentname extends customcert_element_base {
parent::__construct($element); parent::__construct($element);
} }
/**
* This function renders the form elements when adding a customcert element.
*
* @param stdClass $mform the edit_form instance.
* @return array the form elements
*/
public function render_form_elements($mform) {
// The identifier.
$id = $this->element->id;
// Add element header.
$mform->addElement('header', 'headerelement_' . $id, get_string('page', 'customcert', $this->element->pagenum) . " - "
. get_string('pluginname', 'customcertelement_studentname'));
parent::render_common_form_elements($mform);
}
/** /**
* Handles displaying the element on the pdf. * Handles displaying the element on the pdf.
* *

View file

@ -26,13 +26,6 @@
$string['addcertpage'] = 'Add another certificate page'; $string['addcertpage'] = 'Add another certificate page';
$string['addelement'] = 'Add element'; $string['addelement'] = 'Add element';
$string['colour'] = 'Colour';
$string['commonformelements'] = 'Common form elements';
$string['commonformelements_help'] = 'These are the most common attributes shared between multiple customcert elements.<br /><br />
<strong>Font Size:</strong> This is the size of the font in points.<br />
<strong>Colour:</strong> This is the colour of the font.<br />
<strong>Position x:</strong> This is the position in pixels from the top left corner you wish the element to display in the x direction.<br />
<strong>Position Y:</strong> This is the position in pixels from the top left corner you wish the element to display in the y direction.<br />';
$string['coursetimereq'] = 'Required minutes in course'; $string['coursetimereq'] = 'Required minutes in course';
$string['coursetimereq_help'] = 'Enter here the minimum amount of time, in minutes, that a student must be logged into the course before they will be able to receive the certificate.'; $string['coursetimereq_help'] = 'Enter here the minimum amount of time, in minutes, that a student must be logged into the course before they will be able to receive the certificate.';
$string['customcert:addinstance'] = 'Add a new custom certificate instance'; $string['customcert:addinstance'] = 'Add a new custom certificate instance';
@ -45,7 +38,11 @@ $string['deleteelementconfirm'] = 'Are you sure you want to delete this element?
$string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?';
$string['editcustomcert'] = 'Edit custom certificate'; $string['editcustomcert'] = 'Edit custom certificate';
$string['font'] = 'Font'; $string['font'] = 'Font';
$string['font_help'] = 'The font used when generating this element.';
$string['fontcolour'] = 'Colour';
$string['fontcolour_help'] = 'The colour of the font.';
$string['fontsize'] = 'Size'; $string['fontsize'] = 'Size';
$string['fontsize_help'] = 'The size of the font in points.';
$string['height'] = 'Height'; $string['height'] = 'Height';
$string['heightnotvalid'] = 'The height has to be a valid number.'; $string['heightnotvalid'] = 'The height has to be a valid number.';
$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['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.';
@ -65,7 +62,9 @@ $string['pluginadministration'] = 'Custom Certificate administration';
$string['pluginname'] = 'Custom Certificate'; $string['pluginname'] = 'Custom Certificate';
$string['portrait'] = 'Portrait'; $string['portrait'] = 'Portrait';
$string['posx'] = 'Position X'; $string['posx'] = 'Position X';
$string['posx_help'] = 'This is the position in pixels from the top left corner you wish the element to display in the x direction.';
$string['posy'] = 'Postion Y'; $string['posy'] = 'Postion Y';
$string['posy_help'] = 'This is the position in pixels from the top left corner you wish the element to display in the y direction.';
$string['uploadimage'] = 'Upload image'; $string['uploadimage'] = 'Upload image';
$string['width'] = 'Width'; $string['width'] = 'Width';
$string['widthnotvalid'] = 'The width has to be a valid number.'; $string['widthnotvalid'] = 'The width has to be a valid number.';