1
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
2
login VARCHAR UNIQUE NOT NULL,
4
3
loginid SERIAL PRIMARY KEY NOT NULL,
5
login VARCHAR UNIQUE NOT NULL,
7
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
8
'enabled', 'disabled'))
9
DEFAULT 'no_agreement',
10
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
12
'lecturer', 'admin')),
13
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
14
nick VARCHAR NOT NULL,
20
fullname VARCHAR NOT NULL,
21
studentid VARCHAR, -- may be null
26
CREATE TABLE subject (
27
subjectid SERIAL PRIMARY KEY NOT NULL,
28
subj_code VARCHAR UNIQUE NOT NULL,
29
subj_name VARCHAR NOT NULL,
30
subj_short_name VARCHAR UNIQUE NOT NULL,
34
CREATE TABLE semester (
35
semesterid SERIAL PRIMARY KEY NOT NULL,
8
studentid VARCHAR -- may be null
11
CREATE TABLE offering (
12
offeringid SERIAL PRIMARY KEY NOT NULL,
13
subj_name VARCHAR NOT NULL,
14
subj_code VARCHAR NOT NULL,
36
15
year CHAR(4) NOT NULL,
37
16
semester CHAR(1) NOT NULL,
39
UNIQUE (year, semester)
42
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
45
IF OLD.active = true AND NEW.active = false THEN
46
UPDATE enrolment SET active=false WHERE offeringid IN (
47
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
53
CREATE TRIGGER deactivate_semester_enrolments
54
AFTER UPDATE ON semester
55
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
57
CREATE TABLE offering (
58
offeringid SERIAL PRIMARY KEY NOT NULL,
59
subject INT4 REFERENCES subject (subjectid) NOT NULL,
60
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
61
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
62
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
63
UNIQUE (subject, semesterid)
67
CREATE TABLE project_set (
68
projectsetid SERIAL PRIMARY KEY NOT NULL,
69
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
70
max_students_per_group INTEGER NOT NULL DEFAULT 4
73
20
CREATE TABLE project (
74
21
projectid SERIAL PRIMARY KEY NOT NULL,
77
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
24
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
81
28
CREATE TABLE project_group (
82
29
groupnm VARCHAR NOT NULL,
83
30
groupid SERIAL PRIMARY KEY NOT NULL,
84
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
31
offeringid INT4 REFERENCES offering (offeringid),
86
33
createdby INT4 REFERENCES login (loginid) NOT NULL,
87
34
epoch TIMESTAMP NOT NULL,
88
UNIQUE (projectsetid, groupnm)
35
UNIQUE (offeringid, groupnm)
91
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
96
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
97
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;
99
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
103
' LANGUAGE 'plpgsql';
105
CREATE TRIGGER check_group_namespacing
106
BEFORE INSERT OR UPDATE ON project_group
107
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
109
38
CREATE TABLE group_invitation (
110
39
loginid INT4 REFERENCES login (loginid) NOT NULL,
111
40
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
134
61
special_supp_result VARCHAR,
136
active BOOL NOT NULL DEFAULT true,
137
63
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();
157
66
CREATE TABLE assessed (
158
67
assessedid SERIAL PRIMARY KEY NOT NULL,
159
68
loginid INT4 REFERENCES login (loginid),
160
69
groupid INT4 REFERENCES project_group (groupid),
161
projectid INT4 REFERENCES project (projectid) NOT NULL,
162
70
-- exactly one of loginid and groupid must be non-null
163
71
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
164
72
OR (loginid IS NULL AND groupid IS NOT NULL))
167
75
CREATE TABLE project_extension (
168
76
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
77
projectid INT4 REFERENCES project (projectid) NOT NULL,
169
78
deadline TIMESTAMP NOT NULL,
170
79
approver INT4 REFERENCES login (loginid) NOT NULL,
174
CREATE TABLE project_submission (
175
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
176
path VARCHAR NOT NULL,
177
revision INT4 NOT NULL
180
83
CREATE TABLE project_mark (
181
84
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
85
projectid INT4 REFERENCES project (projectid) NOT NULL,
183
87
marker INT4 REFERENCES login (loginid) NOT NULL,
190
--TODO: Add in a field for the user-friendly identifier
191
94
CREATE TABLE problem (
192
identifier VARCHAR PRIMARY KEY NOT NULL,
201
--TODO: Link worksheets to offerings
202
CREATE TABLE worksheet (
203
worksheetid SERIAL PRIMARY KEY NOT NULL,
204
subject VARCHAR NOT NULL,
205
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
206
identifier VARCHAR NOT NULL,
209
UNIQUE (subject, identifier)
212
CREATE TABLE worksheet_problem (
213
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
214
problemid TEXT REFERENCES problem (identifier) NOT NULL,
216
PRIMARY KEY (worksheetid, problemid)
95
problemid SERIAL PRIMARY KEY NOT NULL,
99
CREATE TABLE problem_tag (
100
problemid INT4 REFERENCES problem (problemid),
101
tag VARCHAR NOT NULL,
103
standard BOOLEAN NOT NULL,
104
added_by INT4 REFERENCES login (loginid) NOT NULL,
105
date TIMESTAMP NOT NULL,
106
PRIMARY KEY (problemid,added_by,tag)
109
CREATE TABLE problem_test_case (
110
problemid INT4 REFERENCES problem (problemid) NOT NULL,
111
testcaseid SERIAL UNIQUE NOT NULL,
114
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
117
CREATE TABLE problem_test_case_tag (
118
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
119
tag VARCHAR NOT NULL,
121
standard BOOLEAN NOT NULL,
122
added_by INT4 REFERENCES login (loginid) NOT NULL,
123
date TIMESTAMP NOT NULL,
124
PRIMARY KEY (testcaseid,added_by,tag)
219
127
CREATE TABLE problem_attempt (
220
problemid VARCHAR REFERENCES problem (identifier) NOT NULL,
128
problemid INT4 REFERENCES problem (problemid) NOT NULL,
221
129
loginid INT4 REFERENCES login (loginid) NOT NULL,
222
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
223
130
date TIMESTAMP NOT NULL,
224
131
attempt VARCHAR NOT NULL,
225
132
complete BOOLEAN NOT NULL,
226
active BOOLEAN NOT NULL DEFAULT true,
227
133
PRIMARY KEY (problemid,loginid,date)
230
CREATE TABLE problem_save (
231
problemid INT4 REFERENCES problem (problemid) NOT NULL,
232
loginid INT4 REFERENCES login (loginid) NOT NULL,
233
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
234
date TIMESTAMP NOT NULL,
235
text VARCHAR NOT NULL,
236
PRIMARY KEY (problemid,loginid)
239
136
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
241
CREATE TABLE test_suite (
242
suiteid SERIAL NOT NULL,
243
problemid TEXT REFERENCES problem (identifier) NOT NULL,
246
PRIMARY KEY (problemid, suiteid)
249
CREATE TABLE test_case (
250
testid SERIAL NOT NULL,
251
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
259
PRIMARY KEY (testid, suiteid)
138
CREATE TABLE problem_attempt_breakdown (
139
problemid INT4 REFERENCES problem (problemid) NOT NULL,
140
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
141
loginid INT4 REFERENCES login (loginid) NOT NULL,
142
date TIMESTAMP NOT NULL,
146
CREATE TABLE problem_prerequisite (
147
parent INT4 REFERENCES problem (problemid) NOT NULL,
148
child INT4 REFERENCES problem (problemid) NOT NULL,
149
PRIMARY KEY (parent,child)