-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
executable file
·1847 lines (1641 loc) · 62.8 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of the BadgeCerts plugin for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains classes, functions and constants used in 'local_badgecerts' plugin.
*
* @package local_badgecerts
* @copyright 2014 onwards Gregor Anželj, Andraž Prinčič
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Andraž Prinčič <[email protected]>, Gregor Anželj <[email protected]>
*/
defined('MOODLE_INTERNAL') || die();
use local_badgecerts\utils;
/*
* Number of records per page.
*/
define('CERT_PERPAGE', 50);
/*
* Inactive badge certificate means that this badge certificate cannot be used and
* has not been used yet. Its elements can be changed.
*/
define('CERT_STATUS_INACTIVE', 0);
/*
* Active badge certificate means that this badge certificate can be used, but it
* has not been used yet. Can be deactivated for the purpose of changing its elements.
*/
define('CERT_STATUS_ACTIVE', 1);
/*
* Inactive badge certificate can no longer be used, but it has been used in the past
* and therefore its elements cannot be changed.
*/
define('CERT_STATUS_INACTIVE_LOCKED', 2);
/*
* Active badge certificate means that it can be used and has already been used by
* users. Its elements cannot be changed any more.
*/
define('CERT_STATUS_ACTIVE_LOCKED', 3);
/*
* Badge certificate type for site badge certificates.
*/
define('CERT_TYPE_SITE', 1);
/*
* Badge certificate type for course badge certificates.
*/
define('CERT_TYPE_COURSE', 2);
/**
* Class that represents badge certificate.
*
*/
class badge_certificate {
/**
* Instance id.
*
* @var integer
*/
public $id;
/**
* Instance name.
*
* @var string
*/
public $name;
/**
* Description.
*
* @var string
*/
public $description;
/**
* SVG template.
*
* @var string
*/
public $certbgimage;
/**
* Booking ID instance.
*
* @var integer
*/
public $bookingid;
/**
* It's official?
*
* @var bool
*/
public $official;
/**
* Created.
*
* @var timestamp
*/
public $timecreated;
/**
* Last time that was modified.
*
* @var timestamp
*/
public $timemodified;
/**
* User, that created this instance.
*
* @var int
*/
public $usercreated;
/**
* User, that last modified this instance.
*
* @var int
*/
public $usermodified;
/**
* Who issued this certificate.
*
* @var string
*/
public $issuername;
/**
* Contact of issuer.
*
* @var string
*/
public $issuercontact;
/**
* Format.
*
* @var string
*/
public $format;
/**
* Orientatioin.
*
* @var string
*/
public $orientation;
/**
* PDF unit.
*
* @var string
*/
public $unit;
/**
* Type.
*
* @var string
*/
public $type;
/**
* Course id.
*
* @var integer
*/
public $courseid;
/**
* Status.
*
* @var integer
*/
public $status = 0;
/**
* Next time cron need to run.
*
* @var timestamp
*/
public $nextcron;
/**
* Type.
*
* @var integer
*/
public $certtype;
/**
* Quizgrading instance.
*
* @var integer
*/
public $quizgradingid;
/**
* Show qr code.
*
* @var bool
*/
public $qrshow;
/**
* QR code position x.
*
* @var integer
*/
public $qrx;
/**
* QR code position y.
*
* @var integer
*/
public $qry;
/**
* QR code width.
*
* @var int
*/
public $qrw;
/**
* QR code height.
*
* @var int
*/
public $qrh;
/**
* QR code value.
*
* @var string
*/
public $qrdata;
/**
* Start date.
*
* @var timestamp
*/
public $startdate;
/**
* End date.
*
* @var timestamp.
*/
public $enddate;
/**
* Certificate instance - Badge ID.
*
* @var int
*/
public $certid;
/**
* Booking option filters.
*
* @var bool
*/
public $enablebookingoptions;
/**
* Include or exclude.
*
* @var bool
*/
public $optionsincexc;
/**
* Booking options to exlude.
*
* @var string
*/
public $bookingoptions;
/** @var array Badge certificate elements */
public $elements = array();
/**
* Constructs with badge certificate details.
*
* @param int $certid badge certificate ID.
*/
public function __construct($certid) {
global $DB, $CFG;
$this->id = $certid;
$data = $DB->get_record('local_badgecerts', array('id' => $certid));
if (empty($data)) {
print_error('error:nosuchbadgecertificate', 'badges', $certid);
}
foreach ((array) $data as $field => $value) {
if (property_exists($this, $field)) {
if ($field == "certbgimage") {
$this->{$field} = $CFG->dataroot . $value;
} else {
$this->{$field} = $value;
}
}
}
}
/**
* Migrate certificate to tool_certificate.
*/
public function migrate($toolcertificateid) {
global $DB, $USER;
$sqlwhere = '';
$sqlvalues = ['certid' => $this->id];
switch ($this->certtype) {
case 1:
case 4:
// Mod_booking users.
if ($this->bookingid > 0) {
if ($this->enablebookingoptions) {
$yesno = ($this->optionsincexc == 1 ? '' : 'NOT');
$exsql = " AND ans.optionid {$yesno} IN ({$this->bookingoptions})";
} else {
$exsql = '';
}
if ($this->startdate != 0) {
$sqlwhere .= " AND (SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{booking_answers} ans
LEFT JOIN
{booking_options} bo ON bo.id = ans.optionid
WHERE
ans.bookingid = (SELECT
instance
FROM
{course_modules} cm
WHERE
cm.id = c.bookingid)
AND ans.userid = u.id AND ans.completed = 1 AND " .
"bo.coursestarttime >= c.startdate AND bo.courseendtime <= c.enddate {$exsql}) = 1";
} else {
$sqlwhere .= " AND (SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{booking_answers} ans
WHERE
bookingid = (SELECT
instance
FROM
{course_modules} cm
WHERE
cm.id = c.bookingid)
AND ans.userid = u.id AND ans.completed = 1 {$exsql}) = 1";
}
}
break;
case 2:
// Mod_booking teachers.
if ($this->bookingid > 0) {
if ($this->enablebookingoptions) {
$yesno = ($this->optionsincexc == 1 ? '' : 'NOT');
$exsql = " AND tch.optionid {$yesno} IN ({$this->bookingoptions})";
} else {
$exsql = '';
}
if ($this->startdate != 0) {
$sqlwhere .= " AND (SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{booking_teachers} tch
LEFT JOIN
{booking_options} bo ON bo.id = tch.optionid
WHERE
tch.bookingid = (SELECT
instance
FROM
{course_modules} cm
WHERE
cm.id = c.bookingid)
AND tch.userid = u.id AND tch.completed = 1 AND
bo.coursestarttime >= c.startdate AND
bo.courseendtime <= c.enddate {$exsql}) = 1 ";
} else {
$sqlwhere .= " AND (SELECT
CASE WHEN c.bookingid > 0 THEN
(SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{booking_teachers} tch
WHERE
bookingid = (SELECT
instance
FROM
{course_modules} cm
WHERE
cm.id = c.bookingid)
AND tch.userid = u.id AND tch.completed = 1 {$exsql})ELSE 0 END ) = 1 ";
}
}
break;
case 3:
// Mod_quizgrading.
if ($this->quizgradingid > 0) {
$sqlwhere .= " AND c.quizgradingid = :quizgradingid ";
$sqlvalues['quizgradingid'] = $this->quizgradingid;
}
break;
default:
break;
}
$fields = 'DISTINCT u.id, u.username, d.dateissued, d.uniquehash, '
. '(SELECT COUNT(*) nctransfers FROM {local_badgecerts_trasnfers} bcf WHERE bcf.userid = u.id AND '
. 'bcf.badgecertificateid = c.id AND bcf.transfereruserid = u.id) nctransfers,'
. '(SELECT created ndatelasttransfer FROM {local_badgecerts_trasnfers} bcf WHERE bcf.userid = '
. 'u.id AND bcf.badgecertificateid = c.id AND bcf.transfereruserid = u.id ORDER BY created DESC LIMIT 1) ndatelasttransfer';
$from = ' {badge_issued} d JOIN {badge} b ON d.badgeid = b.id JOIN {user} u ON d.userid = u.id JOIN '
. '{local_badgecerts} c ON b.id = c.certid ';
$where = ' c.id = :certid ' . $sqlwhere;
$r = $DB->get_records_sql(
"SELECT {$fields} FROM {$from} WHERE {$where}",
$sqlvalues
);
if ($this->bookingid > 0) {
$coursemodule = get_coursemodule_from_id('booking', $this->bookingid);
$bookingid = $coursemodule->instance;
}
$badges = [];
foreach ($r as $value) {
$user = new stdClass();
$user->userid = $value->id;
$user->hash = $value->uniquehash;
$badges[$value->id] = $user;
}
foreach ($badges as $badge) {
list($cert, $badge, $booking, $user) = get_badge_data($this, $badge);
if ($this->bookingid > 0 && in_array($this->certtype, array(1))) {
$optionids = booking_getbookingoptionsid($bookingid, $badge->userid, $this);
foreach ($optionids as $optionid) {
if (isset($optionid) && $optionid > 0) {
$data = booking_getbookingoptions($coursemodule->id, $optionid);
$template = \tool_certificate\template::instance($toolcertificateid);
try {
$cid = $template->issue_certificate(
$badge->userid,
null,
$data,
'mod_booking',
$coursemodule->instance
);
$DB->execute("UPDATE {booking_answers} SET certificateid = :cid WHERE optionid = :optionid AND userid = :userid", ['cid' => $cid, 'optionid' => $optionid, 'userid' => $badge->userid]);
$event = \local_badgecerts\event\created_event::create(
array('objectid' => $this->id, 'context' => $this->get_context(), 'relateduserid' => $badge->userid)
);
$event->trigger();
} catch (\Throwable $th) {
mtrace("Not issued: {$th->getMessage()}");
$event = \local_badgecerts\event\error_event::create(
array('objectid' => $this->id, 'context' => $this->get_context(), 'relateduserid' => $badge->userid)
);
$event->trigger();
}
}
}
} else {
$event = \local_badgecerts\event\not_suported::create(
array('objectid' => $this->id, 'context' => $this->get_context(), 'relateduserid' => $badge->userid)
);
$event->trigger();
}
}
// Your logic here.
}
/**
* Use to get context instance of a badge certificate.
* @return context instance.
*/
public function get_context() {
if ($this->type == CERT_TYPE_SITE) {
return context_system::instance();
} else if ($this->type == CERT_TYPE_COURSE) {
return context_course::instance($this->courseid);
} else {
debugging('Something is wrong...');
}
}
/**
* Save/update badge certificate information in 'local_badgecerts'
* table only. Cannot be used for updating badge certificate elements.
*
* @return bool Returns true on success.
*/
public function save() {
global $DB;
$DB->set_debug(true);
$fordb = new stdClass();
foreach (get_object_vars($this) as $k => $v) {
$fordb->{$k} = $v;
}
unset($fordb->elements);
$fordb->timemodified = time();
if ($DB->update_record_raw('local_badgecerts', $fordb)) {
return true;
} else {
throw new moodle_exception('error:save', 'badges');
return false;
}
}
/**
* Checks if badge certificate is active.
* Used in badge certificate.
*
* @return bool A status indicating badge certificate is active
*/
public function is_active() {
if (($this->status == CERT_STATUS_ACTIVE) ||
($this->status == CERT_STATUS_ACTIVE_LOCKED)
) {
return true;
}
return false;
}
/**
* Use to get the name of badge certificate status.
*
*/
public function get_status_name() {
return get_string('badgecertificatestatus_' . $this->status, 'local_badgecerts');
}
/**
* Use to set badge certificate status.
* Only active badge certificates can be used.
*
* @param int $status Status from CERT_STATUS constants
*/
public function set_status($status = 0) {
$this->status = $status;
unset($this->certbgimage);
$this->save();
}
/**
* Checks if badge certificate is locked.
* Used in badge certificate editing.
*
* @return bool A status indicating badge certificate is locked
*/
public function is_locked() {
if (($this->status == CERT_STATUS_ACTIVE_LOCKED) ||
($this->status == CERT_STATUS_INACTIVE_LOCKED)
) {
return true;
}
return false;
}
/**
* Fully deletes the badge certificate.
*/
public function delete() {
global $DB;
$fs = get_file_storage();
// Delete badge certificate images.
$certcontext = $this->get_context();
$fs->delete_area_files($certcontext->id, 'certificates', 'certbgimage', $this->id);
// Finally, remove badge certificate itself.
$DB->delete_records('local_badgecerts', array('id' => $this->id));
}
/**
* Generates badge certificate preview in PDF format.
*/
public function preview_badge_certificate() {
global $CFG;
require_once($CFG->libdir . '/tcpdf/tcpdf.php');
$pdf = new TCPDF($this->orientation, $this->unit, $this->format, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
// Remove default header/footer.
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// Set default monospaced font.
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// Override default margins.
$pdf->SetMargins(0, 0, 0, true);
// Set auto page breaks.
$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
// Set image scale factor.
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// Set default font subsetting mode.
$pdf->setFontSubsetting(false);
// Add badge certificate background image.
if ($this->certbgimage) {
// Add a page
// This method has several options, check the source code documentation for more information.
$nr_desc_lines = 10;
$pdf->AddPage();
// Get the current page break margin.
$breakmargin = $pdf->getBreakMargin();
// Get current auto-page-break mode.
$autopagebreak = $pdf->getAutoPageBreak();
// Disable auto-page-break.
$pdf->SetAutoPageBreak(false, 0);
$template = file_get_contents($this->certbgimage);
// Replace all placeholder tags.
$now = time();
$placeholders = array(
'[[recipient-fname]]', // Adds the recipient's first name.
'[[recipient-lname]]', // Adds the recipient's last name.
'[[recipient-flname]]', // Adds the recipient's full name (first, last).
'[[recipient-lfname]]', // Adds the recipient's full name (last, first).
'[[recipient-email]]', // Adds the recipient's email address.
'[[issuer-name]]', // Adds the issuer's name or title.
'[[issuer-contact]]', // Adds the issuer's contact information.
'[[badge-name]]', // Adds the badge's name or title.
'[[badge-desc]]', // Adds the badge's description.
'[[badge-number]]', // Adds the badge's ID number.
'[[badge-course]]', // Adds the name of the course where badge was awarded.
'[[badge-hash]]', // Adds the badge hash value.
'[[datetime-Y]]', // Adds the year.
'[[datetime-d.m.Y]]', // Adds the date in dd.mm.yyyy format.
'[[datetime-d/m/Y]]', // Adds the date in dd/mm/yyyy format.
'[[datetime-F]]', // Adds the date (used in DB datestamps).
'[[datetime-s]]', // Adds Unix Epoch Time timestamp.
'[[booking-name]]', // Adds the seminar instance name.
'[[booking-title]]', // Adds the seminar title.
'[[booking-startdate]]', // Adds the seminar start date.
'[[booking-enddate]]', // Adds the seminar end date.
'[[booking-duration]]', // Adds the seminar duration.
'[[recipient-birthdate]]', // Adds the recipient's date of birth.
'[[recipient-institution]]', // Adds the institution where the recipient is employed.
'[[badge-date-issued]]', // Adds the date when badge was issued.
);
for ($i = 1; $i <= $nr_desc_lines; $i++) {
$placeholders[] = "[[badge-desc-$i]]";
}
$values = array(
get_string('preview:recipientfname', 'local_badgecerts'),
get_string('preview:recipientlname', 'local_badgecerts'),
get_string('preview:recipientflname', 'local_badgecerts'),
get_string('preview:recipientlfname', 'local_badgecerts'),
get_string('preview:recipientemail', 'local_badgecerts'),
get_string('preview:issuername', 'local_badgecerts'),
get_string('preview:issuercontact', 'local_badgecerts'),
get_string('preview:badgename', 'local_badgecerts'),
get_string('preview:badgedesc', 'local_badgecerts'),
$this->id,
get_string('preview:badgecourse', 'local_badgecerts'),
sha1(rand() . $this->usercreated . $this->id . $now),
strftime('%Y', $now),
userdate($now, get_string('datetimeformat', 'local_badgecerts')),
userdate($now, get_string('datetimeformat', 'local_badgecerts')),
strftime('%F', $now),
strftime('%s', $now),
get_string('preview:bookinginstancename', 'local_badgecerts'),
get_string('preview:seminartitle', 'local_badgecerts'),
userdate(strtotime('- 2 month', $now), get_string('datetimeformat', 'local_badgecerts')),
userdate(strtotime('- 1 month', $now), get_string('datetimeformat', 'local_badgecerts')),
get_string('preview:seminarduration', 'local_badgecerts'),
userdate(strtotime('1 January 1970'), get_string('datetimeformat', 'local_badgecerts')),
get_string('preview:recipientinstitution', 'local_badgecerts'),
userdate($now, get_string('datetimeformat', 'local_badgecerts')),
);
$desc_lines = array();
for ($i = 1; $i <= $nr_desc_lines; $i++) {
$desc_linies[] = '';
}
$desc_lines_input = explode(PHP_EOL, get_string('preview:badgedesc', 'local_badgecerts'));
for ($i = 0; $i < count($desc_lines_input); $i++) {
$desc_lines[$i] = $desc_lines_input[$i];
}
$values = array_merge($values, $desc_lines);
$template = str_replace($placeholders, $values, $template);
$pdf->ImageSVG('@' . $template, 0, 0, 0, 0, '', '', '', 0, true);
// Restore auto-page-break status.
$pdf->SetAutoPageBreak($autopagebreak, $breakmargin);
// Set the starting point for the page content.
$pdf->setPageMark();
}
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('preview_' . $this->name . '.pdf', 'I');
}
}
/**
* Insert to log table when user transfer certificate.
*
* @param int $certid Certificate id
* @param int $userid User id
*/
function local_badgecerts_insert_to_log($certid = null, $userid = null) {
global $DB, $USER;
if (!$certid && !$userid) {
return false;
}
$timelog = new stdClass();
$timelog->badgecertificateid = $certid;
$timelog->transfereruserid = $USER->id;
$timelog->userid = $userid;
$timelog->created = time();
$DB->insert_record('local_badgecerts_trasnfers', $timelog);
}
/**
* Get all badge certificates.
*
* @param int $type Type of badges to return
* @param int $courseid Course ID for course badges
* @param string $sort An SQL field to sort by
* @param string $dir The sort direction ASC|DESC
* @param int $page The page or records to return
* @param int $perpage The number of records to return per page
* @param int $user User specific search
*
* @return array $badge Array of records matching criteria
*/
function badges_get_certificates($type, $courseid = 0, $sort = '', $dir = '', $page = 0, $perpage = CERT_PERPAGE, $user = 0) {
global $DB;
$records = array();
$params = array();
$params['type'] = $type;
$where = "bc.type = :type";
$userfields = array('bc.id, bc.name, bc.status');
$usersql = "";
$fields = implode(', ', $userfields);
if ($courseid != 0) {
$where .= "AND bc.courseid = :courseid ";
$params['courseid'] = $courseid;
}
$sorting = (($sort != '' && $dir != '') ? 'ORDER BY ' . $sort . ' ' . $dir : '');
$sql = "SELECT $fields FROM {local_badgecerts} bc $usersql WHERE $where $sorting";
$records = $DB->get_records_sql($sql, $params, intval($page) * intval($perpage), $perpage);
$certs = array();
foreach ($records as $r) {
$cert = new badge_certificate($r->id);
$certs[$r->id] = $cert;
$certs[$r->id]->statstring = $cert->get_status_name();
}
return $certs;
}
/**
* Get all badge certificates for courseid.
* TO-DO: Odstrani, ker se ne potrebuje več!
*
* @param int $courseid Course ID for course badges
*
* @return array $badge Array of records matching criteria
*/
function badges_get_certificates_for_courseid($courseid = 0) {
global $DB;
$records = array();
$params = array();
$where = "bc.courseid = :courseid";
$params['courseid'] = $courseid;
$userfields = array('bc.id, bc.name, bc.status');
$usersql = "";
$fields = implode(', ', $userfields);
$sql = "SELECT $fields FROM {local_badgecerts} bc $usersql WHERE $where";
$records = $DB->get_records_sql($sql, $params);
$certs = array();
foreach ($records as $r) {
$cert = new badge_certificate($r->id);
$certs[$r->id] = $cert;
$certs[$r->id]->statstring = $cert->get_status_name();
}
return $certs;
}
/**
* Returns array of assigned badges that badge certificates
* are already assigned to.
*
* @param int $courseid course ID.
* @param int $certid Certificate instance.
*
* @return array Array containing all the assigned badges
*/
function get_assigned_badge_options($courseid, $certid) {
global $DB;
$records = array();
$params = array();
$where = ' b.courseid = :courseid AND (b.status = 1 OR b.status = 3) AND b.id = :certid ';
$params['courseid'] = $courseid;
$params['certid'] = $certid;
$userfields = array('b.id, b.name');
$usersql = '';
$fields = implode(', ', $userfields);
$sorting = 'ORDER BY b.name ASC ';
$sql = "SELECT $fields FROM {badge} b $usersql WHERE $where $sorting";
$records = $DB->get_records_sql($sql, $params);
$options = array();
foreach ($records as $record) {
$options[$record->id] = $record->name;
}
return $options;
}
/**
* Return true if is connected to booking.
*
* @param int $certid ID of badge
*
* @return bool return true, if is connected to booking
*/
function has_booking($certid) {
global $DB;
$bcert = $DB->get_record('local_badgecerts', array('id' => $certid));
if ($bcert->bookingid > 0) {
return true;
} else {
return false;
}
}
/**
* Get badge certificates for a specific user.
*
* @param int $userid User ID
* @param int $courseid Badge certs earned by a user in a specific course
* @param int $page The page or records to return
* @param int $perpage The number of records to return per page
* @param string $search A simple string to search for
* @param bool $onlypublic Return only public badges
* @return array of certs ordered by decreasing date of issue
*/
function badges_get_user_certificates($userid, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) {
global $DB;
$certs = array();
$quizgrading = '';
$booking = '';
if (utils::check_mod_quizgrading()) {
$quizgrading = 'OR (SELECT CASE WHEN bc.quizgradingid > 0 AND bc.certtype = 3 THEN
(SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{quizgrading_results} AS qr
WHERE
qr.userid = u.id)
ELSE 0 END) = 1';
}
if (utils::check_mod_booking()) {
$booking = 'OR (SELECT
CASE WHEN bc.bookingid > 0 AND bc.certtype = 1 THEN
(SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{booking_answers} ans
LEFT JOIN
{booking_options} bo ON ans.optionid = bo.id
WHERE
ans.bookingid = (SELECT
instance
FROM
{course_modules} AS cm
WHERE
cm.id = bc.bookingid)
AND ans.userid = u.id AND ans.completed = 1 AND CASE WHEN bc.startdate != 0
THEN bo.coursestarttime >= bc.startdate AND bo.courseendtime <= bc.enddate ELSE 1 = 1 END
AND CASE
WHEN bc.enablebookingoptions = 1 AND bc.optionsincexc = 1 THEN find_in_set(ans.optionid, bc.bookingoptions)
WHEN bc.enablebookingoptions = 1 AND bc.optionsincexc = 0 THEN NOT find_in_set(ans.optionid, bc.bookingoptions)
ELSE 1 = 1
END
) ELSE
0 END = 1
OR (SELECT
CASE WHEN bc.bookingid > 0 AND bc.certtype = 4 THEN
(SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{booking_answers} AS ans
LEFT JOIN
{booking_options} bo ON ans.optionid = bo.id
WHERE
ans.bookingid = (SELECT
instance
FROM
{course_modules} AS cm
WHERE
cm.id = bc.bookingid)
AND ans.userid = u.id AND ans.completed = 1 AND CASE WHEN bc.startdate != 0
THEN bo.coursestarttime >= bc.startdate AND bo.courseendtime <= bc.enddate ELSE 1 = 1 END) ELSE
0 END) = 1
OR (SELECT
CASE WHEN bc.bookingid > 0 AND bc.certtype = 2 THEN
(SELECT
CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM
{booking_teachers} tch
LEFT JOIN
{booking_options} bo ON tch.optionid = bo.id
WHERE
tch.bookingid = (SELECT
instance
FROM
{course_modules} AS cm
WHERE
cm.id = bc.bookingid)
AND tch.userid = u.id AND tch.completed = 1 AND
CASE WHEN bc.startdate != 0 THEN bo.coursestarttime >= bc.startdate
AND bo.courseendtime <= bc.enddate ELSE 1 = 1 END) ELSE
0 END
= 1))';
}
$sql = "SELECT
bc.id,
bc.name,
b.courseid,
b.id badgeid,
bi.uniquehash,
b.type,
bc.bookingid,
bc.certtype
FROM {local_badgecerts} bc
LEFT JOIN {badge_issued} bi
ON bi.badgeid = bc.certid
LEFT JOIN {user} u
ON u.id = bi.userid
LEFT JOIN {badge} b
ON b.id = bi.badgeid
WHERE bi.userid = ?
AND bc.certid IS NOT null
AND bc.status >= 1 AND (
((SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END) = 1 AND bc.certtype = 0)
{$quizgrading}
{$booking}
)";
$params[] = $userid;
if (!empty($search)) {
$sql .= ' AND (' . $DB->sql_like('b.name', '?', false) . ') ';
$params[] = "%$search%";