From 819349dfc07f5fd92b92db87bf1caddb57f118ab Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 13 Mar 2013 18:19:08 -0700 Subject: [PATCH] Introduced the date element --- .../date/lang/en/customcertelement_date.php | 30 ++++ elements/date/lib.php | 139 ++++++++++++++++++ elements/date/version.php | 30 ++++ 3 files changed, 199 insertions(+) create mode 100644 elements/date/lang/en/customcertelement_date.php create mode 100644 elements/date/lib.php create mode 100644 elements/date/version.php diff --git a/elements/date/lang/en/customcertelement_date.php b/elements/date/lang/en/customcertelement_date.php new file mode 100644 index 0000000..58c4427 --- /dev/null +++ b/elements/date/lang/en/customcertelement_date.php @@ -0,0 +1,30 @@ +. + +/** + * Strings for component 'customcertelement_date', language 'en'. + * + * @package customcertelement_date + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$string['dateformat'] = 'Date format'; +$string['dateformat_help'] = 'This is the format of the date that will be displayed'; +$string['dateitem'] = 'Date item'; +$string['dateitem_help'] = 'This will be the date that is printed on the certificate'; +$string['pluginname'] = 'Date'; diff --git a/elements/date/lib.php b/elements/date/lib.php new file mode 100644 index 0000000..280ca9d --- /dev/null +++ b/elements/date/lib.php @@ -0,0 +1,139 @@ +. + +/** + * The date elements core interaction API. + * + * @package customcertelement_date + * @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_date extends customcert_element_base { + + /** + * Constructor. + * + * @param stdClass $element the element data + */ + function __construct($element) { + parent::__construct($element); + } + + /** + * This function renders the form elements when adding a customcert element. + * + * @param stdClass $mform the edit_form instance. + */ + public function render_form_elements($mform) { + // The identifier. + $id = $this->element->id; + + $dateitem = ''; + $dateformat = ''; + + // Check if there is any data for this element. + if (!empty($this->element->data)) { + $dateinfo = json_decode($this->element->data); + $dateitem = $dateinfo->dateitem; + $dateformat = $dateinfo->dateformat; + } + + $mform->addElement('select', 'dateitem_' . $id, get_string('dateitem', 'customcertelement_date'), $this->get_date_options()); + $mform->addElement('select', 'dateformat_' . $id, get_string('dateformat', 'customcertelement_date'), $this->get_date_formats()); + + parent::render_form_elements($mform); + + $mform->setDefault('dateitem_' . $id, $dateitem); + $mform->setDefault('dateformat_' . $id, $dateformat); + + // Add help buttons. + $mform->addHelpButton('dateitem_' . $id, 'dateitem', 'customcertelement_date'); + $mform->addHelpButton('dateformat_' . $id, 'dateformat', 'customcertelement_date'); + } + + /** + * This will handle how form data will be saved into the data column in the + * customcert column. + * + * @param stdClass $data the form data. + * @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 = $data->$dateitem; + $dateformat = 'dateformat_' . $id; + $dateformat = $data->$dateformat; + + // Array of data we will be storing in the database. + $arrtostore = array( + 'dateitem' => $dateitem, + 'dateformat' => $dateformat + ); + + // Encode these variables before saving into the DB. + return json_encode($arrtostore); + } + + /** + * Handles displaying the element on the pdf. + * + * @param $pdf the pdf object, see lib/pdflib.php + * @todo functionality missing, add when we start rendering the pdf + */ + public function display($pdf) { + global $USER; + + // TO DO. + } + + /** + * Helper function to return all the date options. + * + * @return array the list of date options + */ + public function get_date_options() { + $dateoptions['1'] = get_string('issueddate', 'certificate'); + $dateoptions['2'] = get_string('completiondate', 'certificate'); + + return $dateoptions; + } + + /** + * Helper function to return all the date formats. + * + * @return array the list of date formats + */ + public function get_date_formats() { + $dateformats = array(); + $dateformats[] = 'January 1, 2000'; + $dateformats[] = 'January 1st, 2000'; + $dateformats[] = '1 January 2000'; + $dateformats[] = 'January 2000'; + $dateformats[] = get_string('userdateformat', 'certificate'); + + return $dateformats; + } +} diff --git a/elements/date/version.php b/elements/date/version.php new file mode 100644 index 0000000..a12ad42 --- /dev/null +++ b/elements/date/version.php @@ -0,0 +1,30 @@ +. + +/** + * This file contains the version information for the date plugin. + * + * @package customcertelement_date + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$plugin->version = 2013031300; +$plugin->requires = 2012112900; +$plugin->component = 'customcertelement_date';