From 9d0eb727e5aea54f274a24ea0fc9e2f82bd3b9ae Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 20 Feb 2013 20:21:57 +0800 Subject: [PATCH] Introduced customisable elements to the customcert Elements are sub-plugins that have the same abilities as a core plugin, such as creating tables, capabilities and performing upgrades. Currently elements can define - * What additional form elements are rendered when adding the element to the customcert customisation page. * How the data from the additional form elements is validated and saved. * How the element is rendered on the PDF. * How the element handles removing data when it is deleted. --- db/access.php | 2 + db/install.xml | 31 +-- db/subplugins.php | 29 +++ edit.php | 57 +++- edit_form.php | 129 +++++----- elements/element.class.php | 241 +++++++++++++++++ .../lang/en/customcertelement_studentname.php | 26 ++ elements/studentname/lib.php | 53 ++++ elements/studentname/version.php | 29 +++ lang/en/customcert.php | 24 +- lib.php | 243 ++++++++++++++---- mod_form.php | 1 - version.php | 2 +- 13 files changed, 723 insertions(+), 144 deletions(-) create mode 100644 db/subplugins.php create mode 100644 elements/element.class.php create mode 100644 elements/studentname/lang/en/customcertelement_studentname.php create mode 100644 elements/studentname/lib.php create mode 100644 elements/studentname/version.php diff --git a/db/access.php b/db/access.php index f714dd2..77540ee 100644 --- a/db/access.php +++ b/db/access.php @@ -24,6 +24,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +defined('MOODLE_INTERNAL') || die(); + $capabilities = array( 'mod/customcert:addinstance' => array( diff --git a/db/install.xml b/db/install.xml index d36610c..868b773 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -32,15 +32,14 @@ - +
- - - + + @@ -49,20 +48,22 @@
- +
- - - - - - - + + + + + + + + + - - + +
diff --git a/db/subplugins.php b/db/subplugins.php new file mode 100644 index 0000000..fa5e8b3 --- /dev/null +++ b/db/subplugins.php @@ -0,0 +1,29 @@ +. + +/** + * Definition of sub-plugins. + * + * @package mod + * @subpackage customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$subplugins = array('customcertelement' => 'mod/customcert/elements'); diff --git a/edit.php b/edit.php index 8a8c190..7a49ec6 100644 --- a/edit.php +++ b/edit.php @@ -18,7 +18,8 @@ /** * Edit the customcert settings. * - * @package mod_customcert + * @package mod + * @subpackage customcert * @copyright Mark Nelson * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -26,11 +27,13 @@ require_once('../../config.php'); require_once($CFG->dirroot . '/mod/customcert/lib.php'); require_once($CFG->dirroot . '/mod/customcert/edit_form.php'); +require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); $cmid = required_param('cmid', PARAM_INT); $moveup = optional_param('moveup', 0, PARAM_INT); $movedown = optional_param('movedown', 0, PARAM_INT); -$delete = optional_param('delete', 0, PARAM_INT); +$deleteelement = optional_param('deleteelement', 0, PARAM_INT); +$deletepage = optional_param('deletepage', 0, PARAM_INT); $confirm = optional_param('confirm', 0, PARAM_INT); $cm = get_coursemodule_from_id('customcert', $cmid, 0, false, MUST_EXIST); @@ -59,8 +62,12 @@ if ((!empty($moveup)) || (!empty($movedown))) { $DB->set_field('customcert_pages', 'pagenumber', $swapcertpage->pagenumber, array('id' => $movecertpage->id)); $DB->set_field('customcert_pages', 'pagenumber', $movecertpage->pagenumber, array('id' => $swapcertpage->id)); } -} else if ((!empty($delete)) && (!empty($confirm))) { - customcert_delete_page($delete); +} else if ((!empty($deletepage)) && (!empty($confirm))) { // Check if we are deleting a page. + customcert_delete_page($deletepage); +} else if ((!empty($deleteelement)) && (!empty($confirm))) { // Check if we are deleting an element. + // Ensure element exists and delete it. + $element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST); + customcert_delete_element($element); } $mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id, @@ -74,32 +81,64 @@ if ($data = $mform->get_data()) { // Save any page data. customcert_save_page_data($data); - // Check if they requested to delete a page. + // Flag to determine if we are deleting anything. + $deleting = false; + + // Loop through the data. foreach ($data as $key => $value) { + // Check if they requested to delete a page. if (strpos($key, 'deletecertpage_') !== false) { + // Get the pageid. $pageid = str_replace('deletecertpage_', '', $key); + // Set deletion flag to true. + $deleting = true; + // Create the message. + $message = get_string('deletepageconfirm', 'customcert'); // Create the link options. $nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id)); $yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id, - 'delete' => $pageid, + 'deletepage' => $pageid, 'confirm' => 1, 'sesskey' => sesskey())); + } else if (strpos($key, 'deleteelement_') !== false) { // Check if they requested to delete a page element. + // Get the element id. + $elementid = str_replace('deleteelement_', '', $key); + // Set deletion flag to true. + $deleting = true; + // Create the message. + $message = get_string('deleteelementconfirm', 'customcert'); + // Create the link options. + $nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id)); + $yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id, + 'deleteelement' => $elementid, + 'confirm' => 1, + 'sesskey' => sesskey())); + } else if (strpos($key, 'addelement_') !== false) { // Check if they chose to add an element to a page. + // Get the page id. + $pageid = str_replace('addelement_', '', $key); + // Get the element. + $element = "element_" . $pageid; + $element = $data->$element; + customcert_add_element($element, $pageid); + } + + // Check if we are deleting either a page or an element. + if ($deleting) { // Show a confirmation page. - $strheading = get_string('deletecertpage', 'customcert'); + $strheading = get_string('deleteconfirm', 'customcert'); $PAGE->navbar->add($strheading); $PAGE->set_title($strheading); $PAGE->set_heading($COURSE->fullname); $PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid)); echo $OUTPUT->header(); echo $OUTPUT->heading($strheading); - $message = get_string('deletecertpageconfirm', 'customcert'); echo $OUTPUT->confirm($message, $yesurl, $nourl); echo $OUTPUT->footer(); exit(); } } - // If they chose to add another page, enter it into database. + // Check if they chose to add a page. if (!empty($data->addcertpage)) { customcert_add_page($data); } diff --git a/edit_form.php b/edit_form.php index a3047ec..98dcae3 100644 --- a/edit_form.php +++ b/edit_form.php @@ -35,32 +35,27 @@ class mod_customcert_edit_form extends moodleform { /** * The instance id. */ - protected $id = null; + private $id = null; /** * The course. */ - protected $course = null; + private $course = null; /** * The total number of pages for this cert. */ - protected $numpages = 1; - - /** - * The orientation options. - */ - protected $orientationoptions = array(); - - /** - * The image options. - */ - protected $imageoptions = array(); + private $numpages = 1; /** * The filemanager options. */ - protected $filemanageroptions = array(); + private $filemanageroptions = array(); + + /** + * The array of element objects loaded on this form. + */ + private $elementobjects = array(); /** * Form definition. @@ -69,9 +64,6 @@ class mod_customcert_edit_form extends moodleform { global $CFG, $DB, $OUTPUT; $this->id = $this->_customdata['customcertid']; - $this->orientationoptions = array('L' => get_string('landscape', 'customcert'), - 'P' => get_string('portrait', 'customcert')); - $this->imageoptions = customcert_get_images(); $this->filemanageroptions = array('maxbytes' => $this->_customdata['course']->maxbytes, 'subdirs' => 1, 'accepted_types' => 'image'); @@ -135,13 +127,6 @@ class mod_customcert_edit_form extends moodleform { // Set the height. $element = $mform->getElement('height_'.$p->id); $element->setValue($p->height); - // Set the background image. - $element = $mform->getElement('backgroundimage_'.$p->id); - $element->setValue($p->backgroundimage); - // Now get the page text fields. - if ($textfields = $DB->get_records('customcert_text_fields', array())) { - - } } } } @@ -161,20 +146,27 @@ class mod_customcert_edit_form extends moodleform { foreach ($data as $key => $value) { if (strpos($key, 'width_') !== false) { $page = str_replace('width_', '', $key); - // Validate that the weight is a valid value. - if (!isset($data['width_'.$page]) || !is_number($data['width_'.$page])) { - $errors['width_'.$page] = get_string('widthnotvalid', 'customcert'); + // Validate that the width is a valid value. + if (!isset($data['width_' . $page]) || !is_number($data['width_' . $page])) { + $errors['width_' . $page] = get_string('widthnotvalid', 'customcert'); } } if (strpos($key, 'height_') !== false) { $page = str_replace('height_', '', $key); // Validate that the height is a valid value. - if (!isset($data['height_'.$page]) || !is_number($data['height_'.$page])) { - $errors['height_'.$page] = get_string('heightnotvalid', 'customcert'); + if (!isset($data['height_' . $page]) || !is_number($data['height_' . $page])) { + $errors['height_' . $page] = get_string('heightnotvalid', 'customcert'); } } } + // Go through each element and perform validation. + if (!empty($this->elementobjects)) { + foreach ($this->elementobjects as $e) { + $errors += $e->validate_form_elements($data, $files); + } + } + return $errors; } @@ -184,67 +176,80 @@ class mod_customcert_edit_form extends moodleform { * @param stdClass $page the customcert page **/ private function add_customcert_page_elements($page = null) { - global $DB, $OUTPUT; + global $CFG, $DB, $OUTPUT; // Create the form object. $mform =& $this->_form; - // If page is null we are adding a customcert, not editing one, so set identifier to 1. + // Get the elements that are available + $elementsavailable = customcert_get_elements(); + + // If page is null we are adding a customcert, not editing one, so set pageid to 1. if (is_null($page)) { - $identifier = 1; + $pageid = 1; $pagenum = 1; } else { - $identifier = $page->id; + $pageid = $page->id; $pagenum = $page->pagenumber; } - $mform->addElement('header', 'page_'.$identifier, get_string('page', 'customcert', $pagenum)); + $mform->addElement('header', 'page_' . $pageid, get_string('page', 'customcert', $pagenum)); // 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' => $identifier)); + $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'moveup' => $pageid)); $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' => $identifier)); + $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'movedown' => $pageid)); $mform->addElement('html', $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')))); } - $mform->addElement('select', 'orientation_'.$identifier, get_string('orientation', 'customcert'), $this->orientationoptions); - $mform->setDefault('orientation_'.$identifier, 'P'); - $mform->addHelpButton('orientation_'.$identifier, 'orientation', 'customcert'); + $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('text', 'width_'.$identifier, get_string('width', 'customcert')); - $mform->addRule('width_'.$identifier, null, 'required', null, 'client'); - $mform->addHelpButton('width_'.$identifier, 'width', 'customcert'); + $mform->addElement('text', 'width_' . $pageid, get_string('width', 'customcert')); + $mform->addRule('width_' . $pageid, null, 'required', null, 'client'); + $mform->addHelpButton('width_' . $pageid, 'width', 'customcert'); - $mform->addElement('text', 'height_'.$identifier, get_string('height', 'customcert')); - $mform->addRule('height_'.$identifier, null, 'required', null, 'client'); - $mform->addHelpButton('height_'.$identifier, 'height', 'customcert'); + $mform->addElement('text', 'height_' . $pageid, get_string('height', 'customcert')); + $mform->addRule('height_' . $pageid, null, 'required', null, 'client'); + $mform->addHelpButton('height_' . $pageid, 'height', 'customcert'); - // Get the other image options. - $mform->addElement('select', 'backgroundimage_'.$identifier, get_string('backgroundimage', 'customcert'), $this->imageoptions); - $mform->setDefault('backgroundimage_'.$identifier, 0); - $mform->addHelpButton('backgroundimage_'.$identifier, 'backgroundimage', 'customcert'); + // Check if there are elements to add. + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'id ASC')) { + // Loop through and add the ones present. + foreach ($elements as $element) { + $classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php"; + // 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. + if (file_exists($classfile)) { + $classname = "customcert_element_{$element->element}"; + $e = new $classname($element); + $e->render_form_elements($mform); + // Add this to the objects array. + $this->elementobjects[] = $e; + // Add submit button to delete this. + $mform->addElement('submit', 'deleteelement_' . $element->id, get_string('delete', 'customcert')); + } + } + } - // Add text fields. - $textgroup = array(); - $textgroup[] =& $mform->createElement('text', 'certtext_'.$identifier, '', - array('cols' => '40', 'rows' => '4', 'wrap' => 'virtual')); - $group = $mform->createElement('group', 'customcerttextgroup_'.$identifier, - get_string('addtext', 'customcert'), $textgroup); - - $count = (is_null($page)) ? 1 : $DB->count_records('customcert_text_fields', array('customcertpageid' => $identifier)) + 1; - $this->repeat_elements(array($group), $count, array(), 'customcertimagerepeats_'.$identifier, 'imageadd_'.$identifier, 1, - get_string('addanothertextfield', 'customcert'), true); + $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. if ($pagenum > 1) { - $mform->addElement('html', '
'); - $mform->addElement('submit', 'deletecertpage_'.$identifier, get_string('deletecertpage', 'customcert')); - $mform->addElement('html', '
'); + $mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletecertpage'))); + $mform->addElement('submit', 'deletecertpage_' . $pageid, get_string('deletecertpage', 'customcert')); + $mform->addElement('html', html_writer::end_tag('div')); } } } diff --git a/elements/element.class.php b/elements/element.class.php new file mode 100644 index 0000000..b01edab --- /dev/null +++ b/elements/element.class.php @@ -0,0 +1,241 @@ +. + + +/** + * The base class for the customcert elements. + * + * @package mod + * @subpackage customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +class customcert_element_base { + + /** + * The data for the element we are adding. + */ + public $element; + + /** + * Constructor. + * + * @param stdClass $element the element data + */ + function __construct($element) { + $this->element = new stdClass(); + $this->element = $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 overriden 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 = new stdClass(); + $data->pageid = $pageid; + $data->element = $element; + $data->font = 'Times-Roman'; + $data->size = '12'; + $data->colour = 'FFFFFF'; + $data->posx = '250'; + $data->posy = '250'; + $data->timecreated = time(); + + $DB->insert_record('customcert_elements', $data); + } + + /** + * This function renders the form elements when adding a customcert element. + * Can be overridden if more functionality is needed. + * + * @param stdClass $mform the edit_form instance. + * @return array the form elements + */ + 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. + $id = $this->element->id; + + // The label. + $label = get_string('pluginname', 'customcertelement_' . $this->element->element); + + // String we are going to use often. + $strrequired = get_string('required'); + + // The common group of elements. + $group = array(); + $group[] = $mform->createElement('select', 'font_' . $id, '', customcert_get_fonts()); + $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. + $mform->addElement('group', 'elementfieldgroup_' . $id, $label, $group, + array(' ' . get_string('fontsize', 'customcert') . ' ', ' ' . get_string('colour', 'customcert') . ' ', + ' ' . get_string('posx', 'customcert') . ' ', ' ' . get_string('posy', 'customcert') . ' '), false); + + // 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); + + if ($numtimesadded == 0) { + $mform->addHelpButton('elementfieldgroup_' . $id, 'commonformelements', 'customcert'); + } + + $numtimesadded++; + } + + /** + * Performs validation on the element values. + * Can be overridden if more functionality is needed. + * + * @param array $data the submitted data + * @return array the validation errors + */ + public function validate_form_elements($data, $files) { + $errors = array(); + + // The identifier. + $id = $this->element->id; + + // Get the group name. + $group = 'elementfieldgroup_' . $id; + + // Get the colour. + $colour = 'colour_' . $id; + $colour = $data[$colour]; + $colour = ltrim($colour, "#"); + // Check if colour is not a valid hexadecimal value. + if(!preg_match("/[0-9A-F]{6}/i", $colour)) { + $errors[$group] = get_string('invalidcolour', 'customcert'); + } + + // Get position X. + $posx = 'posx_' . $id; + $posx = $data[$posx]; + // Check if posx is not numeric or less than 0. + if ((!is_numeric($posx)) || ($posx < 0)) { + if (!empty($errors[$group])) { + $errors[$group] .= "
"; + $errors[$group] .= get_string('invalidposition', 'customcert', 'X'); + } else { + $errors[$group] = get_string('invalidposition', 'customcert', 'X'); + } + } + + // Get position Y. + $posy = 'posy_' . $id; + $posy = $data[$posy]; + // Check if posy is not numeric or less than 0. + if ((!is_numeric($posy)) || ($posy < 0)) { + if (!empty($errors[$group])) { + $errors[$group] .= "
"; + $errors[$group] .= get_string('invalidposition', 'customcert', 'Y'); + } else { + $errors[$group] = get_string('invalidposition', 'customcert', 'Y'); + } + } + + return $errors; + } + + /** + * Handles saving the form elements created by this element. + * Can be overriden if more functionality is needed. + * + * @param stdClass $data the form data. + */ + 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. + $font = 'font_' . $id; + $size = 'size_' . $id; + $colour = 'colour_' . $id; + $posx = 'posx_' . $id; + $posy = 'posy_' . $id; + + // Get the data from the form. + $element = new stdClass(); + $element->id = $id; + $element->font = (!empty($data->$font)) ? $data->$font : null; + $element->size = (!empty($data->$size)) ? $data->$size : null; + $element->colour = (!empty($data->$colour)) ? ltrim($data->$colour, "#") : null; + $element->posx = (!empty($data->$posx)) ? $data->$posx : null; + $element->posy = (!empty($data->$posy)) ? $data->$posy : null; + + // Ok, now update record in the database. + $DB->update_record('customcert_elements', $element); + } + + /** + * Handles displaying the element on the pdf. + * Must be overriden. + * + * @param stdClass the pdf object, see lib/pdflib.php + */ + public function display($pdf) { + // Must be overriden. + return false; + } + + /** + * Handles deleting any data this element may have introduced. + * Can be overriden if more functionality is needed. + * + * @return bool success return true if deletion success, false otherwise + */ + public function delete_element() { + global $DB; + + return $DB->delete_records('customcert_elements', array('id' => $this->element->id)); + } +} \ No newline at end of file diff --git a/elements/studentname/lang/en/customcertelement_studentname.php b/elements/studentname/lang/en/customcertelement_studentname.php new file mode 100644 index 0000000..539dafb --- /dev/null +++ b/elements/studentname/lang/en/customcertelement_studentname.php @@ -0,0 +1,26 @@ +. + +/** + * Strings for component 'customcertelement_studentname', language 'en'. + * + * @package customcertelement_studentname + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['pluginname'] = 'Student name'; diff --git a/elements/studentname/lib.php b/elements/studentname/lib.php new file mode 100644 index 0000000..0128b1a --- /dev/null +++ b/elements/studentname/lib.php @@ -0,0 +1,53 @@ +. + +/** + * The studentname elements core interaction API. + * + * @package customcertelement_studentname + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); + +class customcert_element_studentname extends customcert_element_base { + + /** + * Constructor. + * + * @param stdClass $element the element data + */ + function __construct($element) { + parent::__construct($element); + } + + /** + * Handles displaying the element on the pdf. + * + * @param $pdf the pdf object, see lib/pdflib.php + */ + public function display($pdf) { + global $USER; + + $pdf->setFont($this->element->font, '', $this->element->size); + $pdf->SetXY($this->element->posx, $this->element->posy); + $pdf->writeHTMLCell(0, 0, '', '', fullname($USER), 0, 0, 0, true, $align); + } +} \ No newline at end of file diff --git a/elements/studentname/version.php b/elements/studentname/version.php new file mode 100644 index 0000000..44fd9d7 --- /dev/null +++ b/elements/studentname/version.php @@ -0,0 +1,29 @@ +. + +/** + * This file contains the version information for the studentname text plugin. + * + * @package customcertelement_studentname + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$plugin->version = 2013021901; +$plugin->requires = 2012062500; +$plugin->component = 'customcertelement_studentname'; diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 3fdb42e..822816a 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -16,7 +16,7 @@ // along with Moodle. If not, see . /** - * Language strings for the customcert module + * Language strings for the customcert module. * * @package mod * @subpackage customcert @@ -24,23 +24,33 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string['addtext'] = 'Add Text'; -$string['addanothertextfield'] = 'Add another text field'; $string['addcertpage'] = 'Add another certificate page'; -$string['backgroundimage'] = 'Background image'; -$string['backgroundimage_help'] = 'This image will be take up the entire PDF page as the background image.'; +$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.

+Font Size: This is the size of the font in points.
+Colour: This is the colour of the font.
+Position x: This is the position in pixels from the top left corner you wish the element to display in the x direction.
+Position Y: This is the position in pixels from the top left corner you wish the element to display in the y direction.
'; $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['customcert:addinstance'] = 'Add a new custom certificate instance'; $string['customcert:manage'] = 'Manage a custom certificate'; $string['customcert:view'] = 'View a custom certificate'; +$string['delete'] = 'Delete'; $string['deletecertpage'] = 'Delete certificate page'; -$string['deletecertpageconfirm'] = 'Are you sure you want to delete this certificate page?'; +$string['deleteconfirm'] = 'Delete confirmation'; +$string['deleteelementconfirm'] = 'Are you sure you want to delete this element?'; +$string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['editcustomcert'] = 'Edit custom certificate'; +$string['fontsize'] = 'Size'; $string['height'] = 'Height'; $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['intro'] = 'Introduction'; +$string['invalidcolour'] = 'Please select a valid hexadecimal colour.'; +$string['invalidposition'] = 'Please select a positive number for position {$a}.'; $string['landscape'] = 'Landscape'; $string['modulename'] = 'Custom Certificate'; $string['modulenameplural'] = 'Custom Certificates'; @@ -53,6 +63,8 @@ $string['page'] = 'Page {$a}'; $string['pluginadministration'] = 'Custom Certificate administration'; $string['pluginname'] = 'Custom Certificate'; $string['portrait'] = 'Portrait'; +$string['posx'] = 'Position X'; +$string['posy'] = 'Postion Y'; $string['uploadimage'] = 'Upload image'; $string['width'] = 'Width'; $string['widthnotvalid'] = 'The width has to be a valid number.'; diff --git a/lib.php b/lib.php index 705e67c..b955465 100644 --- a/lib.php +++ b/lib.php @@ -24,10 +24,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -require_once($CFG->dirroot.'/course/lib.php'); -require_once($CFG->dirroot.'/grade/lib.php'); -require_once($CFG->dirroot.'/grade/querylib.php'); - /** * Add customcert instance. * @@ -38,8 +34,7 @@ require_once($CFG->dirroot.'/grade/querylib.php'); function customcert_add_instance($data, $mform) { global $DB; - $time = time(); - $data->timecreated = $time; + $data->timecreated = time(); $data->timemodified = $data->timecreated; return $DB->insert_record('customcert', $data); @@ -75,11 +70,11 @@ function customcert_delete_instance($id) { global $DB; // Ensure the customcert exists. - if (!$customcert = $DB->get_record('customcert', array('id' => $id))) { + if (!$DB->get_record('customcert', array('id' => $id))) { return false; } - // Get the course module used when deleting files associated to the customcert. + // Get the course module as it is used when deleting files. if (!$cm = get_coursemodule_from_instance('customcert', $id)) { return false; } @@ -89,24 +84,28 @@ function customcert_delete_instance($id) { return false; } - // Delete the customcert issues. - if (!$DB->delete_records('customcert_issues', array('customcertid' => $id))) { - return false; - } - - // Get all the customcert pages. - if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $id))) { - // Loop through pages. - foreach ($pages as $p) { - // Delete the text fields. - if (!$DB->delete_records('customcert_text_fields', array('customcertpageid' => $p->id))) { + // Delete the elements. + $sql = "SELECT e.* + FROM {customcert_elements} e + INNER JOIN {customcert_pages} p + ON e.pageid = p.id + WHERE p.customcertid = :customcertid"; + if ($elements = $DB->get_records_sql($sql, array('customcertid' => $id))) { + foreach ($elements as $element) { + if (!customcert_delete_element($element)) { return false; } } - // Delete the pages - if (!$DB->delete_records('customcert_pages', array('customcertid' => $id))) { - return false; - } + } + + // Delete the pages. + if (!$DB->delete_records('customcert_pages', array('customcertid' => $id))) { + return false; + } + + // Delete the customcert issues. + if (!$DB->delete_records('customcert_issues', array('customcertid' => $id))) { + return false; } // Delete any files associated with the customcert. @@ -333,6 +332,102 @@ function customcert_upload_imagefiles($draftitemid) { file_save_draft_area_files($draftitemid, context_system::instance()->id, 'mod_customcert', 'image', 0); } +/** + * Return the list of possible elements to add. + * + * @return array the list of images that can be used. + */ +function customcert_get_elements() { + global $CFG; + + // Array to store the element types. + $options = array(); + + // Check that the directory exists. + $elementdir = "$CFG->dirroot/mod/customcert/elements"; + if (file_exists($elementdir)) { + // Get directory contents. + $elementfolders = new DirectoryIterator($elementdir); + // Loop through the elements folder. + foreach ($elementfolders as $elementfolder) { + // If it is not a directory or it is '.' or '..', skip it. + if (!$elementfolder->isDir() || $elementfolder->isDot()) { + continue; + } + // Check that the standard class file exists, if not we do + // not want to display it as an option as it will not work. + $foldername = $elementfolder->getFilename(); + $classfile = "$elementdir/$foldername/lib.php"; + if (file_exists($classfile)) { + require_once($classfile); + $component = "customcertelement_{$foldername}"; + $options[$foldername] = get_string('pluginname', $component); + } + } + + } + + return $options; +} + +/** + * Return the list of possible fonts to use. + */ +function customcert_get_fonts() { + global $CFG; + + // Array to store the available fonts. + $options = array(); + + // Location of fonts in Moodle. + $fontdir = "$CFG->dirroot/lib/tcpdf/fonts"; + // Check that the directory exists. + if (file_exists($fontdir)) { + // Get directory contents. + $fonts = new DirectoryIterator($fontdir); + // Loop through the font folder. + foreach ($fonts as $font) { + // If it is not a file, or either '.' or '..', or + // the extension is not php, or we can not open file, + // skip it. + if (!$font->isFile() || $font->isDot() || ($font->getExtension() != 'php')) { + continue; + } + // Set the name of the font to null, the include next should then set this + // value, if it is not set then the file does not include the necessary data. + $name = null; + // Some of the TCPDF files include files that are not present, so we have to + // suppress warnings, this is the TCPDF libraries fault, grrr. + @include("$fontdir/$font"); + // If no $name variable in file, skip it. + if (is_null($name)) { + continue; + } + // Format the font name, so "FontName-Style" becomes "Font Name - Style". + $formatname = preg_replace("/([a-z])([A-Z])/", "$1 $2", $name); + $formatname = preg_replace("/([a-zA-Z])-([a-zA-Z])/", "$1 - $2", $formatname); + $options[$name] = $formatname; + ksort($options); + } + + } + + return $options; +} + +/** + * Return the list of possible font sizes to use. + */ +function customcert_get_font_sizes() { + // Array to store the sizes. + $sizes = array(); + + for ($i = 1; $i <= 60; $i++) { + $sizes[$i] = $i; + } + + return $sizes; +} /** * Handles saving page data. @@ -340,7 +435,7 @@ function customcert_upload_imagefiles($draftitemid) { * @param stdClass $data the customcert data */ function customcert_save_page_data($data) { - global $DB; + global $CFG, $DB; // Set the time to a variable. $time = time(); @@ -356,43 +451,64 @@ function customcert_save_page_data($data) { $page->orientation = $data->orientation_1; $page->width = $data->width_1; $page->height = $data->height_1; - $page->backgroundimage = $data->backgroundimage_1; $page->pagenumber = 1; $page->timecreated = $time; $page->timemodified = $time; // Insert the page. $DB->insert_record('customcert_pages', $page); } else { - // Go through the data and check for any page data. - foreach ($data as $key => $value) { - if (strpos($key, 'orientation_') !== false) { - // Get the page id. - $pageid = str_replace('orientation_', '', $key); - // Get the rest of the elements now. - $orientation = "orientation_$pageid"; - $width = "width_$pageid"; - $height = "height_$pageid"; - $backgroundimage = "backgroundimage_$pageid"; - // Create the page to update. - $page = new stdClass(); - $page->id = $pageid; - $page->customcertid = $data->id; - $page->orientation = $data->$orientation; - $page->width = $data->$width; - $page->height = $data->$height; - $page->backgroundimage = $data->$backgroundimage; - $page->timemodified = $time; + // Get the existing pages and save the page data. + if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $data->id))) { + // Loop through existing pages. + 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; + // Create the page data to update the DB with. + $p = new stdClass(); + $p->id = $page->id; + $p->orientation = $data->$orientation; + $p->width = $data->$width; + $p->height = $data->$height; + $p->timemodified = $time; // Update the page. - $DB->update_record('customcert_pages', $page); + $DB->update_record('customcert_pages', $p); + // Get the elements for the page. + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { + // Loop through the elements. + foreach ($elements as $element) { + // Check that the standard class file exists. + $classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; + if (file_exists($classfile)) { + $classname = "customcert_element_{$element->element}"; + $e = new $classname($element); + $e->save_form_elements($data); + } + } + } } } } } +/** + * Handles adding another element to a page in the customcert. + * + * @param string $element the name of the element + * @param int $pageid the page id we are saving it to + */ +function customcert_add_element($element, $pageid) { + global $CFG; + + $classname = "customcert_element_{$element}"; + $classname::add_element($element, $pageid); +} + /** * Handles adding another page to the customcert. * - * @param stdClass $data the customcert data + * @param stdClass $data the form data */ function customcert_add_page($data) { global $DB; @@ -408,8 +524,10 @@ function customcert_add_page($data) { $pagenum = $maxnum->maxpagenumber + 1; } - // New page creation. + // Store time in a variable. $time = time(); + + // New page creation. $page = new stdClass(); $page->customcertid = $data->id; $page->orientation = 'P'; @@ -427,7 +545,7 @@ function customcert_add_page($data) { * @param int $pageid the customcert page */ function customcert_delete_page($pageid) { - global $DB; + global $CFG, $DB; // Get the page. $page = $DB->get_record('customcert_pages', array('id' => $pageid), '*', MUST_EXIST); @@ -435,8 +553,12 @@ function customcert_delete_page($pageid) { // Delete this page. $DB->delete_records('customcert_pages', array('id' => $page->id)); - // Delete any text fields belonging to this page. - $DB->delete_records('customcert_text_fields', array('customcertpageid' => $page->id)); + // The element may have some extra tasks it needs to complete to completely delete itself. + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { + foreach ($elements as $element) { + customcert_delete_element($element); + } + } // Now we want to decrease the page number values of // the pages that are greater than the page we deleted. @@ -448,6 +570,27 @@ function customcert_delete_page($pageid) { 'pagenumber' => $page->pagenumber)); } +/** + * Handles deleting an element. + * + * @param stdClass $element the element + * @param bool returns true if success, false otherwise + */ +function customcert_delete_element($element) { + global $CFG; + + // Check that the standard class file exists. + $classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; + if (file_exists($classfile)) { + require_once($classfile); + $classname = "customcert_element_{$element->element}"; + $e = new $classname($element); + return $e->delete_element(); + } + + return false; +} + /** * Get the time the user has spent in the course. * diff --git a/mod_form.php b/mod_form.php index ec750fa..2fa09c1 100644 --- a/mod_form.php +++ b/mod_form.php @@ -52,7 +52,6 @@ class mod_customcert_mod_form extends moodleform_mod { $this->add_intro_editor(false, get_string('intro', 'customcert')); - // Design Options. $mform->addElement('header', 'options', get_string('options', 'customcert')); $mform->addElement('text', 'requiredtime', get_string('coursetimereq', 'customcert'), array('size' => '3')); diff --git a/version.php b/version.php index 2344d66..dbfc508 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late */ -$module->version = 2012120701; // The current module version (Date: YYYYMMDDXX) +$module->version = 2013022101; // The current module version (Date: YYYYMMDDXX) $module->requires = 2012062500; // Requires this Moodle version $module->cron = 0; // Period for cron to check this module (secs) $module->component = 'mod_customcert';