|
| 1 | +<?php |
| 2 | +// Include required Moodle configuration and custom certificate library. |
| 3 | +require_once(__DIR__ . '/../../config.php'); |
| 4 | +require_once($CFG->dirroot . '/mod/customcert/lib.php'); |
| 5 | + |
| 6 | +// Set up the page context before processing any parameters. |
| 7 | +// This ensures that Moodle properly initializes the page and handles any errors gracefully. |
| 8 | +$context = context_system::instance(); |
| 9 | +$PAGE->set_context($context); |
| 10 | +$PAGE->set_url('/mod/customcert/view_user_cert.php'); |
| 11 | +$PAGE->set_title('View certificate'); |
| 12 | +$PAGE->set_heading('View certificate'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Displays an error message in a formatted Moodle page and exits. |
| 16 | + * |
| 17 | + * This function helps standardize error handling by rendering the page |
| 18 | + * properly and showing the error message in an alert box. |
| 19 | + * |
| 20 | + * @param string $message The error message to display. |
| 21 | + */ |
| 22 | +function display_error_page($message) { |
| 23 | + global $OUTPUT; |
| 24 | + |
| 25 | + echo $OUTPUT->header(); // Display the page header. |
| 26 | + echo $OUTPUT->box($message, 'alert alert-danger'); // Display the error message in a styled box. |
| 27 | + echo $OUTPUT->footer(); // Display the page footer. |
| 28 | + exit; // Stop further execution. |
| 29 | +} |
| 30 | + |
| 31 | +// Retrieve certificate code and verification token from URL parameters. |
| 32 | +// 'optional_param' is used instead of 'required_param' to avoid Moodle throwing an automatic error page. |
| 33 | +$cert_code = optional_param('cert_code', '', PARAM_ALPHANUMEXT); |
| 34 | +$token = optional_param('token', '', PARAM_ALPHANUMEXT); |
| 35 | + |
| 36 | +// Ensure both required parameters are provided. |
| 37 | +if (empty($cert_code) || empty($token)) { |
| 38 | + display_error_page('Certificate code or verification token is missing. Please check the URL and try again.'); |
| 39 | +} |
| 40 | + |
| 41 | +// Validate the provided token by regenerating it using the expected algorithm. |
| 42 | +$expected_token = calculate_signature($cert_code); |
| 43 | +if ($token !== $expected_token) { |
| 44 | + display_error_page('The verification token is invalid for this certificate. Please check the URL and try again.'); |
| 45 | +} |
| 46 | + |
| 47 | +// Retrieve the certificate issue entry using the provided certificate code. |
| 48 | +// This helps fetch the associated user ID to verify ownership. |
| 49 | +$issue = $DB->get_record('customcert_issues', ['code' => $cert_code], '*'); |
| 50 | + |
| 51 | +if (!$issue) { |
| 52 | + display_error_page('The certificate with the provided code could not be found. Please verify the certificate code and try again.'); |
| 53 | +} |
| 54 | + |
| 55 | +// Fetch the certificate associated with the retrieved issue. |
| 56 | +// The certificate must be one of the recognized eCard types: 'Cognitive eCard' or 'Completion eCard'. |
| 57 | +$certificate = $DB->get_record_sql(" |
| 58 | + SELECT * FROM {customcert} |
| 59 | + WHERE id = ? AND name IN ('Cognitive eCard', 'Completion eCard') |
| 60 | +", [$issue->customcertid]); |
| 61 | + |
| 62 | +if (!$certificate) { |
| 63 | + display_error_page('The certificate type is not valid or does not exist. Please contact the site administrator for assistance.'); |
| 64 | +} |
| 65 | + |
| 66 | +// Retrieve the corresponding template for the fetched certificate. |
| 67 | +// The template defines the layout and content of the generated certificate. |
| 68 | +$template = $DB->get_record('customcert_templates', ['id' => $certificate->templateid]); |
| 69 | +if (!$template) { |
| 70 | + display_error_page('The certificate template could not be found. Please contact the site administrator for assistance.'); |
| 71 | +} |
| 72 | + |
| 73 | +try { |
| 74 | + // Convert the template record into a template object. |
| 75 | + // This object provides methods to generate and render the certificate. |
| 76 | + $template = new \mod_customcert\template($template); |
| 77 | + |
| 78 | + // Generate and output the certificate PDF. |
| 79 | + // 'false' indicates that the PDF is displayed inline instead of being force-downloaded. |
| 80 | + // The second parameter ensures the certificate is generated for the correct user. |
| 81 | + $template->generate_pdf(false, $issue->userid); |
| 82 | +} catch (Exception $e) { |
| 83 | + // Catch any errors that may occur while generating the certificate PDF. |
| 84 | + display_error_page('There was an error generating the certificate PDF. Please try again later or contact support if the problem persists.'); |
| 85 | +} |
| 86 | + |
| 87 | +// Prevent further execution after rendering the certificate. |
| 88 | +exit; |
0 commit comments