Skip to content

Commit

Permalink
Webservice: List issued certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
michallohnisky committed Oct 2, 2024
1 parent 7431bd6 commit b44d79a
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
126 changes: 126 additions & 0 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]),
])
);
}
}
8 changes: 8 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];

0 comments on commit b44d79a

Please sign in to comment.