Skip to content

Commit b44d79a

Browse files
Webservice: List issued certificates
1 parent 7431bd6 commit b44d79a

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

classes/external.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,130 @@ public static function delete_issue($certificateid, $issueid) {
230230
public static function delete_issue_returns() {
231231
return new external_value(PARAM_BOOL, 'True if successful, false otherwise');
232232
}
233+
234+
/**
235+
* Returns list_issues parameters.
236+
*
237+
* @return external_function_parameters
238+
*/
239+
public static function list_issues_parameters() {
240+
return new external_function_parameters(
241+
[
242+
'timecreatedfrom' => new external_value(PARAM_INT, 'Timestamp. Returns items created after this date (included).', VALUE_OPTIONAL),
243+
]
244+
);
245+
}
246+
247+
/**
248+
* Returns array of issued certificates.
249+
*
250+
* @param ?int $timecreatedfrom Timestamp. Returns items created after this date (included).
251+
* @return array
252+
*/
253+
public static function list_issues($timecreatedfrom = null) {
254+
global $DB;
255+
256+
$params = [
257+
'timecreatedfrom' => $timecreatedfrom,
258+
];
259+
self::validate_parameters(self::list_issues_parameters(), $params);
260+
261+
262+
$output = [];
263+
264+
list($namefields, $nameparams) = \core_user\fields::get_sql_fullname();
265+
$sql = "SELECT ci.*, $namefields as fullname, u.username, u.email, ct.id as templateid, ct.name as templatename, ct.contextid
266+
FROM {customcert_issues} ci
267+
JOIN {user} u
268+
ON ci.userid = u.id
269+
JOIN {customcert} c
270+
ON ci.customcertid = c.id
271+
JOIN {customcert_templates} ct
272+
ON c.templateid = ct.id";
273+
if ($timecreatedfrom) {
274+
$sql .= " WHERE ci.timecreated >= :timecreatedfrom";
275+
$nameparams['timecreatedfrom'] = $timecreatedfrom;
276+
}
277+
278+
if ($issues = $DB->get_records_sql($sql, $nameparams)) {
279+
280+
foreach ($issues as $issue) {
281+
282+
// Generate PDF
283+
284+
$template = new \stdClass();
285+
$template->id = $issue->templateid;
286+
$template->name = $issue->templatename;
287+
$template->contextid = $issue->contextid;
288+
$template = new \mod_customcert\template($template);
289+
290+
$ctname = str_replace(' ', '_', mb_strtolower($template->get_name()));
291+
$pdfname = $ctname . '_' . 'certificate.pdf';
292+
$filecontents = $template->generate_pdf(false, $issue->userid, true);
293+
294+
295+
$output[] = [
296+
'issue' => [
297+
'id' => $issue->id,
298+
'customcertid' => $issue->customcertid,
299+
'code' => $issue->code,
300+
'emailed' => $issue->emailed,
301+
'timecreated' => $issue->timecreated,
302+
],
303+
'user' => [
304+
'id' => $issue->userid,
305+
'fullname' => $issue->fullname,
306+
'username' => $issue->username,
307+
'email' => $issue->email,
308+
],
309+
'template' => [
310+
'id' => $issue->templateid,
311+
'name' => $issue->templatename,
312+
'contextid' => $issue->contextid,
313+
],
314+
'pdf' => [
315+
'name' => $pdfname,
316+
'content' => base64_encode($filecontents),
317+
],
318+
];
319+
}
320+
321+
}
322+
323+
return $output;
324+
}
325+
326+
/**
327+
* Returns the list_issues result value.
328+
*
329+
* @return external_multiple_structure
330+
*/
331+
public static function list_issues_returns() {
332+
return new external_multiple_structure(
333+
new external_single_structure([
334+
'issue' => new external_single_structure([
335+
'id' => new external_value(PARAM_INT, 'issue id'),
336+
'customcertid' => new external_value(PARAM_INT, 'customcert id'),
337+
'code' => new external_value(PARAM_TEXT, 'code'),
338+
'emailed' => new external_value(PARAM_BOOL, 'emailed'),
339+
'timecreated' => new external_value(PARAM_INT, 'time created'),
340+
]),
341+
'user' => new external_single_structure([
342+
'id' => new external_value(PARAM_INT, 'id of user'),
343+
'fullname' => new external_value(PARAM_TEXT, 'fullname'),
344+
'username' => new external_value(PARAM_TEXT, 'username'),
345+
'email' => new external_value(PARAM_TEXT, 'email'),
346+
]),
347+
'template' => new external_single_structure([
348+
'id' => new external_value(PARAM_INT, 'template id'),
349+
'name' => new external_value(PARAM_TEXT, 'template name'),
350+
'contextid' => new external_value(PARAM_INT, 'context id'),
351+
]),
352+
'pdf' => new external_single_structure([
353+
'name' => new external_value(PARAM_TEXT, 'name'),
354+
'content' => new external_value(PARAM_TEXT, 'base64 content'),
355+
]),
356+
])
357+
);
358+
}
233359
}

db/services.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,12 @@
5050
'type' => 'read',
5151
'ajax' => true,
5252
],
53+
'mod_customcert_list_issues' => [
54+
'classname' => 'mod_customcert\external',
55+
'methodname' => 'list_issues',
56+
'classpath' => '',
57+
'description' => 'List issued certificates',
58+
'type' => 'read',
59+
'ajax' => true,
60+
],
5361
];

0 commit comments

Comments
 (0)