moodle-mod_htmlcert/edit.php

119 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2021-11-24 07:29:43 +00:00
// This file is part of the htmlcert 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/>.
/**
2021-11-24 07:29:43 +00:00
* Edit the htmlcert settings.
*
2021-11-24 07:29:43 +00:00
* @package mod_htmlcert
* @copyright 2013 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
$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);
}
$confirm = optional_param('confirm', 0, PARAM_INT);
// Edit an existing template.
if ($tid) {
// Create the template object.
2021-11-24 07:29:43 +00:00
$template = $DB->get_record('htmlcert_templates', array('id' => $tid), '*', MUST_EXIST);
$template = new \mod_htmlcert\template($template);
// Set the context.
$contextid = $template->get_contextid();
// Set the page url.
2021-11-24 07:29:43 +00:00
$pageurl = new moodle_url('/mod/htmlcert/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.
2021-11-24 07:29:43 +00:00
$pageurl = new moodle_url('/mod/htmlcert/edit.php', array('contextid' => $contextid));
}
$context = context::instance_by_id($contextid);
if ($context->contextlevel == CONTEXT_MODULE) {
2021-11-24 07:29:43 +00:00
$cm = get_coursemodule_from_id('htmlcert', $context->instanceid, 0, false, MUST_EXIST);
require_login($cm->course, false, $cm);
2021-11-24 07:29:43 +00:00
$htmlcert = $DB->get_record('htmlcert', ['id' => $cm->instance], '*', MUST_EXIST);
$title = $htmlcert->name;
$heading = format_string($title);
} else {
require_login();
$title = $SITE->fullname;
$heading = $title;
}
2021-11-24 07:29:43 +00:00
require_capability('mod/htmlcert:manage', $context);
// Set up the page.
2021-11-24 07:29:43 +00:00
\mod_htmlcert\page_helper::page_setup($pageurl, $context, $title);
if ($context->contextlevel == CONTEXT_SYSTEM) {
// We are managing a template - add some navigation.
2021-11-24 07:29:43 +00:00
$PAGE->navbar->add(get_string('managetemplates', 'htmlcert'),
new moodle_url('/mod/htmlcert/manage_templates.php'));
if (!$tid) {
2021-11-24 07:29:43 +00:00
$PAGE->navbar->add(get_string('edithtmlcert', 'htmlcert'));
} else {
2021-11-24 07:29:43 +00:00
$PAGE->navbar->add(get_string('edithtmlcert', 'htmlcert'),
new moodle_url('/mod/htmlcert/edit.php', ['tid' => $tid]));
}
}
if ($tid) {
2021-11-24 07:29:43 +00:00
$mform = new \mod_htmlcert\edit_form($pageurl, array('tid' => $tid));
// Set the name for the form.
$mform->set_data(array('name' => $template->get_name(), 'html' => ''));
} else {
2021-11-24 07:29:43 +00:00
$mform = new \mod_htmlcert\edit_form($pageurl);
}
if ($data = $mform->get_data()) {
// If there is no id, then we are creating a template.
if (!$tid) {
2021-11-24 07:29:43 +00:00
$template = \mod_htmlcert\template::create($data->name, $contextid);
}
// Save any data for the template.
$template->save($data);
// Check if we want to preview this custom certificate.
if (!empty($data->previewbtn)) {
$template->generate_pdf(true);
exit();
}
// Redirect to the editing page to show form with recent updates.
2021-11-24 07:29:43 +00:00
$url = new moodle_url('/mod/htmlcert/edit.php', array('tid' => $template->get_id()));
redirect($url);
}
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
$mform->display();
if ($tid && $context->contextlevel == CONTEXT_MODULE) {
2021-11-24 07:29:43 +00:00
$loadtemplateurl = new moodle_url('/mod/htmlcert/load_template.php', array('tid' => $tid));
$loadtemplateform = new \mod_htmlcert\load_template_form($loadtemplateurl, array('context' => $context), 'post',
2017-08-09 11:05:57 +00:00
'', array('id' => 'loadtemplateform'));
$loadtemplateform->display();
}
echo $OUTPUT->footer();