28
29
subjectid SERIAL PRIMARY KEY NOT NULL,
29
30
subj_code VARCHAR UNIQUE NOT NULL,
30
31
subj_name VARCHAR NOT NULL,
31
subj_short_name VARCHAR UNIQUE NOT NULL
32
subj_short_name VARCHAR UNIQUE NOT NULL,
34
36
CREATE TABLE semester (
35
37
semesterid SERIAL PRIMARY KEY NOT NULL,
36
38
year CHAR(4) NOT NULL,
37
39
semester CHAR(1) NOT NULL,
38
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
39
'current', 'future')) DEFAULT 'current',
40
41
UNIQUE (year, semester)
44
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
47
IF OLD.active = true AND NEW.active = false THEN
48
UPDATE enrolment SET active=false WHERE offeringid IN (
49
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
55
CREATE TRIGGER deactivate_semester_enrolments
56
AFTER UPDATE ON semester
57
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
43
59
CREATE TABLE offering (
44
60
offeringid SERIAL PRIMARY KEY NOT NULL,
45
61
subject INT4 REFERENCES subject (subjectid) NOT NULL,
46
62
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
49
63
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
50
64
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
51
65
UNIQUE (subject, semesterid)
57
71
CREATE TABLE project_set (
58
72
projectsetid SERIAL PRIMARY KEY NOT NULL,
59
73
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
60
max_students_per_group INTEGER
74
max_students_per_group INTEGER NOT NULL DEFAULT 4
63
77
CREATE TABLE project (
64
78
projectid SERIAL PRIMARY KEY NOT NULL,
65
short_name TEXT NOT NULL,
69
81
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
70
deadline TIMESTAMP NOT NULL
73
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
78
IF TG_OP = ''UPDATE'' THEN
79
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
83
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
84
PERFORM 1 FROM project, project_set
85
WHERE project_set.offeringid = oid AND
86
project.projectsetid = project_set.projectsetid AND
87
project.short_name = NEW.short_name;
89
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
95
CREATE TRIGGER check_project_namespacing
96
BEFORE INSERT OR UPDATE ON project
97
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
99
85
CREATE TABLE project_group (
100
86
groupnm VARCHAR NOT NULL,
101
87
groupid SERIAL PRIMARY KEY NOT NULL,
153
137
PRIMARY KEY (loginid,offeringid)
140
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
145
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
146
IF NOT active AND NEW.active = true THEN
147
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
151
' LANGUAGE 'plpgsql';
153
CREATE TRIGGER confirm_active_semester
154
BEFORE INSERT OR UPDATE ON enrolment
155
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
156
157
CREATE TABLE assessed (
157
158
assessedid SERIAL PRIMARY KEY NOT NULL,
158
159
loginid INT4 REFERENCES login (loginid),
203
--TODO: Link worksheets to offerings
205
204
CREATE TABLE worksheet (
206
worksheetid SERIAL PRIMARY KEY,
207
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
208
identifier TEXT NOT NULL,
211
assessable BOOLEAN NOT NULL,
212
seq_no INT4 NOT NULL,
213
format TEXT NOT NUll,
214
UNIQUE (offeringid, identifier)
217
CREATE TABLE worksheet_exercise (
218
ws_ex_id SERIAL PRIMARY KEY,
219
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
220
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
221
seq_no INT4 NOT NULL,
222
active BOOLEAN NOT NULL DEFAULT true,
223
optional BOOLEAN NOT NULL,
224
UNIQUE (worksheetid, exerciseid)
227
CREATE TABLE exercise_attempt (
205
worksheetid SERIAL PRIMARY KEY NOT NULL,
206
subject VARCHAR NOT NULL,
207
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
208
identifier VARCHAR NOT NULL,
211
UNIQUE (subject, identifier)
214
CREATE TABLE worksheet_problem (
215
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
216
problemid TEXT REFERENCES problem (identifier) NOT NULL,
218
PRIMARY KEY (worksheetid, problemid)
221
CREATE TABLE problem_attempt (
222
problemid VARCHAR REFERENCES problem (identifier) NOT NULL,
228
223
loginid INT4 REFERENCES login (loginid) NOT NULL,
229
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
224
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
230
225
date TIMESTAMP NOT NULL,
231
attempt TEXT NOT NULL,
226
attempt VARCHAR NOT NULL,
232
227
complete BOOLEAN NOT NULL,
233
228
active BOOLEAN NOT NULL DEFAULT true,
234
PRIMARY KEY (loginid, ws_ex_id, date)
229
PRIMARY KEY (problemid,loginid,date)
237
CREATE TABLE exercise_save (
232
CREATE TABLE problem_save (
233
problemid INT4 REFERENCES problem (problemid) NOT NULL,
238
234
loginid INT4 REFERENCES login (loginid) NOT NULL,
239
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
235
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
240
236
date TIMESTAMP NOT NULL,
242
PRIMARY KEY (loginid, ws_ex_id)
237
text VARCHAR NOT NULL,
238
PRIMARY KEY (problemid,loginid)
241
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
243
-- TABLES FOR EXERCISES IN DATABASE --
245
244
CREATE TABLE test_suite (
246
suiteid SERIAL PRIMARY KEY,
247
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
245
suiteid SERIAL NOT NULL,
246
problemid TEXT REFERENCES problem (identifier) NOT NULL,
248
247
description TEXT,
249
PRIMARY KEY (problemid, suiteid)
254
252
CREATE TABLE test_case (
255
testid SERIAL PRIMARY KEY,
256
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
263
CREATE TABLE suite_variable (
264
varid SERIAL PRIMARY KEY,
253
testid SERIAL NOT NULL,
265
254
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
268
var_type TEXT NOT NULL,
272
CREATE TABLE test_case_part (
273
partid SERIAL PRIMARY KEY,
274
testid INT4 REFERENCES test_case (testid) NOT NULL,
275
part_type TEXT NOT NULL,
262
PRIMARY KEY (testid, suiteid)