From 6d30dee4ec3436cabbcc8f5dee6aeccbfd740703 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 30 Mar 2023 05:31:01 +0000 Subject: [PATCH] Add get_certificate.php --- get_certificate.php | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 get_certificate.php diff --git a/get_certificate.php b/get_certificate.php new file mode 100644 index 0000000..d71119b --- /dev/null +++ b/get_certificate.php @@ -0,0 +1,80 @@ +libdir . '/clilib.php'); + +$usage = "Export a certificate PDF + +Usage: + # php get_certificate.php --module= --course= --username= + # php get_certificate.php [--help|-h] + +Options: + -h --help Print this help. + --paramname= Describe the parameter and the meaning of its values. +"; + +list($options, $unrecognised) = cli_get_params([ + 'help' => false, + 'module' => null, + 'course' => null, + 'username' => null, +], [ + 'h' => 'help' +]); + +if ($unrecognised) { + $unrecognised = implode(PHP_EOL . ' ', $unrecognised); + cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised)); +} + +if ($options['help']) { + cli_writeln($usage); + exit(2); +} + +if (empty($options['course'])) { + cli_error('Missing mandatory argument course.', 2); +} + +if (empty($options['username'])) { + cli_error('Missing mandatory argument username.', 2); +} + +try { + $user = $DB->get_record('user', array('username' => $options['username']), "*", MUST_EXIST); +} catch (Exception $ex) { + cli_error('User ' . $options['username'] . ' does not exist.'); +} + +try { + $course = $DB->get_record('course', array('shortcode' => $options['course']), "*", MUST_EXIST); +} catch (Exception $ex) { + cli_error('Course ' . $options['course'] . ' does not exist.'); +} + +try { + $certificate = $DB->get_record($options['module'], array('course' => $course->id), "*", MUST_EXIST); +} catch (Exception $ex) { + cli_error('Course ' . $options['course'] . ' does not have a ' . $options['module'] . ' certificate.'); +} + +$template = $DB->get_record($options['module'] . '_templates', array('id' => $certificate->templateid), "*", MUST_EXIST); + +if ($options['module'] == 'customcert') { + require_once("../../mod/customcert/classes/template.php"); + $template = new \mod_customcert\template($template); +} else { + require_once("../../mod/htmlcert/classes/template.php"); + $template = new \mod_htmlcert\template($template); +} + +try { + $issue = $DB->get_record($options['module'] . '_issues', array($options['module'] . 'id' => $certificate->id, $options['userid'] => $user->id), "*", MUST_EXIST); +} catch (Exception $ex) { + cli_error('User ' . $options['username'] . ' does not have the "' . $certificate->name . '" certificate.'); +} + +$output = $template->generate_pdf(false, $user->id, true); + +cli_write($output);