From 08c4f03ee71654212fb073ae45659f48448d8d39 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 9 Apr 2013 13:27:55 +0800 Subject: [PATCH] Fixed issue where elements for the customcert page with the id 1 were being displayed (if it existed) for newly created customcerts --- edit_form.php | 45 ++++++++++++++++++++++++--------------------- lib.php | 2 +- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/edit_form.php b/edit_form.php index e41d05f..ee018cd 100644 --- a/edit_form.php +++ b/edit_form.php @@ -238,27 +238,30 @@ class mod_customcert_edit_form extends moodleform { $mform->addElement('html', html_writer::end_tag('div')); } - // Check if there are elements to add. - if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), '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)) { - // Add element header. - $mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " . - get_string('pluginname', 'customcertelement_' . $element->element)); - // Add the page number to the element so we can use within the element. - $element->pagenum = $pagenum; - // Get the classname. - $classname = "customcert_element_{$element->element}"; - $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')); + // Check that this page is not a newly created one with no data in the database. + if (!is_null($page)) { + // 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)) { + // Add element header. + $mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " . + get_string('pluginname', 'customcertelement_' . $element->element)); + // Add the page number to the element so we can use within the element. + $element->pagenum = $pagenum; + // Get the classname. + $classname = "customcert_element_{$element->element}"; + $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')); + } } } } diff --git a/lib.php b/lib.php index b12e5ec..2c99887 100644 --- a/lib.php +++ b/lib.php @@ -442,7 +442,7 @@ function customcert_save_page_data($data) { // Get the total number of pages that exist for this customcert. $totalpages = $DB->count_records('customcert_pages', array('customcertid' => $data->id)); - // Check if this file has any pages yet, if not we are inserting. + // Check if this customcert has any pages yet, if not we are inserting. if ($totalpages == 0) { // Create the page to insert. $page = new stdClass();