From b44d79ada9f2d1f72ee0ff72779f5be22f9283d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Lohnisk=C3=BD?= Date: Wed, 2 Oct 2024 08:41:21 +0200 Subject: [PATCH] Webservice: List issued certificates --- classes/external.php | 126 +++++++++++++++++++++++++++++++++++++++++++ db/services.php | 8 +++ 2 files changed, 134 insertions(+) diff --git a/classes/external.php b/classes/external.php index 3d2f9362..ecebd429 100644 --- a/classes/external.php +++ b/classes/external.php @@ -230,4 +230,130 @@ public static function delete_issue($certificateid, $issueid) { public static function delete_issue_returns() { return new external_value(PARAM_BOOL, 'True if successful, false otherwise'); } + + /** + * Returns list_issues parameters. + * + * @return external_function_parameters + */ + public static function list_issues_parameters() { + return new external_function_parameters( + [ + 'timecreatedfrom' => new external_value(PARAM_INT, 'Timestamp. Returns items created after this date (included).', VALUE_OPTIONAL), + ] + ); + } + + /** + * Returns array of issued certificates. + * + * @param ?int $timecreatedfrom Timestamp. Returns items created after this date (included). + * @return array + */ + public static function list_issues($timecreatedfrom = null) { + global $DB; + + $params = [ + 'timecreatedfrom' => $timecreatedfrom, + ]; + self::validate_parameters(self::list_issues_parameters(), $params); + + + $output = []; + + list($namefields, $nameparams) = \core_user\fields::get_sql_fullname(); + $sql = "SELECT ci.*, $namefields as fullname, u.username, u.email, ct.id as templateid, ct.name as templatename, ct.contextid + FROM {customcert_issues} ci + JOIN {user} u + ON ci.userid = u.id + JOIN {customcert} c + ON ci.customcertid = c.id + JOIN {customcert_templates} ct + ON c.templateid = ct.id"; + if ($timecreatedfrom) { + $sql .= " WHERE ci.timecreated >= :timecreatedfrom"; + $nameparams['timecreatedfrom'] = $timecreatedfrom; + } + + if ($issues = $DB->get_records_sql($sql, $nameparams)) { + + foreach ($issues as $issue) { + + // Generate PDF + + $template = new \stdClass(); + $template->id = $issue->templateid; + $template->name = $issue->templatename; + $template->contextid = $issue->contextid; + $template = new \mod_customcert\template($template); + + $ctname = str_replace(' ', '_', mb_strtolower($template->get_name())); + $pdfname = $ctname . '_' . 'certificate.pdf'; + $filecontents = $template->generate_pdf(false, $issue->userid, true); + + + $output[] = [ + 'issue' => [ + 'id' => $issue->id, + 'customcertid' => $issue->customcertid, + 'code' => $issue->code, + 'emailed' => $issue->emailed, + 'timecreated' => $issue->timecreated, + ], + 'user' => [ + 'id' => $issue->userid, + 'fullname' => $issue->fullname, + 'username' => $issue->username, + 'email' => $issue->email, + ], + 'template' => [ + 'id' => $issue->templateid, + 'name' => $issue->templatename, + 'contextid' => $issue->contextid, + ], + 'pdf' => [ + 'name' => $pdfname, + 'content' => base64_encode($filecontents), + ], + ]; + } + + } + + return $output; + } + + /** + * Returns the list_issues result value. + * + * @return external_multiple_structure + */ + public static function list_issues_returns() { + return new external_multiple_structure( + new external_single_structure([ + 'issue' => new external_single_structure([ + 'id' => new external_value(PARAM_INT, 'issue id'), + 'customcertid' => new external_value(PARAM_INT, 'customcert id'), + 'code' => new external_value(PARAM_TEXT, 'code'), + 'emailed' => new external_value(PARAM_BOOL, 'emailed'), + 'timecreated' => new external_value(PARAM_INT, 'time created'), + ]), + 'user' => new external_single_structure([ + 'id' => new external_value(PARAM_INT, 'id of user'), + 'fullname' => new external_value(PARAM_TEXT, 'fullname'), + 'username' => new external_value(PARAM_TEXT, 'username'), + 'email' => new external_value(PARAM_TEXT, 'email'), + ]), + 'template' => new external_single_structure([ + 'id' => new external_value(PARAM_INT, 'template id'), + 'name' => new external_value(PARAM_TEXT, 'template name'), + 'contextid' => new external_value(PARAM_INT, 'context id'), + ]), + 'pdf' => new external_single_structure([ + 'name' => new external_value(PARAM_TEXT, 'name'), + 'content' => new external_value(PARAM_TEXT, 'base64 content'), + ]), + ]) + ); + } } diff --git a/db/services.php b/db/services.php index 776bf9d1..218e3128 100644 --- a/db/services.php +++ b/db/services.php @@ -50,4 +50,12 @@ 'type' => 'read', 'ajax' => true, ], + 'mod_customcert_list_issues' => [ + 'classname' => 'mod_customcert\external', + 'methodname' => 'list_issues', + 'classpath' => '', + 'description' => 'List issued certificates', + 'type' => 'read', + 'ajax' => true, + ], ];