2012-12-07 09:34:46 +00:00
|
|
|
<?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
|
2013-07-22 05:06:18 +00:00
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit the customcert settings.
|
|
|
|
*
|
2013-04-26 07:30:33 +00:00
|
|
|
* @package mod_customcert
|
2013-07-22 05:06:18 +00:00
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
2012-12-07 09:34:46 +00:00
|
|
|
* @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/edit_form.php');
|
2013-05-03 10:51:41 +00:00
|
|
|
require_once($CFG->dirroot . '/mod/customcert/load_template_form.php');
|
|
|
|
require_once($CFG->dirroot . '/mod/customcert/save_template_form.php');
|
2013-06-21 09:35:14 +00:00
|
|
|
require_once($CFG->dirroot . '/mod/customcert/element/element.class.php');
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
$cmid = required_param('cmid', PARAM_INT);
|
|
|
|
$moveup = optional_param('moveup', 0, PARAM_INT);
|
|
|
|
$movedown = optional_param('movedown', 0, PARAM_INT);
|
2013-04-12 08:24:58 +00:00
|
|
|
$emoveup = optional_param('emoveup', 0, PARAM_INT);
|
|
|
|
$emovedown = optional_param('emovedown', 0, PARAM_INT);
|
2013-02-20 12:21:57 +00:00
|
|
|
$deleteelement = optional_param('deleteelement', 0, PARAM_INT);
|
|
|
|
$deletepage = optional_param('deletepage', 0, PARAM_INT);
|
2012-12-07 09:34:46 +00:00
|
|
|
$confirm = optional_param('confirm', 0, PARAM_INT);
|
|
|
|
|
|
|
|
$cm = get_coursemodule_from_id('customcert', $cmid, 0, false, MUST_EXIST);
|
2013-04-26 07:42:11 +00:00
|
|
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
|
|
|
$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST);
|
2012-12-07 09:34:46 +00:00
|
|
|
$context = context_module::instance($cm->id);
|
|
|
|
|
|
|
|
require_login($course, false, $cm);
|
|
|
|
|
|
|
|
require_capability('mod/customcert:manage', $context);
|
|
|
|
|
2013-05-03 10:51:41 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
// Flag to determine if we are deleting anything.
|
|
|
|
$deleting = false;
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
// Check if they are moving a custom certificate page.
|
|
|
|
if ((!empty($moveup)) || (!empty($movedown))) {
|
|
|
|
// Check if we are moving a page up.
|
|
|
|
if (!empty($moveup)) {
|
|
|
|
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $moveup))) {
|
|
|
|
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber - 1));
|
|
|
|
}
|
|
|
|
} else { // Must be moving a page down.
|
|
|
|
if ($movecertpage = $DB->get_record('customcert_pages', array('id' => $movedown))) {
|
|
|
|
$swapcertpage = $DB->get_record('customcert_pages', array('pagenumber' => $movecertpage->pagenumber + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that there is a page to move, and a page to swap it with.
|
|
|
|
if ($swapcertpage && $movecertpage) {
|
|
|
|
$DB->set_field('customcert_pages', 'pagenumber', $swapcertpage->pagenumber, array('id' => $movecertpage->id));
|
|
|
|
$DB->set_field('customcert_pages', 'pagenumber', $movecertpage->pagenumber, array('id' => $swapcertpage->id));
|
|
|
|
}
|
2013-04-12 08:24:58 +00:00
|
|
|
} else if ((!empty($emoveup)) || (!empty($emovedown))) { // Check if we are moving a custom certificate element.
|
|
|
|
// Check if we are moving an element up.
|
|
|
|
if (!empty($emoveup)) {
|
|
|
|
if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $emoveup))) {
|
|
|
|
$swapcertelement = $DB->get_record('customcert_elements', array('sequence' => $movecertelement->sequence - 1));
|
|
|
|
}
|
|
|
|
} else { // Must be moving a element down.
|
|
|
|
if ($movecertelement = $DB->get_record('customcert_elements', array('id' => $emovedown))) {
|
|
|
|
$swapcertelement = $DB->get_record('customcert_elements', array('sequence' => $movecertelement->sequence + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that there is an element to move, and an element to swap it with.
|
|
|
|
if ($swapcertelement && $movecertelement) {
|
|
|
|
$DB->set_field('customcert_elements', 'sequence', $swapcertelement->sequence, array('id' => $movecertelement->id));
|
|
|
|
$DB->set_field('customcert_elements', 'sequence', $movecertelement->sequence, array('id' => $swapcertelement->id));
|
|
|
|
}
|
2013-06-06 10:59:08 +00:00
|
|
|
} else if (!empty($deletepage)) { // Check if we are deleting a page.
|
2013-06-28 07:17:43 +00:00
|
|
|
if (!empty($confirm)) { // Check they have confirmed the deletion.
|
2013-06-06 10:59:08 +00:00
|
|
|
customcert_delete_page($deletepage);
|
|
|
|
} else {
|
|
|
|
// Set deletion flag to true.
|
|
|
|
$deleting = true;
|
|
|
|
// Create the message.
|
|
|
|
$message = get_string('deletepageconfirm', 'customcert');
|
|
|
|
// Create the link options.
|
|
|
|
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
|
|
|
|
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
|
|
|
|
'deletepage' => $deletepage,
|
|
|
|
'confirm' => 1,
|
|
|
|
'sesskey' => sesskey()));
|
|
|
|
}
|
|
|
|
} else if (!empty($deleteelement)) { // Check if we are deleting an element.
|
2013-06-28 07:17:43 +00:00
|
|
|
if (!empty($confirm)) { // Check they have confirmed the deletion.
|
2013-06-06 10:59:08 +00:00
|
|
|
// Ensure element exists and delete it.
|
|
|
|
$element = $DB->get_record('customcert_elements', array('id' => $deleteelement), '*', MUST_EXIST);
|
|
|
|
// Get an instance of the element class.
|
|
|
|
if ($e = customcert_get_element_instance($element)) {
|
|
|
|
$e->delete_element();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Set deletion flag to true.
|
|
|
|
$deleting = true;
|
|
|
|
// Create the message.
|
|
|
|
$message = get_string('deleteelementconfirm', 'customcert');
|
|
|
|
// Create the link options.
|
|
|
|
$nourl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id));
|
|
|
|
$yesurl = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cm->id,
|
|
|
|
'deleteelement' => $deleteelement,
|
|
|
|
'confirm' => 1,
|
|
|
|
'sesskey' => sesskey()));
|
2013-05-03 10:51:41 +00:00
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
// Check if we are deleting either a page or an element.
|
|
|
|
if ($deleting) {
|
|
|
|
// Show a confirmation page.
|
|
|
|
$strheading = get_string('deleteconfirm', 'customcert');
|
|
|
|
$PAGE->navbar->add($strheading);
|
|
|
|
$PAGE->set_title($strheading);
|
|
|
|
$PAGE->set_heading($course->fullname);
|
|
|
|
$PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid));
|
|
|
|
echo $OUTPUT->header();
|
|
|
|
echo $OUTPUT->heading($strheading);
|
|
|
|
echo $OUTPUT->confirm($message, $yesurl, $nourl);
|
|
|
|
echo $OUTPUT->footer();
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
$mform = new mod_customcert_edit_form('', array('customcertid' => $customcert->id,
|
|
|
|
'cmid' => $cm->id,
|
|
|
|
'course' => $course));
|
|
|
|
|
|
|
|
if ($data = $mform->get_data()) {
|
|
|
|
// Handle file uploads.
|
|
|
|
customcert_upload_imagefiles($data->customcertimage);
|
|
|
|
|
|
|
|
// Save any page data.
|
|
|
|
customcert_save_page_data($data);
|
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
// Check if we are adding a page.
|
|
|
|
if (!empty($data->addcertpage)) { // Check if they chose to add a page.
|
|
|
|
customcert_add_page($data);
|
|
|
|
}
|
2013-02-20 12:21:57 +00:00
|
|
|
|
|
|
|
// Loop through the data.
|
2012-12-07 09:34:46 +00:00
|
|
|
foreach ($data as $key => $value) {
|
2013-06-06 10:59:08 +00:00
|
|
|
if (strpos($key, 'addelement_') !== false) { // Check if they chose to add an element to a page.
|
2013-02-20 12:21:57 +00:00
|
|
|
// Get the page id.
|
|
|
|
$pageid = str_replace('addelement_', '', $key);
|
|
|
|
// Get the element.
|
|
|
|
$element = "element_" . $pageid;
|
|
|
|
$element = $data->$element;
|
2013-06-06 10:59:08 +00:00
|
|
|
// Create the URL to redirect to to add this element.
|
|
|
|
$params = array();
|
|
|
|
$params['cmid'] = $cmid;
|
|
|
|
$params['action'] = 'add';
|
|
|
|
$params['element'] = $element;
|
|
|
|
$params['pageid'] = $pageid;
|
|
|
|
$url = new moodle_url('/mod/customcert/edit_element.php', $params);
|
|
|
|
redirect($url);
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 03:05:42 +00:00
|
|
|
// Check if we want to preview this custom certificate.
|
|
|
|
if (!empty($data->previewbtn)) {
|
2013-06-28 07:17:43 +00:00
|
|
|
customcert_generate_pdf($customcert, true);
|
2013-05-17 03:05:42 +00:00
|
|
|
}
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
// Redirect to the editing page to show form with recent updates.
|
2013-05-06 10:22:55 +00:00
|
|
|
$url = new moodle_url('/mod/customcert/edit.php', array('cmid' => $cmid));
|
|
|
|
redirect($url);
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$PAGE->set_title(get_string('editcustomcert', 'customcert', format_string($customcert->name)));
|
|
|
|
$PAGE->set_heading($course->fullname);
|
|
|
|
$PAGE->set_url('/mod/customcert/edit.php', array('cmid' => $cmid));
|
|
|
|
|
|
|
|
echo $OUTPUT->header();
|
2013-05-28 09:33:17 +00:00
|
|
|
echo $OUTPUT->heading(get_string('editcustomcert', 'customcert'));
|
2012-12-07 09:34:46 +00:00
|
|
|
$mform->display();
|
2013-05-03 10:51:41 +00:00
|
|
|
if (!empty($templates)) {
|
|
|
|
$loadtemplateform->display();
|
|
|
|
}
|
|
|
|
$savetemplateform->display();
|
2012-12-07 09:34:46 +00:00
|
|
|
echo $OUTPUT->footer();
|