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
193
CREATE TABLE problem (
120
problemid SERIAL PRIMARY KEY NOT NULL,
121
identifier VARCHAR UNIQUE NOT NULL,
194
identifier TEXT PRIMARY KEY,
125
203
CREATE TABLE worksheet (
126
worksheetid SERIAL PRIMARY KEY NOT NULL,
127
subject VARCHAR NOT NULL,
204
worksheetid SERIAL PRIMARY KEY,
205
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
128
206
identifier VARCHAR NOT NULL,
129
207
assessable BOOLEAN,
131
UNIQUE (subject, identifier)
209
UNIQUE (offeringid, identifier)
134
212
CREATE TABLE worksheet_problem (
135
213
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
136
problemid INT4 REFERENCES problem (problemid) NOT NULL,
214
problemid TEXT REFERENCES problem (identifier) NOT NULL,
137
215
optional BOOLEAN,
138
216
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
219
CREATE TABLE problem_attempt (
170
problemid INT4 REFERENCES problem (problemid) NOT NULL,
220
problemid TEXT REFERENCES problem (identifier) NOT NULL,
171
221
loginid INT4 REFERENCES login (loginid) NOT NULL,
222
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
172
223
date TIMESTAMP NOT NULL,
173
224
attempt VARCHAR NOT NULL,
174
225
complete BOOLEAN NOT NULL,
175
PRIMARY KEY (problemid,loginid,date)
226
active BOOLEAN NOT NULL DEFAULT true,
227
PRIMARY KEY (problemid,loginid,worksheetid,date)
178
230
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
problemid TEXT REFERENCES problem (identifier) NOT NULL,
232
loginid INT4 REFERENCES login (loginid) NOT NULL,
233
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
234
date TIMESTAMP NOT NULL,
236
PRIMARY KEY (problemid,loginid, worksheetid)
239
CREATE TABLE test_suite (
240
suiteid SERIAL PRIMARY KEY,
241
problemid TEXT REFERENCES problem (identifier) NOT NULL,
248
CREATE TABLE test_case (
249
testid SERIAL PRIMARY KEY,
250
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
257
CREATE TABLE suite_variables (
258
varid SERIAL PRIMARY KEY,
259
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
262
var_type TEXT NOT NULL,
266
CREATE TABLE test_case_parts (
267
partid SERIAL PRIMARY KEY,
268
testid INT4 REFERENCES test_case (testid) NOT NULL,
269
part_type TEXT NOT NULL,