3
CREATE OR REPLACE FUNCTION valid_url_name(name text) RETURNS boolean AS
6
RETURN name ~ E'^[a-z0-9][a-z0-9_\+\.\-]*$';
10
CREATE OR REPLACE FUNCTION valid_login_name(name text) RETURNS boolean AS
13
RETURN name ~ E'^[a-z0-9][a-z0-9@_\+\.\-]*$';
15
$$ LANGUAGE 'plpgsql';
17
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
19
1
CREATE TABLE login (
2
login VARCHAR UNIQUE NOT NULL,
20
3
loginid SERIAL PRIMARY KEY NOT NULL,
21
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
4
unixid INT UNIQUE NOT NULL, -- unix user id
23
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
24
'enabled', 'disabled'))
25
DEFAULT 'no_agreement',
26
admin BOOLEAN NOT NULL DEFAULT false,
27
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
28
nick VARCHAR NOT NULL,
34
fullname VARCHAR NOT NULL,
35
studentid VARCHAR, -- may be null
40
CREATE TABLE subject (
41
subjectid SERIAL PRIMARY KEY NOT NULL,
42
subj_code VARCHAR UNIQUE NOT NULL,
43
subj_name VARCHAR NOT NULL,
44
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
47
CREATE TABLE semester (
48
semesterid SERIAL PRIMARY KEY NOT NULL,
49
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
50
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
51
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
52
'current', 'future')) DEFAULT 'current',
53
UNIQUE (year, semester)
9
studentid VARCHAR -- may be null
56
12
CREATE TABLE offering (
57
13
offeringid SERIAL PRIMARY KEY NOT NULL,
58
subject INT4 REFERENCES subject (subjectid) NOT NULL,
59
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
62
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
63
worksheet_cutoff TIMESTAMP,
64
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
65
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
66
UNIQUE (subject, semesterid)
70
CREATE TABLE project_set (
71
projectsetid SERIAL PRIMARY KEY NOT NULL,
72
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
73
max_students_per_group INTEGER
14
subj_name VARCHAR NOT NULL,
15
subj_code VARCHAR NOT NULL,
16
year CHAR(4) NOT NULL,
17
semester CHAR(1) NOT NULL,
76
21
CREATE TABLE project (
77
22
projectid SERIAL PRIMARY KEY NOT NULL,
78
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
82
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
83
deadline TIMESTAMP NOT NULL
25
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
86
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
91
IF TG_OP = ''UPDATE'' THEN
92
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
96
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
97
PERFORM 1 FROM project, project_set
98
WHERE project_set.offeringid = oid AND
99
project.projectsetid = project_set.projectsetid AND
100
project.short_name = NEW.short_name;
102
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
106
' LANGUAGE 'plpgsql';
108
CREATE TRIGGER check_project_namespacing
109
BEFORE INSERT OR UPDATE ON project
110
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
112
29
CREATE TABLE project_group (
113
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
30
groupnm VARCHAR NOT NULL,
114
31
groupid SERIAL PRIMARY KEY NOT NULL,
115
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
32
offeringid INT4 REFERENCES offering (offeringid),
117
34
createdby INT4 REFERENCES login (loginid) NOT NULL,
118
35
epoch TIMESTAMP NOT NULL,
119
UNIQUE (projectsetid, groupnm)
36
UNIQUE (offeringid, groupnm)
122
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
127
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
128
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;
130
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
134
' LANGUAGE 'plpgsql';
136
CREATE TRIGGER check_group_namespacing
137
BEFORE INSERT OR UPDATE ON project_group
138
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
140
39
CREATE TABLE group_invitation (
141
40
loginid INT4 REFERENCES login (loginid) NOT NULL,
142
41
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
156
48
CREATE TABLE group_member (
157
49
loginid INT4 REFERENCES login (loginid),
158
50
groupid INT4 REFERENCES project_group (groupid),
51
projectid INT4 REFERENCES project (projectid),
52
UNIQUE (loginid,projectid),
159
53
PRIMARY KEY (loginid,groupid)
162
56
CREATE TABLE enrolment (
163
57
loginid INT4 REFERENCES login (loginid),
164
58
offeringid INT4 REFERENCES offering (offeringid),
165
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
166
'lecturer')) DEFAULT 'student',
168
60
special_result VARCHAR,
170
62
special_supp_result VARCHAR,
172
active BOOL NOT NULL DEFAULT true,
173
64
PRIMARY KEY (loginid,offeringid)
177
68
assessedid SERIAL PRIMARY KEY NOT NULL,
178
69
loginid INT4 REFERENCES login (loginid),
179
70
groupid INT4 REFERENCES project_group (groupid),
180
projectid INT4 REFERENCES project (projectid) NOT NULL,
181
71
-- exactly one of loginid and groupid must be non-null
182
72
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
183
73
OR (loginid IS NULL AND groupid IS NOT NULL))
186
CREATE UNIQUE INDEX assessed_loginid_key ON assessed(loginid, projectid) WHERE loginid IS NOT NULL;
187
CREATE UNIQUE INDEX assessed_groupid_key ON assessed(groupid, projectid) WHERE groupid IS NOT NULL;
189
76
CREATE TABLE project_extension (
190
extensionid SERIAL PRIMARY KEY,
191
77
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
78
projectid INT4 REFERENCES project (projectid) NOT NULL,
192
79
deadline TIMESTAMP NOT NULL,
193
80
approver INT4 REFERENCES login (loginid) NOT NULL,
197
CREATE TABLE project_submission (
198
submissionid SERIAL PRIMARY KEY,
199
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
200
path VARCHAR NOT NULL,
201
revision INT4 NOT NULL,
202
date_submitted TIMESTAMP NOT NULL,
203
submitter INT4 REFERENCES login (loginid) NOT NULL
206
84
CREATE TABLE project_mark (
207
85
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
86
projectid INT4 REFERENCES project (projectid) NOT NULL,
209
88
marker INT4 REFERENCES login (loginid) NOT NULL,
217
CREATE TABLE exercise (
218
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
221
description_xhtml_cache TEXT,
228
CREATE TABLE worksheet (
229
worksheetid SERIAL PRIMARY KEY,
230
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
231
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
234
data_xhtml_cache TEXT,
235
assessable BOOLEAN NOT NULL,
236
published BOOLEAN NOT NULL DEFAULT true,
237
seq_no INT4 NOT NULL,
238
format TEXT NOT NUll,
239
UNIQUE (offeringid, identifier)
242
CREATE TABLE worksheet_exercise (
243
ws_ex_id SERIAL PRIMARY KEY,
244
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
245
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
246
seq_no INT4 NOT NULL,
247
active BOOLEAN NOT NULL DEFAULT true,
248
optional BOOLEAN NOT NULL,
249
UNIQUE (worksheetid, exerciseid)
252
CREATE TABLE exercise_attempt (
95
CREATE TABLE problem (
96
problemid SERIAL PRIMARY KEY NOT NULL,
100
CREATE TABLE problem_tag (
101
problemid INT4 REFERENCES problem (problemid),
102
tag VARCHAR NOT NULL,
104
standard BOOLEAN NOT NULL,
105
added_by INT4 REFERENCES login (loginid) NOT NULL,
106
date TIMESTAMP NOT NULL,
107
PRIMARY KEY (problemid,added_by,tag)
110
CREATE TABLE problem_test_case (
111
problemid INT4 REFERENCES problem (problemid) NOT NULL,
112
testcaseid SERIAL UNIQUE NOT NULL,
115
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
118
CREATE TABLE problem_test_case_tag (
119
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
120
tag VARCHAR NOT NULL,
122
standard BOOLEAN NOT NULL,
123
added_by INT4 REFERENCES login (loginid) NOT NULL,
124
date TIMESTAMP NOT NULL,
125
PRIMARY KEY (testcaseid,added_by,tag)
128
CREATE TABLE problem_attempt (
129
problemid INT4 REFERENCES problem (problemid) NOT NULL,
253
130
loginid INT4 REFERENCES login (loginid) NOT NULL,
254
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
255
131
date TIMESTAMP NOT NULL,
256
attempt TEXT NOT NULL,
132
attempt VARCHAR NOT NULL,
257
133
complete BOOLEAN NOT NULL,
258
active BOOLEAN NOT NULL DEFAULT true,
259
PRIMARY KEY (loginid, ws_ex_id, date)
134
PRIMARY KEY (problemid,loginid,date)
262
CREATE TABLE exercise_save (
137
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
139
CREATE TABLE problem_attempt_breakdown (
140
problemid INT4 REFERENCES problem (problemid) NOT NULL,
141
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
263
142
loginid INT4 REFERENCES login (loginid) NOT NULL,
264
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
265
143
date TIMESTAMP NOT NULL,
267
PRIMARY KEY (loginid, ws_ex_id)
270
CREATE TABLE test_suite (
271
suiteid SERIAL PRIMARY KEY,
272
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
279
CREATE TABLE test_case (
280
testid SERIAL PRIMARY KEY,
281
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
288
CREATE TABLE suite_variable (
289
varid SERIAL PRIMARY KEY,
290
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
293
var_type TEXT NOT NULL,
297
CREATE TABLE test_case_part (
298
partid SERIAL PRIMARY KEY,
299
testid INT4 REFERENCES test_case (testid) NOT NULL,
300
part_type TEXT NOT NULL,
147
CREATE TABLE problem_prerequisite (
148
parent INT4 REFERENCES problem (problemid) NOT NULL,
149
child INT4 REFERENCES problem (problemid) NOT NULL,
150
PRIMARY KEY (parent,child)