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:
parent
c0d081f849
commit
4962b72938
8
edit.php
8
edit.php
|
@ -160,6 +160,9 @@ if ($data = $mform->get_data()) {
|
|||
$element = "element_" . $pageid;
|
||||
$element = $data->$element;
|
||||
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.
|
||||
|
@ -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.
|
||||
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid));
|
||||
redirect($url);
|
||||
|
|
|
@ -72,21 +72,18 @@ class mod_customcert_edit_form extends moodleform {
|
|||
foreach ($pages as $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('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions);
|
||||
|
||||
$mform->closeHeaderBefore('submitbtn');
|
||||
|
||||
// Add the submit buttons.
|
||||
$group = array();
|
||||
$group[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
|
||||
$group[] = $mform->createElement('submit', 'addcertpage', get_string('addcertpage', 'customcert'));
|
||||
$mform->addElement('group', 'loadtemplategroup', '', $group, '', false);
|
||||
$group[] = $mform->createElement('submit', 'submitbtn', get_string('savechanges'));
|
||||
$mform->addElement('group', 'submitbtngroup', '', $group, '', false);
|
||||
|
||||
$mform->addElement('hidden', 'id');
|
||||
$mform->setType('id', PARAM_INT);
|
||||
|
@ -175,20 +172,14 @@ class mod_customcert_edit_form extends moodleform {
|
|||
*
|
||||
* @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;
|
||||
|
||||
// Create the form object.
|
||||
$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;
|
||||
$pagenum = $page->pagenumber;
|
||||
}
|
||||
|
||||
$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'));
|
||||
$mform->addElement('group', 'elementgroup', '', $group, '', false);
|
||||
|
||||
// Add option to delete this page if it is not the first page.
|
||||
if ($pagenum > 1) {
|
||||
$mform->addElement('submit', 'addcertpage_' . $pageid, get_string('addcertpage', 'customcert'));
|
||||
|
||||
// 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('submit', 'deletecertpage_' . $pageid, get_string('deletecertpage', 'customcert'));
|
||||
$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.
|
||||
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'sequence ASC')) {
|
||||
// Get the total number of elements.
|
||||
|
@ -282,5 +273,4 @@ class mod_customcert_edit_form extends moodleform {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
54
lib.php
54
lib.php
|
@ -53,8 +53,12 @@ function customcert_add_instance($data, $mform) {
|
|||
$data->protection = customcert_set_protection($data);
|
||||
$data->timecreated = time();
|
||||
$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.
|
||||
$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.
|
||||
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $data->id))) {
|
||||
// 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) {
|
||||
global $DB;
|
||||
|
||||
// Set the number of the page we are creating.
|
||||
$pagenum = 1;
|
||||
// Check if there already pages that exist, if so, overwrite value.
|
||||
$sql = "SELECT MAX(pagenumber) as maxpagenumber
|
||||
FROM {customcert_pages}
|
||||
WHERE customcertid = :id";
|
||||
// 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))) {
|
||||
$pagenum = $maxnum->maxpagenumber + 1;
|
||||
// If no pageid is passed then we are creating the first page.
|
||||
if (empty($data->pageid)) {
|
||||
$pagenumber = 1;
|
||||
} else { // Create a page after an existing one.
|
||||
// Get the page we are inserting the new one after.
|
||||
$currentpage = $DB->get_record('customcert_pages', array('id' => $data->pageid), '*', MUST_EXIST);
|
||||
|
||||
// Increase the page numbers of the pages that are going
|
||||
// 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.
|
||||
|
@ -599,7 +593,7 @@ function customcert_add_page($data) {
|
|||
$page->orientation = 'P';
|
||||
$page->width = '210';
|
||||
$page->height = '297';
|
||||
$page->pagenumber = $pagenum;
|
||||
$page->pagenumber = $pagenumber;
|
||||
$page->timecreated = $time;
|
||||
$page->timemodified = $time;
|
||||
|
||||
|
@ -626,7 +620,7 @@ function customcert_delete_page($pageid) {
|
|||
foreach ($elements as $element) {
|
||||
// Get an instance of the element class.
|
||||
if ($e = customcert_get_element_instance($element)) {
|
||||
return $e->delete_element();
|
||||
$e->delete_element();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue