The first page is now created when the customcert instance is added and the button to create new pages has been moved

This commit is contained in:
Mark Nelson 2013-05-16 17:12:51 +08:00
parent c0d081f849
commit 4962b72938
3 changed files with 103 additions and 121 deletions

View file

@ -160,6 +160,9 @@ if ($data = $mform->get_data()) {
$element = "element_" . $pageid; $element = "element_" . $pageid;
$element = $data->$element; $element = $data->$element;
customcert_add_element($element, $pageid); customcert_add_element($element, $pageid);
} else if (strpos($key, 'addcertpage_') !== false) { // Check if they chose to add a page.
$data->pageid = str_replace('addcertpage_', '', $key);
customcert_add_page($data);
} }
// Check if we are deleting either a page or an element. // Check if we are deleting either a page or an element.
@ -178,11 +181,6 @@ if ($data = $mform->get_data()) {
} }
} }
// Check if they chose to add a page.
if (!empty($data->addcertpage)) {
customcert_add_page($data);
}
// Redirect to the editing page to show form with recent updates. // Redirect to the editing page to show form with recent updates.
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid)); $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid));
redirect($url); redirect($url);

View file

@ -72,21 +72,18 @@ class mod_customcert_edit_form extends moodleform {
foreach ($pages as $p) { foreach ($pages as $p) {
$this->add_customcert_page_elements($p); $this->add_customcert_page_elements($p);
} }
} else {
$this->add_customcert_page_elements();
} }
$mform->closeHeaderBefore('addcertpage');
$mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert')); $mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert'));
$mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions); $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
$mform->closeHeaderBefore('submitbtn');
// Add the submit buttons. // Add the submit buttons.
$group = array(); $group = array();
$group[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges')); $group[] = $mform->createElement('submit', 'submitbtn', get_string('savechanges'));
$group[] = $mform->createElement('submit', 'addcertpage', get_string('addcertpage', 'customcert')); $mform->addElement('group', 'submitbtngroup', '', $group, '', false);
$mform->addElement('group', 'loadtemplategroup', '', $group, '', false);
$mform->addElement('hidden', 'id'); $mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT); $mform->setType('id', PARAM_INT);
@ -175,20 +172,14 @@ class mod_customcert_edit_form extends moodleform {
* *
* @param stdClass $page the customcert page * @param stdClass $page the customcert page
**/ **/
private function add_customcert_page_elements($page = null) { private function add_customcert_page_elements($page) {
global $CFG, $DB, $OUTPUT; global $CFG, $DB, $OUTPUT;
// Create the form object. // Create the form object.
$mform =& $this->_form; $mform =& $this->_form;
// If page is null we are adding a customcert, not editing one, so set pageid to 1.
if (is_null($page)) {
$pageid = 1;
$pagenum = 1;
} else {
$pageid = $page->id; $pageid = $page->id;
$pagenum = $page->pagenumber; $pagenum = $page->pagenumber;
}
$mform->addElement('header', 'page_' . $pageid, get_string('page', 'customcert', $pagenum)); $mform->addElement('header', 'page_' . $pageid, get_string('page', 'customcert', $pagenum));
@ -227,15 +218,15 @@ class mod_customcert_edit_form extends moodleform {
$group[] = $mform->createElement('submit', 'addelement_' . $pageid, get_string('addelement', 'customcert')); $group[] = $mform->createElement('submit', 'addelement_' . $pageid, get_string('addelement', 'customcert'));
$mform->addElement('group', 'elementgroup', '', $group, '', false); $mform->addElement('group', 'elementgroup', '', $group, '', false);
// Add option to delete this page if it is not the first page. $mform->addElement('submit', 'addcertpage_' . $pageid, get_string('addcertpage', 'customcert'));
if ($pagenum > 1) {
// Add option to delete this page if there is more than one page.
if ($this->numpages > 1) {
$mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton'))); $mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton')));
$mform->addElement('submit', 'deletecertpage_' . $pageid, get_string('deletecertpage', 'customcert')); $mform->addElement('submit', 'deletecertpage_' . $pageid, get_string('deletecertpage', 'customcert'));
$mform->addElement('html', html_writer::end_tag('div')); $mform->addElement('html', html_writer::end_tag('div'));
} }
// 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. // Check if there are elements to add.
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'sequence ASC')) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'sequence ASC')) {
// Get the total number of elements. // Get the total number of elements.
@ -282,5 +273,4 @@ class mod_customcert_edit_form extends moodleform {
} }
} }
} }
}
} }

54
lib.php
View file

@ -53,8 +53,12 @@ function customcert_add_instance($data, $mform) {
$data->protection = customcert_set_protection($data); $data->protection = customcert_set_protection($data);
$data->timecreated = time(); $data->timecreated = time();
$data->timemodified = $data->timecreated; $data->timemodified = $data->timecreated;
$data->id = $DB->insert_record('customcert', $data);
return $DB->insert_record('customcert', $data); // Add a page to this certificate.
customcert_add_page($data);
return $data->id;
} }
/** /**
@ -488,23 +492,6 @@ function customcert_save_page_data($data) {
// Set the time to a variable. // Set the time to a variable.
$time = time(); $time = time();
// Get the total number of pages that exist for this customcert.
$totalpages = $DB->count_records('customcert_pages', array('customcertid' => $data->id));
// Check if this customcert has any pages yet, if not we are inserting.
if ($totalpages == 0) {
// Create the page to insert.
$page = new stdClass();
$page->customcertid = $data->id;
$page->orientation = $data->orientation_1;
$page->width = $data->width_1;
$page->height = $data->height_1;
$page->pagenumber = 1;
$page->timecreated = $time;
$page->timemodified = $time;
// Insert the page.
$DB->insert_record('customcert_pages', $page);
} else {
// Get the existing pages and save the page data. // Get the existing pages and save the page data.
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $data->id))) { if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $data->id))) {
// Loop through existing pages. // Loop through existing pages.
@ -534,7 +521,6 @@ function customcert_save_page_data($data) {
} }
} }
} }
}
} }
/** /**
@ -579,15 +565,23 @@ function customcert_add_element($element, $pageid) {
function customcert_add_page($data) { function customcert_add_page($data) {
global $DB; global $DB;
// Set the number of the page we are creating. // If no pageid is passed then we are creating the first page.
$pagenum = 1; if (empty($data->pageid)) {
// Check if there already pages that exist, if so, overwrite value. $pagenumber = 1;
$sql = "SELECT MAX(pagenumber) as maxpagenumber } else { // Create a page after an existing one.
FROM {customcert_pages} // Get the page we are inserting the new one after.
WHERE customcertid = :id"; $currentpage = $DB->get_record('customcert_pages', array('id' => $data->pageid), '*', MUST_EXIST);
// Get the current max page number and add 1 to page number for new page.
if ($maxnum = $DB->get_record_sql($sql, array('id' => $data->id))) { // Increase the page numbers of the pages that are going
$pagenum = $maxnum->maxpagenumber + 1; // to be in front of the new page we are creating
$sql = "UPDATE {customcert_pages}
SET pagenumber = pagenumber + 1
WHERE customcertid = :customcertid
AND pagenumber > :pagenumber";
$DB->execute($sql, array('customcertid' => $currentpage->customcertid,
'pagenumber' => $currentpage->pagenumber));
$pagenumber = $currentpage->pagenumber + 1;
} }
// Store time in a variable. // Store time in a variable.
@ -599,7 +593,7 @@ function customcert_add_page($data) {
$page->orientation = 'P'; $page->orientation = 'P';
$page->width = '210'; $page->width = '210';
$page->height = '297'; $page->height = '297';
$page->pagenumber = $pagenum; $page->pagenumber = $pagenumber;
$page->timecreated = $time; $page->timecreated = $time;
$page->timemodified = $time; $page->timemodified = $time;
@ -626,7 +620,7 @@ function customcert_delete_page($pageid) {
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // Get an instance of the element class.
if ($e = customcert_get_element_instance($element)) { if ($e = customcert_get_element_instance($element)) {
return $e->delete_element(); $e->delete_element();
} }
} }
} }