From 4962b729382027a4495d61c163c2e769710a0b46 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 16 May 2013 17:12:51 +0800 Subject: [PATCH] The first page is now created when the customcert instance is added and the button to create new pages has been moved --- edit.php | 8 ++-- edit_form.php | 114 +++++++++++++++++++++++--------------------------- lib.php | 102 +++++++++++++++++++++----------------------- 3 files changed, 103 insertions(+), 121 deletions(-) diff --git a/edit.php b/edit.php index 2108e09..f5aacd5 100644 --- a/edit.php +++ b/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); diff --git a/edit_form.php b/edit_form.php index 7a59c15..b66f003 100644 --- a/edit_form.php +++ b/edit_form.php @@ -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; - } + $pageid = $page->id; + $pagenum = $page->pagenumber; $mform->addElement('header', 'page_' . $pageid, get_string('page', 'customcert', $pagenum)); @@ -227,58 +218,57 @@ 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. - $numelements = count($elements); - // Loop through and add the ones present. - foreach ($elements as $element) { - $classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php"; - // It's possible this element was added to the database then the folder was deleted, if - // this is the case we do not want to render these elements as an error will occur. - if (file_exists($classfile)) { - // Add element header. - $mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " . - get_string('pluginname', 'customcertelement_' . $element->element)); - // We do not need to expand these elements if the modified time is greater than the created time as it - // means the values have already been altered by the user - ie. the element has not just been created. - if ($element->timemodified > $element->timecreated) { - $mform->setExpanded('headerelement_' . $element->id, false); - } else { - $mform->setExpanded('headerelement_' . $element->id, true); - } - // 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. - $classname = "customcert_element_{$element->element}"; - $e = new $classname($element); - $e->render_form_elements($mform); - // Add this to the objects array. - $this->elementobjects[] = $e; - // Add submit button to delete this. - $mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton'))); - $mform->addElement('submit', 'deleteelement_' . $element->id, get_string('deleteelement', 'customcert')); - $mform->addElement('html', html_writer::end_tag('div')); + // 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. + $numelements = count($elements); + // Loop through and add the ones present. + foreach ($elements as $element) { + $classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php"; + // It's possible this element was added to the database then the folder was deleted, if + // this is the case we do not want to render these elements as an error will occur. + if (file_exists($classfile)) { + // Add element header. + $mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " . + get_string('pluginname', 'customcertelement_' . $element->element)); + // We do not need to expand these elements if the modified time is greater than the created time as it + // means the values have already been altered by the user - ie. the element has not just been created. + if ($element->timemodified > $element->timecreated) { + $mform->setExpanded('headerelement_' . $element->id, false); + } else { + $mform->setExpanded('headerelement_' . $element->id, true); } + // 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. + $classname = "customcert_element_{$element->element}"; + $e = new $classname($element); + $e->render_form_elements($mform); + // Add this to the objects array. + $this->elementobjects[] = $e; + // Add submit button to delete this. + $mform->addElement('html', html_writer::start_tag('div', array('class' => 'deletebutton'))); + $mform->addElement('submit', 'deleteelement_' . $element->id, get_string('deleteelement', 'customcert')); + $mform->addElement('html', html_writer::end_tag('div')); } } } diff --git a/lib.php b/lib.php index a78dc66..c510bc8 100644 --- a/lib.php +++ b/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,48 +492,30 @@ 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. - foreach ($pages as $page) { - // Get the name of the fields we want from the form. - $orientation = 'orientation_' . $page->id; - $width = 'width_' . $page->id; - $height = 'height_' . $page->id; - // Create the page data to update the DB with. - $p = new stdClass(); - $p->id = $page->id; - $p->orientation = $data->$orientation; - $p->width = $data->$width; - $p->height = $data->$height; - $p->timemodified = $time; - // Update the page. - $DB->update_record('customcert_pages', $p); - // Get the elements for the page. - if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { - // Loop through the elements. - foreach ($elements as $element) { - // Get an instance of the element class. - if ($e = customcert_get_element_instance($element)) { - $e->save_form_elements($data); - } + // Get the existing pages and save the page data. + if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $data->id))) { + // Loop through existing pages. + foreach ($pages as $page) { + // Get the name of the fields we want from the form. + $orientation = 'orientation_' . $page->id; + $width = 'width_' . $page->id; + $height = 'height_' . $page->id; + // Create the page data to update the DB with. + $p = new stdClass(); + $p->id = $page->id; + $p->orientation = $data->$orientation; + $p->width = $data->$width; + $p->height = $data->$height; + $p->timemodified = $time; + // Update the page. + $DB->update_record('customcert_pages', $p); + // Get the elements for the page. + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { + // Loop through the elements. + foreach ($elements as $element) { + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { + $e->save_form_elements($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(); } } }