-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEducation Functions.sql
More file actions
371 lines (325 loc) · 11.6 KB
/
Education Functions.sql
File metadata and controls
371 lines (325 loc) · 11.6 KB
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
create function Education.createLibraryID(@entryDate datetime, @majorID INT, @personID INT)
returns INT
as begin
return year(@entryDate)*1000000 + @majorID*10000 + @personID
end;
GO
create function Education.has_passed_prereqs (@studentID INT, @courseID INT)
returns BIT
as begin
if not exists(select 1 from Education.Person where ID = @studentID and isActive = 1 and role = 'student')
return 0;
declare @passed BIT = 1;
if exists (
select 1
from Education.Prereq P
where P.courseID = @courseID
and not exists (
select 1
from Education.Takes T
where T.studentID = @studentID
and T.courseID = P.prereqID
and T.status = 'passed'
)
)
begin
set @passed = 0;
end
return @passed;
end;
GO
create function Education.get_current_term(@entryDate datetime)
returns INT
as begin
declare @currMonth INT = MONTH(GETDATE());
declare @currYear INT = YEAR(GETDATE());
declare @entryMonth INT = MONTH(@entryDate);
declare @entryYear INT = YEAR(@entryDate);
declare @yearDiff INT = @currYear - @entryYear;
declare @term INT;
set @term = @yearDiff * 2;
if @currMonth between 1 and 5
set @term = @term + 1;
else
set @term = @term + 2;
if @entryMonth between 1 and 5
set @term = @term - 1;
else
set @term = @term - 2;
return @term + 1;
end;
GO
create function Education.get_term(@entryDate datetime, @specificDate datetime)
returns INT
as begin
declare @currMonth INT = MONTH(@specificDate);
declare @currYear INT = YEAR(@specificDate);
declare @entryMonth INT = MONTH(@entryDate);
declare @entryYear INT = YEAR(@entryDate);
declare @yearDiff INT = @currYear - @entryYear;
declare @term INT;
set @term = @yearDiff * 2;
if @currMonth between 1 and 5
set @term = @term + 1;
else
set @term = @term + 2;
if @entryMonth between 1 and 5
set @term = @term - 1;
else
set @term = @term - 2;
return @term + 1;
end;
GO
create function Education.SuggestCoursesForStudent(@studentID INT, @date DATETIME)
returns table
as return (
select DISTINCT C.courseID, C.title, ST.term
from Education.Student S
join Education.Person P on P.ID = S.studentID
join Education.MajorChart MC on MC.majorID = P.majorID
join Education.SuggestTerm ST on ST.chartID = MC.chartID
join Education.Course C on C.courseID = ST.courseID
where
P.ID = @studentID and P.isActive = 1 and
ST.term <= Education.get_term((select entryDate from Education.Person where ID = @studentID), @date)
and not exists(
select 1
from Education.Takes T
where T.studentID = @studentID
and T.courseID = ST.courseID
and T.status = 'passed'
)
and Education.has_passed_prereqs(@studentID, ST.courseID) = 1
);
GO
create function Education.validateNationalCode(@NationalID varchar(20))
returns BIT
as begin
declare @National_ID_Validation_Result BIT = 1;
if LEN(@NationalID) != 10
begin
set @National_ID_Validation_Result = 0;
return @National_ID_Validation_Result;
end;
if ISNUMERIC(@NationalID+'.0e0')=0
begin
set @National_ID_Validation_Result = 0
return @National_ID_Validation_Result;
end;
if SUBSTRING(@NationalID, 1, 1)=0 and SUBSTRING(@NationalID, 2, 1)=0 and SUBSTRING(@NationalID, 3, 1)=0
begin
set @National_ID_Validation_Result = 0
return @National_ID_Validation_Result;
end;
if (@NationalID='0000000000' or @NationalID='1111111111' or @NationalID='2222222222' or @NationalID='3333333333'
or @NationalID='4444444444' or @NationalID='5555555555' or @NationalID='6666666666' or @NationalID='7777777777'
or @NationalID='8888888888' or @NationalID='9999999999' )
begin
set @National_ID_Validation_Result = 0
return @National_ID_Validation_Result;
end;
begin
declare @b AS INT;
set @b=((10*SUBSTRING(@NationalID, 1, 1) + 9*SUBSTRING(@NationalID, 2, 1) + 8*SUBSTRING(@NationalID, 3, 1)+7*SUBSTRING(@NationalID, 4, 1)+6*SUBSTRING(@NationalID, 5, 1)+
5*SUBSTRING(@NationalID, 6, 1) + 4*SUBSTRING(@NationalID, 7, 1) + 3*SUBSTRING(@NationalID, 8, 1) + 2*SUBSTRING(@NationalID,9, 1))%11)
declare @ControlBit AS TINYINT=SUBSTRING(@NationalID,10,1)
if @b<2
begin
if @ControlBit!=@b set @National_ID_Validation_Result = 0;
end;
if @b>=2
begin
if @ControlBit!=11-@b set @National_ID_Validation_Result = 0;
end;
end;
return @National_ID_Validation_Result;
end;
GO
create function Education.get_currnet_term_stuID (@stuID INT,@currentDate datetime)
returns INT
as begin
if not exists(select 1 from Education.Person where ID = @stuID and isActive = 1 and role = 'student')
return NULL;
declare @num_term_student INT;
select @num_term_student = Education.get_term( (select entryDate from Education.Person where id = @stuID), @currentDate)
return @num_term_student
end;
go
create function Education.getSemesterStartDate(@semester INT, @year INT)
returns DATETIME
as begin
DECLARE @startDate DATETIME;
if @semester = 1
set @startDate = CAST(CAST(@year AS varchar(4)) + '-01-01' AS DATETIME);
else if @semester = 2
set @startDate = CAST(CAST(@year AS varchar(4)) + '-06-15' AS DATETIME);
else
set @startDate = NULL;
return @startDate;
end;
go
create function Education.TermGPA(@stuID INT,@term_number INT)
returns DECIMAL(5,2)
as begin
declare @total_credit INT;
declare @total_score DECIMAL(10,2);
select
@total_credit = sum(c.credit),
@total_score = sum(c.credit*t.grade*1.0)
from Education.Takes as T
join Education.Course as c on c.courseID = T.courseID
join Education.Section as S on S.courseID = T.courseID and S.secID = T.sectionID
join Education.Person as P on P.ID = T.studentID
where Education.get_term(P.entryDate, Education.getSemesterStartDate(s.semester, s.year)) = @term_number
and t.studentID = @stuID and t.status not in ('ongoing');
if @total_score is null or @total_credit is null or @total_credit = 0
return null;
return @total_score/@total_credit;
end;
go
create function Education.Weeklyschedule(@stuID INT)
returns table
as return(
select C.courseID, C.title, C.credit, P.firstName+' '+P.lastName as InstructorName,
TS.daysOfWeek, TS.startTime, TS.endTime,
B.name +' ' + CAST(CR.roomNumber as varchar) as ClassRoom
from Education.Takes as T
join Education.Course as c on c.courseID = T.courseID
join Education.Section as S on S.courseID = T.courseID and S.secID = T.sectionID
join Education.TimeSlot as TS on TS.timeSlotID = S.timeSlotID
join Education.Instructor as I on I.instructorID = S.instructorID
join Education.Person as P on P.ID = I.instructorID
join Education.Classroom as CR on CR.classroomID = S.classroomID
join Education.Building as B on B.buildingID = CR.buildingID
join Education.Person as P2 on P2.ID = T.studentID
where T.studentID = @stuID and T.status = 'ongoing' and P2.isActive = 1
);
go
create function Education.totalGPA (@stuID INT)
returns DECIMAL(5,2)
as begin
declare @total_credit INt;
declare @total_score DECIMAL(10,2);
select @total_credit = sum(c.credit), @total_score = sum (c.credit * t.grade* 1.0)
from Education.Takes as T join
Education.SuggestTerm as ST on st.courseID = t.courseID
join Education.Course as C on c.courseID = T.courseID
where t.studentID = @stuID and t.status not in ('ongoing');
if @total_score IS NULL OR @total_credit IS NULL OR @total_credit = 0
RETURN NULL;
return @total_score/@total_credit
end;
go
create function Education.TermTranscript(@stuID INT,@term_number INT)
returns table
as return(
select C.courseID,C.title,C.credit,T.grade,
TS.daysOfWeek,TS.startTime,TS.endTime
from Education.Takes as T
join Education.Course as c on c.courseID = T.courseID
join Education.Section as S on S.courseID = T.courseID and S.secID = T.sectionID
join Education.TimeSlot as TS on TS.timeSlotID = S.timeSlotID
join Education.Person as P on P.ID = T.studentID
where T.studentID = @stuID and T.status in ('passed','rejected')
and Education.get_term(P.entryDate, Education.getSemesterStartDate(S.semester, S.year)) = @term_number
);
go
create function Education.hasPassed(@stuID INT, @status_ varchar(100))
returns @Result TABLE (
studentID INT,
courseID INT,
sectionID INT,
grade DECIMAL(5,2),
status varchar(50)
)
as begin
if @status_ not in ('all', 'ongoing', 'passed', 'rejected')
return;
if @status_ = 'ongoing'
begin
insert into @Result
select * from Education.Takes
where studentID = @stuID and status = 'ongoing';
end
else if @status_ = 'passed'
begin
insert into @Result
select * from Education.Takes
where studentID = @stuID and status = 'passed';
end
else if @status_ = 'rejected'
begin
insert into @Result
select * from Education.Takes
where studentID = @stuID and status = 'rejected';
end
else
begin
insert into @Result
select * from Education.Takes
where studentID = @stuID;
end
return;
end;
go
create function Education.Log_search(@usertype_ varchar(100))
returns table
as return(
select l.userId, l.action ,l.logTime from
Education.Log as l
where l.userType = @usertype_);
go
create function Education.total_passedcredit(@stuID INT)
returns INT
as begin
declare @total_credit INT;
select @total_credit = sum(c.credit)
from Education.Takes as T
join Education.Course as C on T.courseID = C.courseID
where t.studentID = @stuID and t.status = 'passed';
return ISNULL(@total_credit, 0);
end;
go
create function Education.CheckTimeConflict(@studentID INT, @sectionID INT)
returns BIT
as begin
if not exists(select 1 from Education.Person where ID = @studentID and isActive = 1 and role = 'student')
return 0;
DECLARE @conflict BIT = 0;
DECLARE @newStart TIME, @newEnd TIME, @newDays Nvarchar(50);
select @newStart = ts.startTime, @newEnd = ts.endTime, @newDays = ts.daysOfWeek
from Education.Section s join Education.TimeSlot ts on s.timeSlotID = ts.timeSlotID
WHERE s.secID = @sectionID;
if exists (
select 1
from Education.Takes t
join Education.Section s on t.sectionID = s.secID
join Education.TimeSlot ts on s.timeSlotID = ts.timeSlotID
where t.studentID = @studentID and t.status = 'ongoing'
and (ts.daysOfWeek like '%' + @newDays + '%' OR @newDays like '%' + ts.daysOfWeek + '%') and @newStart < ts.endTime and @newEnd > ts.startTime
)
begin
set @conflict = 1;
end;
return @conflict;
end;
GO
create function Education.isSectionInCurrentTerm(@sectionID INT, @currentDate DATETIME)
returns BIT
as
begin
declare @sectionYear INT, @sectionSemester INT;
declare @currentYear INT, @currentSemester INT;
select @sectionYear = year, @sectionSemester = semester
from Education.Section
where secID = @sectionID;
set @currentYear = YEAR(@currentDate);
if @currentDate < Education.getSemesterStartDate(2, @currentYear)
set @currentSemester = 1;
else
set @currentSemester = 2;
if @sectionYear = @currentYear and @sectionSemester = @currentSemester
return 1;
return 0;
end;
GO