-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutfClassRollsMissing
245 lines (215 loc) · 8.81 KB
/
utfClassRollsMissing
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
CREATE function [woodcroft].[utfClassRollsMissing] (
@DateTime DateTime,
@DelayMinutes INT = 15)
returns @MissingClassRolls table (
DayNumber int,
PeriodNumber int,
Lesson varchar(100),
ClassCode varchar(100),
TimeFrom Time,
TimeTo Time,
Description varchar(500),
NormalYearLevel int,
StaffID int,
ReliefTeacherId int)
as begin
/*
AUTHOR: Benjamin Selby
DATE: June 2022
Outputs a list of the classes which have finished so far today (with a time offset
parameter to allow staff some time after a class has ended to fill in their rolls)
where a roll has NOT been submitted yet (ie. teacher has not done a class roll
as expected).
One complexity is that we need to wait until multi-period classes have finished
before sending notifications (we don't want to send a notification after the first
period of a multi-period class has ended, but while the second period is in progress).
So, we must first identify multi-period classes and only notify when class rolls are
missing after the final period has ended.
Excludes any classes where all students are absent (e.g. on camp), although
technically staff should still be completing rolls for those classes.
MODIFICATIONS ================================================================
[2022/08/03 SELBY_B] Removed code which determined which classes have finished
before the cutoff time, replaced with a function call to
[woodcroft].[utfClassPeriodsCombined]. That function incorporates the
logic required to determine start/finish times of multi-period classes.
[2022.08.19 selby_b] Added code which merges on the Daily Organiser cancelled
classes table and excludes them from output, so we don't mail out reminders
to staff when a class has been cancelled.
*/
/* ========================================================================== */
/* INIT. */
/* ========================================================================== */
set @DateTime = isnull(@DateTime, getdate())
declare @Date Date = cast(@DateTime as Date)
declare @Time Time = cast(@DateTime as Time)
declare @Year INT = year(@Date)
declare @Semester INT = case when month(@Date) <= 6 then 1 else 2 end
declare @DayNumber INT = DATEPART(weekday, @Date) - 1
/* ========================================================================== */
/* Get all classes scheduled for today with at least one student who is not
absent from school. Although technically teachers should do rolls for classes
where all students are absent, we have decided to ignore these for now. */
/* ========================================================================== */
declare @StudentAbsences table (
DayNumber int,
PeriodNumber int,
ClassCode varchar(200),
Room varchar(100),
ClassStartTime time,
ClassEndTime time,
AbsenceEventTypeCode varchar(200),
AbsenceReasonCode varchar(100),
EventComment varchar(500),
AbsenceEventTime time,
SchoolInOutStatus varchar(10))
insert into @StudentAbsences
select distinct
TT.DayNumber,
TT.PeriodNumber,
TT.ClassCode,
TT.Room,
cast(TTD.TimeFrom as Time) as ClassStartTime,
cast(TTD.TimeTo as Time) as ClassEndTime,
AE.AbsenceEventTypeCode,
AE.AbsenceReasonCode,
AE.EventComment,
AE.EventTime,
AE.SchoolInOutStatus
from StudentClasses as STC
-- INNER JOIN to remove any classes not scheduled for today.
INNER JOIN Timetable as TT
on TT.FileYear = @Year
and TT.FileSemester = @Semester
and TT.DayNumber = @DayNumber
and STC.ClassCode = TT.ClassCode
left join TimetableDefinition as TTD
on TTD.FileYear = @Year
and TTD.FileSemester = @Semester
and TTD.DayNumber = 0
and TT.PeriodNumber = TTD.PeriodNumber
left join AbsenceEvents as AE
on @Date = AE.EventDate
and STC.ID = AE.ID
and SupersededByAbsenceEventsSeq is NULL
where STC.FileYear = @Year
and STC.FileSemester = @Semester
and (STC.StopDate is NULL
or STC.StopDate > @DateTime)
delete from @StudentAbsences
where AbsenceEventTypeCode = 'AllDayAbsence'
-- Exclude students expected to leave or who have left before a class finishes.
or (AbsenceEventTypeCode = 'ExcursionOutIn'
and SchoolInOutStatus = 'Out'
and AbsenceEventTime <= ClassEndTime)
or (AbsenceEventTypeCode in ('ExpectedOut', 'SignOut')
and AbsenceEventTime <= ClassEndTime)
-- Exclude students expected to arrive or who have arrived after a class starts.
or (AbsenceEventTypeCode = 'ExcursionOutIn'
and SchoolInOutStatus = 'In'
and ClassStartTime <= AbsenceEventTime)
or (AbsenceEventTypeCode in ('ExpectedIn', 'SignIn')
and ClassStartTime <= AbsenceEventTime)
declare @ClassesWithStudentsPresent table (
DayNumber INT,
PeriodNumber INT,
ClassCode VARCHAR(200))
insert into @ClassesWithStudentsPresent
select distinct
DayNumber,
PeriodNumber,
ClassCode
from @StudentAbsences
/* ==================================================================================== */
/* FINAL OUTPUT. */
/* ==================================================================================== */
;
with MyReliefStaff as (
select
RLF.*,
ISNULL(TCH.ID, EMG.ID) as ReliefTeacherId
from dailyOrganiser.uReliefs as RLF
left join dailyOrganiser.uTeachers as TCH
on RLF.ReliefTeacherCode = TCH.Code
left join dailyOrganiser.uEmergencyTeachers as EMG
on RLF.ReliefTeacherCode = EMG.Code
where @Date = RLF.ReliefDate
)
, CancelledClasses as (
select distinct
Date,
Period,
ClassCode
from dailyOrganiser.uLessonCancellations
where Date = @Date
)
insert into @MissingClassRolls (
DayNumber,
PeriodNumber,
Lesson,
ClassCode,
TimeFrom,
TimeTo,
Description,
NormalYearLevel,
StaffID,
ReliefTeacherId)
select
TT.DayNumber,
TT.PeriodNumber,
TTD.Description as Lesson,
TT.ClassCode,
cast(TTD.TimeFrom as Time) as TimeFrom,
cast(TTD.TimeTo as Time) as TimeTo,
SUC.Description,
SUC.NormalYearLevel,
TT.StaffID,
RLF.ReliefTeacherId
from Timetable as TT
left join dbo.TimetableDefinition as TTD
on @Year = TTD.FileYear
and @Semester = TTD.FileSemester
-- TimetableDefinition only uses day number of zero.
and TTD.DayNumber = 0
and TT.PeriodNumber = TTD.PeriodNumber
and TTD.AttendanceEntryEnabledFlag = 1
left join dbo.SubjectClasses as SUC
on @Year = SUC.FileYear
and @Semester = SUC.FileSemester
and TT.ClassCode = SUC.ClassCode
left join AttendanceMaster as AM
on @Date = AM.AttendanceDate
and @DayNumber = AM.AttendanceDayNumber
and TT.PeriodNumber = AM.AttendancePeriod
and TT.ClassCode = AM.ClassCode
left join MyReliefStaff as RLF
on TT.ClassCode = RLF.ClassCode
and TT.PeriodNumber = case
when RLF.Period = 'TG' then 1
else RLF.Period + 1
end
-- Remove any classes with no students present.
INNER JOIN (
select distinct ClassCode
from @ClassesWithStudentsPresent) AS CSP(ClassCode)
on TT.ClassCode = CSP.ClassCode
-- Only keep classes which have finished before cutoff time.
INNER join woodcroft.utfClassPeriodsCombined(@Date) as CP
on TT.ClassCode = CP.ClassCode
and TT.PeriodNumber between CP.PeriodStart and CP.PeriodEnd
and CP.DateTimeTo <= dateadd(Minute, -@DelayMinutes, @DateTime)
left join CancelledClasses as CC
on TT.ClassCode = CC.ClassCode
and TT.PeriodNumber = case
when CC.Period = 'TG' then 1
else CC.Period + 1 end
where TT.FileYear = @Year
and TT.FileSemester = @Semester
and TT.DayNumber = @DayNumber
and TT.StaffID is not NULL
and TT.StaffID <> 0
and AM.AttendanceMasterSeq is NULL
and CC.ClassCode is NULL
order by TT.ClassCode, TT.PeriodNumber
return
end
GO