@@ -230,4 +230,130 @@ public static function delete_issue($certificateid, $issueid) {
230
230
public static function delete_issue_returns () {
231
231
return new external_value (PARAM_BOOL , 'True if successful, false otherwise ' );
232
232
}
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
+ }
233
359
}
0 commit comments