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)),
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 TEXT NOT NULL CHECK (valid_url_name(year)),
50
url_name TEXT NOT NULL CHECK (valid_url_name(url_name)),
52
display_name TEXT NOT NULL,
53
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
54
'current', 'future')) DEFAULT 'current',
55
UNIQUE (year, url_name),
7
studentid VARCHAR -- may be null
59
10
CREATE TABLE offering (
60
11
offeringid SERIAL PRIMARY KEY NOT NULL,
61
subject INT4 REFERENCES subject (subjectid) NOT NULL,
62
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
65
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
66
worksheet_cutoff TIMESTAMP,
67
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
68
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
69
UNIQUE (subject, semesterid)
73
CREATE TABLE project_set (
74
projectsetid SERIAL PRIMARY KEY NOT NULL,
75
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
76
max_students_per_group INTEGER
12
subj_name VARCHAR NOT NULL,
13
subj_code VARCHAR NOT NULL,
14
year CHAR(4) NOT NULL,
15
semester CHAR(1) NOT NULL,
79
19
CREATE TABLE project (
80
20
projectid SERIAL PRIMARY KEY NOT NULL,
81
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
85
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
86
deadline TIMESTAMP NOT NULL
23
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
89
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
94
IF TG_OP = ''UPDATE'' THEN
95
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
99
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
100
PERFORM 1 FROM project, project_set
101
WHERE project_set.offeringid = oid AND
102
project.projectsetid = project_set.projectsetid AND
103
project.short_name = NEW.short_name;
105
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
109
' LANGUAGE 'plpgsql';
111
CREATE TRIGGER check_project_namespacing
112
BEFORE INSERT OR UPDATE ON project
113
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
115
27
CREATE TABLE project_group (
116
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
28
groupnm VARCHAR NOT NULL,
117
29
groupid SERIAL PRIMARY KEY NOT NULL,
118
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
30
offeringid INT4 REFERENCES offering (offeringid),
120
32
createdby INT4 REFERENCES login (loginid) NOT NULL,
121
33
epoch TIMESTAMP NOT NULL,
122
UNIQUE (projectsetid, groupnm)
34
UNIQUE (offeringid, groupnm)
125
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
130
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
131
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;
133
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
137
' LANGUAGE 'plpgsql';
139
CREATE TRIGGER check_group_namespacing
140
BEFORE INSERT OR UPDATE ON project_group
141
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
143
37
CREATE TABLE group_invitation (
144
38
loginid INT4 REFERENCES login (loginid) NOT NULL,
145
39
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
159
46
CREATE TABLE group_member (
160
47
loginid INT4 REFERENCES login (loginid),
161
48
groupid INT4 REFERENCES project_group (groupid),
49
projectid INT4 REFERENCES project (projectid),
50
UNIQUE (loginid,projectid),
162
51
PRIMARY KEY (loginid,groupid)
165
54
CREATE TABLE enrolment (
166
55
loginid INT4 REFERENCES login (loginid),
167
56
offeringid INT4 REFERENCES offering (offeringid),
168
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
169
'lecturer')) DEFAULT 'student',
171
58
special_result VARCHAR,
173
60
special_supp_result VARCHAR,
175
active BOOL NOT NULL DEFAULT true,
176
62
PRIMARY KEY (loginid,offeringid)
180
66
assessedid SERIAL PRIMARY KEY NOT NULL,
181
67
loginid INT4 REFERENCES login (loginid),
182
68
groupid INT4 REFERENCES project_group (groupid),
183
projectid INT4 REFERENCES project (projectid) NOT NULL,
184
69
-- exactly one of loginid and groupid must be non-null
185
70
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
186
71
OR (loginid IS NULL AND groupid IS NOT NULL))
189
CREATE UNIQUE INDEX assessed_loginid_key ON assessed(loginid, projectid) WHERE loginid IS NOT NULL;
190
CREATE UNIQUE INDEX assessed_groupid_key ON assessed(groupid, projectid) WHERE groupid IS NOT NULL;
192
74
CREATE TABLE project_extension (
193
extensionid SERIAL PRIMARY KEY,
194
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
75
assessedid INT4 REFERENCES assessed (assesedid) NOT NULL,
76
projectid INT4 REFERENCES project (projectid) NOT NULL,
77
deadline TIMESTAMP NOT NULL,
196
78
approver INT4 REFERENCES login (loginid) NOT NULL,
200
CREATE TABLE project_submission (
201
submissionid SERIAL PRIMARY KEY,
202
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
203
path VARCHAR NOT NULL,
204
revision INT4 NOT NULL,
205
date_submitted TIMESTAMP NOT NULL,
206
submitter INT4 REFERENCES login (loginid) NOT NULL
209
82
CREATE TABLE project_mark (
210
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
83
assessedid INT4 REFERENCES assessed (assesedid) NOT NULL,
84
projectid INT4 REFERENCES project (projectid) NOT NULL,
212
86
marker INT4 REFERENCES login (loginid) NOT NULL,
220
CREATE TABLE exercise (
221
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
224
description_xhtml_cache TEXT,
231
CREATE TABLE worksheet (
232
worksheetid SERIAL PRIMARY KEY,
233
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
234
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
237
data_xhtml_cache TEXT,
238
assessable BOOLEAN NOT NULL,
239
published BOOLEAN NOT NULL DEFAULT true,
240
seq_no INT4 NOT NULL,
241
format TEXT NOT NUll,
242
UNIQUE (offeringid, identifier)
245
CREATE TABLE worksheet_exercise (
246
ws_ex_id SERIAL PRIMARY KEY,
247
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
248
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
249
seq_no INT4 NOT NULL,
250
active BOOLEAN NOT NULL DEFAULT true,
251
optional BOOLEAN NOT NULL,
252
UNIQUE (worksheetid, exerciseid)
255
CREATE TABLE exercise_attempt (
93
CREATE TABLE problem (
94
problemid SERIAL PRIMARY KEY NOT NULL,
98
CREATE TABLE problem_tag (
99
problemid INT4 REFERENCES tutorial_problem (problemid),
100
tag VARCHAR NOT NULL,
102
standard BOOLEAN NOT NULL,
103
added_by INT4 REFERENCES login (loginid) NOT NULL,
104
date TIMESTAMP NOT NULL,
105
PRIMARY KEY (problemid,added_by,tag)
108
CREATE TABLE problem_test_case (
109
problemid INT4 REFERENCES problem (problemid) NOT NULL,
110
testcaseid SERIAL UNIQUE NOT NULL,
113
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
116
CREATE TABLE problem_test_case_tag (
117
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
118
tag VARCHAR NOT NULL,
120
standard BOOLEAN NOT NULL,
121
added_by INT4 REFERENCES login (loginid) NOT NULL,
122
date TIMESTAMP NOT NULL,
123
PRIMARY KEY (testcaseid,added_by,tag)
126
CREATE TABLE problem_attempt (
127
problemid INT4 REFERENCES problem (problemid) NOT NULL,
256
128
loginid INT4 REFERENCES login (loginid) NOT NULL,
257
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
258
129
date TIMESTAMP NOT NULL,
259
attempt TEXT NOT NULL,
130
attempt VARCHAR NOT NULL,
260
131
complete BOOLEAN NOT NULL,
261
active BOOLEAN NOT NULL DEFAULT true,
262
PRIMARY KEY (loginid, ws_ex_id, date)
132
PRIMARY KEY (problemid,loginid,date)
265
CREATE TABLE exercise_save (
135
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
137
CREATE TABLE problem_attempt_breakdown (
138
problemid INT4 REFERENCES problem (problemid) NOT NULL,
139
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
266
140
loginid INT4 REFERENCES login (loginid) NOT NULL,
267
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
268
141
date TIMESTAMP NOT NULL,
270
PRIMARY KEY (loginid, ws_ex_id)
273
CREATE TABLE test_suite (
274
suiteid SERIAL PRIMARY KEY,
275
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
282
CREATE TABLE test_case (
283
testid SERIAL PRIMARY KEY,
284
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
291
CREATE TABLE suite_variable (
292
varid SERIAL PRIMARY KEY,
293
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
296
var_type TEXT NOT NULL,
300
CREATE TABLE test_case_part (
301
partid SERIAL PRIMARY KEY,
302
testid INT4 REFERENCES test_case (testid) NOT NULL,
303
part_type TEXT NOT NULL,
145
CREATE TABLE problem_prerequisite (
146
parent INT4 REFERENCES problem (problemid) NOT NULL,
147
child INT4 REFERENCES problem (problemid) NOT NULL,
148
PRIMARY KEY (parent,child)