You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
6.6 KiB
160 lines
6.6 KiB
<?php
|
|
|
|
// This file is part of the Certificate 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 certificate
|
|
*
|
|
* @package mod
|
|
* @subpackage certificate
|
|
* @copyright Mark Nelson <markn@moodle.com>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
require_once("../../config.php");
|
|
require_once("$CFG->dirroot/mod/certificate/deprecatedlib.php");
|
|
require_once("$CFG->dirroot/mod/certificate/lib.php");
|
|
require_once("$CFG->libdir/pdflib.php");
|
|
|
|
$id = required_param('id', PARAM_INT); // Course Module ID
|
|
$action = optional_param('action', '', PARAM_ALPHA);
|
|
$edit = optional_param('edit', -1, PARAM_BOOL);
|
|
|
|
if (!$cm = get_coursemodule_from_id('certificate', $id)) {
|
|
print_error('Course Module ID was incorrect');
|
|
}
|
|
if (!$course = $DB->get_record('course', array('id'=> $cm->course))) {
|
|
print_error('course is misconfigured');
|
|
}
|
|
if (!$certificate = $DB->get_record('certificate', array('id'=> $cm->instance))) {
|
|
print_error('course module is incorrect');
|
|
}
|
|
|
|
require_login($course->id, false, $cm);
|
|
$context = context_module::instance($cm->id);
|
|
require_capability('mod/certificate:view', $context);
|
|
|
|
// log update
|
|
add_to_log($course->id, 'certificate', 'view', "view.php?id=$cm->id", $certificate->id, $cm->id);
|
|
$completion=new completion_info($course);
|
|
$completion->set_module_viewed($cm);
|
|
|
|
// Initialize $PAGE, compute blocks
|
|
$PAGE->set_url('/mod/certificate/view.php', array('id' => $cm->id));
|
|
$PAGE->set_context($context);
|
|
$PAGE->set_cm($cm);
|
|
$PAGE->set_title(format_string($certificate->name));
|
|
$PAGE->set_heading(format_string($course->fullname));
|
|
|
|
// Set the context
|
|
$context = context_module::instance($cm->id);
|
|
|
|
if (($edit != -1) and $PAGE->user_allowed_editing()) {
|
|
$USER->editing = $edit;
|
|
}
|
|
|
|
// Add block editing button
|
|
if ($PAGE->user_allowed_editing()) {
|
|
$editvalue = $PAGE->user_is_editing() ? 'off' : 'on';
|
|
$strsubmit = $PAGE->user_is_editing() ? get_string('blockseditoff') : get_string('blocksediton');
|
|
$url = new moodle_url($CFG->wwwroot . '/mod/certificate/view.php', array('id' => $cm->id, 'edit' => $editvalue));
|
|
$PAGE->set_button($OUTPUT->single_button($url, $strsubmit));
|
|
}
|
|
|
|
// Check if the user can view the certificate
|
|
if ($certificate->requiredtime && !has_capability('mod/certificate:manage', $context)) {
|
|
if (certificate_get_course_time($course->id) < ($certificate->requiredtime * 60)) {
|
|
$a = new stdClass;
|
|
$a->requiredtime = $certificate->requiredtime;
|
|
notice(get_string('requiredtimenotmet', 'certificate', $a), "$CFG->wwwroot/course/view.php?id=$course->id");
|
|
die;
|
|
}
|
|
}
|
|
|
|
// Check if the user has completed the course
|
|
if (!$completion->is_course_complete($USER->id) && !has_capability('mod/certificate:manage', $context)) {
|
|
notice(get_string('completionrequired','certificate'), "$CFG->wwwroot/course/view.php?id=$course->id");
|
|
die;
|
|
}
|
|
|
|
// Create new certificate record, or return existing record
|
|
$certrecord = certificate_get_issue($course, $USER, $certificate, $cm);
|
|
|
|
make_cache_directory('tcpdf');
|
|
|
|
// Load the specific certificate type.
|
|
require("$CFG->dirroot/mod/certificate/type/$certificate->certificatetype/certificate.php");
|
|
|
|
if (empty($action)) { // Not displaying PDF
|
|
echo $OUTPUT->header();
|
|
|
|
/// find out current groups mode
|
|
groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/certificate/view.php?id=' . $cm->id);
|
|
$currentgroup = groups_get_activity_group($cm);
|
|
$groupmode = groups_get_activity_groupmode($cm);
|
|
|
|
if (has_capability('mod/certificate:manage', $context)) {
|
|
$numusers = count(certificate_get_issues($certificate->id, 'ci.timecreated ASC', $groupmode, $cm));
|
|
$url = html_writer::tag('a', get_string('viewcertificateviews', 'certificate', $numusers),
|
|
array('href' => $CFG->wwwroot . '/mod/certificate/report.php?id=' . $cm->id));
|
|
echo html_writer::tag('div', $url, array('class' => 'reportlink'));
|
|
}
|
|
|
|
if (!empty($certificate->intro)) {
|
|
echo $OUTPUT->box(format_module_intro('certificate', $certificate, $cm->id), 'generalbox', 'intro');
|
|
}
|
|
|
|
if ($attempts = certificate_get_attempts($certificate->id)) {
|
|
echo certificate_print_attempts($course, $certificate, $attempts);
|
|
}
|
|
if ($certificate->delivery == 0) {
|
|
$str = get_string('openwindow', 'certificate');
|
|
} elseif ($certificate->delivery == 1) {
|
|
$str = get_string('opendownload', 'certificate');
|
|
} elseif ($certificate->delivery == 2) {
|
|
$str = get_string('openemail', 'certificate');
|
|
}
|
|
echo html_writer::tag('p', $str, array('style' => 'text-align:center'));
|
|
$linkname = get_string('getcertificate', 'certificate');
|
|
// Add to log, only if we are reissuing
|
|
add_to_log($course->id, 'certificate', 'view', "view.php?id=$cm->id", $certificate->id, $cm->id);
|
|
|
|
$link = new moodle_url('/mod/certificate/view.php?id='.$cm->id.'&action=get');
|
|
$button = new single_button($link, $linkname);
|
|
$button->add_action(new popup_action('click', $link, 'view'.$cm->id, array('height' => 600, 'width' => 800)));
|
|
|
|
echo html_writer::tag('div', $OUTPUT->render($button), array('style' => 'text-align:center'));
|
|
echo $OUTPUT->footer($course);
|
|
exit;
|
|
} else { // Output to pdf
|
|
// Remove full-stop at the end if it exists, to avoid "..pdf" being created and being filtered by clean_filename
|
|
$certname = rtrim($certificate->name, '.');
|
|
$filename = clean_filename("$certname.pdf");
|
|
if ($certificate->savecert == 1) {
|
|
// PDF contents are now in $file_contents as a string
|
|
$file_contents = $pdf->Output('', 'S');
|
|
certificate_save_pdf($file_contents, $certrecord->id, $filename, $context->id);
|
|
}
|
|
if ($certificate->delivery == 0) {
|
|
$pdf->Output($filename, 'I'); // open in browser
|
|
} elseif ($certificate->delivery == 1) {
|
|
$pdf->Output($filename, 'D'); // force download when create
|
|
} elseif ($certificate->delivery == 2) {
|
|
certificate_email_student($course, $certificate, $certrecord, $context);
|
|
$pdf->Output($filename, 'I'); // open in browser
|
|
$pdf->Output('', 'S'); // send
|
|
}
|
|
}
|
|
|