diff --git a/db/install.xml b/db/install.xml index c7f23d7..5e7bb22 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -32,7 +32,7 @@ - +
@@ -68,5 +68,52 @@
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
diff --git a/edit.php b/edit.php index 84f45d3..b4d1c09 100644 --- a/edit.php +++ b/edit.php @@ -26,6 +26,8 @@ require_once('../../config.php'); require_once($CFG->dirroot . '/mod/customcert/lib.php'); require_once($CFG->dirroot . '/mod/customcert/edit_form.php'); +require_once($CFG->dirroot . '/mod/customcert/load_template_form.php'); +require_once($CFG->dirroot . '/mod/customcert/save_template_form.php'); require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php'); $cmid = required_param('cmid', PARAM_INT); @@ -46,6 +48,24 @@ require_login($course, false, $cm); require_capability('mod/customcert:manage', $context); +// The form for loading a customcert templates. +$templates = customcert_get_templates(); +$loadtemplateform = new mod_customcert_load_template_form('', array('cmid' => $cm->id, 'templates' => $templates)); +// The form for saving the current information as a template. +$savetemplateform = new mod_customcert_save_template_form('', array('cmid' => $cm->id)); + +// Check if they chose to load a customcert template and redirect. +if ($data = $loadtemplateform->get_data()) { + $url = new moodle_url('/mod/customcert/load_template.php', array('cmid' => $cmid, 'tid' => $data->template)); + redirect($url); +} + +// Check if they chose to save the current information and redirect. +if ($data = $savetemplateform->get_data()) { + $url = new moodle_url('/mod/customcert/save_template.php', array('cmid' => $cmid, 'name' => $data->name)); + redirect($url); +} + // Check if they are moving a custom certificate page. if ((!empty($moveup)) || (!empty($movedown))) { // Check if we are moving a page up. @@ -84,7 +104,10 @@ if ((!empty($moveup)) || (!empty($movedown))) { } else if ((!empty($deleteelement)) && (!empty($confirm))) { // Check if we are deleting an element. // Ensure element exists and delete it. $element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST); - customcert_delete_element($element); + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { + return $e->delete_element(); + } } $mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id, @@ -171,4 +194,8 @@ $PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid)); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('editcustomcert', 'mod_customcert')); $mform->display(); +if (!empty($templates)) { + $loadtemplateform->display(); +} +$savetemplateform->display(); echo $OUTPUT->footer(); diff --git a/edit_form.php b/edit_form.php index 70e5828..c0721f4 100644 --- a/edit_form.php +++ b/edit_form.php @@ -78,20 +78,22 @@ class mod_customcert_edit_form extends moodleform { $mform->closeHeaderBefore('addcertpage'); - $mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert')); - $mform->addElement('header', 'uploadimage', get_string('uploadimage', 'customcert')); $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '', $this->filemanageroptions); + // 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); + $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); $mform->setDefault('id', $this->id); $mform->addElement('hidden', 'cmid'); $mform->setType('cmid', PARAM_INT); $mform->setDefault('cmid', $this->_customdata['cmid']); - - $this->add_action_buttons(false); } /** @@ -179,9 +181,6 @@ class mod_customcert_edit_form extends moodleform { // Create the form object. $mform =& $this->_form; - // Get the elements that are available - $elementsavailable = customcert_get_elements(); - // If page is null we are adding a customcert, not editing one, so set pageid to 1. if (is_null($page)) { $pageid = 1; @@ -222,7 +221,7 @@ class mod_customcert_edit_form extends moodleform { $mform->addHelpButton('height_' . $pageid, 'height', 'customcert'); $group = array(); - $group[] = $mform->createElement('select', 'element_' . $pageid, '', $elementsavailable); + $group[] = $mform->createElement('select', 'element_' . $pageid, '', customcert_get_elements()); $group[] = $mform->createElement('submit', 'addelement_' . $pageid, get_string('addelement', 'customcert')); $mform->addElement('group', 'elementgroup', '', $group, '', false); diff --git a/elements/element.class.php b/elements/element.class.php index 25c78b2..8927665 100644 --- a/elements/element.class.php +++ b/elements/element.class.php @@ -135,7 +135,7 @@ class customcert_element_base { $mform->addHelpButton('colour_' . $id, 'fontcolour', 'customcert'); $mform->addHelpButton('posx_' . $id, 'posx', 'customcert'); $mform->addHelpButton('posy_' . $id, 'posy', 'customcert'); - } + } /** * Performs validation on the element values. @@ -221,6 +221,28 @@ class customcert_element_base { return null; } + /** + * This will handle how individual elements save their data + * to a template to be loaded later. + * Can be overridden if more functionality is needed. + * + * @param stdClass $data the form data. + */ + public function save_data_to_template($data) { + return true; + } + + /** + * This will handle how individual elements load their data + * from a template to an existing customcert. + * Can be overridden if more functionality is needed. + * + * @param stdClass $data the form data. + */ + public function load_data_from_template($data) { + return true; + } + /** * Handles rendering the element on the pdf. * Must be overridden. diff --git a/includes/colourpicker.php b/includes/colourpicker.php index 3d71f6a..4e8908c 100644 --- a/includes/colourpicker.php +++ b/includes/colourpicker.php @@ -56,9 +56,9 @@ class MoodleQuickForm_customcert_colourpicker extends HTML_QuickForm_text { $content = ''; $content .= html_writer::start_tag('div', array('class' => 'form-colourpicker defaultsnext')); $content .= html_writer::tag('div', $OUTPUT->pix_icon('i/loading', get_string('loading', 'admin'), 'moodle', - array('class' => 'loadingicon')), array('class' => 'admin_colourpicker clearfix')); + array('class' => 'loadingicon')), array('class' => 'admin_colourpicker clearfix')); $content .= html_writer::empty_tag('input', array('type' => 'text', 'id' => $this->getAttribute('id'), - 'name' => $this->getName(), 'value' => $this->getValue(), 'size' => '12')); + 'name' => $this->getName(), 'value' => $this->getValue(), 'size' => '12')); $content .= html_writer::end_tag('div'); return $content; diff --git a/lang/en/customcert.php b/lang/en/customcert.php index f3c4c17..02ffec5 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -37,6 +37,8 @@ $string['deleteelementconfirm'] = 'Are you sure you want to delete this element? $string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?'; $string['description'] = 'Description'; $string['editcustomcert'] = 'Edit custom certificate'; +$string['errorloadingelement'] = 'Error loading the element "{$a}"'; +$string['errorsavingelement'] = 'Error saving the element "{$a}"'; $string['font'] = 'Font'; $string['font_help'] = 'The font used when generating this element.'; $string['fontcolour'] = 'Colour'; @@ -51,6 +53,9 @@ $string['invalidcolour'] = 'Invalid colour chosen, please enter a valid HTML col $string['invalidposition'] = 'Please select a positive number for position {$a}.'; $string['issued'] = 'Issued'; $string['landscape'] = 'Landscape'; +$string['load'] = 'Load'; +$string['loadtemplate'] = 'Load template'; +$string['loadtemplatemsg'] = 'Are you sure you wish to load this template? This will remove any existing pages and elements for this certificate.'; $string['modulename'] = 'Custom Certificate'; $string['modulenameplural'] = 'Custom Certificates'; $string['name'] = 'Name'; @@ -66,7 +71,11 @@ $string['posx'] = 'Position X'; $string['posx_help'] = 'This is the position in pixels from the top left corner you wish the element to display in the x direction.'; $string['posy'] = 'Postion Y'; $string['posy_help'] = 'This is the position in pixels from the top left corner you wish the element to display in the y direction.'; +$string['save'] = 'Save'; +$string['savetemplate'] = 'Save template'; $string['summaryofissue'] = 'Summary of issue'; +$string['templatename'] = 'Template name'; +$string['templatenameexists'] = 'That template name is currently in use, please choose another.'; $string['uploadimage'] = 'Upload image'; $string['viewcustomcertissues'] = 'View {$a} issued custom certificates'; $string['width'] = 'Width'; diff --git a/lib.php b/lib.php index 758a090..7a242e1 100644 --- a/lib.php +++ b/lib.php @@ -92,8 +92,9 @@ function customcert_delete_instance($id) { WHERE p.customcertid = :customcertid"; if ($elements = $DB->get_records_sql($sql, array('customcertid' => $id))) { foreach ($elements as $element) { - if (!customcert_delete_element($element)) { - return false; + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { + return $e->delete_element(); } } } @@ -359,6 +360,7 @@ function customcert_get_elements() { $foldername = $elementfolder->getFilename(); $classfile = "$elementdir/$foldername/lib.php"; if (file_exists($classfile)) { + // Need to require this file in case if we choose to add this element. require_once($classfile); $component = "customcertelement_{$foldername}"; $options[$foldername] = get_string('pluginname', $component); @@ -485,11 +487,8 @@ function customcert_save_page_data($data) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { // Loop through the elements. foreach ($elements as $element) { - // Check that the standard class file exists. - $classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; - if (file_exists($classfile)) { - $classname = "customcert_element_{$element->element}"; - $e = new $classname($element); + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { $e->save_form_elements($data); } } @@ -499,6 +498,27 @@ function customcert_save_page_data($data) { } } +/** + * Returns an instance of the element class. + * + * @param stdClass $element the element + * @return stdClass|bool returns the instance of the element class, or false if element + * class does not exists. + */ +function customcert_get_element_instance($element) { + global $CFG; + + $classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; + // Ensure this necessary file exists. + if (file_exists($classfile)) { + require_once($classfile); + $classname = "customcert_element_{$element->element}"; + return new $classname($element); + } + + return false; +} + /** * Handles adding another element to a page in the customcert. * @@ -563,7 +583,10 @@ function customcert_delete_page($pageid) { // The element may have some extra tasks it needs to complete to completely delete itself. if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { foreach ($elements as $element) { - customcert_delete_element($element); + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { + return $e->delete_element(); + } } } @@ -578,24 +601,15 @@ function customcert_delete_page($pageid) { } /** - * Handles deleting an element. + * Returns a list of all the templates. * * @param stdClass $element the element * @param bool returns true if success, false otherwise */ -function customcert_delete_element($element) { - global $CFG; +function customcert_get_templates() { + global $DB; - // Check that the standard class file exists. - $classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; - if (file_exists($classfile)) { - require_once($classfile); - $classname = "customcert_element_{$element->element}"; - $e = new $classname($element); - return $e->delete_element(); - } - - return false; + return $DB->get_records_menu('customcert_template', array(), 'name ASC', 'id, name'); } /** @@ -771,12 +785,8 @@ function customcert_generate_pdf($customcert, $userid) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) { // Loop through and display. foreach ($elements as $element) { - // Check that the standard class file exists. - $classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; - if (file_exists($classfile)) { - require_once($classfile); - $classname = "customcert_element_{$element->element}"; - $e = new $classname($element); + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { $e->render($pdf, $userid); } } diff --git a/load_template.php b/load_template.php new file mode 100644 index 0000000..250df18 --- /dev/null +++ b/load_template.php @@ -0,0 +1,130 @@ +. + +/** + * Handles loading a customcert template. + * + * @package mod_customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once('../../config.php'); +require_once($CFG->dirroot . '/mod/customcert/lib.php'); +require_once($CFG->dirroot . '/mod/customcert/save_template_form.php'); + +$cmid = required_param('cmid', PARAM_INT); +$tid = required_param('tid', PARAM_INT); +$confirm = optional_param('confirm', 0, PARAM_INT); + +$cm = get_coursemodule_from_id('customcert', $cmid, 0, false, MUST_EXIST); +$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); +$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST); +$template = $DB->get_record('customcert_template', array('id' => $tid), '*', MUST_EXIST); +$context = context_module::instance($cm->id); + +require_login($course, false, $cm); + +require_capability('mod/customcert:manage', $context); + +// Check that they have confirmed they wish to load the template. +if ($confirm) { + // First, remove all the existing elements and pages. + $sql = "SELECT e.* + FROM {customcert_elements} e + INNER JOIN {customcert_pages} p + ON e.pageid = p.id + WHERE p.customcertid = :customcertid"; + if ($elements = $DB->get_records_sql($sql, array('customcertid' => $customcert->id))) { + foreach ($elements as $element) { + // Get an instance of the element class. + if ($e = customcert_get_element_instance($element)) { + $e->delete_element(); + } + } + } + + // Delete the pages. + $DB->delete_records('customcert_pages', array('customcertid' => $customcert->id)); + + // Store the current time in a variable. + $time = time(); + + // Now, get the template data. + if ($templatepages = $DB->get_records('customcert_template_pages', array('templateid' => $tid))) { + // Create an array to store any errors loading an element to a template. + $errors = array(); + // Loop through the pages. + foreach ($templatepages as $templatepage) { + $page = clone($templatepage); + $page->customcertid = $customcert->id; + $page->timecreated = $time; + $page->timemodified = $time; + // Insert into the database. + $page->id = $DB->insert_record('customcert_pages', $page); + // Now go through the elements. + if ($templateelements = $DB->get_records('customcert_template_elements', array('templatepageid' => $templatepage->id))) { + foreach ($templateelements as $templateelement) { + $element = clone($templateelement); + $element->pageid = $page->id; + $element->timecreated = $time; + $element->timemodified = $time; + // Ok, now we want to insert this into the database. + $element->id = $DB->insert_record('customcert_elements', $element); + // Load any other information the element may need to for the template. + if ($e = customcert_get_element_instance($element)) { + if (!$e->load_data_from_template($element)) { + // Remove from the customcert_elements table. + $DB->delete_records('customcert_elements', array('id' => $element->id)); + // Add the error message to the array to display later. + $errors[] = get_string('errorloadingelement', 'customcert', $element->element); + } + } + } + } + } + } + + // Get any errors caused by the loading of an element and put into a message. + $message = ''; + if (!empty($errors)) { + foreach ($errors as $e) { + $message .= $OUTPUT->notification($e) . '
'; + } + } + + // Redirect. + $url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid)); + redirect($url, $message); +} + + +// Create the link options. +$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid)); +$yesurl = new moodle_url('/mod/customcert/load_template.php', array('cmid' => $cmid, + 'tid' => $tid, + 'confirm' => 1)); +// Show a confirmation page. +$strheading = get_string('loadtemplate', 'customcert'); +$PAGE->navbar->add($strheading); +$PAGE->set_title($strheading); +$PAGE->set_heading($COURSE->fullname); +$PAGE->set_url('/mod/customcert/load_template.php', array('cmid' => $cmid, 'tid' => $tid)); +echo $OUTPUT->header(); +echo $OUTPUT->heading($strheading); +echo $OUTPUT->confirm(get_string('loadtemplatemsg', 'customcert'), $yesurl, $nourl); +echo $OUTPUT->footer(); \ No newline at end of file diff --git a/load_template_form.php b/load_template_form.php new file mode 100644 index 0000000..a39f881 --- /dev/null +++ b/load_template_form.php @@ -0,0 +1,52 @@ +. + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +require_once($CFG->libdir . '/formslib.php'); + +/** + * The form for loading customcert templates. + * + * @package mod_customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_customcert_load_template_form extends moodleform { + + /** + * Form definition. + */ + function definition() { + $mform =& $this->_form; + + $mform->addElement('header', 'loadtemplateheader', get_string('loadtemplate', 'customcert')); + + $group = array(); + $group[] = $mform->createElement('select', 'template', '', $this->_customdata['templates']); + $group[] = $mform->createElement('submit', 'loadtemplatesubmit', get_string('load', 'customcert')); + + $mform->addElement('group', 'loadtemplategroup', '', $group, '', false); + + // Set the type. + $mform->setType('template', PARAM_INT); + + $mform->addElement('hidden', 'cmid'); + $mform->setType('cmid', PARAM_INT); + $mform->setDefault('cmid', $this->_customdata['cmid']); + } +} diff --git a/save_template.php b/save_template.php new file mode 100644 index 0000000..f854bd1 --- /dev/null +++ b/save_template.php @@ -0,0 +1,98 @@ +. + +/** + * Handles saving customcert templates. + * + * @package mod_customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once('../../config.php'); +require_once($CFG->dirroot . '/mod/customcert/lib.php'); +require_once($CFG->dirroot . '/mod/customcert/save_template_form.php'); + +$cmid = required_param('cmid', PARAM_INT); +$name = required_param('name', PARAM_TEXT); + +$cm = get_coursemodule_from_id('customcert', $cmid, 0, false, MUST_EXIST); +$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); +$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST); +$context = context_module::instance($cm->id); + +require_login($course, false, $cm); + +require_capability('mod/customcert:manage', $context); + +// Store the current time in a variable. +$time = time(); + +// Create the template. +$template = new stdClass(); +$template->name = $name; +$template->timecreated = $time; +$template->timemodified = $time; +$template->id = $DB->insert_record('customcert_template', $template); + +// Get the pages of the customcert we are copying. +if ($pages = $DB->get_records('customcert_pages', array('customcertid' => $customcert->id))) { + // Create an array to store any errors saving an element to a template. + $errors = array(); + // Loop through and copy the data. + foreach ($pages as $page) { + // Insert into the template page table. + $templatepage = clone($page); + $templatepage->templateid = $template->id; + $templatepage->timecreated = $time; + $templatepage->timemodified = $time; + $templatepage->id = $DB->insert_record('customcert_template_pages', $templatepage); + // Get the elements. + if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { + // Loop through the elements. + foreach ($elements as $element) { + // Insert into the template element table. + $templateelement = clone($element); + $templateelement->templatepageid = $templatepage->id; + $templateelement->timecreated = $time; + $templateelement->timemodified = $time; + $templateelement->id = $DB->insert_record('customcert_template_elements', $templateelement); + // Save any other information the element may need to for the template. + if ($e = customcert_get_element_instance($element)) { + if (!$e->save_data_to_template($element)) { + // Remove from the customcert_template_elements table. + $DB->delete_records('customcert_template_elements', array('id' => $templateelement->id)); + // Add the error message to the array to display later. + $errors[] = get_string('errorsavingelement', 'customcert', $element->element); + } + } + } + } + } +} + +// Get any errors caused by the loading of an element and put into a message. +$message = ''; +if (!empty($errors)) { + foreach ($errors as $e) { + $message .= $OUTPUT->notification($e) . '
'; + } +} + +// Redirect. +$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid)); +redirect($url, $message); \ No newline at end of file diff --git a/save_template_form.php b/save_template_form.php new file mode 100644 index 0000000..41b2bb7 --- /dev/null +++ b/save_template_form.php @@ -0,0 +1,77 @@ +. + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +require_once($CFG->libdir . '/formslib.php'); + +/** + * The form for handling saving customcert templates. + * + * @package mod_customcert + * @copyright Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_customcert_save_template_form extends moodleform { + + /** + * Form definition. + */ + function definition() { + $mform =& $this->_form; + + $mform->addElement('header', 'savetemplateheader', get_string('savetemplate', 'customcert')); + + $group = array(); + $group[] = $mform->createElement('text', 'name'); + $group[] = $mform->createElement('submit', 'savetemplatesubmit', get_string('save', 'customcert')); + + $mform->addElement('group', 'savetemplategroup', get_string('templatename', 'customcert'), $group, '', false); + + // Set the template name to required and set the type. + $mform->addGroupRule('savetemplategroup', array( + 'name' => array( + array(null, 'required', null, 'client') + ) + )); + $mform->setType('name', PARAM_NOTAGS); + + $mform->addElement('hidden', 'cmid'); + $mform->setType('cmid', PARAM_INT); + $mform->setDefault('cmid', $this->_customdata['cmid']); + } + + /** + * Some basic validation. + * + * @param $data + * @param $files + * @return array the errors that were found + */ + public function validation($data, $files) { + global $DB; + + $errors = parent::validation($data, $files); + + // Ensure the name does not already exist. + if ($DB->record_exists('customcert_template', array('name' => $data['name']))) { + $errors['savetemplategroup'] = get_string('templatenameexists', 'customcert'); + } + + return $errors; + } +}