From fa5d45eef3ad9b9624ddcc47e25373cb23b04c97 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 6 Jun 2013 18:59:08 +0800 Subject: [PATCH] Completely refactored the code for editing a custom certificate The elements for each page are listed in a table below the other page settings, meaning each page has a separate table for listing their elements. Each element is now edited in a pop-up, rather than having all the element settings displayed at once on the form causing it to look overcrowded. There is also no longer any need for the function add_element to add default values for the elements when they are added, as a a form is shown when you click to add an element where you can enter the values before it is created. Note: I did not create a db/upgrade.php script to add the new database columns as this module should not be currently used by anyone. --- db/install.xml | 6 +- edit.php | 122 +++++++++++++----------- edit_element.php | 109 +++++++++++++++++++++ edit_element_form.php | 78 +++++++++++++++ edit_form.php | 131 ++++++++++++------------- elements/code/version.php | 2 +- elements/date/lib.php | 53 ++++++----- elements/date/version.php | 2 +- elements/element.class.php | 159 +++++++++++++------------------ elements/grade/lib.php | 71 +++++++------- elements/grade/version.php | 2 +- elements/image/lib.php | 88 +++++++---------- elements/image/version.php | 2 +- elements/studentname/version.php | 2 +- elements/text/lib.php | 21 ++-- elements/text/version.php | 2 +- lang/en/customcert.php | 7 ++ lib.php | 46 ++------- mod_form.php | 2 +- 19 files changed, 521 insertions(+), 384 deletions(-) create mode 100644 edit_element.php create mode 100644 edit_element_form.php diff --git a/db/install.xml b/db/install.xml index 1ec8331..50b0d56 100644 --- a/db/install.xml +++ b/db/install.xml @@ -53,7 +53,8 @@ - + + @@ -100,7 +101,8 @@ - + + diff --git a/edit.php b/edit.php index cd06724..616764e 100644 --- a/edit.php +++ b/edit.php @@ -66,6 +66,9 @@ if ($data = $savetemplateform->get_data()) { redirect($url); } +// Flag to determine if we are deleting anything. +$deleting = false; + // Check if they are moving a custom certificate page. if ((!empty($moveup)) || (!empty($movedown))) { // Check if we are moving a page up. @@ -99,15 +102,56 @@ if ((!empty($moveup)) || (!empty($movedown))) { $DB->set_field('customcert_elements', 'sequence', $swapcertelement->sequence, array('id' => $movecertelement->id)); $DB->set_field('customcert_elements', 'sequence', $movecertelement->sequence, array('id' => $swapcertelement->id)); } -} 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); - // Get an instance of the element class. - if ($e = customcert_get_element_instance($element)) { - $e->delete_element(); +} else if (!empty($deletepage)) { // Check if we are deleting a page. + if (!empty($confirm)) { // Check they have confirmed the deletion + customcert_delete_page($deletepage); + } else { + // 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, + 'deletepage' => $deletepage, + 'confirm' => 1, + 'sesskey' => sesskey())); } +} else if (!empty($deleteelement)) { // Check if we are deleting an element. + if (!empty($confirm)) { // Check they have confirmed the deletion + // Ensure element exists and delete it. + $element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST); + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { + $e->delete_element(); + } + } else { + // 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' => $deleteelement, + 'confirm' => 1, + 'sesskey' => sesskey())); + } +} + +// Check if we are deleting either a page or an element. +if ($deleting) { + // Show a confirmation page. + $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); + echo $OUTPUT->confirm($message, $yesurl, $nourl); + echo $OUTPUT->footer(); + exit(); } $mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id, @@ -121,63 +165,27 @@ if ($data = $mform->get_data()) { // Save any page data. customcert_save_page_data($data); - // Flag to determine if we are deleting anything. - $deleting = false; + // Check if we are adding a page. + if (!empty($data->addcertpage)) { // Check if they chose to add a page. + customcert_add_page($data); + } // 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, - '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. + 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); - } else if (strpos($key, 'addcertpage_') !== false) { // Check if they chose to add a page. - $data->pageid = str_replace('addcertpage_', '', $key); - customcert_add_page($data); - } - - // Check if we are deleting either a page or an element. - if ($deleting) { - // Show a confirmation page. - $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); - echo $OUTPUT->confirm($message, $yesurl, $nourl); - echo $OUTPUT->footer(); - exit(); + // Create the URL to redirect to to add this element. + $params = array(); + $params['cmid'] = $cmid; + $params['action'] = 'add'; + $params['element'] = $element; + $params['pageid'] = $pageid; + $url = new moodle_url('/mod/customcert/edit_element.php', $params); + redirect($url); } } diff --git a/edit_element.php b/edit_element.php new file mode 100644 index 0000000..040383e --- /dev/null +++ b/edit_element.php @@ -0,0 +1,109 @@ +. + +/** + * Edit a customcert element. + * + * @package mod_customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once('../../config.php'); +require_once($CFG->dirroot . '/mod/customcert/edit_element_form.php'); +require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); + +$cmid = required_param('cmid', PARAM_INT); +$action = required_param('action', PARAM_ALPHA); +$popup = optional_param('popup', '0', PARAM_INT); + +$cm = get_coursemodule_from_id('customcert', $cmid, 0, false, MUST_EXIST); +$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); +$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST); +$context = context_module::instance($cm->id); + +if ($action == 'edit') { + // The id of the element must be supplied if we are currently editing one. + $id = required_param('id', PARAM_INT); + $element = $DB->get_record('customcert_elements', array('id' => $id), '*', MUST_EXIST); + $pageurl = new moodle_url('/mod/customcert/edit_element.php', array('id' => $id, 'cmid' => $cmid, 'action' => $action, 'popup' => $popup)); +} else { // Must be adding an element. + // Page id must be supplied in order to add an element. + $pageid = required_param('pageid', PARAM_INT); + // Create the new element object, will have no data. + $element = new stdClass(); + $element->element = required_param('element', PARAM_ALPHA); + // Set the page url. + $params = array(); + $params['cmid'] = $cmid; + $params['action'] = 'add'; + $params['element'] = $element->element; + $params['pageid'] = $pageid; + $params['popup'] = $popup; + $pageurl = new moodle_url('/mod/customcert/edit_element.php', $params); +} + +require_login($course, false, $cm); + +require_capability('mod/customcert:manage', $context); + +if ($popup) { + $PAGE->set_pagelayout('popup'); +} else { + $PAGE->set_heading($course->fullname); +} + +$PAGE->set_title(get_string('editcustomcert', 'customcert', format_string($customcert->name))); +$PAGE->set_url($pageurl); + +$mform = new mod_customcert_edit_element_form($pageurl, array('element' => $element, 'cmid' => $cmid)); + +// Check if they cancelled. +if ($mform->is_cancelled()) { + if ($popup) { + close_window(); + } else { + $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid)); + redirect($url); + } +} + +if ($data = $mform->get_data()) { + // Set the id, or page id depending on if we are editing an element, or adding a new one. + if ($action == 'edit') { + $data->id = $id; + } else { + $data->pageid = $pageid; + } + // Set the element variable. + $data->element = $element->element; + // Get an instance of the element class. + if ($e = customcert_get_element_instance($data)) { + $e->save_form_elements($data); + } + if ($popup) { + close_window(); + } else { + $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid)); + redirect($url); + } +} + +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('editcustomcert', 'customcert')); +$mform->display(); +echo $OUTPUT->footer(); diff --git a/edit_element_form.php b/edit_element_form.php new file mode 100644 index 0000000..f2046de --- /dev/null +++ b/edit_element_form.php @@ -0,0 +1,78 @@ +. + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +require_once($CFG->dirroot . '/course/moodleform_mod.php'); +require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php'); + +MoodleQuickForm::registerElementType('customcert_colourpicker', + $CFG->dirroot . '/mod/customcert/includes/colourpicker.php', 'MoodleQuickForm_customcert_colourpicker'); + +/** + * The form for handling editing a customcert element. + * + * @package mod_customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_customcert_edit_element_form extends moodleform { + + /** + * The element object. + */ + private $element; + + /** + * Form definition. + */ + function definition() { + $mform =& $this->_form; + + $element = $this->_customdata['element']; + + // Add the field for the name of the variable, this is required for all elements. + $mform->addElement('text', 'name', get_string('elementname', 'customcert')); + $mform->setType('name', PARAM_TEXT); + $mform->setDefault('name', get_string('pluginname', 'customcertelement_' . $element->element)); + $mform->addRule('name', get_string('required'), 'required', null, 'client'); + $mform->addHelpButton('name', 'elementname', 'customcert'); + + $this->element = customcert_get_element_instance($element); + $this->element->render_form_elements($mform); + + $this->add_action_buttons(true); + } + + /** + * Fill in the current page data for this customcert. + */ + function definition_after_data() { + $this->element->definition_after_data($this->_form); + } + + /** + * Validation. + * + * @param $data + * @param $files + * @return array the errors that were found + */ + public function validation($data, $files) { + return $this->element->validate_form_elements($data, $files); + } +} diff --git a/edit_form.php b/edit_form.php index 3d01310..9be3d31 100644 --- a/edit_form.php +++ b/edit_form.php @@ -18,7 +18,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); require_once($CFG->dirroot . '/course/moodleform_mod.php'); -require_once($CFG->dirroot . '/mod/customcert/lib.php'); require_once($CFG->dirroot . '/mod/customcert/includes/colourpicker.php'); MoodleQuickForm::registerElementType('customcert_colourpicker', @@ -48,11 +47,6 @@ class mod_customcert_edit_form extends moodleform { */ private $filemanageroptions = array(); - /** - * The array of element objects loaded on this form. - */ - private $elementobjects = array(); - /** * Form definition. */ @@ -74,6 +68,10 @@ class mod_customcert_edit_form extends moodleform { } } + $mform->closeHeaderBefore('addcertpage'); + + $mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert')); + $mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert')); $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions); @@ -158,13 +156,6 @@ class mod_customcert_edit_form extends moodleform { } } - // 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; } @@ -174,7 +165,7 @@ class mod_customcert_edit_form extends moodleform { * @param stdClass $page the customcert page **/ private function add_customcert_page_elements($page) { - global $CFG, $DB, $OUTPUT; + global $DB, $OUTPUT; // Create the form object. $mform =& $this->_form; @@ -211,64 +202,74 @@ class mod_customcert_edit_form extends moodleform { $mform->addRule('pageheight_' . $page->id, null, 'required', null, 'client'); $mform->addHelpButton('pageheight_' . $page->id, 'height', 'customcert'); + // Check if there are elements to add. + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) { + // Get the total number of elements. + $numelements = count($elements); + // Create a table to display these elements. + $table = new html_table(); + $table->head = array(get_string('name', 'customcert'), get_string('type', 'customcert'), '', ''); + // If we have more than one element then we can change the order, so add extra column for the up and down arrow. + if ($numelements > 1) { + $table->head[] = ''; + } + $table->align = array('left', 'left', 'center', 'center'); + if ($numelements > 1) { + $table->align[] = 'center'; + } + // Loop through and add the elements to the table. + foreach ($elements as $element) { + $row = new html_table_row(); + $row->cells[] = $element->name; + $row->cells[] = $element->element; + // Link to delete the element. + $deletelink = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'deleteelement' => $element->id)); + $deletelink = html_writer::tag('a', get_string('delete', 'customcert'), array('href' => $deletelink->out(false))); + $row->cells[] = $deletelink; + // Link to configure this element. + $params = array(); + $params['id'] = $element->id; + $params['cmid'] = $this->_customdata['cmid']; + $params['action'] = 'edit'; + $nopopupconfigurelink = new moodle_url('/mod/customcert/edit_element.php', $params); + $params['popup'] = 1; + $popupconfigurelink = new moodle_url('/mod/customcert/edit_element.php', $params); + $action = new popup_action('click', $popupconfigurelink, 'edit_element_popup', array('height' => 400, 'width' => 600)); + $configurelink = $OUTPUT->action_link($nopopupconfigurelink->out(false), get_string('configure', 'customcert'), $action); + $row->cells[] = $configurelink; + // Now display any moving arrows if they are needed. + if ($numelements > 1) { + // Only display the move up arrow if it is not the first. + $moveicons = ''; + if ($element->sequence > 1) { + $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'emoveup' => $element->id)); + $moveicons .= $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 ($element->sequence < $numelements) { + $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'emovedown' => $element->id)); + $moveicons .= $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown'))); + } + $row->cells[] = $moveicons; + } + $table->data[] = $row; + } + // Add the table to the form. + $mform->addElement('static', 'elements_' . $page->id, get_string('elements', 'customcert'), html_writer::table($table)); + $mform->addHelpButton('elements_' . $page->id, 'elements', 'customcert'); + } + $group = array(); $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_' . $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_' . $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' => $page->id), 'sequence ASC')) { - // Get the total number of elements. - $numelements = count($elements); - // 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)) { - // Add element header. - $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. - if ($element->timemodified > $element->timecreated) { - $mform->setExpanded('headerelement_' . $element->id, false); - } else { - $mform->setExpanded('headerelement_' . $element->id, true); - } - // Only display the move up arrow if it is not the first. - if ($element->sequence > 1) { - $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'emoveup' => $element->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 ($element->sequence < $numelements) { - $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'emovedown' => $element->id)); - $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 = $page->pagenumber; - // Get the classname. - $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('html', html_writer::start_tag('div', array('class' => 'deletebutton'))); - $mform->addElement('submit', 'deleteelement_' . $element->id, get_string('deleteelement', 'customcert')); - $mform->addElement('html', html_writer::end_tag('div')); - } - } + // Link to delete the element. + $deletelink = new moodle_url('/mod/customcert/edit.php', array('cmid' => $this->_customdata['cmid'], 'deletepage' => $page->id)); + $deletelink = html_writer::tag('a', get_string('deletecertpage', 'customcert'), array('href' => $deletelink->out(false), 'class' => 'deletebutton')); + $mform->addElement('html', html_writer::tag('div', $deletelink, array('class' => 'deletebutton'))); } } } diff --git a/elements/code/version.php b/elements/code/version.php index 9a3bb37..b197104 100644 --- a/elements/code/version.php +++ b/elements/code/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2013060500; +$plugin->version = 2013060600; $plugin->requires = 2013040500; // Requires this Moodle version. $plugin->component = 'customcertelement_code'; diff --git a/elements/date/lib.php b/elements/date/lib.php index b0aea60..03fc90a 100644 --- a/elements/date/lib.php +++ b/elements/date/lib.php @@ -26,38 +26,49 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); +require_once($CFG->dirroot . '/mod/customcert/elements/grade/lib.php'); class customcert_element_date extends customcert_element_base { + /** + * Constructor. + * + * @param stdClass $element the element data + */ + function __construct($element) { + parent::__construct($element); + + // Set the item and format for this element. + $dateitem = ''; + $dateformat = ''; + + if (!empty($this->element->data)) { + $dateinfo = json_decode($this->element->data); + $dateitem = $dateinfo->dateitem; + $dateformat = $dateinfo->dateformat; + } + + $this->element->dateitem = $dateitem; + $this->element->dateformat = $dateformat; + } + /** * This function renders the form elements when adding a customcert element. * * @param stdClass $mform the edit_form instance. */ public function render_form_elements($mform) { - $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; - } - // Get the possible date options. $dateoptions = array(); $dateoptions['1'] = get_string('issueddate', 'certificate'); $dateoptions['2'] = get_string('completiondate', 'certificate'); $dateoptions = $dateoptions + customcert_element_grade::get_grade_items(); - $mform->addElement('select', 'dateitem_' . $this->element->id, get_string('dateitem', 'customcertelement_date'), $dateoptions); - $mform->setDefault('dateitem_' . $this->element->id, $dateitem); - $mform->addHelpButton('dateitem_' . $this->element->id, 'dateitem', 'customcertelement_date'); + $mform->addElement('select', 'dateitem', get_string('dateitem', 'customcertelement_date'), $dateoptions); + $mform->addHelpButton('dateitem', 'dateitem', 'customcertelement_date'); - $mform->addElement('select', 'dateformat_' . $this->element->id, get_string('dateformat', 'customcertelement_date'), customcert_element_date::get_date_formats()); - $mform->setDefault('dateformat_' . $this->element->id, $dateformat); - $mform->addHelpButton('dateformat_' . $this->element->id, 'dateformat', 'customcertelement_date'); + $mform->addElement('select', 'dateformat', get_string('dateformat', 'customcertelement_date'), $this->get_date_formats()); + $mform->addHelpButton('dateformat', 'dateformat', 'customcertelement_date'); parent::render_form_elements($mform); } @@ -70,16 +81,10 @@ class customcert_element_date extends customcert_element_base { * @return string the json encoded array */ public function save_unique_data($data) { - // Get the date item and format from the form. - $dateitem = 'dateitem_' . $this->element->id; - $dateitem = $data->$dateitem; - $dateformat = 'dateformat_' . $this->element->id; - $dateformat = $data->$dateformat; - // Array of data we will be storing in the database. $arrtostore = array( - 'dateitem' => $dateitem, - 'dateformat' => $dateformat + 'dateitem' => $data->dateitem, + 'dateformat' => $data->dateformat ); // Encode these variables before saving into the DB. diff --git a/elements/date/version.php b/elements/date/version.php index 1d02a62..458498c 100644 --- a/elements/date/version.php +++ b/elements/date/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2013041600; +$plugin->version = 2013060700; $plugin->requires = 2013040500; // Requires this Moodle version. $plugin->component = 'customcertelement_date'; diff --git a/elements/element.class.php b/elements/element.class.php index 82f882a..d65f4bf 100644 --- a/elements/element.class.php +++ b/elements/element.class.php @@ -40,29 +40,7 @@ class customcert_element_base { * @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 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->font = 'times'; - $data->size = '12'; - $data->colour = '#000000'; - $data->posx = '0'; - $data->posy = '0'; - - $DB->insert_record('customcert_elements', $data); + $this->element = clone($element); } /** @@ -78,6 +56,24 @@ class customcert_element_base { $this->render_form_elements_position($mform); } + /** + * Sets the data on the form when editing an element. + * Can be overridden if more functionality is needed. + * + * @param stdClass $mform the edit_form instance + * @param array the form elements to set + */ + public function definition_after_data($mform) { + // Loop through the properties of the element and set the values + // of the corresponding form element, if it exists. + foreach ($this->element as $property => $value) { + if ($mform->elementExists($property)) { + $element = $mform->getElement($property); + $element->setValue($value); + } + } + } + /** * Performs validation on the element values. * Can be overridden if more functionality is needed. @@ -101,31 +97,33 @@ class customcert_element_base { * Handles saving the form elements created by this element. * Can be overridden if more functionality is needed. * - * @param stdClass $data the form data. + * @param stdClass $data the form data */ public function save_form_elements($data) { global $DB; - // Get the name of the fields we want from the form. - $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 = $this->element->id; + $element->name = $data->name; $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->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. - $DB->update_record('customcert_elements', $element); + // Check if we are updating, or inserting a new element. + if (!empty($this->element->id)) { // Must be updating a record in the database. + $element->id = $this->element->id; + $DB->update_record('customcert_elements', $element); + } else { // Must be adding a new one. + $element->element = $data->element; + $element->pageid = $data->pageid; + $element->sequence = $this->get_element_sequence($element->pageid); + $element->timecreated = time(); + $DB->insert_record('customcert_elements', $element); + } } /** @@ -133,7 +131,7 @@ class customcert_element_base { * customcert column. * Can be overridden if more functionality is needed. * - * @param stdClass $data the form data. + * @param stdClass $data the form data * @return string the unique data to save */ public function save_unique_data($data) { @@ -199,28 +197,6 @@ 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. @@ -251,28 +227,27 @@ class customcert_element_base { * @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', 'font', get_string('font', 'customcert'), customcert_get_fonts()); + $mform->setType('font', PARAM_TEXT); + $mform->setDefault('font', 'times'); + $mform->addHelpButton('font', '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'); + $mform->addElement('select', 'size', get_string('fontsize', 'customcert'), customcert_get_font_sizes()); + $mform->setType('size', PARAM_INT); + $mform->setDefault('size', 12); + $mform->addHelpButton('size', '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'); + $mform->addElement('customcert_colourpicker', 'colour', get_string('fontcolour', 'customcert')); + $mform->setType('colour', PARAM_RAW); // Need to validate that this is a valid colour. + $mform->setDefault('colour', '#000000'); + $mform->addHelpButton('colour', 'fontcolour', 'customcert'); } /** @@ -281,15 +256,15 @@ class customcert_element_base { * @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', 'posx', get_string('posx', 'customcert'), array('size' => 10)); + $mform->setType('posx', PARAM_INT); + $mform->setDefault('posx', '0'); + $mform->addHelpButton('posx', 'posx', 'customcert'); - $mform->addElement('text', 'posy_' . $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'); + $mform->addElement('text', 'posy', get_string('posy', 'customcert'), array('size' => 10)); + $mform->setType('posy', PARAM_INT); + $mform->setDefault('posy', '0'); + $mform->addHelpButton('posy', 'posy', 'customcert'); } /** @@ -302,10 +277,8 @@ class customcert_element_base { $errors = array(); // Validate the colour. - $colour = 'colour_' . $this->element->id; - $colourdata = $data[$colour]; - if (!$this->validate_colour($colourdata)) { - $errors[$colour] = get_string('invalidcolour', 'customcert'); + if (!$this->validate_colour($data['colour'])) { + $errors['colour'] = get_string('invalidcolour', 'customcert'); } return $errors; @@ -320,18 +293,14 @@ class customcert_element_base { 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'); + 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'); + if ((!isset($data['posy'])) || (!is_numeric($data['posy'])) || ($data['posy'] < 0)) { + $errors['posy'] = get_string('invalidposition', 'customcert', 'Y'); } return $errors; diff --git a/elements/grade/lib.php b/elements/grade/lib.php index 0f55365..1676d03 100644 --- a/elements/grade/lib.php +++ b/elements/grade/lib.php @@ -38,38 +38,47 @@ define('CUSTOMCERT_GRADE_COURSE', '0'); class customcert_element_grade extends customcert_element_base { /** - * This function renders the form elements when adding a customcert element. + * Constructor. * - * @param stdClass $mform the edit_form instance. + * @param stdClass $element the element data */ - public function render_form_elements($mform) { + function __construct($element) { + parent::__construct($element); + + // Set the item and format for this element. $gradeitem = ''; $gradeformat = ''; - // Check if there is any data for this element. if (!empty($this->element->data)) { $gradeinfo = json_decode($this->element->data); $gradeitem = $gradeinfo->gradeitem; $gradeformat = $gradeinfo->gradeformat; } + $this->element->gradeitem = $gradeitem; + $this->element->gradeformat = $gradeformat; + } + + /** + * This function renders the form elements when adding a customcert element. + * + * @param stdClass $mform the edit_form instance. + */ + public function render_form_elements($mform) { // Get the grade items we can display. $gradeitems = array(); $gradeitems[CUSTOMCERT_GRADE_COURSE] = get_string('coursegrade', 'customcertelement_grade'); - $gradeitems = $gradeitems + customcert_element_grade::get_grade_items(); + $gradeitems = $gradeitems + $this->get_grade_items(); // The grade items. - $mform->addElement('select', 'gradeitem_' . $this->element->id, get_string('gradeitem', 'customcertelement_grade'), $gradeitems); - $mform->setType('gradeitem_', PARAM_INT); - $mform->setDefault('gradeitem_' . $this->element->id, $gradeitem); - $mform->addHelpButton('gradeitem_' . $this->element->id, 'gradeitem', 'customcertelement_grade'); + $mform->addElement('select', 'gradeitem', get_string('gradeitem', 'customcertelement_grade'), $gradeitems); + $mform->setType('gradeitem', PARAM_INT); + $mform->addHelpButton('gradeitem', 'gradeitem', 'customcertelement_grade'); // The grade format. - $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_' . $this->element->id, $gradeformat); - $mform->addHelpButton('gradeformat_' . $this->element->id, 'gradeformat', 'customcertelement_grade'); + $mform->addElement('select', 'gradeformat', get_string('gradeformat', 'customcertelement_grade'), $this->get_grade_format_options()); + $mform->setType('gradeformat', PARAM_INT); + $mform->addHelpButton('gradeformat', 'gradeformat', 'customcertelement_grade'); parent::render_form_elements($mform); } @@ -82,16 +91,10 @@ class customcert_element_grade extends customcert_element_base { * @return string the json encoded array */ public function save_unique_data($data) { - // Get the grade item and format from the form. - $gradeitem = 'gradeitem_' . $this->element->id; - $gradeitem = $data->$gradeitem; - $gradeformat = 'gradeformat_' . $this->element->id; - $gradeformat = $data->$gradeformat; - // Array of data we will be storing in the database. $arrtostore = array( - 'gradeitem' => $gradeitem, - 'gradeformat' => $gradeformat + 'gradeitem' => $data->gradeitem, + 'gradeformat' => $data->gradeformat ); // Encode these variables before saving into the DB. @@ -116,7 +119,7 @@ class customcert_element_grade extends customcert_element_base { $gradeinfo = json_decode($this->element->data); // Get the grade for the grade item. - $grade = customcert_element_grade::get_grade($gradeinfo, $USER->id); + $grade = $this->get_grade($gradeinfo, $USER->id); parent::render_content($pdf, $grade); } @@ -169,8 +172,8 @@ class customcert_element_grade extends customcert_element_base { // Get the grade items for this activity. if ($grade_items = grade_get_grade_items_for_activity($mod)) { $mod_item = grade_get_grades($COURSE->id, 'mod', $mod->modname, $mod->instance); - $item = reset($mod_item->items); - if (isset($item->grademax)) { + $gradeitem = reset($mod_item->items); + if (isset($gradeitem->grademax)) { $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; } } @@ -243,22 +246,22 @@ class customcert_element_grade extends customcert_element_base { $module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST); if ($gradeitem = grade_get_grades($cm->course, 'mod', $module->name, $cm->instance, $userid)) { - $item = new grade_item(); - $item->gradetype = GRADE_TYPE_VALUE; - $item->courseid = $cm->course; - $itemproperties = reset($gradeitem->items); - foreach ($itemproperties as $key => $value) { - $item->$key = $value; + $gradeitem = new grade_item(); + $gradeitem->gradetype = GRADE_TYPE_VALUE; + $gradeitem->courseid = $cm->course; + $gradeitemproperties = reset($gradeitem->items); + foreach ($gradeitemproperties as $key => $value) { + $gradeitem->$key = $value; } // Grade for the user. - $grade = $item->grades[$userid]->grade; + $grade = $gradeitem->grades[$userid]->grade; // Create the object we will be returning. $modinfo = new stdClass; $modinfo->name = $DB->get_field($module->name, 'name', array('id' => $cm->instance)); - $modinfo->gradetodisplay = grade_format_gradevalue($grade, $item, true, $gradeformat, 2); + $modinfo->gradetodisplay = grade_format_gradevalue($grade, $gradeitem, true, $gradeformat, 2); if ($grade) { - $modinfo->dategraded = $item->grades[$userid]->dategraded; + $modinfo->dategraded = $gradeitem->grades[$userid]->dategraded; } else { $modinfo->dategraded = time(); } diff --git a/elements/grade/version.php b/elements/grade/version.php index 7feecfd..4b1349e 100644 --- a/elements/grade/version.php +++ b/elements/grade/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2013041600; +$plugin->version = 2013060700; $plugin->requires = 2013040500; // Requires this Moodle version. $plugin->component = 'customcertelement_grade'; diff --git a/elements/image/lib.php b/elements/image/lib.php index 7ed7eaf..0ed29bc 100644 --- a/elements/image/lib.php +++ b/elements/image/lib.php @@ -30,31 +30,14 @@ require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); class customcert_element_image extends customcert_element_base { /** - * 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. + * Constructor. * - * @param string $element the name of the element - * @param int $pageid the page id we are saving it to + * @param stdClass $element the element data */ - public static function add_element($element, $pageid) { - global $DB; + function __construct($element) { + parent::__construct($element); - $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) { + // Set the image, width and height for this element. $image = ''; $width = '0'; $height = '0'; @@ -67,18 +50,26 @@ class customcert_element_image extends customcert_element_base { $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); + $this->element->image = $image; + $this->element->width = $width; + $this->element->height = $height; + } - $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'); + /** + * This function renders the form elements when adding a customcert element. + * + * @param stdClass $mform the edit_form instance. + */ + public function render_form_elements($mform) { + $mform->addElement('select', 'image', get_string('image', 'customcertelement_image'), self::get_images()); - $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'); + $mform->addElement('text', 'width', get_string('width', 'customcertelement_image'), array('size' => 10)); + $mform->setType('width', PARAM_INT); + $mform->addHelpButton('width', 'width', 'customcertelement_image'); + + $mform->addElement('text', 'height', get_string('height', 'customcertelement_image'), array('size' => 10)); + $mform->setType('height', PARAM_INT); + $mform->addHelpButton('height', 'height', 'customcertelement_image'); parent::render_form_elements_position($mform); } @@ -94,18 +85,14 @@ class customcert_element_image extends customcert_element_base { // 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'); + 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'); + if ((!isset($data['height'])) || (!is_numeric($data['height'])) || ($data['height'] < 0)) { + $errors['height'] = get_string('invalidheight', 'customcertelement_image'); } // Validate the position. @@ -122,19 +109,11 @@ class customcert_element_image extends customcert_element_base { * @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 + 'pathnamehash' => $data->image, + 'width' => $data->width, + 'height' => $data->height ); return json_encode($arrtostore); @@ -154,18 +133,15 @@ class customcert_element_image extends customcert_element_base { } $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)) { + if ($file = $fs->get_file_by_hash($imageinfo->pathnamehash)) { $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); + $pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height); } } diff --git a/elements/image/version.php b/elements/image/version.php index e1f256e..55f4a94 100644 --- a/elements/image/version.php +++ b/elements/image/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2013053000; +$plugin->version = 2013060600; $plugin->requires = 2013040500; // Requires this Moodle version. $plugin->component = 'customcertelement_image'; diff --git a/elements/studentname/version.php b/elements/studentname/version.php index d854b1f..c2a13c4 100644 --- a/elements/studentname/version.php +++ b/elements/studentname/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2013041200; +$plugin->version = 2013060600; $plugin->requires = 2013040500; // Requires this Moodle version. $plugin->component = 'customcertelement_studentname'; diff --git a/elements/text/lib.php b/elements/text/lib.php index 833ef00..703dcb7 100644 --- a/elements/text/lib.php +++ b/elements/text/lib.php @@ -29,16 +29,26 @@ require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); class customcert_element_text extends customcert_element_base { + /** + * Constructor. + * + * @param stdClass $element the element data + */ + function __construct($element) { + parent::__construct($element); + + $this->element->text = (!empty($element->data)) ? $element->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) { - $mform->addElement('textarea', 'text_' . $this->element->id, get_string('text', 'customcertelement_text')); - $mform->setType('text_' . $this->element->id, PARAM_RAW); - $mform->setDefault('text_' . $this->element->id, $this->element->data); - $mform->addHelpButton('text_' . $this->element->id, 'text', 'customcertelement_text'); + $mform->addElement('textarea', 'text', get_string('text', 'customcertelement_text')); + $mform->setType('text', PARAM_RAW); + $mform->addHelpButton('text', 'text', 'customcertelement_text'); parent::render_form_elements($mform); } @@ -51,8 +61,7 @@ class customcert_element_text extends customcert_element_base { * @return string the text */ public function save_unique_data($data) { - $text = 'text_' . $this->element->id; - return $data->$text; + return $data->text; } /** diff --git a/elements/text/version.php b/elements/text/version.php index f773cf5..d0e6b92 100644 --- a/elements/text/version.php +++ b/elements/text/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); -$plugin->version = 2013041200; +$plugin->version = 2013060700; $plugin->requires = 2013040500; // Requires this Moodle version. $plugin->component = 'customcertelement_text'; diff --git a/lang/en/customcert.php b/lang/en/customcert.php index f816b3d..9b7c0b0 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -28,11 +28,13 @@ $string['addelement'] = 'Add element'; $string['awardedto'] = 'Awarded to'; $string['code'] = 'Code'; $string['copy'] = 'Copy'; +$string['configure'] = 'Configure'; $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['deleteconfirm'] = 'Delete confirmation'; $string['deleteelement'] = 'Delete element'; @@ -40,6 +42,10 @@ $string['deleteelementconfirm'] = 'Are you sure you want to delete this element? $string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['description'] = 'Description'; $string['editcustomcert'] = 'Edit custom certificate'; +$string['elementname'] = 'Element name'; +$string['elementname_help'] = 'This will be the name used to identify this element when editing a custom certificate. For example, you may have multiple images on a page and will want to distinguish between them quickly when editing the certificate. Note: this will not displayed on the PDF.'; +$string['elements'] = 'Elements'; +$string['elements_help'] = 'These are the list of elements that will be displayed on this PDF page.'; $string['errorloadingelement'] = 'Error loading the element "{$a}"'; $string['errorsavingelement'] = 'Error saving the element "{$a}"'; $string['font'] = 'Font'; @@ -87,6 +93,7 @@ $string['setprotection_help'] = 'Choose the actions you wish to prevent users fr $string['summaryofissue'] = 'Summary of issue'; $string['templatename'] = 'Template name'; $string['templatenameexists'] = 'That template name is currently in use, please choose another.'; +$string['type'] = 'Type'; $string['uploadimage'] = 'Upload image'; $string['viewcustomcertissues'] = 'View {$a} issued custom certificates'; $string['width'] = 'Width'; diff --git a/lib.php b/lib.php index dc6e621..a890bc6 100644 --- a/lib.php +++ b/lib.php @@ -496,16 +496,6 @@ function customcert_save_page_data($data) { $p->timemodified = $time; // Update the 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) { - // Get an instance of the element class. - if ($e = customcert_get_element_instance($element)) { - $e->save_form_elements($data); - } - } - } } } } @@ -531,17 +521,6 @@ function customcert_get_element_instance($element) { return false; } -/** - * 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) { - $classname = "customcert_element_{$element}"; - $classname::add_element($element, $pageid); -} - /** * Handles adding another page to the customcert. * @@ -550,23 +529,14 @@ function customcert_add_element($element, $pageid) { function customcert_add_page($data) { global $DB; - // If no pageid is passed then we are creating the first page. - if (empty($data->pageid)) { - $pagenumber = 1; - } else { // Create a page after an existing one. - // Get the page we are inserting the new one after. - $currentpage = $DB->get_record('customcert_pages', array('id' => $data->pageid), '*', MUST_EXIST); - - // Increase the page numbers of the pages that are going - // to be in front of the new page we are creating - $sql = "UPDATE {customcert_pages} - SET pagenumber = pagenumber + 1 - WHERE customcertid = :customcertid - AND pagenumber > :pagenumber"; - $DB->execute($sql, array('customcertid' => $currentpage->customcertid, - 'pagenumber' => $currentpage->pagenumber)); - - $pagenumber = $currentpage->pagenumber + 1; + // Set the page number to 1 to begin with. + $pagenumber = 1; + // Get the max page number. + $sql = "SELECT MAX(pagenumber) as maxpage + FROM {customcert_pages} cp + WHERE cp.customcertid = :customcertid"; + if ($maxpage = $DB->get_record_sql($sql, array('customcertid' => $data->id))) { + $pagenumber = $maxpage->maxpage + 1; } // Store time in a variable. diff --git a/mod_form.php b/mod_form.php index c98d334..dc2acae 100644 --- a/mod_form.php +++ b/mod_form.php @@ -43,7 +43,7 @@ class mod_customcert_mod_form extends moodleform_mod { if (!empty($CFG->formatstringstriptags)) { $mform->setType('name', PARAM_TEXT); } else { - $mform->setType('name', PARAM_CLEAN); + $mform->setType('name', PARAM_CLEANHTML); } $mform->addRule('name', null, 'required', null, 'client');