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');
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
$tid = optional_param('tid', 0, PARAM_INT);
|
2016-02-20 09:12:32 +00:00
|
|
|
$action = optional_param('action', '', PARAM_ALPHA);
|
|
|
|
if ($action) {
|
|
|
|
$actionid = required_param('aid', PARAM_INT);
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
$confirm = optional_param('confirm', 0, PARAM_INT);
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// Edit an existing template.
|
|
|
|
if ($tid) {
|
|
|
|
// Create the template object.
|
|
|
|
$template = $DB->get_record('customcert_templates', array('id' => $tid), '*', MUST_EXIST);
|
|
|
|
$template = new \mod_customcert\template($template);
|
|
|
|
// Set the context.
|
|
|
|
$contextid = $template->get_contextid();
|
|
|
|
// Set the page url.
|
|
|
|
$pageurl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));
|
|
|
|
} else { // Adding a new template.
|
|
|
|
// Need to supply the contextid.
|
|
|
|
$contextid = required_param('contextid', PARAM_INT);
|
|
|
|
// Set the page url.
|
|
|
|
$pageurl = new moodle_url('/mod/customcert/edit.php', array('contextid' => $contextid));
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
$context = context::instance_by_id($contextid);
|
|
|
|
if ($context->contextlevel == CONTEXT_MODULE) {
|
|
|
|
$cm = get_coursemodule_from_id('customcert', $context->instanceid, 0, false, MUST_EXIST);
|
|
|
|
require_login($cm->course, false, $cm);
|
|
|
|
} else {
|
|
|
|
require_login();
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
require_capability('mod/customcert:manage', $context);
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// Set up the page.
|
|
|
|
\mod_customcert\page_helper::page_setup($pageurl, $context, get_string('editcustomcert', 'customcert'));
|
|
|
|
|
|
|
|
if ($context->contextlevel == CONTEXT_SYSTEM) {
|
|
|
|
// We are managing a template - add some navigation.
|
|
|
|
$PAGE->navbar->add(get_string('managetemplates', 'customcert'),
|
|
|
|
new moodle_url('/mod/customcert/manage_templates.php'));
|
|
|
|
$PAGE->navbar->add(get_string('editcustomcert', 'customcert'));
|
2013-05-03 10:51:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// The form for loading a customcert templates.
|
2016-08-25 04:11:38 +00:00
|
|
|
if ($tid && $DB->count_records('customcert_templates', array('contextid' => context_system::instance()->id)) > 0) {
|
2016-02-16 09:03:34 +00:00
|
|
|
$loadtemplateurl = new moodle_url('/mod/customcert/load_template.php', array('tid' => $tid));
|
|
|
|
$loadtemplateform = new \mod_customcert\load_template_form($loadtemplateurl);
|
2013-05-03 10:51:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
// Flag to determine if we are deleting anything.
|
|
|
|
$deleting = false;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
if ($tid) {
|
2016-02-20 09:12:32 +00:00
|
|
|
switch ($action) {
|
|
|
|
case 'pmoveup' :
|
2016-02-20 09:46:31 +00:00
|
|
|
$template->move_item('page', $actionid, 'up');
|
2016-02-20 09:12:32 +00:00
|
|
|
break;
|
|
|
|
case 'pmovedown' :
|
2016-02-20 09:46:31 +00:00
|
|
|
$template->move_item('page', $actionid, 'down');
|
2016-02-20 09:12:32 +00:00
|
|
|
break;
|
|
|
|
case 'emoveup' :
|
2016-02-20 09:46:31 +00:00
|
|
|
$template->move_item('element', $actionid, 'up');
|
2016-02-20 09:12:32 +00:00
|
|
|
break;
|
|
|
|
case 'emovedown' :
|
2016-02-20 09:46:31 +00:00
|
|
|
$template->move_item('element', $actionid, 'down');
|
2016-02-20 09:12:32 +00:00
|
|
|
break;
|
|
|
|
case 'deletepage' :
|
|
|
|
if (!empty($confirm)) { // Check they have confirmed the deletion.
|
|
|
|
$template->delete_page($actionid);
|
|
|
|
} 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('tid' => $tid));
|
|
|
|
$yesurl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid,
|
|
|
|
'action' => 'deletepage',
|
|
|
|
'aid' => $actionid,
|
|
|
|
'confirm' => 1,
|
|
|
|
'sesskey' => sesskey()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'deleteelement' :
|
|
|
|
if (!empty($confirm)) { // Check they have confirmed the deletion.
|
|
|
|
$template->delete_element($actionid);
|
|
|
|
} 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('tid' => $tid));
|
|
|
|
$yesurl = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid,
|
|
|
|
'action' => 'deleteelement',
|
|
|
|
'aid' => $actionid,
|
|
|
|
'confirm' => 1,
|
|
|
|
'sesskey' => sesskey()));
|
|
|
|
}
|
|
|
|
break;
|
2013-04-12 08:24:58 +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);
|
|
|
|
echo $OUTPUT->header();
|
|
|
|
echo $OUTPUT->heading($strheading);
|
|
|
|
echo $OUTPUT->confirm($message, $yesurl, $nourl);
|
|
|
|
echo $OUTPUT->footer();
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
if ($tid) {
|
|
|
|
$mform = new \mod_customcert\edit_form($pageurl, array('tid' => $tid));
|
|
|
|
// Set the name for the form.
|
|
|
|
$mform->set_data(array('name' => $template->get_name()));
|
|
|
|
} else {
|
|
|
|
$mform = new \mod_customcert\edit_form($pageurl);
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
|
|
|
|
if ($data = $mform->get_data()) {
|
2016-02-16 09:03:34 +00:00
|
|
|
// If there is no id, then we are creating a template.
|
|
|
|
if (!$tid) {
|
|
|
|
$template = \mod_customcert\template::create($data->name, $contextid);
|
|
|
|
|
|
|
|
// Create a page for this template.
|
|
|
|
$pageid = $template->add_page();
|
|
|
|
|
|
|
|
// Associate all the data from the form to the newly created page.
|
|
|
|
$width = 'pagewidth_' . $pageid;
|
|
|
|
$height = 'pageheight_' . $pageid;
|
|
|
|
$leftmargin = 'pageleftmargin_' . $pageid;
|
|
|
|
$rightmargin = 'pagerightmargin_' . $pageid;
|
|
|
|
$rightmargin = 'pagerightmargin_' . $pageid;
|
|
|
|
|
2016-02-20 10:04:56 +00:00
|
|
|
$data->$width = $data->pagewidth_0;
|
|
|
|
$data->$height = $data->pageheight_0;
|
|
|
|
$data->$leftmargin = $data->pageleftmargin_0;
|
|
|
|
$data->$rightmargin = $data->pagerightmargin_0;
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// We may also have clicked to add an element, so these need changing as well.
|
2016-02-20 10:04:56 +00:00
|
|
|
if (isset($data->element_0) && isset($data->addelement_0)) {
|
2016-02-16 09:03:34 +00:00
|
|
|
$element = 'element_' . $pageid;
|
|
|
|
$addelement = 'addelement_' . $pageid;
|
2016-02-20 10:04:56 +00:00
|
|
|
$data->$element = $data->element_0;
|
|
|
|
$data->$addelement = $data->addelement_0;
|
2016-02-16 09:03:34 +00:00
|
|
|
|
|
|
|
// Need to remove the temporary element and add element placeholders so we
|
|
|
|
// don't try add an element to the wrong page.
|
2016-02-20 10:04:56 +00:00
|
|
|
unset($data->element_0);
|
|
|
|
unset($data->addelement_0);
|
2016-02-16 09:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save any data for the template.
|
|
|
|
$template->save($data);
|
|
|
|
|
2012-12-07 09:34:46 +00:00
|
|
|
// Save any page data.
|
2016-02-16 09:03:34 +00:00
|
|
|
$template->save_page($data);
|
2012-12-07 09:34:46 +00:00
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
// Check if we are adding a page.
|
2013-07-25 10:36:43 +00:00
|
|
|
if (!empty($data->addcertpage)) {
|
2016-02-16 09:03:34 +00:00
|
|
|
$template->add_page();
|
2013-06-06 10:59:08 +00:00
|
|
|
}
|
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) {
|
2015-12-06 08:54:19 +00:00
|
|
|
// Check if they chose to add an element to a page.
|
|
|
|
if (strpos($key, 'addelement_') !== false) {
|
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();
|
2016-02-16 09:03:34 +00:00
|
|
|
$params['tid'] = $template->get_id();
|
2013-06-06 10:59:08 +00:00
|
|
|
$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)) {
|
2016-02-16 09:03:34 +00:00
|
|
|
$template->generate_pdf(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.
|
2016-02-16 09:03:34 +00:00
|
|
|
$url = new moodle_url('/mod/customcert/edit.php', array('tid' => $template->get_id()));
|
2013-05-06 10:22:55 +00:00
|
|
|
redirect($url);
|
2012-12-07 09:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2016-02-16 09:03:34 +00:00
|
|
|
if (isset($loadtemplateform)) {
|
2013-05-03 10:51:41 +00:00
|
|
|
$loadtemplateform->display();
|
|
|
|
}
|
2012-12-07 09:34:46 +00:00
|
|
|
echo $OUTPUT->footer();
|