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 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)
7
studentid VARCHAR -- may be null
56
10
CREATE TABLE offering (
57
11
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
12
subj_name VARCHAR NOT NULL,
13
subj_code VARCHAR NOT NULL,
14
year CHAR(4) NOT NULL,
15
semester CHAR(1) NOT NULL,
76
19
CREATE TABLE project (
77
20
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
23
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
27
CREATE TABLE project_group (
113
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
28
groupnm VARCHAR NOT NULL,
114
29
groupid SERIAL PRIMARY KEY NOT NULL,
115
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
30
offeringid INT4 REFERENCES offering (offeringid),
117
32
createdby INT4 REFERENCES login (loginid) NOT NULL,
118
33
epoch TIMESTAMP NOT NULL,
119
UNIQUE (projectsetid, groupnm)
34
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
37
CREATE TABLE group_invitation (
141
38
loginid INT4 REFERENCES login (loginid) NOT NULL,
142
39
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
156
46
CREATE TABLE group_member (
157
47
loginid INT4 REFERENCES login (loginid),
158
48
groupid INT4 REFERENCES project_group (groupid),
49
projectid INT4 REFERENCES project (projectid),
50
UNIQUE (loginid,projectid),
159
51
PRIMARY KEY (loginid,groupid)
162
54
CREATE TABLE enrolment (
163
55
loginid INT4 REFERENCES login (loginid),
164
56
offeringid INT4 REFERENCES offering (offeringid),
165
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
166
'lecturer')) DEFAULT 'student',
168
58
special_result VARCHAR,
170
60
special_supp_result VARCHAR,
172
active BOOL NOT NULL DEFAULT true,
173
62
PRIMARY KEY (loginid,offeringid)
177
66
assessedid SERIAL PRIMARY KEY NOT NULL,
178
67
loginid INT4 REFERENCES login (loginid),
179
68
groupid INT4 REFERENCES project_group (groupid),
180
projectid INT4 REFERENCES project (projectid) NOT NULL,
181
69
-- exactly one of loginid and groupid must be non-null
182
70
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
183
71
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
74
CREATE TABLE project_extension (
190
extensionid SERIAL PRIMARY KEY,
191
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
75
assessedid INT4 REFERENCES assessed (assesedid) NOT NULL,
76
projectid INT4 REFERENCES project (projectid) NOT NULL,
192
77
deadline TIMESTAMP NOT NULL,
193
78
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
82
CREATE TABLE project_mark (
207
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
83
assessedid INT4 REFERENCES assessed (assesedid) NOT NULL,
84
projectid INT4 REFERENCES project (projectid) NOT NULL,
209
86
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 (
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,
253
128
loginid INT4 REFERENCES login (loginid) NOT NULL,
254
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
255
129
date TIMESTAMP NOT NULL,
256
attempt TEXT NOT NULL,
130
attempt VARCHAR NOT NULL,
257
131
complete BOOLEAN NOT NULL,
258
active BOOLEAN NOT NULL DEFAULT true,
259
PRIMARY KEY (loginid, ws_ex_id, date)
132
PRIMARY KEY (problemid,loginid,date)
262
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,
263
140
loginid INT4 REFERENCES login (loginid) NOT NULL,
264
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
265
141
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,
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)