3
-- Check that the provided name is sane for use in URLs.
4
CREATE OR REPLACE FUNCTION valid_url_name(name text) RETURNS boolean AS
7
RETURN name ~ E'^[a-z0-9][a-z0-9_\+\.\-]*$';
11
-- Just like valid_url_name, except that @ is permitted (so we can use a
12
-- reasonable subset of email addresses as usernames).
13
CREATE OR REPLACE FUNCTION valid_login_name(name text) RETURNS boolean AS
16
RETURN name ~ E'^[a-z0-9][a-z0-9@_\+\.\-]*$';
18
$$ LANGUAGE 'plpgsql';
20
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
1
22
CREATE TABLE login (
2
23
loginid SERIAL PRIMARY KEY NOT NULL,
3
login VARCHAR UNIQUE NOT NULL,
24
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
5
26
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
6
'enabled', 'disabled')),
7
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
10
unixid INT UNIQUE NOT NULL, -- unix user id
27
'enabled', 'disabled'))
28
DEFAULT 'no_agreement',
29
admin BOOLEAN NOT NULL DEFAULT false,
30
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
11
31
nick VARCHAR NOT NULL,
12
32
pass_exp TIMESTAMP,
13
33
acct_exp TIMESTAMP,
22
45
CREATE TABLE subject (
23
46
subjectid SERIAL PRIMARY KEY NOT NULL,
24
47
subj_code VARCHAR UNIQUE NOT NULL,
25
48
subj_name VARCHAR NOT NULL,
26
subj_short_name VARCHAR, -- may be null
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
52
CREATE TABLE semester (
53
semesterid SERIAL PRIMARY KEY NOT NULL,
54
year TEXT NOT NULL CHECK (valid_url_name(year)),
55
semester TEXT NOT NULL CHECK (valid_url_name(semester)),
56
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
57
'current', 'future')) DEFAULT 'current',
58
UNIQUE (year, semester)
30
61
CREATE TABLE offering (
31
62
offeringid SERIAL PRIMARY KEY NOT NULL,
32
63
subject INT4 REFERENCES subject (subjectid) NOT NULL,
33
year CHAR(4) NOT NULL,
34
semester CHAR(1) NOT NULL,
35
UNIQUE (subject, year, semester)
64
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
67
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
68
worksheet_cutoff TIMESTAMP,
69
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
70
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
71
UNIQUE (subject, semesterid)
74
-- Projects and groups
75
-- -------------------
77
CREATE TABLE project_set (
78
projectsetid SERIAL PRIMARY KEY NOT NULL,
79
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
80
max_students_per_group INTEGER
38
83
CREATE TABLE project (
39
84
projectid SERIAL PRIMARY KEY NOT NULL,
42
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
85
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
89
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
90
deadline TIMESTAMP NOT NULL
93
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
98
IF TG_OP = ''UPDATE'' THEN
99
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
103
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
104
PERFORM 1 FROM project, project_set
105
WHERE project_set.offeringid = oid AND
106
project.projectsetid = project_set.projectsetid AND
107
project.short_name = NEW.short_name;
109
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
113
' LANGUAGE 'plpgsql';
115
CREATE TRIGGER check_project_namespacing
116
BEFORE INSERT OR UPDATE ON project
117
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
46
119
CREATE TABLE project_group (
47
groupnm VARCHAR NOT NULL,
120
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
48
121
groupid SERIAL PRIMARY KEY NOT NULL,
49
offeringid INT4 REFERENCES offering (offeringid),
122
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
51
124
createdby INT4 REFERENCES login (loginid) NOT NULL,
52
125
epoch TIMESTAMP NOT NULL,
53
UNIQUE (offeringid, groupnm)
126
UNIQUE (projectsetid, groupnm)
129
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
134
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
135
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;
137
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
141
' LANGUAGE 'plpgsql';
143
CREATE TRIGGER check_group_namespacing
144
BEFORE INSERT OR UPDATE ON project_group
145
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
56
147
CREATE TABLE group_invitation (
57
148
loginid INT4 REFERENCES login (loginid) NOT NULL,
58
149
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
65
156
CREATE TABLE group_member (
66
157
loginid INT4 REFERENCES login (loginid),
67
158
groupid INT4 REFERENCES project_group (groupid),
68
projectid INT4 REFERENCES project (projectid),
69
UNIQUE (loginid,projectid),
70
159
PRIMARY KEY (loginid,groupid)
73
162
CREATE TABLE enrolment (
74
163
loginid INT4 REFERENCES login (loginid),
75
164
offeringid INT4 REFERENCES offering (offeringid),
165
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
166
'lecturer')) DEFAULT 'student',
77
168
special_result VARCHAR,
79
170
special_supp_result VARCHAR,
172
active BOOL NOT NULL DEFAULT true,
81
173
PRIMARY KEY (loginid,offeringid)
85
177
assessedid SERIAL PRIMARY KEY NOT NULL,
86
178
loginid INT4 REFERENCES login (loginid),
87
179
groupid INT4 REFERENCES project_group (groupid),
180
projectid INT4 REFERENCES project (projectid) NOT NULL,
88
181
-- exactly one of loginid and groupid must be non-null
89
182
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
90
183
OR (loginid IS NULL AND groupid IS NOT NULL))
186
-- We must use conditional constraints here -- NULL != NULL.
187
CREATE UNIQUE INDEX assessed_loginid_key ON assessed(loginid, projectid) WHERE loginid IS NOT NULL;
188
CREATE UNIQUE INDEX assessed_groupid_key ON assessed(groupid, projectid) WHERE groupid IS NOT NULL;
93
190
CREATE TABLE project_extension (
191
extensionid SERIAL PRIMARY KEY,
94
192
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
95
projectid INT4 REFERENCES project (projectid) NOT NULL,
96
deadline TIMESTAMP NOT NULL,
97
194
approver INT4 REFERENCES login (loginid) NOT NULL,
101
198
CREATE TABLE project_submission (
199
submissionid SERIAL PRIMARY KEY,
102
200
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
103
projectid INT4 REFERENCES project (projectid) NOT NULL,
104
201
path VARCHAR NOT NULL,
105
revision INT4 NOT NULL
202
revision INT4 NOT NULL,
203
date_submitted TIMESTAMP NOT NULL,
204
submitter INT4 REFERENCES login (loginid) NOT NULL
108
207
CREATE TABLE project_mark (
109
208
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
110
projectid INT4 REFERENCES project (projectid) NOT NULL,
111
209
componentid INT4,
112
210
marker INT4 REFERENCES login (loginid) NOT NULL,
119
CREATE TABLE problem (
120
problemid SERIAL PRIMARY KEY NOT NULL,
121
identifier VARCHAR UNIQUE NOT NULL,
219
CREATE TABLE exercise (
220
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
223
description_xhtml_cache TEXT,
125
230
CREATE TABLE worksheet (
126
worksheetid SERIAL PRIMARY KEY NOT NULL,
127
subject VARCHAR NOT NULL,
128
identifier VARCHAR NOT NULL,
131
UNIQUE (subject, identifier)
134
CREATE TABLE worksheet_problem (
135
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
136
problemid INT4 REFERENCES problem (problemid) NOT NULL,
138
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
CREATE TABLE problem_attempt (
170
problemid INT4 REFERENCES problem (problemid) NOT NULL,
231
worksheetid SERIAL PRIMARY KEY,
232
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
233
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
236
data_xhtml_cache TEXT,
237
assessable BOOLEAN NOT NULL,
238
published BOOLEAN NOT NULL DEFAULT true,
239
seq_no INT4 NOT NULL,
240
format TEXT NOT NUll,
241
UNIQUE (offeringid, identifier)
244
CREATE TABLE worksheet_exercise (
245
ws_ex_id SERIAL PRIMARY KEY,
246
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
247
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
248
seq_no INT4 NOT NULL,
249
active BOOLEAN NOT NULL DEFAULT true,
250
optional BOOLEAN NOT NULL,
251
UNIQUE (worksheetid, exerciseid)
254
CREATE TABLE exercise_attempt (
171
255
loginid INT4 REFERENCES login (loginid) NOT NULL,
256
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
172
257
date TIMESTAMP NOT NULL,
173
attempt VARCHAR NOT NULL,
258
attempt TEXT NOT NULL,
174
259
complete BOOLEAN NOT NULL,
175
PRIMARY KEY (problemid,loginid,date)
178
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)
260
active BOOLEAN NOT NULL DEFAULT true,
261
PRIMARY KEY (loginid, ws_ex_id, date)
264
CREATE TABLE exercise_save (
265
loginid INT4 REFERENCES login (loginid) NOT NULL,
266
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
267
date TIMESTAMP NOT NULL,
269
PRIMARY KEY (loginid, ws_ex_id)
272
CREATE TABLE test_suite (
273
suiteid SERIAL PRIMARY KEY,
274
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
281
CREATE TABLE test_case (
282
testid SERIAL PRIMARY KEY,
283
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
290
CREATE TABLE suite_variable (
291
varid SERIAL PRIMARY KEY,
292
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
295
var_type TEXT NOT NULL,
299
CREATE TABLE test_case_part (
300
partid SERIAL PRIMARY KEY,
301
testid INT4 REFERENCES test_case (testid) NOT NULL,
302
part_type TEXT NOT NULL,