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
3 changed files with 103 additions and 121 deletions
8
edit.php
8
edit.php
|
@ -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);
|
||||||
|
|
114
edit_form.php
114
edit_form.php
|
@ -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.
|
$pageid = $page->id;
|
||||||
if (is_null($page)) {
|
$pagenum = $page->pagenumber;
|
||||||
$pageid = 1;
|
|
||||||
$pagenum = 1;
|
|
||||||
} else {
|
|
||||||
$pageid = $page->id;
|
|
||||||
$pagenum = $page->pagenumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
$mform->addElement('header', 'page_' . $pageid, get_string('page', 'customcert', $pagenum));
|
$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'));
|
$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.
|
// Check if there are elements to add.
|
||||||
if (!is_null($page)) {
|
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'sequence ASC')) {
|
||||||
// Check if there are elements to add.
|
// Get the total number of elements.
|
||||||
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $pageid), 'sequence ASC')) {
|
$numelements = count($elements);
|
||||||
// Get the total number of elements.
|
// Loop through and add the ones present.
|
||||||
$numelements = count($elements);
|
foreach ($elements as $element) {
|
||||||
// Loop through and add the ones present.
|
$classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php";
|
||||||
foreach ($elements as $element) {
|
// It's possible this element was added to the database then the folder was deleted, if
|
||||||
$classfile = "{$CFG->dirroot}/mod/customcert/elements/{$element->element}/lib.php";
|
// this is the case we do not want to render these elements as an error will occur.
|
||||||
// It's possible this element was added to the database then the folder was deleted, if
|
if (file_exists($classfile)) {
|
||||||
// this is the case we do not want to render these elements as an error will occur.
|
// Add element header.
|
||||||
if (file_exists($classfile)) {
|
$mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " .
|
||||||
// Add element header.
|
get_string('pluginname', 'customcertelement_' . $element->element));
|
||||||
$mform->addElement('header', 'headerelement_' . $element->id, get_string('page', 'customcert', $pagenum) . " - " .
|
// We do not need to expand these elements if the modified time is greater than the created time as it
|
||||||
get_string('pluginname', 'customcertelement_' . $element->element));
|
// means the values have already been altered by the user - ie. the element has not just been created.
|
||||||
// We do not need to expand these elements if the modified time is greater than the created time as it
|
if ($element->timemodified > $element->timecreated) {
|
||||||
// means the values have already been altered by the user - ie. the element has not just been created.
|
$mform->setExpanded('headerelement_' . $element->id, false);
|
||||||
if ($element->timemodified > $element->timecreated) {
|
} else {
|
||||||
$mform->setExpanded('headerelement_' . $element->id, false);
|
$mform->setExpanded('headerelement_' . $element->id, true);
|
||||||
} 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'));
|
|
||||||
}
|
}
|
||||||
|
// 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'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
102
lib.php
102
lib.php
|
@ -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,48 +492,30 @@ 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.
|
// Get the existing pages and save the page data.
|
||||||
$totalpages = $DB->count_records('customcert_pages', array('customcertid' => $data->id));
|
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $data->id))) {
|
||||||
|
// Loop through existing pages.
|
||||||
// Check if this customcert has any pages yet, if not we are inserting.
|
foreach ($pages as $page) {
|
||||||
if ($totalpages == 0) {
|
// Get the name of the fields we want from the form.
|
||||||
// Create the page to insert.
|
$orientation = 'orientation_' . $page->id;
|
||||||
$page = new stdClass();
|
$width = 'width_' . $page->id;
|
||||||
$page->customcertid = $data->id;
|
$height = 'height_' . $page->id;
|
||||||
$page->orientation = $data->orientation_1;
|
// Create the page data to update the DB with.
|
||||||
$page->width = $data->width_1;
|
$p = new stdClass();
|
||||||
$page->height = $data->height_1;
|
$p->id = $page->id;
|
||||||
$page->pagenumber = 1;
|
$p->orientation = $data->$orientation;
|
||||||
$page->timecreated = $time;
|
$p->width = $data->$width;
|
||||||
$page->timemodified = $time;
|
$p->height = $data->$height;
|
||||||
// Insert the page.
|
$p->timemodified = $time;
|
||||||
$DB->insert_record('customcert_pages', $page);
|
// Update the page.
|
||||||
} else {
|
$DB->update_record('customcert_pages', $p);
|
||||||
// Get the existing pages and save the page data.
|
// Get the elements for the page.
|
||||||
if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $data->id))) {
|
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) {
|
||||||
// Loop through existing pages.
|
// Loop through the elements.
|
||||||
foreach ($pages as $page) {
|
foreach ($elements as $element) {
|
||||||
// Get the name of the fields we want from the form.
|
// Get an instance of the element class.
|
||||||
$orientation = 'orientation_' . $page->id;
|
if ($e = customcert_get_element_instance($element)) {
|
||||||
$width = 'width_' . $page->id;
|
$e->save_form_elements($data);
|
||||||
$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) {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue