Introduced functionality to save and load customcert templates

Note: I did not create a db/upgrade.php script to add the new database tables as this module should not be currently used by anyone.
This commit is contained in:
Mark Nelson 2013-05-03 18:51:41 +08:00
parent bc621a85e3
commit d7174ce3c8
11 changed files with 512 additions and 41 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/customcert/db" VERSION="20130412" COMMENT="XMLDB file for Moodle mod/customcert" <XMLDB PATH="mod/customcert/db" VERSION="20130426" COMMENT="XMLDB file for Moodle mod/customcert"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
> >
@ -32,7 +32,7 @@
<KEY NAME="customcert" TYPE="foreign" FIELDS="customcertid" REFTABLE="customcert" REFFIELDS="id"/> <KEY NAME="customcert" TYPE="foreign" FIELDS="customcertid" REFTABLE="customcert" REFFIELDS="id"/>
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="customcert_pages" COMMENT="Stores each page of a custom cert"> <TABLE NAME="customcert_pages" COMMENT="Stores each page of a customcert">
<FIELDS> <FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/> <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/>
<FIELD NAME="customcertid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/> <FIELD NAME="customcertid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
@ -68,5 +68,52 @@
<KEY NAME="page" TYPE="foreign" FIELDS="pageid" REFTABLE="customcert_pages" REFFIELDS="id"/> <KEY NAME="page" TYPE="foreign" FIELDS="pageid" REFTABLE="customcert_pages" REFFIELDS="id"/>
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="customcert_template" COMMENT="Stores each customcert template">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/>
<FIELD NAME="name" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for customcert_template"/>
</KEYS>
</TABLE>
<TABLE NAME="customcert_template_pages" COMMENT="Stores each page of a customcert template">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/>
<FIELD NAME="templateid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="orientation" TYPE="char" LENGTH="10" NOTNULL="true" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="width" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="pagenumber" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for customcert_template_pages"/>
<KEY NAME="template" TYPE="foreign" FIELDS="templateid" REFTABLE="customcert_template" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="customcert_template_elements" COMMENT="Stores the elements for a given customcert template page">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" ENUM="false"/>
<FIELD NAME="templatepageid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="element" TYPE="text" LENGTH="big" NOTNULL="true" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="data" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="font" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="size" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="colour" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="posx" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="posy" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="sequence" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" ENUM="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for customcert_template_elements"/>
<KEY NAME="templatepage" TYPE="foreign" FIELDS="templatepageid" REFTABLE="customcert_template_pages" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES> </TABLES>
</XMLDB> </XMLDB>

View file

@ -26,6 +26,8 @@
require_once('../../config.php'); require_once('../../config.php');
require_once($CFG->dirroot . '/mod/customcert/lib.php'); require_once($CFG->dirroot . '/mod/customcert/lib.php');
require_once($CFG->dirroot . '/mod/customcert/edit_form.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'); require_once($CFG->dirroot . '/mod/customcert/elements/element.class.php');
$cmid = required_param('cmid', PARAM_INT); $cmid = required_param('cmid', PARAM_INT);
@ -46,6 +48,24 @@ require_login($course, false, $cm);
require_capability('mod/customcert:manage', $context); 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. // Check if they are moving a custom certificate page.
if ((!empty($moveup)) || (!empty($movedown))) { if ((!empty($moveup)) || (!empty($movedown))) {
// Check if we are moving a page up. // 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. } else if ((!empty($deleteelement)) && (!empty($confirm))) { // Check if we are deleting an element.
// Ensure element exists and delete it. // Ensure element exists and delete it.
$element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST); $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, $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->header();
echo $OUTPUT->heading(get_string('editcustomcert', 'mod_customcert')); echo $OUTPUT->heading(get_string('editcustomcert', 'mod_customcert'));
$mform->display(); $mform->display();
if (!empty($templates)) {
$loadtemplateform->display();
}
$savetemplateform->display();
echo $OUTPUT->footer(); echo $OUTPUT->footer();

View file

@ -78,20 +78,22 @@ class mod_customcert_edit_form extends moodleform {
$mform->closeHeaderBefore('addcertpage'); $mform->closeHeaderBefore('addcertpage');
$mform->addElement('submit', 'addcertpage', get_string('addcertpage', 'customcert'));
$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);
// 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->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT); $mform->setType('id', PARAM_INT);
$mform->setDefault('id', $this->id); $mform->setDefault('id', $this->id);
$mform->addElement('hidden', 'cmid'); $mform->addElement('hidden', 'cmid');
$mform->setType('cmid', PARAM_INT); $mform->setType('cmid', PARAM_INT);
$mform->setDefault('cmid', $this->_customdata['cmid']); $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. // Create the form object.
$mform =& $this->_form; $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 page is null we are adding a customcert, not editing one, so set pageid to 1.
if (is_null($page)) { if (is_null($page)) {
$pageid = 1; $pageid = 1;
@ -222,7 +221,7 @@ class mod_customcert_edit_form extends moodleform {
$mform->addHelpButton('height_' . $pageid, 'height', 'customcert'); $mform->addHelpButton('height_' . $pageid, 'height', 'customcert');
$group = array(); $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')); $group[] = $mform->createElement('submit', 'addelement_' . $pageid, get_string('addelement', 'customcert'));
$mform->addElement('group', 'elementgroup', '', $group, '', false); $mform->addElement('group', 'elementgroup', '', $group, '', false);

View file

@ -135,7 +135,7 @@ class customcert_element_base {
$mform->addHelpButton('colour_' . $id, 'fontcolour', 'customcert'); $mform->addHelpButton('colour_' . $id, 'fontcolour', 'customcert');
$mform->addHelpButton('posx_' . $id, 'posx', 'customcert'); $mform->addHelpButton('posx_' . $id, 'posx', 'customcert');
$mform->addHelpButton('posy_' . $id, 'posy', 'customcert'); $mform->addHelpButton('posy_' . $id, 'posy', 'customcert');
} }
/** /**
* Performs validation on the element values. * Performs validation on the element values.
@ -221,6 +221,28 @@ class customcert_element_base {
return null; 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. * Handles rendering the element on the pdf.
* Must be overridden. * Must be overridden.

View file

@ -56,9 +56,9 @@ class MoodleQuickForm_customcert_colourpicker extends HTML_QuickForm_text {
$content = '<label class="accesshide" for="' . $this->getAttribute('id') . '" >' . $this->getLabel() . '</label>'; $content = '<label class="accesshide" for="' . $this->getAttribute('id') . '" >' . $this->getLabel() . '</label>';
$content .= html_writer::start_tag('div', array('class' => 'form-colourpicker defaultsnext')); $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', $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'), $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'); $content .= html_writer::end_tag('div');
return $content; return $content;

View file

@ -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['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?';
$string['description'] = 'Description'; $string['description'] = 'Description';
$string['editcustomcert'] = 'Edit custom certificate'; $string['editcustomcert'] = 'Edit custom certificate';
$string['errorloadingelement'] = 'Error loading the element "{$a}"';
$string['errorsavingelement'] = 'Error saving the element "{$a}"';
$string['font'] = 'Font'; $string['font'] = 'Font';
$string['font_help'] = 'The font used when generating this element.'; $string['font_help'] = 'The font used when generating this element.';
$string['fontcolour'] = 'Colour'; $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['invalidposition'] = 'Please select a positive number for position {$a}.';
$string['issued'] = 'Issued'; $string['issued'] = 'Issued';
$string['landscape'] = 'Landscape'; $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['modulename'] = 'Custom Certificate';
$string['modulenameplural'] = 'Custom Certificates'; $string['modulenameplural'] = 'Custom Certificates';
$string['name'] = 'Name'; $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['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'] = '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['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['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['uploadimage'] = 'Upload image';
$string['viewcustomcertissues'] = 'View {$a} issued custom certificates'; $string['viewcustomcertissues'] = 'View {$a} issued custom certificates';
$string['width'] = 'Width'; $string['width'] = 'Width';

64
lib.php
View file

@ -92,8 +92,9 @@ function customcert_delete_instance($id) {
WHERE p.customcertid = :customcertid"; WHERE p.customcertid = :customcertid";
if ($elements = $DB->get_records_sql($sql, array('customcertid' => $id))) { if ($elements = $DB->get_records_sql($sql, array('customcertid' => $id))) {
foreach ($elements as $element) { foreach ($elements as $element) {
if (!customcert_delete_element($element)) { // Get an instance of the element class.
return false; if ($e = customcert_get_element_instance($element)) {
return $e->delete_element();
} }
} }
} }
@ -359,6 +360,7 @@ function customcert_get_elements() {
$foldername = $elementfolder->getFilename(); $foldername = $elementfolder->getFilename();
$classfile = "$elementdir/$foldername/lib.php"; $classfile = "$elementdir/$foldername/lib.php";
if (file_exists($classfile)) { if (file_exists($classfile)) {
// Need to require this file in case if we choose to add this element.
require_once($classfile); require_once($classfile);
$component = "customcertelement_{$foldername}"; $component = "customcertelement_{$foldername}";
$options[$foldername] = get_string('pluginname', $component); $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))) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) {
// Loop through the elements. // Loop through the elements.
foreach ($elements as $element) { foreach ($elements as $element) {
// Check that the standard class file exists. // Get an instance of the element class.
$classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; if ($e = customcert_get_element_instance($element)) {
if (file_exists($classfile)) {
$classname = "customcert_element_{$element->element}";
$e = new $classname($element);
$e->save_form_elements($data); $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. * 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. // 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))) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) {
foreach ($elements as $element) { 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 stdClass $element the element
* @param bool returns true if success, false otherwise * @param bool returns true if success, false otherwise
*/ */
function customcert_delete_element($element) { function customcert_get_templates() {
global $CFG; global $DB;
// Check that the standard class file exists. return $DB->get_records_menu('customcert_template', array(), 'name ASC', 'id, name');
$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;
} }
/** /**
@ -771,12 +785,8 @@ function customcert_generate_pdf($customcert, $userid) {
if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id), 'sequence ASC')) {
// Loop through and display. // Loop through and display.
foreach ($elements as $element) { foreach ($elements as $element) {
// Check that the standard class file exists. // Get an instance of the element class.
$classfile = "$CFG->dirroot/mod/customcert/elements/{$element->element}/lib.php"; if ($e = customcert_get_element_instance($element)) {
if (file_exists($classfile)) {
require_once($classfile);
$classname = "customcert_element_{$element->element}";
$e = new $classname($element);
$e->render($pdf, $userid); $e->render($pdf, $userid);
} }
} }

130
load_template.php Normal file
View file

@ -0,0 +1,130 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Handles loading a customcert template.
*
* @package mod_customcert
* @copyright Mark Nelson <markn@moodle.com>
* @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) . '<br />';
}
}
// 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();

52
load_template_form.php Normal file
View file

@ -0,0 +1,52 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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 <markn@moodle.com>
* @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']);
}
}

98
save_template.php Normal file
View file

@ -0,0 +1,98 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Handles saving customcert templates.
*
* @package mod_customcert
* @copyright Mark Nelson <markn@moodle.com>
* @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) . '<br />';
}
}
// Redirect.
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid));
redirect($url, $message);

77
save_template_form.php Normal file
View file

@ -0,0 +1,77 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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 <markn@moodle.com>
* @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;
}
}