diff --git a/db/install.xml b/db/install.xml index 1581fe4..fdaa1df 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -59,6 +59,7 @@ + diff --git a/edit.php b/edit.php index 7a49ec6..aaa14e6 100644 --- a/edit.php +++ b/edit.php @@ -32,6 +32,8 @@ require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); $cmid = required_param('cmid', PARAM_INT); $moveup = optional_param('moveup', 0, PARAM_INT); $movedown = optional_param('movedown', 0, PARAM_INT); +$emoveup = optional_param('emoveup', 0, PARAM_INT); +$emovedown = optional_param('emovedown', 0, PARAM_INT); $deleteelement = optional_param('deleteelement', 0, PARAM_INT); $deletepage = optional_param('deletepage', 0, PARAM_INT); $confirm = optional_param('confirm', 0, PARAM_INT); @@ -62,6 +64,22 @@ if ((!empty($moveup)) || (!empty($movedown))) { $DB->set_field('customcert_pages', 'pagenumber', $swapcertpage->pagenumber, array('id' => $movecertpage->id)); $DB->set_field('customcert_pages', 'pagenumber', $movecertpage->pagenumber, array('id' => $swapcertpage->id)); } +} else if ((!empty($emoveup)) || (!empty($emovedown))) { // Check if we are moving a custom certificate element. + // Check if we are moving an element up. + if (!empty($emoveup)) { + if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $emoveup))) { + $swapcertelement = $DB->get_record('customcert_elements', array('sequence' => $movecertelement->sequence - 1)); + } + } else { // Must be moving a element down. + if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $emovedown))) { + $swapcertelement = $DB->get_record('customcert_elements', array('sequence' => $movecertelement->sequence + 1)); + } + } + // Check that there is an element to move, and an element to swap it with. + if ($swapcertelement && $movecertelement) { + $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. diff --git a/edit_form.php b/edit_form.php index 2dff1cd..b087866 100644 --- a/edit_form.php +++ b/edit_form.php @@ -240,7 +240,9 @@ class mod_customcert_edit_form extends moodleform { // 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')) { + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), '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"; @@ -250,6 +252,16 @@ class mod_customcert_edit_form extends moodleform { // Add element header. $mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " . get_string('pluginname', 'customcertelement_' . $element->element)); + // 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 = $pagenum; // Get the classname. diff --git a/elements/element.class.php b/elements/element.class.php index 91d5e97..f537b9b 100644 --- a/elements/element.class.php +++ b/elements/element.class.php @@ -62,11 +62,36 @@ class customcert_element_base { $data->colour = '#000000'; $data->posx = '250'; $data->posy = '250'; + $data->sequence = customcert_element_base::get_element_sequence($pageid); $data->timecreated = time(); $DB->insert_record('customcert_elements', $data); } + /** + * Returns the sequence on a specified customcert page for a + * newly created element. + * + * @param int $pageid the id of the page we are adding this element to + * @return int the element number + */ + public static function get_element_sequence($pageid) { + global $DB; + + // Set the sequence of the element we are creating. + $sequence = 1; + // Check if there already elements that exist, if so, overwrite value. + $sql = "SELECT MAX(sequence) as maxsequence + FROM {customcert_elements} + WHERE pageid = :id"; + // Get the current max sequence on this page and add 1 to get the new sequence. + if ($maxseq = $DB->get_record_sql($sql, array('id' => $pageid))) { + $sequence = $maxseq->maxsequence + 1; + } + + return $sequence; + } + /** * This function renders the form elements when adding a customcert element. * Can be overridden if more functionality is needed. diff --git a/lib.php b/lib.php index be400cb..9df5854 100644 --- a/lib.php +++ b/lib.php @@ -767,7 +767,7 @@ function customcert_generate_pdf($customcert, $userid) { // Add the page to the PDF. $pdf->AddPage($page->orientation, array($page->width, $page->height)); // Get the elements for the page. - if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) { // Loop through and display. foreach ($elements as $element) { // Check that the standard class file exists.