2013-04-09 09:28:32 +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
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles viewing a customcert.
|
|
|
|
*
|
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>
|
2013-04-09 09:28:32 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2013-04-11 07:57:39 +00:00
|
|
|
require_once('../../config.php');
|
2013-04-09 09:28:32 +00:00
|
|
|
|
|
|
|
$id = required_param('id', PARAM_INT);
|
|
|
|
$action = optional_param('action', '', PARAM_ALPHA);
|
|
|
|
|
|
|
|
$cm = get_coursemodule_from_id('customcert', $id, 0, false, MUST_EXIST);
|
|
|
|
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
2013-04-26 07:42:11 +00:00
|
|
|
$customcert = $DB->get_record('customcert', array('id' => $cm->instance), '*', MUST_EXIST);
|
2016-02-16 09:03:34 +00:00
|
|
|
$template = $DB->get_record('customcert_templates', array('id' => $customcert->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);
|
2013-04-09 09:28:32 +00:00
|
|
|
require_capability('mod/customcert:view', $context);
|
|
|
|
|
|
|
|
// Initialise $PAGE.
|
2013-05-06 10:22:55 +00:00
|
|
|
$pageurl = new moodle_url('/mod/customcert/view.php', array('id' => $cm->id));
|
2016-02-16 09:03:34 +00:00
|
|
|
\mod_customcert\page_helper::page_setup($pageurl, $context, format_string($customcert->name));
|
2013-04-09 09:28:32 +00:00
|
|
|
|
2015-03-11 07:29:18 +00:00
|
|
|
// Check if the user can view the certificate based on time spent in course.
|
2016-11-29 05:30:16 +00:00
|
|
|
if ($customcert->requiredtime && !has_capability('mod/customcert:manage', $context)) {
|
2016-02-16 09:03:34 +00:00
|
|
|
if (\mod_customcert\certificate::get_course_time($course->id) < ($customcert->requiredtime * 60)) {
|
2015-03-11 07:29:18 +00:00
|
|
|
$a = new stdClass;
|
|
|
|
$a->requiredtime = $customcert->requiredtime;
|
2017-02-01 06:42:59 +00:00
|
|
|
notice(get_string('requiredtimenotmet', 'customcert', $a), "$CFG->wwwroot/course/view.php?id=$course->id");
|
2015-03-11 07:29:18 +00:00
|
|
|
die;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 06:10:48 +00:00
|
|
|
$event = \mod_customcert\event\course_module_viewed::create(array(
|
|
|
|
'objectid' => $customcert->id,
|
|
|
|
'context' => $context,
|
|
|
|
));
|
|
|
|
$event->add_record_snapshot('course', $course);
|
|
|
|
$event->add_record_snapshot('customcert', $customcert);
|
|
|
|
$event->trigger();
|
|
|
|
|
2013-04-09 09:28:32 +00:00
|
|
|
// Check that no action was passed, if so that means we are not outputting to PDF.
|
|
|
|
if (empty($action)) {
|
|
|
|
// 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 link to the report if there are issues to display.
|
|
|
|
$reportlink = '';
|
2017-06-02 04:42:04 +00:00
|
|
|
if (has_capability('mod/customcert:viewreport', $context)) {
|
2013-04-09 09:28:32 +00:00
|
|
|
// Get the total number of issues.
|
2016-02-16 09:03:34 +00:00
|
|
|
$numissues = \mod_customcert\certificate::get_number_of_issues($customcert->id, $cm, $groupmode);
|
2015-03-11 21:58:52 +00:00
|
|
|
$href = new moodle_urL('/mod/customcert/report.php', array('id' => $cm->id));
|
|
|
|
$url = html_writer::tag('a', get_string('viewcustomcertissues', 'customcert', $numissues),
|
|
|
|
array('href' => $href->out()));
|
|
|
|
$reportlink = html_writer::tag('div', $url, array('class' => 'reportlink'));
|
2013-04-09 09:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the intro content if it exists.
|
|
|
|
$intro = '';
|
|
|
|
if (!empty($customcert->intro)) {
|
|
|
|
$intro = $OUTPUT->box(format_module_intro('customcert', $customcert, $cm->id), 'generalbox', 'intro');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current user has been issued a customcert generate HTML to display the details.
|
|
|
|
$issuelist = '';
|
|
|
|
if ($issues = $DB->get_records('customcert_issues', array('userid' => $USER->id, 'customcertid' => $customcert->id))) {
|
|
|
|
$table = new html_table();
|
|
|
|
$table->class = 'generaltable';
|
|
|
|
$table->head = array(get_string('issued', 'customcert'));
|
|
|
|
$table->align = array('left');
|
|
|
|
$table->attributes = array('style' => 'width:20%; margin:auto');
|
|
|
|
|
|
|
|
foreach ($issues as $issue) {
|
|
|
|
$row = array();
|
|
|
|
$row[] = userdate($issue->timecreated);
|
|
|
|
$table->data[$issue->id] = $row;
|
|
|
|
}
|
|
|
|
|
2018-06-18 10:38:03 +00:00
|
|
|
$issuelist = html_writer::table($table) . "<br />";
|
2013-04-09 09:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the button to download the customcert.
|
|
|
|
$linkname = get_string('getcustomcert', 'customcert');
|
|
|
|
$link = new moodle_url('/mod/customcert/view.php', array('id' => $cm->id, 'action' => 'download'));
|
|
|
|
$downloadbutton = new single_button($link, $linkname);
|
|
|
|
$downloadbutton = html_writer::tag('div', $OUTPUT->render($downloadbutton), array('style' => 'text-align:center'));
|
|
|
|
|
|
|
|
// Output all the page data.
|
|
|
|
echo $OUTPUT->header();
|
2015-03-11 21:58:52 +00:00
|
|
|
groups_print_activity_menu($cm, $pageurl);
|
2013-04-09 09:28:32 +00:00
|
|
|
echo $reportlink;
|
|
|
|
echo $intro;
|
|
|
|
echo $issuelist;
|
|
|
|
echo $downloadbutton;
|
|
|
|
echo $OUTPUT->footer($course);
|
|
|
|
exit;
|
2017-02-02 09:17:41 +00:00
|
|
|
} else { // Output to pdf.
|
2013-04-09 09:28:32 +00:00
|
|
|
// Create new customcert issue record if one does not already exist.
|
|
|
|
if (!$DB->record_exists('customcert_issues', array('userid' => $USER->id, 'customcertid' => $customcert->id))) {
|
2018-05-23 06:01:55 +00:00
|
|
|
\mod_customcert\certificate::issue_certificate($customcert->id, $USER->id);
|
2013-04-09 09:28:32 +00:00
|
|
|
}
|
2016-11-29 07:43:19 +00:00
|
|
|
|
|
|
|
// Set the custom certificate as viewed.
|
|
|
|
$completion = new completion_info($course);
|
|
|
|
$completion->set_module_viewed($cm);
|
|
|
|
|
2017-08-09 11:05:57 +00:00
|
|
|
// Hack alert - don't initiate the download when running Behat.
|
|
|
|
if (defined('BEHAT_SITE_RUNNING')) {
|
|
|
|
redirect(new moodle_url('/mod/customcert/view.php', array('id' => $cm->id)));
|
|
|
|
}
|
|
|
|
|
2013-04-09 09:28:32 +00:00
|
|
|
// Now we want to generate the PDF.
|
2016-02-16 09:03:34 +00:00
|
|
|
$template = new \mod_customcert\template($template);
|
|
|
|
$template->generate_pdf();
|
2017-05-31 08:08:42 +00:00
|
|
|
exit();
|
2013-04-09 09:28:32 +00:00
|
|
|
}
|