moodle-mod_htmlcert/view.php

191 lines
7.6 KiB
PHP
Raw Permalink Normal View History

2013-04-09 09:28:32 +00:00
<?php
2021-11-24 07:29:43 +00:00
// This file is part of the htmlcert module for Moodle - http://moodle.org/
2013-04-09 09:28:32 +00:00
//
// 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
* Handles viewing a htmlcert.
2013-04-09 09:28:32 +00:00
*
2021-11-24 07:29:43 +00:00
* @package mod_htmlcert
2022-09-07 13:59:29 +00:00
* @copyright 2013 Mark Nelson <markn@moodle.com>, 2021-2022 Kumi Systems e.U. <office@kumi.systems>
2013-04-09 09:28:32 +00:00
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
2013-04-09 09:28:32 +00:00
$id = required_param('id', PARAM_INT);
$downloadown = optional_param('downloadown', false, PARAM_BOOL);
$downloadtable = optional_param('download', null, PARAM_ALPHA);
$downloadissue = optional_param('downloadissue', 0, PARAM_INT);
$deleteissue = optional_param('deleteissue', 0, PARAM_INT);
$confirm = optional_param('confirm', false, PARAM_BOOL);
$page = optional_param('page', 0, PARAM_INT);
2021-11-24 07:29:43 +00:00
$perpage = optional_param('perpage', \mod_htmlcert\certificate::HTMLCERT_PER_PAGE, PARAM_INT);
2013-04-09 09:28:32 +00:00
2021-11-24 07:29:43 +00:00
$cm = get_coursemodule_from_id('htmlcert', $id, 0, false, MUST_EXIST);
2013-04-09 09:28:32 +00:00
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
2021-11-24 07:29:43 +00:00
$htmlcert = $DB->get_record('htmlcert', array('id' => $cm->instance), '*', MUST_EXIST);
$template = $DB->get_record('htmlcert_templates', array('id' => $htmlcert->templateid), '*', MUST_EXIST);
2013-04-09 09:28:32 +00:00
// Ensure the user is allowed to view this page.
2015-03-11 22:57:45 +00:00
require_login($course, false, $cm);
2013-05-28 09:00:07 +00:00
$context = context_module::instance($cm->id);
2021-11-24 07:29:43 +00:00
require_capability('mod/htmlcert:view', $context);
2013-04-09 09:28:32 +00:00
2021-11-24 07:29:43 +00:00
$canreceive = has_capability('mod/htmlcert:receiveissue', $context);
$canmanage = has_capability('mod/htmlcert:manage', $context);
$canviewreport = has_capability('mod/htmlcert:viewreport', $context);
2013-04-09 09:28:32 +00:00
// Initialise $PAGE.
2021-11-24 07:29:43 +00:00
$pageurl = new moodle_url('/mod/htmlcert/view.php', array('id' => $cm->id));
\mod_htmlcert\page_helper::page_setup($pageurl, $context, format_string($htmlcert->name));
2013-04-09 09:28:32 +00:00
// Check if the user can view the certificate based on time spent in course.
2021-11-24 07:29:43 +00:00
if ($htmlcert->requiredtime && !$canmanage) {
if (\mod_htmlcert\certificate::get_course_time($course->id) < ($htmlcert->requiredtime * 60)) {
$a = new stdClass;
2021-11-24 07:29:43 +00:00
$a->requiredtime = $htmlcert->requiredtime;
$url = new moodle_url('/course/view.php', ['id' => $course->id]);
2021-11-24 07:29:43 +00:00
notice(get_string('requiredtimenotmet', 'htmlcert', $a), $url);
die;
}
}
// Check if we are deleting an issue.
if ($deleteissue && $canmanage && confirm_sesskey()) {
if (!$confirm) {
2021-11-24 07:29:43 +00:00
$nourl = new moodle_url('/mod/htmlcert/view.php', ['id' => $id]);
$yesurl = new moodle_url('/mod/htmlcert/view.php',
[
'id' => $id,
'deleteissue' => $deleteissue,
'confirm' => 1,
'sesskey' => sesskey()
]
);
// Show a confirmation page.
2021-11-24 07:29:43 +00:00
$PAGE->navbar->add(get_string('deleteconfirm', 'htmlcert'));
$message = get_string('deleteissueconfirm', 'htmlcert');
echo $OUTPUT->header();
2021-11-24 07:29:43 +00:00
echo $OUTPUT->heading(format_string($htmlcert->name));
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
}
// Delete the issue.
2021-11-24 07:29:43 +00:00
$DB->delete_records('htmlcert_issues', array('id' => $deleteissue, 'htmlcertid' => $htmlcert->id));
// Redirect back to the manage templates page.
2021-11-24 07:29:43 +00:00
redirect(new moodle_url('/mod/htmlcert/view.php', array('id' => $id)));
}
2021-11-24 07:29:43 +00:00
$event = \mod_htmlcert\event\course_module_viewed::create(array(
'objectid' => $htmlcert->id,
'context' => $context,
));
$event->add_record_snapshot('course', $course);
2021-11-24 07:29:43 +00:00
$event->add_record_snapshot('htmlcert', $htmlcert);
$event->trigger();
// Check that we are not downloading a certificate PDF.
if (!$downloadown && !$downloadissue) {
2013-04-09 09:28:32 +00:00
// Get the current groups mode.
2015-03-11 21:58:52 +00:00
if ($groupmode = groups_get_activity_groupmode($cm)) {
groups_get_activity_group($cm, true);
}
2013-04-09 09:28:32 +00:00
// Generate the table to the report if there are issues to display.
if ($canviewreport) {
2013-04-09 09:28:32 +00:00
// Get the total number of issues.
2021-11-24 07:29:43 +00:00
$reporttable = new \mod_htmlcert\report_table($htmlcert->id, $cm, $groupmode, $downloadtable);
$reporttable->define_baseurl($pageurl);
if ($reporttable->is_downloading()) {
$reporttable->download();
exit();
}
2013-04-09 09:28:32 +00:00
}
// Generate the intro content if it exists.
$intro = '';
2021-11-24 07:29:43 +00:00
if (!empty($htmlcert->intro)) {
$intro = $OUTPUT->box(format_module_intro('htmlcert', $htmlcert, $cm->id), 'generalbox', 'intro');
2013-04-09 09:28:32 +00:00
}
2021-11-24 07:29:43 +00:00
// If the current user has been issued a htmlcert generate HTML to display the details.
$issuehtml = '';
2021-11-24 07:29:43 +00:00
$issues = $DB->get_records('htmlcert_issues', array('userid' => $USER->id, 'htmlcertid' => $htmlcert->id));
if ($issues && !$canmanage) {
// Get the most recent issue (there should only be one).
$issue = reset($issues);
2021-11-24 07:29:43 +00:00
$issuestring = get_string('receiveddate', 'htmlcert') . ': ' . userdate($issue->timecreated);
$issuehtml = $OUTPUT->box($issuestring);
2013-04-09 09:28:32 +00:00
}
2021-11-24 07:29:43 +00:00
// Create the button to download the htmlcert.
$downloadbutton = '';
if ($canreceive) {
2021-11-24 07:29:43 +00:00
$linkname = get_string('gethtmlcert', 'htmlcert');
$link = new moodle_url('/mod/htmlcert/view.php', array('id' => $cm->id, 'downloadown' => true));
$downloadbutton = new single_button($link, $linkname, 'get', true);
$downloadbutton->class .= ' m-b-1'; // Seems a bit hackish, ahem.
2022-09-07 13:59:29 +00:00
$downloadbutton->add_action(new \popup_action('click', $link));
$downloadbutton = $OUTPUT->render($downloadbutton);
}
2013-04-09 09:28:32 +00:00
// Output all the page data.
echo $OUTPUT->header();
2021-11-24 07:29:43 +00:00
echo $OUTPUT->heading(format_string($htmlcert->name));
2013-04-09 09:28:32 +00:00
echo $intro;
echo $issuehtml;
2013-04-09 09:28:32 +00:00
echo $downloadbutton;
if (isset($reporttable)) {
2021-11-24 07:29:43 +00:00
$numissues = \mod_htmlcert\certificate::get_number_of_issues($htmlcert->id, $cm, $groupmode);
echo $OUTPUT->heading(get_string('listofissues', 'htmlcert', $numissues), 3);
groups_print_activity_menu($cm, $pageurl);
echo $reporttable->out($perpage, false);
}
2013-04-09 09:28:32 +00:00
echo $OUTPUT->footer($course);
exit();
} else if ($canreceive || $canmanage) { // Output to pdf.
// Set the userid value of who we are downloading the certificate for.
$userid = $USER->id;
if ($downloadown) {
2021-11-24 07:29:43 +00:00
// Create new htmlcert issue record if one does not already exist.
if (!$DB->record_exists('htmlcert_issues', array('userid' => $USER->id, 'htmlcertid' => $htmlcert->id))) {
\mod_htmlcert\certificate::issue_certificate($htmlcert->id, $USER->id);
}
// Set the custom certificate as viewed.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
} else if ($downloadissue && $canviewreport) {
$userid = $downloadissue;
}
2017-08-09 11:05:57 +00:00
// Hack alert - don't initiate the download when running Behat.
if (defined('BEHAT_SITE_RUNNING')) {
2021-11-24 07:29:43 +00:00
redirect(new moodle_url('/mod/htmlcert/view.php', array('id' => $cm->id)));
2017-08-09 11:05:57 +00:00
}
\core\session\manager::write_close();
2013-04-09 09:28:32 +00:00
// Now we want to generate the PDF.
2021-11-24 07:29:43 +00:00
$template = new \mod_htmlcert\template($template);
$template->generate_pdf(false, $userid);
exit();
2013-04-09 09:28:32 +00:00
}