Fixed issue where elements for the customcert page with the id 1 were being displayed (if it existed) for newly created customcerts

This commit is contained in:
Mark Nelson 2013-04-09 13:27:55 +08:00
parent d6428b89d1
commit 08c4f03ee7
2 changed files with 25 additions and 22 deletions

View file

@ -238,27 +238,30 @@ class mod_customcert_edit_form extends moodleform {
$mform->addElement('html', html_writer::end_tag('div')); $mform->addElement('html', html_writer::end_tag('div'));
} }
// Check if there are elements to add. // Check that this page is not a newly created one with no data in the database.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'id ASC')) { if (!is_null($page)) {
// Loop through and add the ones present. // Check if there are elements to add.
foreach ($elements as $element) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'id ASC')) {
$classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php"; // Loop through and add the ones present.
// It's possible this element was added to the database then the folder was deleted, if foreach ($elements as $element) {
// this is the case we do not want to render these elements as an error will occur. $classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php";
if (file_exists($classfile)) { // It's possible this element was added to the database then the folder was deleted, if
// Add element header. // this is the case we do not want to render these elements as an error will occur.
$mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " . if (file_exists($classfile)) {
get_string('pluginname', 'customcertelement_' . $element->element)); // Add element header.
// Add the page number to the element so we can use within the element. $mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " .
$element->pagenum = $pagenum; get_string('pluginname', 'customcertelement_' . $element->element));
// Get the classname. // Add the page number to the element so we can use within the element.
$classname = "customcert_element_{$element->element}"; $element->pagenum = $pagenum;
$e = new $classname($element); // Get the classname.
$e->render_form_elements($mform); $classname = "customcert_element_{$element->element}";
// Add this to the objects array. $e = new $classname($element);
$this->elementobjects[] = $e; $e->render_form_elements($mform);
// Add submit button to delete this. // Add this to the objects array.
$mform->addElement('submit', 'deleteelement_' . $element->id, get_string('delete', 'customcert')); $this->elementobjects[] = $e;
// Add submit button to delete this.
$mform->addElement('submit', 'deleteelement_' . $element->id, get_string('delete', 'customcert'));
}
} }
} }
} }

View file

@ -442,7 +442,7 @@ function customcert_save_page_data($data) {
// Get the total number of pages that exist for this customcert. // Get the total number of pages that exist for this customcert.
$totalpages = $DB->count_records('customcert_pages', array('customcertid' => $data->id)); $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) { if ($totalpages == 0) {
// Create the page to insert. // Create the page to insert.
$page = new stdClass(); $page = new stdClass();