Created one function to handle move_* actions

This changes the name of a column in a DB table.
No upgrade script has been written as this activity
has not been officially released.
This commit is contained in:
Mark Nelson 2016-02-20 17:46:31 +08:00
parent d848bc73e8
commit 6889b04df0
5 changed files with 42 additions and 83 deletions

View file

@ -50,7 +50,7 @@ class backup_customcert_activity_structure_step extends backup_activity_structur
$pages = new backup_nested_element('pages'); $pages = new backup_nested_element('pages');
$page = new backup_nested_element('page', array('id'), array( $page = new backup_nested_element('page', array('id'), array(
'customcertid', 'width', 'height', 'leftmargin', 'rightmargin', 'customcertid', 'width', 'height', 'leftmargin', 'rightmargin',
'pagenumber', 'timecreated', 'timemodified')); 'sequence', 'timecreated', 'timemodified'));
// The elements. // The elements.
$element = new backup_nested_element('element', array('id'), array( $element = new backup_nested_element('element', array('id'), array(

View file

@ -58,7 +58,7 @@ class edit_form extends \moodleform {
// Get the number of pages for this module. // Get the number of pages for this module.
if (isset($this->_customdata['tid'])) { if (isset($this->_customdata['tid'])) {
$this->tid = $this->_customdata['tid']; $this->tid = $this->_customdata['tid'];
if ($pages = $DB->get_records('customcert_pages', array('templateid' => $this->tid), 'pagenumber')) { if ($pages = $DB->get_records('customcert_pages', array('templateid' => $this->tid), 'sequence')) {
$this->numpages = count($pages); $this->numpages = count($pages);
foreach ($pages as $p) { foreach ($pages as $p) {
$this->add_customcert_page_elements($p); $this->add_customcert_page_elements($p);
@ -68,7 +68,7 @@ class edit_form extends \moodleform {
// Create a 'fake' page to display the elements on - not yet saved in the DB. // Create a 'fake' page to display the elements on - not yet saved in the DB.
$page = new \stdClass(); $page = new \stdClass();
$page->id = 1; $page->id = 1;
$page->pagenumber = 1; $page->sequence = 1;
$this->add_customcert_page_elements($page); $this->add_customcert_page_elements($page);
} }
@ -186,7 +186,7 @@ class edit_form extends \moodleform {
$mform =& $this->_form; $mform =& $this->_form;
if ($this->numpages > 1) { if ($this->numpages > 1) {
$mform->addElement('header', 'page_' . $page->id, get_string('page', 'customcert', $page->pagenumber)); $mform->addElement('header', 'page_' . $page->id, get_string('page', 'customcert', $page->sequence));
} }
$editlink = '/mod/customcert/edit.php'; $editlink = '/mod/customcert/edit.php';
@ -196,12 +196,12 @@ class edit_form extends \moodleform {
// Place the ordering arrows. // Place the ordering arrows.
// Only display the move up arrow if it is not the first. // Only display the move up arrow if it is not the first.
if ($page->pagenumber > 1) { if ($page->sequence > 1) {
$url = new \moodle_url($editlink, $editlinkparams + array('action' => 'pmoveup', 'aid' => $page->id)); $url = new \moodle_url($editlink, $editlinkparams + array('action' => 'pmoveup', 'aid' => $page->id));
$mform->addElement('html', $OUTPUT->action_icon($url, new \pix_icon('t/up', get_string('moveup')))); $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. // Only display the move down arrow if it is not the last.
if ($page->pagenumber < $this->numpages) { if ($page->sequence < $this->numpages) {
$url = new \moodle_url($editlink, $editlinkparams + array('action' => 'pmovedown', 'aid' => $page->id)); $url = new \moodle_url($editlink, $editlinkparams + array('action' => 'pmovedown', 'aid' => $page->id));
$mform->addElement('html', $OUTPUT->action_icon($url, new \pix_icon('t/down', get_string('movedown')))); $mform->addElement('html', $OUTPUT->action_icon($url, new \pix_icon('t/down', get_string('movedown'))));
} }

View file

@ -84,13 +84,13 @@ class template {
global $DB; global $DB;
// Set the page number to 1 to begin with. // Set the page number to 1 to begin with.
$pagenumber = 1; $sequence = 1;
// Get the max page number. // Get the max page number.
$sql = "SELECT MAX(pagenumber) as maxpage $sql = "SELECT MAX(sequence) as maxpage
FROM {customcert_pages} cp FROM {customcert_pages} cp
WHERE cp.templateid = :templateid"; WHERE cp.templateid = :templateid";
if ($maxpage = $DB->get_record_sql($sql, array('templateid' => $this->id))) { if ($maxpage = $DB->get_record_sql($sql, array('templateid' => $this->id))) {
$pagenumber = $maxpage->maxpage + 1; $sequence = $maxpage->maxpage + 1;
} }
// New page creation. // New page creation.
@ -98,7 +98,7 @@ class template {
$page->templateid = $this->id; $page->templateid = $this->id;
$page->width = '210'; $page->width = '210';
$page->height = '297'; $page->height = '297';
$page->pagenumber = $pagenumber; $page->sequence = $sequence;
$page->timecreated = time(); $page->timecreated = time();
$page->timemodified = $page->timecreated; $page->timemodified = $page->timecreated;
@ -209,10 +209,10 @@ class template {
// Now we want to decrease the page number values of // Now we want to decrease the page number values of
// the pages that are greater than the page we deleted. // the pages that are greater than the page we deleted.
$sql = "UPDATE {customcert_pages} $sql = "UPDATE {customcert_pages}
SET pagenumber = pagenumber - 1 SET sequence = sequence - 1
WHERE templateid = :templateid WHERE templateid = :templateid
AND pagenumber > :pagenumber"; AND sequence > :sequence";
$DB->execute($sql, array('templateid' => $this->id, 'pagenumber' => $page->pagenumber)); $DB->execute($sql, array('templateid' => $this->id, 'sequence' => $page->sequence));
} }
/** /**
@ -254,7 +254,7 @@ class template {
require_once($CFG->libdir . '/pdflib.php'); require_once($CFG->libdir . '/pdflib.php');
// Get the pages for the template, there should always be at least one page for each template. // Get the pages for the template, there should always be at least one page for each template.
if ($pages = $DB->get_records('customcert_pages', array('templateid' => $this->id), 'pagenumber ASC')) { if ($pages = $DB->get_records('customcert_pages', array('templateid' => $this->id), 'sequence ASC')) {
// Create the pdf object. // Create the pdf object.
$pdf = new \pdf(); $pdf = new \pdf();
@ -299,78 +299,37 @@ class template {
} }
/** /**
* Handles moving the certificate page up. * Handles moving an item on a template.
* *
* @param int $pageid * @param string $itemname the item we are moving
* @param int $itemid the id of the item
* @param string $direction the direction
*/ */
public function move_page_up($pageid) { public function move_item($itemname, $itemid, $direction) {
global $DB; global $DB;
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $pageid))) { $table = 'customcert_';
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber - 1)); if ($itemname == 'page') {
$table .= 'pages';
} else { // Must be an element.
$table .= 'elements';
} }
// Check that there is a page to move, and a page to swap it with. if ($moveitem = $DB->get_record($table, array('id' => $itemid))) {
if (isset($swapcertpage) && $movecertpage) { // Check which direction we are going.
$DB->set_field('customcert_pages', 'pagenumber', $swapcertpage->pagenumber, array('id' => $movecertpage->id)); if ($direction == 'up') {
$DB->set_field('customcert_pages', 'pagenumber', $movecertpage->pagenumber, array('id' => $swapcertpage->id)); $sequence = $moveitem->sequence - 1;
} else { // Must be down.
$sequence = $moveitem->sequence + 1;
} }
// Get the item we will be swapping with it.
$swapitem = $DB->get_record($table, array('sequence' => $sequence));
} }
/** // Check that there is an item to move, and an item to swap it with.
* Handles moving the certificate page down. if ($moveitem && isset($swapitem)) {
* $DB->set_field($table, 'sequence', $swapitem->sequence, array('id' => $moveitem->id));
* @param int $pageid $DB->set_field($table, 'sequence', $moveitem->sequence, array('id' => $swapitem->id));
*/
public function move_page_down($pageid) {
global $DB;
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $pageid))) {
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber + 1));
}
// Check that there is a page to move, and a page to swap it with.
if (isset($swapcertpage) && $movecertpage) {
$DB->set_field('customcert_pages', 'pagenumber', $swapcertpage->pagenumber, array('id' => $movecertpage->id));
$DB->set_field('customcert_pages', 'pagenumber', $movecertpage->pagenumber, array('id' => $swapcertpage->id));
}
}
/**
* Handles moving the certificate element up.
*
* @param int $elementid
*/
public function move_element_up($elementid) {
global $DB;
if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $elementid))) {
$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 (isset($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));
}
}
/**
* Handles moving the certificate element down.
*
* @param int $elementid
*/
public function move_element_down($elementid) {
global $DB;
if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $elementid))) {
$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 (isset($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));
} }
} }

View file

@ -56,7 +56,7 @@
<FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/> <FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="leftmargin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/> <FIELD NAME="leftmargin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="rightmargin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/> <FIELD NAME="rightmargin" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="pagenumber" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/> <FIELD NAME="sequence" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/> <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/> <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS> </FIELDS>

View file

@ -78,16 +78,16 @@ $deleting = false;
if ($tid) { if ($tid) {
switch ($action) { switch ($action) {
case 'pmoveup' : case 'pmoveup' :
$template->move_page_up($actionid); $template->move_item('page', $actionid, 'up');
break; break;
case 'pmovedown' : case 'pmovedown' :
$template->move_page_down($actionid); $template->move_item('page', $actionid, 'down');
break; break;
case 'emoveup' : case 'emoveup' :
$template->move_element_up($actionid); $template->move_item('element', $actionid, 'up');
break; break;
case 'emovedown' : case 'emovedown' :
$template->move_element_down($actionid); $template->move_item('element', $actionid, 'down');
break; break;
case 'deletepage' : case 'deletepage' :
if (!empty($confirm)) { // Check they have confirmed the deletion. if (!empty($confirm)) { // Check they have confirmed the deletion.