.
/**
* Manage htmlcert templates.
*
* @package mod_htmlcert
* @copyright 2016 Mark Nelson , 2021 Klaus-Uwe Mitterer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
$contextid = optional_param('contextid', context_system::instance()->id, PARAM_INT);
$action = optional_param('action', '', PARAM_ALPHA);
$confirm = optional_param('confirm', 0, PARAM_INT);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 10, PARAM_INT);
if ($action) {
$tid = required_param('tid', PARAM_INT);
} else {
$tid = optional_param('tid', 0, PARAM_INT);
}
if ($tid) {
$template = $DB->get_record('htmlcert_templates', array('id' => $tid), '*', MUST_EXIST);
$template = new \mod_htmlcert\template($template);
}
$context = context::instance_by_id($contextid);
require_login();
require_capability('mod/htmlcert:manage', $context);
$title = $SITE->fullname;
// Set up the page.
$pageurl = new moodle_url('/mod/htmlcert/manage_templates.php');
\mod_htmlcert\page_helper::page_setup($pageurl, $context, $title);
// Additional page setup.
if ($tid && $action && confirm_sesskey()) {
$PAGE->navbar->add(get_string('managetemplates', 'htmlcert'),
new moodle_url('/mod/htmlcert/manage_templates.php'));
} else {
$PAGE->navbar->add(get_string('managetemplates', 'htmlcert'));
}
$heading = format_string($title, true, ['context' => $context]);
if ($tid) {
if ($action && confirm_sesskey()) {
$nourl = new moodle_url('/mod/htmlcert/manage_templates.php');
$yesurl = new moodle_url('/mod/htmlcert/manage_templates.php',
array(
'tid' => $tid,
'action' => $action,
'confirm' => 1,
'sesskey' => sesskey()
)
);
// Check if we are deleting a template.
if ($action == 'delete') {
if (!$confirm) {
// Show a confirmation page.
$PAGE->navbar->add(get_string('deleteconfirm', 'htmlcert'));
$message = get_string('deletetemplateconfirm', 'htmlcert');
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
}
// Delete the template.
$template->delete();
// Redirect back to the manage templates page.
redirect(new moodle_url('/mod/htmlcert/manage_templates.php'));
} else if ($action == 'duplicate') {
if (!$confirm) {
// Show a confirmation page.
$PAGE->navbar->add(get_string('duplicateconfirm', 'htmlcert'));
$message = get_string('duplicatetemplateconfirm', 'htmlcert');
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
}
// Create another template to copy the data to.
$newtemplate = new \stdClass();
$newtemplate->name = $template->get_name() . ' (' . strtolower(get_string('duplicate', 'htmlcert')) . ')';
$newtemplate->contextid = $template->get_contextid();
$newtemplate->timecreated = time();
$newtemplate->timemodified = $newtemplate->timecreated;
$newtemplateid = $DB->insert_record('htmlcert_templates', $newtemplate);
// Copy the data to the new template.
$template->copy_to_template($newtemplateid);
// Redirect back to the manage templates page.
redirect(new moodle_url('/mod/htmlcert/manage_templates.php'));
}
}
}
$table = new \mod_htmlcert\manage_templates_table($context);
$table->define_baseurl($pageurl);
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
$table->out($perpage, false);
$url = new moodle_url('/mod/htmlcert/edit.php?contextid=' . $contextid);
echo $OUTPUT->single_button($url, get_string('createtemplate', 'htmlcert'), 'get');
echo $OUTPUT->footer();