<?php

include 'tcpdf/tcpdfprotection.php';
require_once 'tcpdf/config/lang/eng.php';
include 'tcpdf/tcpdf.php';
// Load certificate info
$certificateid = $certificate->id;
$certrecord = certificate_get_issue($course, $USER, $certificateid);
$strgrade = get_string('grade', 'certificate');
$strcoursegrade = get_string('coursegrade', 'certificate');
// Date formatting
if ($certificate->printdate > 0) {
    $certificatedate = certificate_date_format('certdate', $certrecord);
} else {
    $certificatedate = "";
}
//Credit hour
$credithour = '';
if ($certificate->printcredithours > 0) {
    $credit = get_string('credithours', 'certificate') . ": " . $certrecord->credits . " " . get_string('hours', 'certificate');
}
//Grade formatting - can be customized if necessary
$grade = '';
if ($certificate->printgradestd > 0) {
    //	if ($modinfo->name) {
    //		$grade = get_string('grade', 'certificate')." ".get_string('on', 'certificate')." ".$modinfo->name.':  '.$certrecord->grade;
    //	} else {
    $grade = $strcoursegrade . ':  ' . $certrecord->grade;
    //	}
    $grade = str_replace('%%P%%', get_string('gradepoints', 'certificate'), $grade);
}
} else {
    $teachers = get_course_teachers($course->id);
}
//Creating pages
$generate = false;
$unenrolment = false;
$type = $certificate->certificatetype;
$certificateid = $certificate->id;
$certrecord = certificate_get_issue($course, $USER, $certificateid);
if ($certificate->printgrade > 1) {
    $modinfo = certificate_mod_grade($course, $certificate->printgrade);
}
//Review certificate
if ($certrecord and !isset($_GET['certificate'])) {
    view_header($course, $certificate, $cm);
    echo "<p align=\"center\">" . get_string('viewed', 'certificate') . "<br /> " . certificate_date_format('timecreated', $certrecord) . ", " . strftime('%X', $certrecord->timecreated) . "</p>";
    echo '<center>';
    echo '<form action="" method="get" name="form1" target="_blank">';
    echo '<input type="hidden" name="id" value=' . $cm->id . ' />';
    echo '<input type="hidden" name="certificate" value=' . $certificate->id . ' />';
    echo '<input type="button" name="Submit" value="' . get_string('backbutton', 'certificate') . '" onClick="JavaScript:history.back();" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="Submit" value="' . $strreviewcertificate . '" />';
    echo '</form>';
    echo '</center>';
    print_footer(NULL, $course);
} else {
    if ($certrecord and isset($_GET['certificate'])) {
        $generate = true;
    } else {
        //Record Certificate
        if (certificate_grade_condition()) {
            if (!isset($_GET['certificate'])) {