Add get_certificate.php
This commit is contained in:
parent
9d13d471c3
commit
6d30dee4ec
1 changed files with 80 additions and 0 deletions
80
get_certificate.php
Normal file
80
get_certificate.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?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);
|
Loading…
Reference in a new issue