@@ -230,4 +230,163 @@ 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 (
243
+ PARAM_INT ,
244
+ 'Timestamp. Returns items created after this date (included). ' ,
245
+ VALUE_OPTIONAL
246
+ ),
247
+ 'userid ' => new external_value (
248
+ PARAM_INT ,
249
+ 'User id. Returns items for this user. ' ,
250
+ VALUE_OPTIONAL
251
+ ),
252
+ 'customcertid ' => new external_value (
253
+ PARAM_INT ,
254
+ 'Customcert id. Returns items for this customcert. ' ,
255
+ VALUE_OPTIONAL
256
+ ),
257
+ ]
258
+ );
259
+ }
260
+
261
+ /**
262
+ * Returns array of issued certificates.
263
+ *
264
+ * @param ?int $timecreatedfrom Timestamp. Returns items created after this date (included).
265
+ * @param ?int $userid User id. Returns items for this user.
266
+ * @param ?int $customcertid Customcert id. Returns items for this customcert.
267
+ * @return array
268
+ */
269
+ public static function list_issues ($ timecreatedfrom = null , $ userid = null , $ customcertid = null ) {
270
+ global $ DB ;
271
+
272
+ $ params = [
273
+ 'timecreatedfrom ' => $ timecreatedfrom ,
274
+ 'userid ' => $ userid ,
275
+ 'customcertid ' => $ customcertid ,
276
+ ];
277
+ self ::validate_parameters (self ::list_issues_parameters (), $ params );
278
+
279
+ $ context = \context_system::instance ();
280
+ require_capability ('mod/customcert:viewallcertificates ' , $ context );
281
+
282
+ $ output = [];
283
+
284
+ list ($ namefields , $ nameparams ) = \core_user \fields::get_sql_fullname ();
285
+ $ sql = "SELECT ci.*,
286
+ $ namefields as fullname, u.username, u.email,
287
+ ct.id as templateid, ct.name as templatename, ct.contextid
288
+ FROM {customcert_issues} ci
289
+ JOIN {user} u
290
+ ON ci.userid = u.id
291
+ JOIN {customcert} c
292
+ ON ci.customcertid = c.id
293
+ JOIN {customcert_templates} ct
294
+ ON c.templateid = ct.id " ;
295
+ $ conditions = [];
296
+ if ($ timecreatedfrom ) {
297
+ $ conditions [] = "ci.timecreated >= :timecreatedfrom " ;
298
+ $ nameparams ['timecreatedfrom ' ] = $ timecreatedfrom ;
299
+ }
300
+ if ($ userid ) {
301
+ $ conditions [] = "ci.userid = :userid " ;
302
+ $ nameparams ['userid ' ] = $ userid ;
303
+ }
304
+ if ($ customcertid ) {
305
+ $ conditions [] = "ci.customcertid = :customcertid " ;
306
+ $ nameparams ['customcertid ' ] = $ customcertid ;
307
+ }
308
+ if ($ conditions ) {
309
+ $ sql .= " WHERE " . implode (" AND " , $ conditions );
310
+ }
311
+
312
+ if ($ issues = $ DB ->get_records_sql ($ sql , $ nameparams )) {
313
+
314
+ foreach ($ issues as $ issue ) {
315
+
316
+ // Generate PDF.
317
+
318
+ $ template = new \stdClass ();
319
+ $ template ->id = $ issue ->templateid ;
320
+ $ template ->name = $ issue ->templatename ;
321
+ $ template ->contextid = $ issue ->contextid ;
322
+ $ template = new \mod_customcert \template ($ template );
323
+
324
+ $ ctname = str_replace (' ' , '_ ' , mb_strtolower ($ template ->get_name ()));
325
+ $ pdfname = $ ctname . '_ ' . 'certificate.pdf ' ;
326
+ $ filecontents = $ template ->generate_pdf (false , $ issue ->userid , true );
327
+
328
+ $ output [] = [
329
+ 'issue ' => [
330
+ 'id ' => $ issue ->id ,
331
+ 'customcertid ' => $ issue ->customcertid ,
332
+ 'code ' => $ issue ->code ,
333
+ 'emailed ' => $ issue ->emailed ,
334
+ 'timecreated ' => $ issue ->timecreated ,
335
+ ],
336
+ 'user ' => [
337
+ 'id ' => $ issue ->userid ,
338
+ 'fullname ' => $ issue ->fullname ,
339
+ 'username ' => $ issue ->username ,
340
+ 'email ' => $ issue ->email ,
341
+ ],
342
+ 'template ' => [
343
+ 'id ' => $ issue ->templateid ,
344
+ 'name ' => $ issue ->templatename ,
345
+ 'contextid ' => $ issue ->contextid ,
346
+ ],
347
+ 'pdf ' => [
348
+ 'name ' => $ pdfname ,
349
+ 'content ' => base64_encode ($ filecontents ),
350
+ ],
351
+ ];
352
+ }
353
+
354
+ }
355
+
356
+ return $ output ;
357
+ }
358
+
359
+ /**
360
+ * Returns the list_issues result value.
361
+ *
362
+ * @return external_multiple_structure
363
+ */
364
+ public static function list_issues_returns () {
365
+ return new external_multiple_structure (
366
+ new external_single_structure ([
367
+ 'issue ' => new external_single_structure ([
368
+ 'id ' => new external_value (PARAM_INT , 'issue id ' ),
369
+ 'customcertid ' => new external_value (PARAM_INT , 'customcert id ' ),
370
+ 'code ' => new external_value (PARAM_TEXT , 'code ' ),
371
+ 'emailed ' => new external_value (PARAM_BOOL , 'emailed ' ),
372
+ 'timecreated ' => new external_value (PARAM_INT , 'time created ' ),
373
+ ]),
374
+ 'user ' => new external_single_structure ([
375
+ 'id ' => new external_value (PARAM_INT , 'id of user ' ),
376
+ 'fullname ' => new external_value (PARAM_TEXT , 'fullname ' ),
377
+ 'username ' => new external_value (PARAM_TEXT , 'username ' ),
378
+ 'email ' => new external_value (PARAM_TEXT , 'email ' ),
379
+ ]),
380
+ 'template ' => new external_single_structure ([
381
+ 'id ' => new external_value (PARAM_INT , 'template id ' ),
382
+ 'name ' => new external_value (PARAM_TEXT , 'template name ' ),
383
+ 'contextid ' => new external_value (PARAM_INT , 'context id ' ),
384
+ ]),
385
+ 'pdf ' => new external_single_structure ([
386
+ 'name ' => new external_value (PARAM_TEXT , 'name ' ),
387
+ 'content ' => new external_value (PARAM_TEXT , 'base64 content ' ),
388
+ ]),
389
+ ])
390
+ );
391
+ }
233
392
}
0 commit comments