Introduced an image element and refactored the code in the process so less code was required in the new element

This commit is contained in:
Mark Nelson 2013-05-30 16:28:47 +08:00
parent 33a711b240
commit 0dc1ef19c9
9 changed files with 482 additions and 200 deletions

View file

@ -57,7 +57,7 @@ class mod_customcert_edit_form extends moodleform {
* Form definition.
*/
function definition() {
global $CFG, $DB, $OUTPUT;
global $DB;
$this->id = $this->_customdata['customcertid'];
$this->filemanageroptions = array('maxbytes' => $this->_customdata['course']->maxbytes,
@ -118,10 +118,10 @@ class mod_customcert_edit_form extends moodleform {
$element = $mform->getElement('orientation_' . $p->id);
$element->setValue($p->orientation);
// Set the width.
$element = $mform->getElement('width_' . $p->id);
$element = $mform->getElement('pagewidth_' . $p->id);
$element->setValue($p->width);
// Set the height.
$element = $mform->getElement('height_' . $p->id);
$element = $mform->getElement('pageheight_' . $p->id);
$element->setValue($p->height);
}
}
@ -140,20 +140,20 @@ class mod_customcert_edit_form extends moodleform {
// Go through the data and check any width or height values.
foreach ($data as $key => $value) {
if (strpos($key, 'width_') !== false) {
$page = str_replace('width_', '', $key);
$widthid = 'width_' . $page;
if (strpos($key, 'pagewidth_') !== false) {
$page = str_replace('pagewidth_', '', $key);
$widthid = 'pagewidth_' . $page;
// Validate that the width is a valid value.
if ((!isset($data[$widthid])) || (!is_numeric($data[$widthid])) || ($data[$widthid] <= 0)) {
$errors[$widthid] = get_string('widthnotvalid', 'customcert');
$errors[$widthid] = get_string('invalidwidth', 'customcert');
}
}
if (strpos($key, 'height_') !== false) {
$page = str_replace('height_', '', $key);
$heightid = 'height_' . $page;
if (strpos($key, 'pageheight_') !== false) {
$page = str_replace('pageheight_', '', $key);
$heightid = 'pageheight_' . $page;
// Validate that the height is a valid value.
if ((!isset($data[$heightid])) || (!is_numeric($data[$heightid])) || ($data[$heightid] <= 0)) {
$errors[$heightid] = get_string('heightnotvalid', 'customcert');
$errors[$heightid] = get_string('invalidheight', 'customcert');
}
}
}
@ -179,57 +179,54 @@ class mod_customcert_edit_form extends moodleform {
// Create the form object.
$mform =& $this->_form;
$pageid = $page->id;
$pagenum = $page->pagenumber;
$mform->addElement('header', 'page_' . $pageid, get_string('page', 'customcert', $pagenum));
$mform->addElement('header', 'page_' . $page->id, get_string('page', 'customcert', $page->pagenumber));
// Place the ordering arrows.
// Only display the move up arrow if it is not the first.
if ($pagenum > 1) {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'moveup' => $pageid));
if ($page->pagenumber > 1) {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'moveup' => $page->id));
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup'))));
}
// Only display the move down arrow if it is not the last.
if ($pagenum < $this->numpages) {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'movedown' => $pageid));
if ($page->pagenumber < $this->numpages) {
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'movedown' => $page->id));
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))));
}
$orientationoptions = array('L' => get_string('landscape', 'customcert'),
'P' => get_string('portrait', 'customcert'));
$mform->addElement('select', 'orientation_' . $pageid, get_string('orientation', 'customcert'), $orientationoptions);
$mform->setDefault('orientation_' . $pageid, 'P');
$mform->addHelpButton('orientation_' . $pageid, 'orientation', 'customcert');
$mform->addElement('select', 'orientation_' . $page->id, get_string('orientation', 'customcert'), $orientationoptions);
$mform->setDefault('orientation_' . $page->id, 'P');
$mform->addHelpButton('orientation_' . $page->id, 'orientation', 'customcert');
$mform->addElement('text', 'width_' . $pageid, get_string('width', 'customcert'));
$mform->setType('width_' . $pageid, PARAM_INT);
$mform->setDefault('width_' . $pageid, '210');
$mform->addRule('width_' . $pageid, null, 'required', null, 'client');
$mform->addHelpButton('width_' . $pageid, 'width', 'customcert');
$mform->addElement('text', 'pagewidth_' . $page->id, get_string('width', 'customcert'));
$mform->setType('pagewidth_' . $page->id, PARAM_INT);
$mform->setDefault('pagewidth_' . $page->id, '210');
$mform->addRule('pagewidth_' . $page->id, null, 'required', null, 'client');
$mform->addHelpButton('pagewidth_' . $page->id, 'width', 'customcert');
$mform->addElement('text', 'height_' . $pageid, get_string('height', 'customcert'));
$mform->setType('height_' . $pageid, PARAM_INT);
$mform->setDefault('height_' . $pageid, '297');
$mform->addRule('height_' . $pageid, null, 'required', null, 'client');
$mform->addHelpButton('height_' . $pageid, 'height', 'customcert');
$mform->addElement('text', 'pageheight_' . $page->id, get_string('height', 'customcert'));
$mform->setType('pageheight_' . $page->id, PARAM_INT);
$mform->setDefault('pageheight_' . $page->id, '297');
$mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client');
$mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert');
$group = array();
$group[] = $mform->createElement('select', 'element_' . $pageid, '', customcert_get_elements());
$group[] = $mform->createElement('submit', 'addelement_' . $pageid, get_string('addelement', 'customcert'));
$group[] = $mform->createElement('select', 'element_' . $page->id, '', customcert_get_elements());
$group[] = $mform->createElement('submit', 'addelement_' . $page->id, get_string('addelement', 'customcert'));
$mform->addElement('group', 'elementgroup', '', $group, '', false);
$mform->addElement('submit', 'addcertpage_' . $pageid, get_string('addcertpage', 'customcert'));
$mform->addElement('submit', 'addcertpage_' . $page->id, get_string('addcertpage', 'customcert'));
// Add option to delete this page if there is more than one page.
if ($this->numpages > 1) {
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton')));
$mform->addElement('submit', 'deletecertpage_' . $pageid, get_string('deletecertpage', 'customcert'));
$mform->addElement('submit', 'deletecertpage_' . $page->id, get_string('deletecertpage', 'customcert'));
$mform->addElement('html', html_writer::end_tag('div'));
}
// Check if there are elements to add.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'sequence ASC')) {
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
// Get the total number of elements.
$numelements = count($elements);
// Loop through and add the ones present.
@ -239,7 +236,7 @@ class mod_customcert_edit_form extends moodleform {
// this is the case we do not want to render these elements as an error will occur.
if (file_exists($classfile)) {
// Add element header.
$mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " .
$mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $page->pagenumber) . " - " .
get_string('pluginname', 'customcertelement_' . $element->element));
// We do not need to expand these elements if the modified time is greater than the created time as it
// means the values have already been altered by the user - ie. the element has not just been created.
@ -259,7 +256,7 @@ class mod_customcert_edit_form extends moodleform {
$mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))));
}
// Add the page number to the element so we can use within the element.
$element->pagenum = $pagenum;
$element->pagenum = $page->pagenumber;
// Get the classname.
$classname = "customcert_element_{$element->element}";
$e = new $classname($element);

View file

@ -44,9 +44,6 @@ class customcert_element_date extends customcert_element_base {
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements($mform) {
// The identifier.
$id = $this->element->id;
$dateitem = '';
$dateformat = '';
@ -62,17 +59,17 @@ class customcert_element_date extends customcert_element_base {
$dateoptions['1'] = get_string('issueddate', 'certificate');
$dateoptions['2'] = get_string('completiondate', 'certificate');
$dateoptions = $dateoptions + customcert_element_grade::get_grade_items();
$mform->addElement('select', 'dateitem_' . $id, get_string('dateitem', 'customcertelement_date'), $dateoptions);
$mform->addElement('select', 'dateformat_' . $id, get_string('dateformat', 'customcertelement_date'), customcert_element_date::get_date_formats());
$mform->addElement('select', 'dateitem_' . $this->element->id, get_string('dateitem', 'customcertelement_date'), $dateoptions);
$mform->addElement('select', 'dateformat_' . $this->element->id, get_string('dateformat', 'customcertelement_date'), customcert_element_date::get_date_formats());
parent::render_form_elements($mform);
$mform->setDefault('dateitem_' . $id, $dateitem);
$mform->setDefault('dateformat_' . $id, $dateformat);
$mform->setDefault('dateitem_' . $this->element->id, $dateitem);
$mform->setDefault('dateformat_' . $this->element->id, $dateformat);
// Add help buttons.
$mform->addHelpButton('dateitem_' . $id, 'dateitem', 'customcertelement_date');
$mform->addHelpButton('dateformat_' . $id, 'dateformat', 'customcertelement_date');
$mform->addHelpButton('dateitem_' . $this->element->id, 'dateitem', 'customcertelement_date');
$mform->addHelpButton('dateformat_' . $this->element->id, 'dateformat', 'customcertelement_date');
}
/**
@ -83,13 +80,10 @@ class customcert_element_date extends customcert_element_base {
* @return string the json encoded array
*/
public function save_unique_data($data) {
// The identifier.
$id = $this->element->id;
// Get the date item and format from the form.
$dateitem = 'dateitem_' . $id;
$dateitem = 'dateitem_' . $this->element->id;
$dateitem = $data->$dateitem;
$dateformat = 'dateformat_' . $id;
$dateformat = 'dateformat_' . $this->element->id;
$dateformat = $data->$dateformat;
// Array of data we will be storing in the database.

View file

@ -55,48 +55,16 @@ class customcert_element_base {
public static function add_element($element, $pageid) {
global $DB;
// Set the time as a variable.
$time = time();
$data = new stdClass();
$data->pageid = $pageid;
$data->element = $element;
$data = customcert_element_base::get_required_attributes($element, $pageid);
$data->font = 'times';
$data->size = '12';
$data->colour = '#000000';
$data->posx = '250';
$data->posy = '250';
$data->sequence = customcert_element_base::get_element_sequence($pageid);
$data->timecreated = $time;
$data->timemodified = $time;
$data->posx = '0';
$data->posy = '0';
$DB->insert_record('customcert_elements', $data);
}
/**
* Returns the sequence on a specified customcert page for a
* newly created element.
*
* @param int $pageid the id of the page we are adding this element to
* @return int the element number
*/
public static function get_element_sequence($pageid) {
global $DB;
// Set the sequence of the element we are creating.
$sequence = 1;
// Check if there already elements that exist, if so, overwrite value.
$sql = "SELECT MAX(sequence) as maxsequence
FROM {customcert_elements}
WHERE pageid = :id";
// Get the current max sequence on this page and add 1 to get the new sequence.
if ($maxseq = $DB->get_record_sql($sql, array('id' => $pageid))) {
$sequence = $maxseq->maxsequence + 1;
}
return $sequence;
}
/**
* This function renders the form elements when adding a customcert element.
* Can be overridden if more functionality is needed.
@ -104,39 +72,10 @@ class customcert_element_base {
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements($mform) {
// The identifier.
$id = $this->element->id;
// Commonly used string.
$strrequired = get_string('required');
// The common elements.
$mform->addElement('select', 'font_' . $id, get_string('font', 'customcert'), customcert_get_fonts());
$mform->addElement('select', 'size_' . $id, get_string('fontsize', 'customcert'), customcert_get_font_sizes());
$mform->addElement('customcert_colourpicker', 'colour_' . $id, get_string('fontcolour', 'customcert'));
$mform->addElement('text', 'posx_' . $id, get_string('posx', 'customcert'), array('size' => 10));
$mform->addElement('text', 'posy_' . $id, get_string('posy', 'customcert'), array('size' => 10));
// 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 that this is a valid colour.
$mform->setType('posx_' . $id, PARAM_INT);
$mform->setType('posy_' . $id, PARAM_INT);
// 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);
// 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');
// Render the common elements.
$this->render_form_elements_font($mform);
$this->render_form_elements_colour($mform);
$this->render_form_elements_position($mform);
}
/**
@ -148,31 +87,12 @@ class customcert_element_base {
* @return array the validation errors
*/
public function validate_form_elements($data, $files) {
// Array to return the errors.
$errors = array();
// The identifier.
$id = $this->element->id;
// Validate the colour.
$colour = 'colour_' . $id;
$colourdata = $data[$colour];
if (!$this->validate_colour($colourdata)) {
$errors[$colour] = get_string('invalidcolour', 'customcert');
}
// Get position X.
$posx = 'posx_' . $id;
// 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');
}
// Get position Y.
$posy = 'posy_' . $id;
// 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');
}
// Common validation methods.
$errors += $this->validate_form_elements_colour($data);
$errors += $this->validate_form_elements_position($data);
return $errors;
}
@ -186,26 +106,22 @@ class customcert_element_base {
public function save_form_elements($data) {
global $DB;
// The identifier.
$id = $this->element->id;
// Get the name of the fields we want from the form.
$datainfo = $this->save_unique_data($data);
$font = 'font_' . $id;
$size = 'size_' . $id;
$colour = 'colour_' . $id;
$posx = 'posx_' . $id;
$posy = 'posy_' . $id;
$font = 'font_' . $this->element->id;
$size = 'size_' . $this->element->id;
$colour = 'colour_' . $this->element->id;
$posx = 'posx_' . $this->element->id;
$posy = 'posy_' . $this->element->id;
// Get the data from the form.
$element = new stdClass();
$element->id = $id;
$element->data = $datainfo;
$element->font = (!empty($data->$font)) ? $data->$font : null;
$element->size = (!empty($data->$size)) ? $data->$size : null;
$element->colour = (!empty($data->$colour)) ? $data->$colour : null;
$element->posx = (!empty($data->$posx)) ? $data->$posx : null;
$element->posy = (!empty($data->$posy)) ? $data->$posy : null;
$element->id = $this->element->id;
$element->data = $this->save_unique_data($data);
$element->font = (isset($data->$font)) ? $data->$font : null;
$element->size = (isset($data->$size)) ? $data->$size : null;
$element->colour = (isset($data->$colour)) ? $data->$colour : null;
$element->posx = (isset($data->$posx)) ? $data->$posx : null;
$element->posy = (isset($data->$posy)) ? $data->$posy : null;
$element->timemodified = time();
// Ok, now update record in the database.
@ -284,6 +200,144 @@ class customcert_element_base {
return $DB->delete_records('customcert_elements', array('id' => $this->element->id));
}
/**
* Helper function responsible for setting the default values for the required variables
* for an element. This should be set the same way regardless of elements.
*
* @param string $element the name of the element
* @param int $pageid the page id we are saving it to
* @return stdClass the required attributes
*/
public static function get_required_attributes($element, $pageid) {
// Set the time as a variable.
$time = time();
$data = new stdClass();
$data->pageid = $pageid;
$data->element = $element;
$data->sequence = customcert_element_base::get_element_sequence($pageid);
$data->timecreated = $time;
$data->timemodified = $time;
return $data;
}
/**
* Helper function that returns the sequence on a specified customcert page for a
* newly created element.
*
* @param int $pageid the id of the page we are adding this element to
* @return int the element number
*/
public static function get_element_sequence($pageid) {
global $DB;
// Set the sequence of the element we are creating.
$sequence = 1;
// Check if there already elements that exist, if so, overwrite value.
$sql = "SELECT MAX(sequence) as maxsequence
FROM {customcert_elements}
WHERE pageid = :id";
// Get the current max sequence on this page and add 1 to get the new sequence.
if ($maxseq = $DB->get_record_sql($sql, array('id' => $pageid))) {
$sequence = $maxseq->maxsequence + 1;
}
return $sequence;
}
/**
* Helper function to render the font elements.
*
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements_font($mform) {
$mform->addElement('select', 'font_' . $this->element->id, get_string('font', 'customcert'), customcert_get_fonts());
$mform->setType('font_' . $this->element->id, PARAM_TEXT);
$mform->setDefault('font_' . $this->element->id, $this->element->font);
$mform->addHelpButton('font_' . $this->element->id, 'font', 'customcert');
$mform->addElement('select', 'size_' . $this->element->id, get_string('fontsize', 'customcert'), customcert_get_font_sizes());
$mform->setType('size_' . $this->element->id, PARAM_INT);
$mform->setDefault('size_' . $this->element->id, $this->element->size);
$mform->addHelpButton('size_' . $this->element->id, 'fontsize', 'customcert');
}
/**
* Helper function to render the colour elements.
*
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements_colour($mform) {
$mform->addElement('customcert_colourpicker', 'colour_' . $this->element->id, get_string('fontcolour', 'customcert'));
$mform->setType('colour_' . $this->element->id, PARAM_RAW); // Need to validate that this is a valid colour.
$mform->setDefault('colour_' . $this->element->id, $this->element->colour);
$mform->addHelpButton('colour_' . $this->element->id, 'fontcolour', 'customcert');
}
/**
* Helper function to render the position elements.
*
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements_position($mform) {
$mform->addElement('text', 'posx_' . $this->element->id, get_string('posx', 'customcert'), array('size' => 10));
$mform->setType('posx_' . $this->element->id, PARAM_INT);
$mform->setDefault('posx_' . $this->element->id, $this->element->posx);
$mform->addHelpButton('posx_' . $this->element->id, 'posx', 'customcert');
$mform->addElement('text', 'posy_' . $this->element->id, get_string('posy', 'customcert'), array('size' => 10));
$mform->setType('posy_' . $this->element->id, PARAM_INT);
$mform->setDefault('posy_' . $this->element->id, $this->element->posy);
$mform->addHelpButton('posy_' . $this->element->id, 'posy', 'customcert');
}
/**
* Helper function to performs validation on the colour element.
*
* @param array $data the submitted data
* @return array the validation errors
*/
public function validate_form_elements_colour($data) {
$errors = array();
// Validate the colour.
$colour = 'colour_' . $this->element->id;
$colourdata = $data[$colour];
if (!$this->validate_colour($colourdata)) {
$errors[$colour] = get_string('invalidcolour', 'customcert');
}
return $errors;
}
/**
* Helper function to performs validation on the position elements.
*
* @param array $data the submitted data
* @return array the validation errors
*/
public function validate_form_elements_position($data) {
$errors = array();
// Get position X.
$posx = 'posx_' . $this->element->id;
// 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');
}
// Get position Y.
$posy = 'posy_' . $this->element->id;
// 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;
}
/**
* Sets the font for the element.
*
@ -314,7 +368,6 @@ class customcert_element_base {
$font = substr($font, 0, -1);
$attr .= 'B';
}
// Check if this font is going to be bold.
$pdf->setFont($font, $attr, $this->element->size);
}

View file

@ -52,9 +52,6 @@ class customcert_element_grade extends customcert_element_base {
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements($mform) {
// The identifier.
$id = $this->element->id;
$gradeitem = '';
$gradeformat = '';
@ -71,17 +68,17 @@ class customcert_element_grade extends customcert_element_base {
$gradeitems = $gradeitems + customcert_element_grade::get_grade_items();
// The grade items.
$mform->addElement('select', 'gradeitem_' . $id, get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
$mform->addElement('select', 'gradeitem_' . $this->element->id, get_string('gradeitem', 'customcertelement_grade'), $gradeitems);
$mform->setType('gradeitem_', PARAM_INT);
$mform->setDefault('gradeitem_' . $id, $gradeitem);
$mform->addHelpButton('gradeitem_' . $id, 'gradeitem', 'customcertelement_grade');
$mform->setDefault('gradeitem_' . $this->element->id, $gradeitem);
$mform->addHelpButton('gradeitem_' . $this->element->id, 'gradeitem', 'customcertelement_grade');
// The grade format.
$mform->addElement('select', 'gradeformat_' . $id, get_string('gradeformat', 'customcertelement_grade'),
$mform->addElement('select', 'gradeformat_' . $this->element->id, get_string('gradeformat', 'customcertelement_grade'),
customcert_element_grade::get_grade_format_options());
$mform->setType('gradeformat_', PARAM_INT);
$mform->setDefault('gradeformat_' . $id, $gradeformat);
$mform->addHelpButton('gradeformat_' . $id, 'gradeformat', 'customcertelement_grade');
$mform->setDefault('gradeformat_' . $this->element->id, $gradeformat);
$mform->addHelpButton('gradeformat_' . $this->element->id, 'gradeformat', 'customcertelement_grade');
parent::render_form_elements($mform);
}
@ -94,13 +91,10 @@ class customcert_element_grade extends customcert_element_base {
* @return string the json encoded array
*/
public function save_unique_data($data) {
// The identifier.
$id = $this->element->id;
// Get the grade item and format from the form.
$gradeitem = 'gradeitem_' . $id;
$gradeitem = 'gradeitem_' . $this->element->id;
$gradeitem = $data->$gradeitem;
$gradeformat = 'gradeformat_' . $id;
$gradeformat = 'gradeformat_' . $this->element->id;
$gradeformat = $data->$gradeformat;
// Array of data we will be storing in the database.

View file

@ -0,0 +1,33 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'customcertelement_image', language 'en'.
*
* @package customcertelement_image
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['height'] = 'Height';
$string['height_help'] = 'Height of the image in the page. If equal to zero, it is automatically calculated.';
$string['image'] = 'Image';
$string['invalidheight'] = 'The height has to be a valid number greater than 0.';
$string['invalidwidth'] = 'The width has to be a valid number greater than 0.';
$string['pluginname'] = 'Image';
$string['width'] = 'Width';
$string['width_help'] = 'Width of the image in the page. If equal to zero, it is automatically calculated.';

202
elements/image/lib.php Normal file
View file

@ -0,0 +1,202 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The image elements core interaction API.
*
* @package customcertelement_image
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
class customcert_element_image extends customcert_element_base {
/**
* Constructor.
*
* @param stdClass $element the element data
*/
function __construct($element) {
parent::__construct($element);
}
/**
* This function is responsible for adding the element for the first time
* to the database when no data has yet been specified, default values set.
* Can be overridden if more functionality is needed.
*
* @param string $element the name of the element
* @param int $pageid the page id we are saving it to
*/
public static function add_element($element, $pageid) {
global $DB;
$data = customcert_element_base::get_required_attributes($element, $pageid);
$data->width = '0';
$data->height = '0';
$data->posx = '0';
$data->posy = '0';
$DB->insert_record('customcert_elements', $data);
}
/**
* This function renders the form elements when adding a customcert element.
*
* @param stdClass $mform the edit_form instance.
*/
public function render_form_elements($mform) {
$image = '';
$width = '0';
$height = '0';
// Check if there is any data for this element.
if (!empty($this->element->data)) {
$imageinfo = json_decode($this->element->data);
$image = $imageinfo->pathnamehash;
$width = $imageinfo->width;
$height = $imageinfo->height;
}
$mform->addElement('select', 'image_' . $this->element->id, get_string('image', 'customcertelement_image'), self::get_images());
$mform->setDefault('image_' . $this->element->id, $image);
$mform->addElement('text', 'imagewidth_' . $this->element->id, get_string('width', 'customcertelement_image'), array('size' => 10));
$mform->setType('imagewidth_' . $this->element->id, PARAM_INT);
$mform->setDefault('imagewidth_' . $this->element->id, $width);
$mform->addHelpButton('imagewidth_' . $this->element->id, 'width', 'customcertelement_image');
$mform->addElement('text', 'imageheight_' . $this->element->id, get_string('height', 'customcertelement_image'), array('size' => 10));
$mform->setType('imageheight_' . $this->element->id, PARAM_INT);
$mform->setDefault('imageheight_' . $this->element->id, $height);
$mform->addHelpButton('imageheight_' . $this->element->id, 'height', 'customcertelement_image');
parent::render_form_elements_position($mform);
}
/**
* Performs validation on the element values.
*
* @param array $data the submitted data
* @param array $files the submitted files
* @return array the validation errors
*/
public function validate_form_elements($data, $files) {
// Array to return the errors.
$errors = array();
// Get width.
$width = 'imagewidth_' . $this->element->id;
// Check if width is not set, or not numeric or less than 0.
if ((!isset($data[$width])) || (!is_numeric($data[$width])) || ($data[$width] < 0)) {
$errors[$width] = get_string('invalidwidth', 'customcertelement_image');
}
// Get height.
$height = 'imageheight_' . $this->element->id;
// Check if height is not set, or not numeric or less than 0.
if ((!isset($data[$height])) || (!is_numeric($data[$height])) || ($data[$height] < 0)) {
$errors[$height] = get_string('invalidheight', 'customcertelement_image');
}
// Validate the position.
$errors += $this->validate_form_elements_position($data);
return $errors;
}
/**
* This will handle how form data will be saved into the data column in the
* customcert_elements table.
*
* @param stdClass $data the form data.
* @return string the json encoded array
*/
public function save_unique_data($data) {
// Get the date item and format from the form.
$image = 'image_' . $this->element->id;
$image = $data->$image;
$width = 'imagewidth_' . $this->element->id;
$width = $data->$width;
$height = 'imageheight_' . $this->element->id;
$height = $data->$height;
// Array of data we will be storing in the database.
$arrtostore = array(
'pathnamehash' => $image,
'width' => $width,
'height' => $height
);
return json_encode($arrtostore);
}
/**
* Handles rendering the element on the pdf.
*
* @param stdClass $pdf the pdf object
* @param int $userid
*/
public function render($pdf, $userid) {
global $CFG;
// If there is no element data, we have nothing to display.
if (empty($this->element->data)) {
return;
}
$imageinfo = json_decode($this->element->data);
$image = $imageinfo->pathnamehash;
$width = $imageinfo->width;
$height = $imageinfo->height;
// Get the image.
$fs = get_file_storage();
if ($file = $fs->get_file_by_hash($image)) {
$contenthash = $file->get_contenthash();
$l1 = $contenthash[0] . $contenthash[1];
$l2 = $contenthash[2] . $contenthash[3];
$location = $CFG->dataroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash;
$pdf->Image($location, $this->element->posx, $this->element->posy, $width, $height);
}
}
/**
* Return the list of possible images to use.
*
* @return array the list of images that can be used.
*/
public static function get_images() {
// Create file storage object.
$fs = get_file_storage();
// The array used to store the images.
$arrfiles = array();
$arrfiles[0] = get_string('noimage', 'customcert');
if ($files = $fs->get_area_files(context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) {
foreach ($files as $hash => $file) {
$arrfiles[$hash] = $file->get_filename();
}
}
return $arrfiles;
}
}

View file

@ -0,0 +1,30 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the version information for the image plugin.
*
* @package customcertelement_image
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
$plugin->version = 2013053000;
$plugin->requires = 2013040500; // Requires this Moodle version.
$plugin->component = 'customcertelement_image';

View file

@ -50,10 +50,11 @@ $string['fontsize'] = 'Size';
$string['fontsize_help'] = 'The size of the font in points.';
$string['getcustomcert'] = 'Get your custom certificate';
$string['height'] = 'Height';
$string['heightnotvalid'] = 'The height has to be a valid number greater than 0.';
$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['invalidcolour'] = 'Invalid colour chosen, please enter a valid HTML colour name, or a six-digit, or three-digit hexadecimal colour.';
$string['invalidposition'] = 'Please select a positive number for position {$a}.';
$string['invalidheight'] = 'The height 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['load'] = 'Load';
@ -89,5 +90,4 @@ $string['templatenameexists'] = 'That template name is currently in use, please
$string['uploadimage'] = 'Upload image';
$string['viewcustomcertissues'] = 'View {$a} issued custom certificates';
$string['width'] = 'Width';
$string['widthnotvalid'] = 'The width has to be a valid number greater than 0.';
$string['width_help'] = 'This is the width of the certificate PDF in mm. For reference an A4 piece of paper is 210mm wide and a letter is 216mm wide.';

25
lib.php
View file

@ -353,27 +353,6 @@ function customcert_extend_settings_navigation(settings_navigation $settings, na
return $customcertnode->trim_if_empty();
}
/**
* Return the list of possible images to use.
*
* @return array the list of images that can be used.
*/
function customcert_get_images() {
// Create file storage object.
$fs = get_file_storage();
// The array used to store the images.
$arrfiles = array();
$arrfiles[0] = get_string('noimage', 'customcert');
if ($files = $fs->get_area_files(context_system::instance()->id, 'mod_customcert', 'image', false, 'filename', false)) {
foreach ($files as $hash => $file) {
$arrfiles[$hash] = $file->get_filename();
}
}
return $arrfiles;
}
/**
* Handles uploading an image for the customcert module.
*
@ -509,8 +488,8 @@ function customcert_save_page_data($data) {
foreach ($pages as $page) {
// Get the name of the fields we want from the form.
$orientation = 'orientation_' . $page->id;
$width = 'width_' . $page->id;
$height = 'height_' . $page->id;
$width = 'pagewidth_' . $page->id;
$height = 'pageheight_' . $page->id;
// Create the page data to update the DB with.
$p = new stdClass();
$p->id = $page->id;