Fixed sequence values when deleting an element

This commit is contained in:
Mark Nelson 2015-03-13 20:53:11 -07:00
parent 53cb267836
commit e85807e298
2 changed files with 29 additions and 6 deletions

View file

@ -118,12 +118,7 @@ if ((!empty($moveup)) || (!empty($movedown))) {
}
} 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();
}
customcert_delete_element($deleteelement);
} else {
// Set deletion flag to true.
$deleting = true;

View file

@ -274,6 +274,34 @@ function customcert_add_page($data) {
$DB->insert_record('customcert_pages', $page);
}
/**
* Handles deleting an element from the customcert.
*
* @param int $elementid the customcert page
*/
function customcert_delete_element($elementid) {
global $DB;
// Ensure element exists and delete it.
$element = $DB->get_record('customcert_elements', array('id' => $elementid), '*', MUST_EXIST);
// Get an instance of the element class.
if ($e = customcert_get_element_instance($element)) {
$e->delete_element();
} else {
// The plugin files are missing, so just remove the entry from the DB.
$DB->delete_records('customcert_elements', array('id' => $elementid));
}
// Now we want to decrease the sequence numbers of the elements
// that are greater than the element we deleted.
$sql = "UPDATE {customcert_elements}
SET sequence = sequence - 1
WHERE pageid = :pageid
AND sequence > :sequence";
$DB->execute($sql, array('pageid' => $element->pageid, 'sequence' => $element->sequence));
}
/**
* Handles deleting a page from the customcert.
*