moodle-local_replication/get_certificate.php
2023-03-30 05:31:01 +00:00

80 lines
2.4 KiB
PHP

<?php
define('CLI_SCRIPT', true);
require_once("../../config.php");
require_once($CFG->libdir . '/clilib.php');
$usage = "Export a certificate PDF
Usage:
# php get_certificate.php --module=<htmlcert|customcert> --course=<value> --username=<value>
# php get_certificate.php [--help|-h]
Options:
-h --help Print this help.
--paramname=<value> 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);