22
29
CREATE TABLE subject (
23
30
subjectid SERIAL PRIMARY KEY NOT NULL,
24
31
subj_code VARCHAR UNIQUE NOT NULL,
25
32
subj_name VARCHAR NOT NULL,
26
subj_short_name VARCHAR, -- may be null
33
subj_short_name VARCHAR UNIQUE NOT NULL,
37
CREATE TABLE semester (
38
semesterid SERIAL PRIMARY KEY NOT NULL,
39
year CHAR(4) NOT NULL,
40
semester CHAR(1) NOT NULL,
42
UNIQUE (year, semester)
45
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
48
IF OLD.active = true AND NEW.active = false THEN
49
UPDATE enrolment SET active=false WHERE offeringid IN (
50
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
56
CREATE TRIGGER deactivate_semester_enrolments
57
AFTER UPDATE ON semester
58
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
30
60
CREATE TABLE offering (
31
61
offeringid SERIAL PRIMARY KEY NOT NULL,
32
62
subject INT4 REFERENCES subject (subjectid) NOT NULL,
33
year CHAR(4) NOT NULL,
34
semester CHAR(1) NOT NULL,
35
UNIQUE (subject, year, semester)
63
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
64
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
65
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
66
UNIQUE (subject, semesterid)
69
-- Projects and groups
70
-- -------------------
72
CREATE TABLE project_set (
73
projectsetid SERIAL PRIMARY KEY NOT NULL,
74
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
75
max_students_per_group INTEGER NOT NULL DEFAULT 4
38
78
CREATE TABLE project (
39
79
projectid SERIAL PRIMARY KEY NOT NULL,
42
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
82
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
46
86
CREATE TABLE project_group (
47
87
groupnm VARCHAR NOT NULL,
48
88
groupid SERIAL PRIMARY KEY NOT NULL,
49
offeringid INT4 REFERENCES offering (offeringid),
89
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
51
91
createdby INT4 REFERENCES login (loginid) NOT NULL,
52
92
epoch TIMESTAMP NOT NULL,
53
UNIQUE (offeringid, groupnm)
93
UNIQUE (projectsetid, groupnm)
96
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
101
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
102
PERFORM 1 FROM project_group, project_set WHERE project_set.offeringid = oid AND project_group.projectsetid = project_set.projectsetid AND project_group.groupnm = NEW.groupnm;
104
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
108
' LANGUAGE 'plpgsql';
110
CREATE TRIGGER check_group_namespacing
111
BEFORE INSERT OR UPDATE ON project_group
112
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
56
114
CREATE TABLE group_invitation (
57
115
loginid INT4 REFERENCES login (loginid) NOT NULL,
58
116
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
79
135
special_supp_result VARCHAR,
137
active BOOL NOT NULL DEFAULT true,
81
138
PRIMARY KEY (loginid,offeringid)
141
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
146
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
147
IF NOT active AND NEW.active = true THEN
148
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
152
' LANGUAGE 'plpgsql';
154
CREATE TRIGGER confirm_active_semester
155
BEFORE INSERT OR UPDATE ON enrolment
156
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
84
158
CREATE TABLE assessed (
85
159
assessedid SERIAL PRIMARY KEY NOT NULL,
86
160
loginid INT4 REFERENCES login (loginid),
87
161
groupid INT4 REFERENCES project_group (groupid),
162
projectid INT4 REFERENCES project (projectid) NOT NULL,
88
163
-- exactly one of loginid and groupid must be non-null
89
164
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
90
165
OR (loginid IS NULL AND groupid IS NOT NULL))
119
CREATE TABLE problem (
120
problemid SERIAL PRIMARY KEY NOT NULL,
121
identifier VARCHAR UNIQUE NOT NULL,
193
CREATE TABLE exercise (
194
identifier TEXT PRIMARY KEY,
125
203
CREATE TABLE worksheet (
126
worksheetid SERIAL PRIMARY KEY NOT NULL,
127
subject VARCHAR NOT NULL,
128
identifier VARCHAR NOT NULL,
131
UNIQUE (subject, identifier)
134
CREATE TABLE worksheet_problem (
135
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
136
problemid INT4 REFERENCES problem (problemid) NOT NULL,
138
PRIMARY KEY (worksheetid, problemid)
141
CREATE TABLE problem_tag (
142
problemid INT4 REFERENCES problem (problemid),
143
tag VARCHAR NOT NULL,
145
standard BOOLEAN NOT NULL,
146
added_by INT4 REFERENCES login (loginid) NOT NULL,
147
date TIMESTAMP NOT NULL,
148
PRIMARY KEY (problemid,added_by,tag)
151
CREATE TABLE problem_test_case (
152
problemid INT4 REFERENCES problem (problemid) NOT NULL,
153
testcaseid SERIAL UNIQUE NOT NULL,
156
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
159
CREATE TABLE problem_test_case_tag (
160
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
161
tag VARCHAR NOT NULL,
163
standard BOOLEAN NOT NULL,
164
added_by INT4 REFERENCES login (loginid) NOT NULL,
165
date TIMESTAMP NOT NULL,
166
PRIMARY KEY (testcaseid,added_by,tag)
169
CREATE TABLE problem_attempt (
170
problemid INT4 REFERENCES problem (problemid) NOT NULL,
204
worksheetid SERIAL PRIMARY KEY,
205
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
206
identifier TEXT NOT NULL,
209
assessable BOOLEAN NOT NULL,
210
seq_no INT4 NOT NULL,
211
format TEXT NOT NUll,
212
UNIQUE (offeringid, identifier)
215
CREATE TABLE worksheet_exercise (
216
ws_ex_id SERIAL PRIMARY KEY,
217
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
218
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
219
seq_no INT4 NOT NULL,
220
active BOOLEAN NOT NULL DEFAULT true,
221
optional BOOLEAN NOT NULL,
222
UNIQUE (worksheetid, exerciseid)
225
CREATE TABLE exercise_attempt (
171
226
loginid INT4 REFERENCES login (loginid) NOT NULL,
227
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
172
228
date TIMESTAMP NOT NULL,
173
attempt VARCHAR NOT NULL,
229
attempt TEXT NOT NULL,
174
230
complete BOOLEAN NOT NULL,
175
PRIMARY KEY (problemid,loginid,date)
178
CREATE TABLE problem_save (
179
problemid INT4 REFERENCES problem (problemid) NOT NULL,
180
loginid INT4 REFERENCES login (loginid) NOT NULL,
181
date TIMESTAMP NOT NULL,
182
text VARCHAR NOT NULL,
183
PRIMARY KEY (problemid,loginid)
186
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
188
CREATE TABLE problem_attempt_breakdown (
189
problemid INT4 REFERENCES problem (problemid) NOT NULL,
190
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
191
loginid INT4 REFERENCES login (loginid) NOT NULL,
192
date TIMESTAMP NOT NULL,
196
CREATE TABLE problem_prerequisite (
197
parent INT4 REFERENCES problem (problemid) NOT NULL,
198
child INT4 REFERENCES problem (problemid) NOT NULL,
199
PRIMARY KEY (parent,child)
231
active BOOLEAN NOT NULL DEFAULT true,
232
PRIMARY KEY (loginid, ws_ex_id, date)
235
CREATE TABLE exercise_save (
236
loginid INT4 REFERENCES login (loginid) NOT NULL,
237
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
238
date TIMESTAMP NOT NULL,
240
PRIMARY KEY (loginid, ws_ex_id)
243
CREATE TABLE test_suite (
244
suiteid SERIAL PRIMARY KEY,
245
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
252
CREATE TABLE test_case (
253
testid SERIAL PRIMARY KEY,
254
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
261
CREATE TABLE suite_variable (
262
varid SERIAL PRIMARY KEY,
263
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
266
var_type TEXT NOT NULL,
270
CREATE TABLE test_case_part (
271
partid SERIAL PRIMARY KEY,
272
testid INT4 REFERENCES test_case (testid) NOT NULL,
273
part_type TEXT NOT NULL,