-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovidClassroomContacts
1364 lines (1046 loc) · 40.2 KB
/
CovidClassroomContacts
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
/*
This was created to generate a list of student classroom contacts so that parents could be
mailed out notification emails. It incorporates shared classrooms for certain day/periods,
student absences, bus passenger lists, and excludes students who are already isolating
so that they don't get redundant notifications.
*/
/* ==================================================================================== */
/* USER VARIABLES. */
/* ==================================================================================== */
/*
MANDATORY:
-YearLevel
OR
-StudentId
OR
-StaffId
*/
DECLARE @YearLevel int = NULL
DECLARE @StaffId INT = NULL
DECLARE @StudentId INT = 12345 --NULL
/*
MANDATORY:
- ContactDate & IncludeDaysPrior
OR
- ContactDateList
*/
DECLARE @ContactDate Date = '2022-03-23'
DECLARE @IncludeDaysPrior int = 2
DECLARE @ContactDateList VARCHAR(MAX) = NULL -- e.g. '2022-02-15,2022-02-16,2022-02-17'
/*
Date the student tested positive on. Note this may be different to the dates
that the student was at school. Also, we can't always be certain that this is correct.
I am only entering the day on which I was notified, unless a test date is specified.
OPTIONAL: Comment regarding test.
*/
DECLARE @PositiveTestDate DATE = '2022-03-23'
DECLARE @TestComment VARCHAR(MAX) = ''
/*
MANDATORY: Number of days we expect students to be in isolation after a positive test result.
This is used to determine if we should notify parents if a student has already
tested covid-positive but has had another classroom contact.
*/
DECLARE @IsolationDays INT = 10
/* ==================================================================================== */
/* CONTROL VARIABLES */
/* ==================================================================================== */
/* Use this to run section of code which adds extra manually identified students.
This should be modified on a per-student basis. If in doubt, set to 0. */
DECLARE @IncludeExtraStudents BIT = 0
/* Add bus passengers automatically from a data file from the BusMinder website?
This needs to be downloaded every day and should include data for the past week or so. */
DECLARE @AddBusPassengersAutomatically BIT = 1
/* Include extra classes and periods e.g. when classes have been combined in
a way which has not been timetabled. */
DECLARE @AddExtraClassPeriods BIT = 0
/* Remove specific day periods manually e.g. if the student was in sick bay but
had not signed out. */
DECLARE @RemoveDayPeriods BIT = 0
/* Remove all international parents (default to Y). */
DECLARE @RemoveInternationalParents BIT = 1
/* Create output CSVs automatically? If not they should be saved manually. */
DECLARE @CreateCsvOutput BIT = 1
/* ==================================================================================== */
/* Working variables. */
/* ==================================================================================== */
DECLARE @TodayStr VARCHAR(100) = (select format((select getdate()), 'dd.MM.yyyy'))
DECLARE @InfectedName VARCHAR(MAX)
DECLARE @DebugDatasets BIT = 1
DECLARE @LogMessage VARCHAR(MAX)
DECLARE @ErrorMessage VARCHAR(MAX)
DECLARE @SqlCmd NVARCHAR(MAX)
DECLARE @Cmd VARCHAR(8000) -- XP_CMDSHELL can only handle VARCHAR(8000), not MAX.
DECLARE @RC INT
/* ==================================================================================== */
/* #################################################################################### */
/* BEGIN MAIN. */
/* #################################################################################### */
/* ==================================================================================== */
/* ==================================================================================== */
/* CONTACT DATES LIST. */
/* ==================================================================================== */
if OBJECT_ID('tempdb.dbo.#DateList') is not NULL drop table #DateList
CREATE TABLE #DateList (
Date DATE)
if @ContactDate is not NULL and @ContactDateList is not NULL BEGIN
set @ErrorMessage = 'ERROR!!! Multiple date variables specified. Exiting.'
RETURN
-- GOTO REPORT_ERROR
END
ELSE IF @ContactDateList is not NULL begin
/* User has provided a list of dates - use recursion to
split the string into a dataset. */
; with DateListSplitter(DateString, DateList) as (
select
SUBSTRING(@ContactDateList, 1, CHARINDEX(',', @ContactDateList) - 1),
STUFF(@ContactDateList, 1, CHARINDEX(',', @ContactDateList), '')
union all
select
SUBSTRING(DateList, 1, CHARINDEX(',', DateList + ',') - 1),
STUFF(DateList + ',', 1, CHARINDEX(',', DateList + ','), '')
from DateListSplitter
where LEN(DateList) > 1
)
insert into #DateList
select CAST(DateString AS DATE)
from DateListSplitter
end
else if @ContactDate is not NULL begin
/* Use SQL recursion here to generate a list of all dates
from the initial contact date counting back for the number of days included. */
; With DateList(Date, DayCounter) as (
select @ContactDate, 0
union all
select DATEADD(day, -(1 + DayCounter), @ContactDate), DayCounter + 1
from DateList
where DayCounter < @IncludeDaysPrior
)
insert into #DateList
select Date
from DateList
end
ELSE BEGIN
SET @ErrorMessage = 'ERROR!!! Required date control variables not set properly. Exiting.'
RETURN
-- GOTO REPORT_ERROR
END
if @DebugDatasets = 1
select *
from #DateList
/* ============================================================================================ */
/* LIST OF CLASSES TO OBTAIN CONTACTS FOR. */
/* ============================================================================================ */
/*
Select only the classes which were timetabled for some period
on the corresponding days.
Note that Junior School generally only timetables the tutor group class.
*/
if OBJECT_ID('tempdb.dbo.#ClassTimetable') is not NULL
drop table #ClassTimetable
create table #ClassTimetable (
FileYear INT,
FileSemester INT,
ClassCode VARCHAR(100),
Date DATE,
DayNumber INT,
DayName VARCHAR(100),
PeriodNumber INT,
StaffID INT,
Room VARCHAR(100),
ClassStart TIME,
ClassFinish TIME)
/*
The classes which get identified depend on user variables, e.g.
if a StudentId is specified then we select all classes that student
was in on the nominated dates; if a year level is specified, we
select all classes for that year level - and so forth.
*/
if @StudentId is NOT NULL begin
set @InfectedName = (SELECT NameExternal from Community where ID = @StudentId)
SET @LogMessage = 'Getting class contacts for student ' + @InfectedName
print @LogMessage
/*
Get all classes for this student which were timetabled for the specified dates,
and where the student was not absent.
*/
if OBJECT_ID('tempdb.dbo.#ClassTimetableAbsences') is not NULL
drop table #ClassTimetableAbsences
select distinct
STC.FileYear,
STC.FileSemester,
STC.ClassCode,
DL.Date,
DATEPART(WEEKDAY, DL.Date) - 1 as DayNumber,
DATENAME(WEEKDAY, dl.Date) as DayName,
TT.PeriodNumber,
TT.StaffID,
TT.Room,
CAST(TTD.TimeFrom AS TIME) as ClassStart,
CAST(TTD.TimeTo AS TIME) as ClassFinish,
AE.AbsenceEventTypeCode,
AE.SchoolInOutStatus,
AE.EventTime as AbsenceEventTime
into #ClassTimetableAbsences
from StudentClasses as STC
left join #DateList as DL
on 1=1
left join Timetable as TT
on STC.FileYear = TT.FileYear
and STC.FileSemester = TT.FileSemester
and DATEPART(WEEKDAY, DL.Date) - 1 = TT.DayNumber
and STC.ClassCode = TT.ClassCode
left join TimetableDefinition as TTD
on STC.FileYear = TTD.FileYear
AND STC.FileSemester = TTD.FileSemester
and TT.PeriodNumber = TTD.PeriodNumber
-- Exclude non-attendance periods such as lunch, after school, etc.
and TTD.AttendanceEntryEnabledFlag = 1
left join AbsenceEvents as AE
on STC.ID = AE.ID
and AE.EventDate = DL.Date
and AE.SupersededByAbsenceEventsSeq is NULL
and AE.AbsenceEventTypeCode in (
'AllDayAbsence', 'SignIn', 'SignOut')
where
STC.FileYear in (
select distinct Year(Date)
from #DateList)
AND STC.FileSemester in (
select distinct
case when month(Date) <= 6 then 1
else 2 end
from #DateList)
and STC.ID = @StudentId
and (STC.StopDate is NULL
or DATEDIFF(day, getdate(), STC.StopDate) > 0)
AND TT.TimetableSeq is not NULL
if @DebugDatasets = 1
select *
from #ClassTimetableAbsences
order by DayNumber, PeriodNumber
/* Remove any classes that the infected student was absent for. */
insert into #ClassTimetable
SELECT distinct
CTA.FileYear,
CTA.FileSemester,
CTA.ClassCode,
CTA.Date,
CTA.DayNumber,
CTA.DayName,
CTA.PeriodNumber,
CTA.StaffID,
CTA.Room,
CTA.ClassStart,
CTA.ClassFinish
from #ClassTimetableAbsences as CTA
left join StudentYears as SY
on CTA.FileYear = SY.FileYear
and @StudentId = SY.ID
left join luYearLevel as luYL
on SY.YearLevel = luYL.Code
where
AbsenceEventTypeCode is NULL
or (
/* Keep sign in/out for students in JS because their
timetable is not reliable. */
luYL.Campus = 'J'
and AbsenceEventTypeCode <> 'AllDayAbsence')
or (
AbsenceEventTypeCode <> 'AllDayAbsence'
/* Exclude a class if sign in/out absence event places the student
out of school during that class period. */
and NOT (AbsenceEventTypeCode = 'SignIn'
and DATEDIFF(Minute, CTA.ClassFinish, CAST(AbsenceEventTime AS TIME)) > 0)
and NOT (AbsenceEventTypeCode = 'SignOut'
and DATEDIFF(Minute, CAST(AbsenceEventTime AS TIME), CTA.ClassStart) > 0))
SELECT @RC = @@ROWCOUNT
print 'Got ' + cast(@RC as varchar(10))
+ ' day periods where the infected student was not absent.'
end
else if @StaffId is NOT NULL begin
set @InfectedName = (select NameExternal from Community where ID = @StaffId)
set @LogMessage = 'Getting all classroom contacts for staff member ' + @InfectedName
print @LogMessage
/*
Get all classes for this staff member which were timetabled for
the specified dates.
We assume for now that the staff member was present for all dates.
*/
insert into #ClassTimetable
select
SCS.FileYear,
SCS.FileSemester,
SCS.ClassCode,
DL.Date,
DATEPART(WEEKDAY, DL.Date) - 1 as DayNumber,
DATENAME(weekday, dl.Date) as DayName,
TT.PeriodNumber,
TT.StaffID,
TT.Room,
CAST(TTD.TimeFrom AS TIME) as ClassStart,
CAST(TTD.TimeTo AS TIME) as ClassFinish
from SubjectClassStaff as SCS
left join #DateList as DL
on 1=1
left join Timetable as TT
on SCS.FileYear = TT.FileYear
and SCS.FileSemester = TT.FileSemester
and DATEPART(WEEKDAY, DL.Date) - 1 = TT.DayNumber
and SCS.ClassCode = TT.ClassCode
left join TimetableDefinition as TTD
on SCS.FileYear = TTD.FileYear
AND SCS.FileSemester = TTD.FileSemester
and TT.PeriodNumber = TTD.PeriodNumber
-- Exclude non-attendance periods such as lunch, after school, etc.
and TTD.AttendanceEntryEnabledFlag = 1
where
SCS.FileYear in (
select distinct Year(Date)
from #DateList)
AND SCS.FileSemester in (
select distinct
case when month(Date) <= 6 then 1
else 2 end
from #DateList)
and SCS.StaffID = @StaffId
AND TT.TimetableSeq is not NULL
end
else if @YearLevel is NOT NULL begin
print 'Getting all classroom contacts for YearLevel '
+ cast(@YearLevel as varchar(10))
/* Get all classes for this year level which were timetabled on the nominated dates. */
insert into #ClassTimetable
select
SUC.FileYear,
SUC.FileSemester,
SUC.ClassCode,
DL.Date,
DATEPART(WEEKDAY, DL.Date) - 1 as DayNumber,
DATENAME(weekday, dl.Date) as DayName,
TT.PeriodNumber,
TT.StaffID,
TT.Room,
CAST(TTD.TimeFrom AS TIME) as ClassStart,
CAST(TTD.TimeTo AS TIME) as ClassFinish
from SubjectClasses as SUC
left join #DateList as DL
on 1=1
left join Timetable as TT
on SUC.FileYear = TT.FileYear
and SUC.FileSemester = TT.FileSemester
and DATEPART(WEEKDAY, DL.Date) - 1 = TT.DayNumber
and SUC.ClassCode = TT.ClassCode
left join TimetableDefinition as TTD
on SUC.FileYear = TTD.FileYear
AND SUC.FileSemester = TTD.FileSemester
and TT.PeriodNumber = TTD.PeriodNumber
-- Exclude non-attendance periods such as lunch, after school, etc.
and TTD.AttendanceEntryEnabledFlag = 1
where
SUC.FileYear in (
select distinct Year(Date)
from #DateList)
AND SUC.FileSemester in (
select distinct
case when month(Date) <= 6 then 1
else 2 end
from #DateList)
AND SUC.NormalYearLevel = @YearLevel
AND TT.TimetableSeq is not NULL
end
else begin
set @ErrorMessage = 'ERROR!!! One or more necessary variables not set. Exiting.'
RETURN
--GOTO REPORT_ERROR
end
if @DebugDatasets = 1
select *
from #ClassTimetable
order by DayNumber, PeriodNumber, ClassCode
/*
Add any other classes which were scheduled for the same day and period
in the same room (sometimes multiple classes share a room).
*/
insert into #ClassTimetable
output
'INSERTED SHARED ROOM' as Action,
INSERTED.*
select
TT.FileYear,
TT.FileSemester,
TT.ClassCode,
DL.Date as Date,
TT.DayNumber,
DATENAME(WEEKDAY, DL.Date) as DayName,
TT.PeriodNumber,
TT.StaffID,
TT.Room,
CAST(TTD.TimeFrom AS TIME) as ClassStart,
CAST(TTD.TimeTo AS TIME) as ClassFinish
from Timetable as TT
left join #DateList as DL
on TT.DayNumber = DATEPART(WEEKDAY, DL.Date) - 1
left join TimetableDefinition as TTD
on TT.FileYear = TTD.FileYear
AND TT.FileSemester = TTD.FileSemester
and TT.PeriodNumber = TTD.PeriodNumber
-- Exclude non-attendance periods such as lunch, after school, etc.
and TTD.AttendanceEntryEnabledFlag = 1
where
TT.Room <> ''
and TT.Room is not NULL
-- Get any classes with same time & location but NOT same class code.
and exists (
select 1
from #ClassTimetable as CTT
where TT.FileYear = CTT.FileYear
and TT.FileSemester = CTT.FileSemester
and TT.DayNumber = CTT.DayNumber
and TT.PeriodNumber = CTT.PeriodNumber
and TT.Room = CTT.Room
and TT.ClassCode <> CTT.ClassCode)
SELECT @RC = @@ROWCOUNT
print 'Added ' + cast(@RC as varchar(10))
+ ' class periods which were timetabled for the same rooms & periods.'
if @RC > 0 and @DebugDatasets = 1
select *
from #ClassTimetable
order by DayNumber, PeriodNumber, ClassCode
if @AddExtraClassPeriods = 1 begin
; with ExtraClassPeriods as (
select
T.*,
TTD.TimeFrom as ClassStart,
TTD.TimeTo as ClassFinish
from (values
(2022, 1, 'XXXXXX', '2022-03-17', 4, 'Thursday', 1, 12345, '')
) as T(
FileYear, FileSemester, ClassCode, Date, DayNumber,
DayName, PeriodNumber, StaffId, Room)
left join TimetableDefinition as TTD
on T.FileYear = TTD.FileYear
and T.FileSemester = TTD.FileSemester
and T.PeriodNumber = TTD.PeriodNumber
)
insert into #ClassTimetable (
FileYear, FileSemester, ClassCode, Date, DayNumber,
DayName, PeriodNumber, StaffId,
Room, ClassStart, ClassFinish)
output
'INSERTED CLASS DAY PERIOD' AS Action,
INSERTED.*
select *
from ExtraClassPeriods
set @RC = @@ROWCOUNT
PRINT 'Added ' + CAST(@RC as varchar(10)) + ' extra class periods manually.'
if @RC > 0 and @DebugDatasets = 1
select *
from #ClassTimetable
order by DayNumber, PeriodNumber, ClassCode
end
if @RemoveDayPeriods = 1 begin
delete
from #ClassTimetable
output 'DELETED DAY PERIOD' as Action, DELETED.*
where
Date = '2022-03-10'
and PeriodNumber in (3, 4)
set @RC = @@ROWCOUNT
PRINT 'Removed ' + CAST(@RC as varchar(10)) + ' day periods manually.'
if @RC > 0 and @DebugDatasets = 1
select *
from #ClassTimetable
order by DayNumber, PeriodNumber, ClassCode
end
/* ============================================================================================ */
/* GET STUDENTS FOR THE CONTACT CLASSES. */
/* ============================================================================================ */
if OBJECT_ID('tempdb.dbo.#ClassStudents') is not NULL
drop table #ClassStudents
select
CTT.*,
SUC.Description as ClassDescription,
SUC.NormalYearLevel as ClassNormalYearLevel,
STC.ID as StudentId,
SY.YearLevel as StudentYearLevel
into #ClassStudents
from #ClassTimetable AS CTT
INNER join StudentClasses as STC
on STC.FileYear = CTT.FileYear
and STC.FileSemester = CTT.FileSemester
and CTT.ClassCode = STC.ClassCode
and (STC.StopDate is NULL
or DATEDIFF(day, CTT.Date, STC.StopDate) > 0)
INNER JOIN StudentYears as SY
on STC.ID = SY.ID
and CTT.FileYear = SY.FileYear
and SY.Status not in ('LEF', 'EXT')
left join SubjectClasses as SUC
on STC.FileYear = SUC.FileYear
and STC.FileSemester = SUC.FileSemester
and STC.ClassCode = SUC.ClassCode
where
-- Obviously we don't need to notify the infected student's parents.
(@StudentId is NULL
or STC.ID <> @StudentId)
if @DebugDatasets = 1
select *
from #ClassStudents
ORDER BY Date, ClassCode, StudentId
/*
Exclude focused study classes for any Y12 student as the classes in Synergy
are not valid (students may move around across multiple rooms).
However, for Focused Study Y12s, we must locate which room they are in by talking
to the HoY etc., then obtain close contacts for that room.
Y11 focused study classes SHOULD be included as per usual.
*/
delete
from #ClassStudents
output 'DELETED Y12 FOC STUDY' as Action, DELETED.*
where StudentYearLevel = 12
and ClassDescription like '%foc%stud%'
set @RC = @@ROWCOUNT
if @RC > 0 and @DebugDatasets = 1 begin
print 'Removed ' + cast(@RC as varchar(100)) + ' Y12 students in Focused Study.'
print 'Don''t forget to track down student rolls for Focused Study and add students who shared a room with contact!'
select *
from #ClassStudents
ORDER BY Date, ClassCode, StudentId
end
/* ============================================================================================ */
/* REMOVE STUDENTS WHO WERE ABSENT. */
/* ============================================================================================ */
/*
We exclude a student if they signed in after a class finished, or signed out
before a class started, or (obviously) if they have an all-day absence.
*/
if OBJECT_ID('tempdb.dbo.#StudentAbsences') is not NULL drop table #StudentAbsences
select distinct
CS.*,
AE.AbsenceEventTypeCode,
CAST(AE.EventTime AS TIME) as EventTime
into #StudentAbsences
from #ClassStudents as CS
left join AbsenceEvents as AE
on CS.Date = AE.EventDate
and CS.StudentId = AE.ID
and AE.SupersededByAbsenceEventsSeq is NULL
and AE.AbsenceEventTypeCode in (
'AllDayAbsence', 'SignIn', 'SignOut')
order by CS.Date, CS.PeriodNumber, CS.StudentId
if @DebugDatasets = 1
select *
from #StudentAbsences
if OBJECT_ID('tempdb.dbo.#ContactsOnSite') is not NULL
drop table #ContactsOnSite
select distinct
SA.*
into #ContactsOnSite
from #StudentAbsences as SA
left join StudentYears as SY
on SA.FileYear = SY.FileYear
and @StudentId = SY.ID
left join luYearLevel as luYL
on SY.YearLevel = luYL.Code
and luYL.Campus = 'WC'
where
SA.AbsenceEventTypeCode is NULL
or (
/* Keep sign in/out for students in JS because their
timetable is not reliable. */
SY.YearLevel <= 6
and AbsenceEventTypeCode <> 'AllDayAbsence' )
OR (
/* If an absence event is found for any student, we determine if the absence event
places them out of school during a particular class period. If so, remove. */
SA.AbsenceEventTypeCode <> 'AllDayAbsence'
and not (SA.AbsenceEventTypeCode = 'SignIn'
and DATEDIFF(Minute, SA.ClassFinish, CAST(SA.EventTime AS TIME)) > 0)
and not (SA.AbsenceEventTypeCode = 'SignOut'
and DATEDIFF(Minute, CAST(SA.EventTime AS TIME), SA.ClassStart) > 0))
order by
SA.Date, SA.PeriodNumber, SA.StudentId
if @DebugDatasets = 1
select *
from #ContactsOnSite
order by ClassCode, StudentId
/* ============================================================================================ */
/* IDENTIFY BUS TRIPS FOR STUDENT AND ADD FELLOW PASSENGERS. */
/* ============================================================================================ */
if @AddBusPassengersAutomatically = 1 begin
PRINT 'Adding bus passenger data automatically from BusMinder report file for today.'
if OBJECT_ID('tempdb.dbo.#BusPassengers') is not NULL drop table #BusPassengers
create table #BusPassengers (
StudentId int,
Name varchar(500),
Trip varchar(500),
Stop varchar(500),
BoardingDateTime DateTime,
Price decimal(10, 2))
/* Need to use command string because OPENROWSET doesn't allow concatenated paths. */
set @SqlCmd =
'INSERT INTO #BusPassengers
SELECT *
from OPENROWSET (
BULK ''\\<SERVER_NAME>\reports$\InputDataFiles\BusMinder_'
+ (SELECT FORMAT(GETDATE(), 'yyyy.MM.dd'))
+ '.csv'',
formatfile = ''\\<SERVER_NAME>\reports$\InputDataFiles\BusMinder_input_fmt.xml'',
firstrow=2
) as T'
EXEC sp_executesql @SqlCmd
/* Confirm that the bus data file contains information for the dates we are concerned with. */
if OBJECT_ID('tempdb.dbo.#MissingBusDataDates') is not NULL drop table #MissingBusDataDates
select distinct COS.Date
into #MissingBusDataDates
from #ContactsOnSite as COS
where COS.Date not in (
select distinct cast(BoardingDateTime as Date)
from #BusPassengers)
set @RC = @@ROWCOUNT
if @RC <> 0 begin
print 'WARNING: No bus passenger data found for the following dates: '
set @LogMessage = (
SELECT STUFF(
(select ', ' + cast(Date as varchar(100))
from #MissingBusDataDates for xml path('')),
1, 2, ''))
print @LogMessage
print 'WARNING: Make sure you have downloaded the most recent BusMinder report before proceeding.'
RETURN
END
/* Check which trips this student was on, if any.
Add fellow passengers to the Classroom Contacts data. */
if OBJECT_ID('tempdb.dbo.#InfectedStudentTrips') is not NULL drop table #InfectedStudentTrips
select distinct
Trip,
cast(BP.BoardingDateTime as DATE) as TripDate
into #InfectedStudentTrips
from #BusPassengers as BP
where BP.StudentId = @StudentId
and cast(BP.BoardingDateTime as DATE) in (
select distinct Date
from #ContactsOnSite)
set @RC = @@ROWCOUNT
if @RC = 0 begin
print 'Infected student NOT found in bus passenger data for contact days.'
end
else if @RC > 0 begin
print 'Infected student travelled on the following bus trips on contact days: '
set @LogMessage = (
SELECT STUFF(
(select
', '
+ cast(Trip as varchar(100))
+ ' - '
+ format(TripDate, 'dd/MM/yyyy')
from #InfectedStudentTrips for xml path('')),
1, 2, ''))
print @LogMessage
insert into #ContactsOnSite
OUTPUT
'STUDENT INSERT BUS TRIP' AS Action,
INSERTED.*
select distinct
YEAR(BP.BoardingDateTime) AS FileYear,
CASE WHEN DATEPART(MONTH, BP.BoardingDateTime) <= 6 THEN 1 ELSE 2 END AS FileSemester,
NULL as ClassCode,
CAST(BP.BoardingDateTime as DATE) as Date,
DATEPART(WEEKDAY, BP.BoardingDateTime) - 1 as DayNumber,
DATENAME(WEEKDAY, BP.BoardingDateTime) as DayName,
NULL as PeriodNumber,
NULL as StaffId,
NULL as Room,
NULL as ClassStart,
NULL as ClassFinish,
NULL as ClassDescription,
NULL as ClassNormalyearLevel,
BP.StudentId,
NULL as StudentYearLevel,
NULL as AbsenceEventTypeCode,
NULL as EventTime
from #BusPassengers as BP
INNER join #InfectedStudentTrips as IST
ON BP.Trip = IST.Trip
AND CAST(BP.BoardingDateTime AS DATE) = IST.TripDate
where cast(BP.BoardingDateTime as DATE) in (
select distinct Date
from #ContactsOnSite)
print 'Added ' + CAST(@@ROWCOUNT AS VARCHAR(10)) + ' bus passengers automatically.'
if @DebugDatasets = 1
select *
from #ContactsOnSite
end
end
/* ============================================================================================ */
/* ADD EXTRA CLASSROOM CONTACTS WHO HAVE BEEN MANUALLY IDENTIFIED. */
/* ============================================================================================ */
/*
Note that this step assumes students are present - no absences are checked for.
*/
if @IncludeExtraStudents = 1 begin
insert into #ContactsOnSite
OUTPUT
'STUDENT INSERT MANUAL' AS Action,
INSERTED.*
select
DATEPART(YEAR, GETDATE()) AS FileYear,
CASE WHEN DATEPART(MONTH, GETDATE()) <= 6 THEN 1 ELSE 2 END AS FileSemester,
NULL as ClassCode,
NULL as Date,
NULL as DayNumber,
NULL as DayName,
NULL as PeriodNumber,
NULL as StaffId,
NULL as Room,
NULL as ClassStart,
NULL as ClassFinish,
NULL as ClassDescription,
NULL as ClassNormalyearLevel,
Extras.ID,
NULL as StudentYearLevel,
NULL as AbsenceEventTypeCode,
NULL as EventTime
from (values
(12345)
) as Extras(Id)
print 'Added ' + CAST(@@ROWCOUNT AS VARCHAR(10))
+ ' extra students who have been manually identified.'
select *
from #ContactsOnSite
end
else begin
print 'No extra students manually added to this extract.'
end
/* ============================================================================================ */
/* REMOVE STUDENTS WHO ARE CURRENTLY ISOLATING. */
/* ============================================================================================ */
/*
If a student's parents have already notified us that the student is covid positive and is
isolating, we don't need to notify them that their child is a classroom contact for the
current student. So we remove them from output.
*/
declare @IsolatingStudents varchar(max) = (
SELECT
STUFF(
(select distinct
',' + CAST(StudentId AS VARCHAR(10))
from #ContactsOnSite
where StudentId in (
select ID
from woodcroft.uCovidPositive
where DATEDIFF(DAY, TestDate, GETDATE()) <= @IsolationDays)
for xml path('')),
1, 1, ''))
PRINT 'Students currently isolating who will be removed: ' + ISNULL(@IsolatingStudents, 'NONE')
if @IsolatingStudents is not NULL begin
declare @SQL NVARCHAR(MAX) = '
delete
from #ContactsOnSite
output
''STUDENT DELETED ISOLATING'' AS Action,
DELETED.*
where StudentId in (' + @IsolatingStudents + ')'
exec sp_executesql @SQL
select @RC = @@ROWCOUNT
print 'Deleted ' + CAST(@RC AS VARCHAR(100)) + ' class contact records for students who are already isolating.'
if @DebugDatasets = 1 and @RC > 0
select *
from #ContactsOnSite
order by ClassCode, StudentId
end
/* ============================================================================================ */
/* FINAL CLEAN-UP: Delete any records containing the infected source student - obviously
don't need to notify their parents. */
/* ============================================================================================ */
delete
from #ContactsOnSite
where StudentId = @StudentId
SET @RC = @@ROWCOUNT
if @RC > 0 begin
print 'Removed ' + cast(@RC as varchar(10)) + ' records who were the infected student.'
end
/* ============================================================================================ */
/* PARENTS TO CONTACT. */
/* ============================================================================================ */
if OBJECT_ID('tempdb.dbo.#MyStudentContacts') is not NULL drop table #MyStudentContacts
/* Primary contacts. */
select distinct
COS.StudentId,
SC.ContactType,
SC.LinkedID as ContactId
INTO #MyStudentContacts
from #ContactsOnSite as COS
left join StudentContacts as SC
on COS.StudentId = SC.id
and SC.ContactType <> 'SCT'