-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEducation Procedures.sql
More file actions
243 lines (211 loc) · 6.36 KB
/
Education Procedures.sql
File metadata and controls
243 lines (211 loc) · 6.36 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
create procedure Education.AssignAdvisor
@stuID INT, @advisorID INT
as begin
if not exists(select 1 from Education.Person where ID = @stuID and isActive = 1 and role = 'student')
begin
print('student not found or is not active');
return;
end;
update Education.Student
set advisorID = @advisorID
where studentID = @stuID;
end;
GO
create procedure Education.updateStudentGrade
@stuID INT, @sectionID INT, @newGrade INT
as begin
if not exists(select 1 from Education.Person where ID = @stuID and isActive = 1 and role = 'student')
begin
print('student not found or is not active');
return;
end;
update Education.Takes
set grade = @newGrade,
status = case
when @newGrade >= 10 then 'passed'
when @newGrade <10 then 'rejected'
else 'ongoing'
end
where studentID = @stuID and sectionID = @sectionID;
end;
GO
create procedure Education.addBuilding
@name varchar(50)
as begin
insert into Education.Building(name)
values (@name);
end;
GO
create procedure Education.addDepartment
@name varchar(50), @buildingID INT
as begin
insert into Education.Department(name, buildingID)
values (@name, @buildingID);
end;
GO
create procedure Education.addMajor
@name varchar(50), @departmentID INT
as begin
insert into Education.Major(name, departmentID)
values (@name, @departmentID);
end;
GO
create procedure Education.addCourse
@courseMajorID INT, @title varchar(50), @credit INT
as begin
insert into Education.Course(courseMajorID, title, credit)
values (@courseMajorID, @title, @credit);
end;
GO
create procedure Education.addSection
@courseID INT, @semester INT, @year INT, @classroomID INT, @instructorID INT, @timeSlotID INT
as begin
insert into Education.Section(courseID, semester, year, classroomID, instructorID, timeSlotID)
values(@courseID, @semester, @year, @classroomID, @instructorID, @timeSlotID);
end;
GO
create procedure Education.addClassroom
@capacity INT, @buildingID INT, @roomNum INT
as begin
insert into Education.Classroom(buildingID, roomNumber, capacity)
values(@buildingID, @roomNum, @capacity);
end;
GO
create procedure Education.addPerson
@firstName varchar(50), @lastName varchar(50), @nationalCode varchar(50), @entryDate datetime, @majorID INT,
@role varchar(20)
as begin
if @role in ('student', 'instructor', 'librarian')
begin
insert into Education.Person(firstName, lastName, nationalCode, majorID, role, entryDate)
values (@firstName, @lastName, @nationalCode, @majorID, @role, @entryDate);
end;
end;
GO
create procedure Education.deactivatePerson
@ID INT
as begin
update p
set p.isActive = 0
from Education.Person p where p.ID = @ID;
end;
GO
create procedure Education.takeSection
@studentID INT, @sectionID INT, @currDate DATETIME
as begin
if not exists(select 1 from Education.Person where ID = @studentID and isActive = 1 and role = 'student')
begin
print('student not found or is not active');
return;
end;
if not exists(SELECT 1 FROM Education.Student WHERE studentID = @studentID)
begin
print('invalid studentID');
return;
end;
if not exists(SELECT 1 FROM Education.Section WHERE secID = @sectionID)
begin
print('this class is not exists');
return;
end;
if Education.isSectionInCurrentTerm(@sectionID, @currDate) = 0
begin
print('This section is not in the current term.');
return;
end;
declare @capacity INT = (select capacity from Education.Section join Education.Classroom C on Section.classroomID = C.classroomID where secID = @sectionID);
declare @currCount INT = (
select count(*) from Education.takes where sectionID = @sectionID
);
if @currCount >= @capacity
begin
print('this section is full');
return;
end
declare @courseID INT;
select @courseID = courseID from Education.Section where secID = @sectionID;
if exists (select 1 from Education.takes
where studentID = @studentID and @courseID = courseID and status = 'passed')
begin
print('student passed this Course before.');
return;
end;
if Education.has_passed_prereqs(@studentID, @courseID) = 0
begin
print('student dont passed its prereq before.');
return;
end;
if Education.CheckTimeConflict(@studentID, @sectionID) = 1
begin
print('Time conflict detected.');
return;
end;
INSERT INTO Education.Takes(studentID, courseID, sectionID, grade, status)
VALUES (@studentID, @courseID, @sectionID, NULL, 'ongoing');
print ('successfully registered.');
END;
go
create procedure Education.addTimeSlot
@startTime TIME, @endTime TIME, @daysOfWeek VARCHAR(20)
as begin
if exists (select 1 from Education.TimeSlot where startTime = @startTime AND endTime = @endTime AND daysOfWeek = @daysOfWeek)
begin
print('a time slot with these details already exists.');
return;
end
if @endTime <= @startTime
begin
print('end time must be after start time.');
return;
end
insert into Education.TimeSlot(startTime, endTime, daysOfWeek)
values(@startTime, @endTime, @daysOfWeek);
print('Time slot added successfully.');
end;
GO
create procedure Education.AssignInstructorspecialist
@instructorid INT, @specialist varchar(100)
as begin
update Education.Instructor
set specialist = @specialist
where instructorID = @instructorid
end;
go
create procedure Education.addPrereq
@courseID INT, @prereqID INT
as begin
insert into Education.Prereq(courseID, prereqID)
values (@courseID, @prereqID);
end;
go
create procedure Education.addMajorChart
@chartID INT, @majorID INT
as begin
insert into Education.MajorChart(chartID, majorID)
values (@chartID, @majorID);
end;
go
create procedure Education.addSuggestTerm
@chartID INT, @courseID INT, @term INT
as begin
insert into Education.SuggestTerm(chartID, courseID, term)
values (@chartID, @courseID, @term);
end;
go
create procedure Education.dropSection
@studentID INT, @sectionID INT
as begin
if not exists(select 1 from Education.Person where ID = @studentID and isActive = 1 and role = 'student')
begin
print('student not found or is not active');
return;
end;
if not exists(select 1 from Education.Takes where studentID = @studentID and sectionID = @sectionID)
begin
print('student has not taken this section');
return;
end;
delete from Education.Takes
where studentID = @studentID and sectionID = @sectionID;
end;
go