forked from epicopensource/webinar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
3669 lines (3126 loc) · 119 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
require_once $CFG->libdir.'/gradelib.php';
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->dirroot.'/lib/adminlib.php';
/**
* Definitions for setting notification types
*/
/**
* Utility definitions
*/
define('WEBINAR_ICAL', 1);
define('WEBINAR_TEXT', 2);
define('WEBINAR_BOTH', 3);
define('WEBINAR_INVITE', 4);
define('WEBINAR_CANCEL', 8);
/**
* Definitions for use in forms
*/
define('WEBINAR_INVITE_BOTH', 7); // Send a copy of both 4+1+2
define('WEBINAR_INVITE_TEXT', 6); // Send just a plain email 4+2
define('WEBINAR_INVITE_ICAL', 5); // Send just a combined text/ical message 4+1
define('WEBINAR_CANCEL_BOTH', 11); // Send a copy of both 8+2+1
define('WEBINAR_CANCEL_TEXT', 10); // Send just a plan email 8+2
define('WEBINAR_CANCEL_ICAL', 9); // Send just a combined text/ical message 8+1
// Name of the custom field where the manager's email address is stored
define('WEBINAR_MANAGERSEMAIL_FIELD', 'managersemail');
// Custom field related constants
define('WEBINAR_CUSTOMFIELD_DELIMITTER', ';');
define('WEBINAR_CUSTOMFIELD_TYPE_TEXT', 0);
define('WEBINAR_CUSTOMFIELD_TYPE_SELECT', 1);
define('WEBINAR_CUSTOMFIELD_TYPE_MULTISELECT', 2);
// Calendar-related constants
define('WEBINAR_CALENDAR_MAX_NAME_LENGTH', 15);
// Signup status codes (remember to update $WEBINAR_STATUS)
define('WEBINAR_STATUS_USER_CANCELLED', 10);
define('WEBINAR_STATUS_SESSION_CANCELLED', 20);
define('WEBINAR_STATUS_DECLINED', 30);
define('WEBINAR_STATUS_REQUESTED', 40);
define('WEBINAR_STATUS_APPROVED', 50);
define('WEBINAR_STATUS_WAITLISTED', 60);
define('WEBINAR_STATUS_BOOKED', 70);
define('WEBINAR_STATUS_NO_SHOW', 80);
define('WEBINAR_STATUS_PARTIALLY_ATTENDED', 90);
define('WEBINAR_STATUS_FULLY_ATTENDED', 100);
// This array must match the status codes above, and the values
// must equal the end of the constant name but in lower case
$WEBINAR_STATUS = array(
WEBINAR_STATUS_USER_CANCELLED => 'user_cancelled',
WEBINAR_STATUS_SESSION_CANCELLED => 'session_cancelled',
WEBINAR_STATUS_DECLINED => 'declined',
WEBINAR_STATUS_REQUESTED => 'requested',
WEBINAR_STATUS_APPROVED => 'approved',
WEBINAR_STATUS_WAITLISTED => 'waitlisted',
WEBINAR_STATUS_BOOKED => 'booked',
WEBINAR_STATUS_NO_SHOW => 'no_show',
WEBINAR_STATUS_PARTIALLY_ATTENDED => 'partially_attended',
WEBINAR_STATUS_FULLY_ATTENDED => 'fully_attended',
);
/**
* Returns the human readable code for a webinar status
*
* @param int $statuscode One of the WEBINAR_STATUS* constants
* @return string Human readable code
*/
function webinar_get_status($statuscode) {
global $WEBINAR_STATUS;
// Check code exists
if (!isset($WEBINAR_STATUS[$statuscode])) {
error('WEBINAR status code does not exist: '.$statuscode);
}
// Get code
$string = $WEBINAR_STATUS[$statuscode];
// Check to make sure the status array looks to be up-to-date
if (constant('WEBINAR_STATUS_'.strtoupper($string)) != $statuscode) {
error('WEBINAR status code array does not appear to be up-to-date: '.$statuscode);
}
return $string;
}
/**
* Prints the cost amount along with the appropriate currency symbol.
*
* To set your currency symbol, set the appropriate 'locale' in
* lang/en_utf8/langconfig.php (or the equivalent file for your
* language).
*
* @param $amount Numerical amount without currency symbol
* @param $htmloutput Whether the output is in HTML or not
*/
function webinar_format_cost($amount, $htmloutput=true) {
setlocale(LC_MONETARY, get_string('locale'));
$localeinfo = localeconv();
$symbol = $localeinfo['currency_symbol'];
if (empty($symbol)) {
// Cannot get the locale information, default to en_US.UTF-8
return '£' . $amount;
}
// Character between the currency symbol and the amount
$separator = '';
if ($localeinfo['p_sep_by_space']) {
$separator = $htmloutput ? ' ' : ' ';
}
// The symbol can come before or after the amount
if ($localeinfo['p_cs_precedes']) {
return $symbol . $separator . $amount;
}
else {
return $amount . $separator . $symbol;
}
}
/**
* Returns the effective cost of a session depending on the presence
* or absence of a discount code.
*
* @param class $sessiondata contains the discountcost and normalcost
*/
function webinar_cost($userid, $sessionid, $sessiondata, $htmloutput=true) {
global $CFG, $DB;
if ($DB->count_records_sql("SELECT COUNT(*)
FROM {$CFG->prefix}webinar_signups su,
{$CFG->prefix}webinar_sessions se
WHERE su.sessionid=$sessionid
AND su.userid=$userid
AND su.sessionid = se.id") > 0) {
return webinar_format_cost($sessiondata->discountcost, $htmloutput);
} else {
return webinar_format_cost($sessiondata->normalcost, $htmloutput);
}
}
/**
* Human-readable version of the duration field used to display it to
* users
*
* @param integer $duration duration in hours
*/
function webinar_format_duration($duration) {
$components = explode(':', $duration);
if ($components and count($components) > 1) {
// e.g. "1:30" => "1 hour and 30 minutes"
$hours = $components[0];
$minutes = $components[1];
}
else {
// e.g. "1.5" => "1 hour and 30 minutes"
$minutes = round($duration - floor($duration) * 60);
$hours = floor($duration);
}
$string = '';
if (1 == $hours) {
$string = get_string('onehour', 'webinar');
} elseif ($hours > 1) {
$string = get_string('xhours', 'webinar', $hours);
}
// Insert separator between hours and minutes
if ($string != '') {
$string .= ' ';
}
if (1 == $minutes) {
$string .= get_string('oneminute', 'webinar');
} elseif ($minutes > 0) {
$string .= get_string('xminutes', 'webinar', $minutes);
}
return $string;
}
/**
* Converts minutes to hours
*/
function webinar_minutes_to_hours($minutes) {
if ($minutes > 0) {
$hours = floor($minutes / 60.0);
$mins = $minutes - ($hours * 60.0);
return "$hours:$mins";
}
else {
return $minutes;
}
}
/**
* Converts hours to minutes
*/
function webinar_hours_to_minutes($hours)
{
$components = explode(':', $hours);
if ($components and count($components) > 1) {
// e.g. "1:45" => 105 minutes
$hours = $components[0];
$minutes = $components[1];
return $hours * 60.0 + $minutes;
}
else {
// e.g. "1.75" => 105 minutes
return round($hours * 60.0);
}
}
/**
* Turn undefined manager messages into empty strings and deal with checkboxes
*/
function webinar_fix_settings($webinar) {
if (empty($webinar->emailmanagerconfirmation)) {
$webinar->confirmationinstrmngr = null;
}
if (empty($webinar->emailmanagerreminder)) {
$webinar->reminderinstrmngr = null;
}
if (empty($webinar->emailmanagercancellation)) {
$webinar->cancellationinstrmngr = null;
}
if (empty($webinar->thirdpartywaitlist)) {
$webinar->thirdpartywaitlist = 0;
}
if (empty($webinar->showoncalendar)) {
$webinar->showoncalendar = 0;
}
if (empty($webinar->approvalreqd)) {
$webinar->approvalreqd = 0;
}
}
/**
* Given an object containing all the necessary data, (defined by the
* form in mod.html) this function will create a new instance and
* return the id number of the new instance.
*/
function webinar_add_instance($webinar) {
global $DB;
$webinar->timemodified = time();
webinar_fix_settings($webinar);
if ($webinar->id = $DB->insert_record('webinar', $webinar)) {
webinar_grade_item_update($webinar);
}
return $webinar->id;
}
/**
* Given an object containing all the necessary data, (defined by the
* form in mod.html) this function will update an existing instance
* with new data.
*/
function webinar_update_instance($webinar) {
global $DB;
$webinar->id = $webinar->instance;
webinar_fix_settings($webinar);
if ($return = $DB->update_record('webinar', $webinar)) {
webinar_grade_item_update($webinar);
}
return $return;
}
/**
* Given an ID of an instance of this module, this function will
* permanently delete the instance and any data that depends on it.
*/
function webinar_delete_instance($id) {
global $CFG, $DB;
if (!$webinar = $DB->get_record('webinar', array('id' => $id))) {
return false;
}
$result = true;
// //begin_sql();
if (!$DB->delete_records_select(
'webinar_signups_status',
"signupid IN
(
SELECT
id
FROM
{$CFG->prefix}webinar_signups
WHERE
sessionid IN
(
SELECT
id
FROM
{$CFG->prefix}webinar_sessions
WHERE
webinar = {$webinar->id}
)
)
")) {
$result = false;
}
if (!$DB->delete_records_select('webinar_signups', "sessionid IN (SELECT id FROM {$CFG->prefix}webinar_sessions WHERE webinar = {$webinar->id})")) {
$result = false;
}
if (!$DB->delete_records_select('webinar_sessions_dates', "sessionid in (SELECT id FROM {$CFG->prefix}webinar_sessions WHERE webinar = $webinar->id)")) {
$result = false;
}
if (!$DB->delete_records('webinar_sessions', array('webinar' => $webinar->id))) {
$result = false;
}
if (!$DB->delete_records('webinar', array('id' => $webinar->id))) {
$result = false;
}
if (!$DB->delete_records('event', array('modulename' => 'webinar', 'instance' => $webinar->id))) {
$result = false;
}
if (!webinar_grade_item_delete($webinar)) {
$result = false;
}
if ($result) {
//commit_sql();
} else {
//rollback_sql();
}
return $result;
}
/**
* Prepare the user data to go into the database.
*/
function webinar_cleanup_session_data($session) {
// Only numbers allowed here
$session->capacity = preg_replace('/[^\d]/', '', $session->capacity);
$MAX_CAPACITY = 100000;
if ($session->capacity < 1) {
$session->capacity = 1;
}
elseif ($session->capacity > $MAX_CAPACITY) {
$session->capacity = $MAX_CAPACITY;
}
// Get the decimal point separator
//setlocale(LC_MONETARY, get_string('locale'));
$localeinfo = localeconv();
$symbol = $localeinfo['decimal_point'];
if (empty($symbol)) {
// Cannot get the locale information, default to en_US.UTF-8
$symbol = '.';
}
return $session;
}
/**
* Create a new entry in the webinar_sessions table
*/
function webinar_add_session($session, $sessiondates)
{
global $USER, $DB;
$session->timecreated = time();
$session = webinar_cleanup_session_data($session);
$eventname = $DB->get_field('webinar', 'name', array('id' => $session->webinar));
if ($session->id = $DB->insert_record('webinar_sessions', $session)) {
if (empty($sessiondates)) {
// Insert a dummy date record
$date = new object();
$date->sessionid = $session->id;
$date->timestart = 0;
$date->timefinish = 0;
if (!$DB->insert_record('webinar_sessions_dates', $date)) {
//rollback_sql();
return false;
}
}
else {
foreach ($sessiondates as $date) {
$date->sessionid = $session->id;
if (!$DB->insert_record('webinar_sessions_dates', $date)) {
//rollback_sql();
return false;
}
}
}
// Put the sessions in this user's calendar
// (i.e. we're assuming it's the teacher)
$session->sessiondates = $sessiondates;
webinar_add_session_to_user_calendar($session, $eventname, $USER->id, 'session');
return $session->id;
} else {
//rollback_sql();
return false;
}
}
/**
* Modify an entry in the webinar_sessions table
*/
function webinar_update_session($session, $sessiondates) {
global $DB;
$session->timemodified = time();
$session = webinar_cleanup_session_data($session);
if (!$DB->update_record('webinar_sessions', $session)) {
//rollback_sql();
return false;
}
if (!$DB->delete_records('webinar_sessions_dates', array('sessionid' => $session->id))) {
//rollback_sql();
return false;
}
if (empty($sessiondates)) {
// Insert a dummy date record
$date = new object();
$date->sessionid = $session->id;
$date->timestart = 0;
$date->timefinish = 0;
if (!$DB->insert_record('webinar_sessions_dates', $date)) {
//rollback_sql();
return false;
}
}
else {
foreach ($sessiondates as $date) {
$date->sessionid = $session->id;
if (!$DB->insert_record('webinar_sessions_dates', $date)) {
//rollback_sql();
return false;
}
}
}
// Update Calendar entries for students and teachers
$session->sessiondates = $sessiondates;
if (!webinar_update_calendar_events($session, 'booking')) {
//rollback_sql();
return false;
}
if (!webinar_update_calendar_events($session, 'session')) {
//rollback_sql();
return false;
}
return webinar_update_attendees($session);
}
/**
* Update attendee list status' on booking size change
*/
function webinar_update_attendees($session) {
global $USER, $DB;
// Get webinar
if (!$webinar = $DB->get_record('webinar', array('id' => $session->webinar))) {
error('Could not load webinar record');
}
// Get course
if (!$course = $DB->get_record('course', array('id' => $webinar->course))) {
error('Could not load course record');
}
// Update user status'
$users = webinar_get_attendees($session->id);
if ($users) {
// Convert earliest signed up users to booked, and make the rest waitlisted
$capacity = $session->capacity;
// Count number of booked users
$booked = 0;
foreach ($users as $user) {
if ($user->statuscode == WEBINAR_STATUS_BOOKED) {
$booked++;
}
}
// If booked less than capacity, book some new users
if ($booked < $capacity) {
foreach ($users as $user) {
if ($booked >= $capacity) {
break;
}
if ($user->statuscode == WEBINAR_STATUS_WAITLISTED) {
if (!webinar_user_signup($session, $webinar, $course, '', 0, WEBINAR_STATUS_BOOKED, $user->id)) {
//rollback_sql();
return false;
}
$booked++;
}
}
}
}
return $session->id;
}
/**
* Return an array of all webinar activities in the current course
*/
function webinar_get_webinar_menu() {
global $CFG;
if ($webinars = $DB->get_records_sql("SELECT f.id, c.shortname, f.name
FROM {$CFG->prefix}course c, {$CFG->prefix}webinar f
WHERE c.id = f.course
ORDER BY c.shortname, f.name")) {
$i=1;
foreach ($webinars as $webinar) {
$f = $webinar->id;
$webinarmenu[$f] = $webinar->shortname.' --- '.$webinar->name;
$i++;
}
return $webinarmenu;
} else {
return '';
}
}
/**
* Delete entry from the webinar_sessions table along with all
* related details in other tables
*
* @param object $session Record from webinar_sessions
*/
function webinar_delete_session($session)
{
global $CFG, $DB;
$webinar = $DB->get_record('webinar', array('id' => $session->webinar));
// Cancel user signups (and notify users)
$signedupusers = $DB->get_records_sql(
"
SELECT DISTINCT
userid
FROM
{$CFG->prefix}webinar_signups s
LEFT JOIN
{$CFG->prefix}webinar_signups_status ss
ON ss.signupid = s.id
WHERE
s.sessionid = $session->id
AND ss.superceded = 0
AND ss.statuscode >= ".WEBINAR_STATUS_REQUESTED."
"
);
if ($signedupusers and count($signedupusers) > 0) {
foreach ($signedupusers as $user) {
if (webinar_user_cancel($session, $user->userid, true)) {
webinar_send_cancellation_notice($webinar, $session, $user->userid);
}
else {
return false; // Cannot rollback since we notified users already
}
}
}
//begin_sql();
// Remove entries from the teacher calendars
if (!$DB->delete_records_select('event', "modulename = 'webinar' AND eventtype = 'webinarsession' AND instance = $webinar->id")) {
//rollback_sql();
return false;
}
// Remove entry from site-wide calendar
webinar_remove_session_from_site_calendar($session);
// Remove entry from site-wide calendar
webinar_remove_session_from_site_calendar($session);
// Delete session details
if (!$DB->delete_records('webinar_sessions', array('id' => $session->id))) {
//rollback_sql();
return false;
}
if (!$DB->delete_records('webinar_sessions_dates', array('sessionid' => $session->id))) {
//rollback_sql();
return false;
}
if (!$DB->delete_records_select(
'webinar_signups_status',
"signupid IN
(
SELECT
id
FROM
{$CFG->prefix}webinar_signups
WHERE
sessionid = {$session->id}
)
")) {
$result = false;
}
if (!$DB->delete_records('webinar_signups', array('sessionid' => $session->id))) {
//rollback_sql();
return false;
}
//commit_sql();
return true;
}
/**
* Subsitute the placeholders in email templates for the actual data
*/
function webinar_email_substitutions($msg, $webinarname, $reminderperiod, $user, $session, $sessionid)
{
global $CFG, $DB;
if (empty($msg)) {
return '';
}
// Scheduled session
$sessiondate = userdate($session->sessiondates->timestart, get_string('strftimedate'));
$starttime = userdate($session->sessiondates[0]->timestart, get_string('strftimetime'));
$finishtime = userdate($session->sessiondates[0]->timefinish, get_string('strftimetime'));
$alldates = '';
foreach ($session->sessiondates as $date) {
if ($alldates != '') {
$alldates .= "\n";
}
$alldates .= userdate($date->timestart, get_string('strftimedate')).', ';
$alldates .= userdate($date->timestart, get_string('strftimetime')).
' to '.userdate($date->timefinish, get_string('strftimetime'));
}
$msg = str_replace(get_string('placeholder:webinarname', 'webinar'), $webinarname,$msg);
$msg = str_replace(get_string('placeholder:firstname', 'webinar'), $user->firstname,$msg);
$msg = str_replace(get_string('placeholder:lastname', 'webinar'), $user->lastname,$msg);
$msg = str_replace(get_string('placeholder:cost', 'webinar'), webinar_cost($user->id, $sessionid, $session, false),$msg);
$msg = str_replace(get_string('placeholder:alldates', 'webinar'), $alldates,$msg);
/*$msg = str_replace(get_string('placeholder:sessiondate', 'webinar'), $sessiondate,$msg);
$msg = str_replace(get_string('placeholder:starttime', 'webinar'), $starttime,$msg);
$msg = str_replace(get_string('placeholder:finishtime', 'webinar'), $finishtime,$msg);*/
$msg = str_replace(get_string('placeholder:duration', 'webinar'), webinar_format_duration($session->duration),$msg);
if (empty($session->details)) {
$msg = str_replace(get_string('placeholder:details', 'webinar'), '',$msg);
}
else {
$msg = str_replace(get_string('placeholder:details', 'webinar'), html_to_text($session->details),$msg);
}
$msg = str_replace(get_string('placeholder:reminderperiod', 'webinar'), $reminderperiod, $msg);
// Replace more meta data
$msg = str_replace(get_string('placeholder:attendeeslink', 'webinar'), $CFG->wwwroot.'/mod/webinar/attendees.php?s='.$session->id, $msg);
// Custom session fields (they look like "session:shortname" in the templates)
$customfields = webinar_get_session_customfields();
$customdata = $DB->get_records('webinar_session_data', array('sessionid' => $session->id), '', 'fieldid, data');
foreach ($customfields as $field) {
$placeholder = "[session:{$field->shortname}]";
$data = '';
if (!empty($customdata[$field->id])) {
$data = $customdata[$field->id]->data;
}
$msg = str_replace($placeholder, $data, $msg);
}
$msg = str_replace('£','£',$msg);
return $msg;
}
/**
* Function to be run periodically according to the moodle cron
*/
function webinar_cron()
{
return true;
}
/**
* Returns true if the session has started, that is if one of the
* session dates is in the past.
*
* @param class $session record from the webinar_sessions table
* @param integer $timenow current time
*/
function webinar_has_session_started($session, $timenow) {
foreach ($session->sessiondates as $date) {
if ($date->timestart < $timenow) {
return true;
}
}
return false;
}
/**
* Returns true if the session has started and has not yet finished.
*
* @param class $session record from the webinar_sessions table
* @param integer $timenow current time
*/
function webinar_is_session_in_progress($session, $timenow) {
foreach ($session->sessiondates as $date) {
if ($date->timefinish > $timenow && $date->timestart < $timenow) {
return true;
}
}
return false;
}
/**
* Get all of the dates for a given session
*/
function webinar_get_session_dates($sessionid) {
global $DB;
$ret = array();
if ($dates = $DB->get_records('webinar_sessions_dates', array('sessionid' => $sessionid), 'timestart')) {
$i = 0;
foreach ($dates as $date) {
$ret[$i++] = $date;
}
}
return $ret;
}
/**
* Get a record from the webinar_sessions table
*
* @param integer $sessionid ID of the session
*/
function webinar_get_session($sessionid) {
global $DB;
$session = $DB->get_record('webinar_sessions', array('id' => $sessionid));
if ($session) {
$session->sessiondates = webinar_get_session_dates($sessionid);
}
return $session;
}
/**
* Get all records from webinar_sessions for a given webinar activity and location
*
* @param integer $webinarid ID of the activity
* @param string $location location filter (optional)
*/
function webinar_get_sessions($webinarid, $location='')
{
global $CFG, $DB;
$fromclause = "FROM {$CFG->prefix}webinar_sessions s";
$locationwhere = '';
if (!empty($location)) {
$fromclause = "FROM {$CFG->prefix}webinar_session_data d
JOIN {$CFG->prefix}webinar_sessions s ON s.id = d.sessionid";
$locationwhere = " AND d.data = '$location'";
}
$sessions = $DB->get_records_sql("SELECT s.*
$fromclause
LEFT OUTER JOIN (SELECT sessionid, min(timestart) AS mintimestart
FROM {$CFG->prefix}webinar_sessions_dates GROUP BY sessionid) m ON m.sessionid = s.id
WHERE s.webinar = $webinarid
$locationwhere
ORDER BY m.mintimestart");
if ($sessions) {
foreach ($sessions as $key => $value) {
//$sessions[$key]->duration = webinar_minutes_to_hours($sessions[$key]->duration);
$sessions[$key]->sessiondates = webinar_get_session_dates($value->id);
}
}
return $sessions;
}
/**
* Get a grade for the given user from the gradebook.
*
* @param integer $userid ID of the user
* @param integer $courseid ID of the course
* @param integer $webinarid ID of the Webinar activity
*
* @returns object String grade and the time that it was graded
*/
function webinar_get_grade($userid, $courseid, $webinarid) {
$ret = new object;
$ret->grade = 0;
$ret->dategraded = 0;
$grading_info = grade_get_grades($courseid, 'mod', 'webinar', $webinarid, $userid);
if (!empty($grading_info->items)) {
$ret->grade = $grading_info->items[0]->grades[$userid]->str_grade;
$ret->dategraded = $grading_info->items[0]->grades[$userid]->dategraded;
}
return $ret;
}
/**
* Get list of users attending a given session
*/
function webinar_get_attendees($sessionid)
{
global $CFG, $DB;
$records = $DB->get_records_sql("
SELECT
u.id,
su.id AS submissionid,
u.firstname,
u.lastname,
u.email,
f.id AS webinarid,
f.course,
ss.grade,
ss.statuscode,
sign.timecreated
FROM
{$CFG->prefix}webinar f
JOIN
{$CFG->prefix}webinar_sessions s
ON s.webinar = f.id
JOIN
{$CFG->prefix}webinar_signups su
ON s.id = su.sessionid
JOIN
{$CFG->prefix}webinar_signups_status ss
ON su.id = ss.signupid
LEFT JOIN
(
SELECT
ss.signupid,
MAX(ss.timecreated) AS timecreated
FROM
{$CFG->prefix}webinar_signups_status ss
INNER JOIN
{$CFG->prefix}webinar_signups s
ON s.id = ss.signupid
AND s.sessionid = $sessionid
WHERE
ss.statuscode IN (".WEBINAR_STATUS_BOOKED.",".WEBINAR_STATUS_WAITLISTED.")
GROUP BY
ss.signupid
) sign
ON su.id = sign.signupid
JOIN
{$CFG->prefix}user u
ON u.id = su.userid
WHERE
s.id = $sessionid
AND ss.superceded != 1
AND ss.statuscode >= ".WEBINAR_STATUS_APPROVED."
ORDER BY
sign.timecreated ASC,
ss.timecreated ASC
");
return $records;
}
/**
* Get a single attendee of a session
*/
function webinar_get_attendee($sessionid, $userid)
{
global $CFG, $DB;
$record = $DB->get_record_sql("
SELECT
u.id,
su.id AS submissionid,
u.firstname,
u.lastname,
u.email,
s.discountcost,
f.id AS webinarid,
f.course,
ss.grade,
ss.statuscode
FROM
{$CFG->prefix}webinar f
JOIN
{$CFG->prefix}webinar_sessions s
ON s.webinar = f.id
JOIN
{$CFG->prefix}webinar_signups su
ON s.id = su.sessionid
JOIN
{$CFG->prefix}webinar_signups_status ss
ON su.id = ss.signupid
JOIN
{$CFG->prefix}user u
ON u.id = su.userid
WHERE
s.id = $sessionid
AND ss.superceded != 1
AND u.id = $userid
");
if (!$record) {
return false;
}
return $record;
}
/**
* Return all user fields to include in exports
*/
function webinar_get_userfields()
{
global $CFG;
static $userfields = null;
if (null == $userfields) {
$userfields = array();
if (function_exists('grade_export_user_fields')) {
$fieldnames = grade_export_user_fields();
foreach ($fieldnames as $key => $obj) {
$userfields[$obj->shortname] = $obj->fullname;
}
}
else {
// Set default fields if the grade export patch is not
// detected (see MDL-17346)
$fieldnames = array('firstname', 'lastname', 'email', 'city',
'idnumber', 'institution', 'department', 'address');
foreach ($fieldnames as $shortname) {
$userfields[$shortname] = get_string($shortname);
}
$userfields['managersemail'] = get_string('manageremail', 'webinar');
}
}
return $userfields;
}
/**
* Download the list of users attending at least one of the sessions
* for a given webinar activity
*/
function webinar_download_attendance($webinarname, $webinarid, $location, $format) {
global $CFG;
$timenow = time();
$timeformat = str_replace(' ', '_', get_string('strftimedate'));
$downloadfilename = clean_filename($webinarname.'_'.userdate($timenow, $timeformat));
$dateformat = 0;