function certificate_prepare_issue($course, $user)
{
    global $certificate;
    if (record_exists("certificate_issues", "certificateid", $certificate->id, "userid", $user->id)) {
        return get_record("certificate_issues", "certificateid", $certificate->id, "userid", $user->id);
    } else {
        if (certificate_grade_condition()) {
            $timecreated = time();
            $certdate = certificate_generate_date($certificate, $course);
            if (!$certdate) {
                $certdate = $timecreated;
            }
            $code = certificate_generate_code();
            if ($certificate->printgrade == 1) {
                $coursegrade = get_course_grade($course->id);
            } else {
                if ($certificate->printgrade > 1) {
                    $coursegrade = certificate_mod_grade($course, $certificate->printgrade);
                }
            }
            if ($certificate->gradefmt == 1) {
                $gradeinput = $coursegrade->percentage . '%';
            } else {
                if ($certificate->gradefmt == 2) {
                    $gradeinput = $coursegrade->points . ' %%P%%';
                } else {
                    if ($certificate->gradefmt == 3) {
                        $gradeinput = certificate_get_gradeletter($coursegrade->percentage);
                    }
                }
            }
            $studentname = str_replace('\'', '\\\'', certificate_generate_studentname($course, $user));
            insert_record("certificate_issues", array("certificateid" => $certificate->id, "userid" => $user->id, "timecreated" => $timecreated, "studentname" => $studentname, "code" => $code, "classname" => str_replace('\'', '\\\'', $course->fullname), "certdate" => $certdate, "credits" => $certificate->credithours, "grade" => $gradeinput), false);
            certificate_email_teachers($certificate);
        }
    }
}
        $group->id = 0;
        // Not in a group, never mind
    }
    $teachers = get_group_teachers($course->id, $group->id);
    // Works even if not in group
} 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'])) {