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';
1
20
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
3
22
CREATE TABLE login (
4
23
loginid SERIAL PRIMARY KEY NOT NULL,
5
login VARCHAR UNIQUE NOT NULL,
24
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
7
26
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
8
27
'enabled', 'disabled'))
9
28
DEFAULT 'no_agreement',
10
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
12
'lecturer', 'admin')),
29
admin BOOLEAN NOT NULL DEFAULT false,
13
30
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
14
31
nick VARCHAR NOT NULL,
15
32
pass_exp TIMESTAMP,
29
46
subjectid SERIAL PRIMARY KEY NOT NULL,
30
47
subj_code VARCHAR UNIQUE NOT NULL,
31
48
subj_name VARCHAR NOT NULL,
32
subj_short_name VARCHAR UNIQUE NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
36
52
CREATE TABLE semester (
37
53
semesterid SERIAL PRIMARY KEY NOT NULL,
38
year CHAR(4) NOT NULL,
39
semester CHAR(1) NOT NULL,
54
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
55
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
56
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
57
'current', 'future')) DEFAULT 'current',
41
58
UNIQUE (year, semester)
44
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
47
IF OLD.active = true AND NEW.active = false THEN
48
UPDATE enrolment SET active=false WHERE offeringid IN (
49
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
55
CREATE TRIGGER deactivate_semester_enrolments
56
AFTER UPDATE ON semester
57
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
59
61
CREATE TABLE offering (
60
62
offeringid SERIAL PRIMARY KEY NOT NULL,
61
63
subject INT4 REFERENCES subject (subjectid) NOT NULL,
62
64
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
63
67
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
64
68
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
65
69
UNIQUE (subject, semesterid)
71
75
CREATE TABLE project_set (
72
76
projectsetid SERIAL PRIMARY KEY NOT NULL,
73
77
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
74
max_students_per_group INTEGER NOT NULL DEFAULT 4
78
max_students_per_group INTEGER
77
81
CREATE TABLE project (
78
82
projectid SERIAL PRIMARY KEY NOT NULL,
83
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
81
87
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
88
deadline TIMESTAMP NOT NULL
91
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
96
IF TG_OP = ''UPDATE'' THEN
97
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
101
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
102
PERFORM 1 FROM project, project_set
103
WHERE project_set.offeringid = oid AND
104
project.projectsetid = project_set.projectsetid AND
105
project.short_name = NEW.short_name;
107
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
111
' LANGUAGE 'plpgsql';
113
CREATE TRIGGER check_project_namespacing
114
BEFORE INSERT OR UPDATE ON project
115
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
85
117
CREATE TABLE project_group (
86
groupnm VARCHAR NOT NULL,
118
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
87
119
groupid SERIAL PRIMARY KEY NOT NULL,
88
120
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
193
CREATE TABLE problem (
194
problemid SERIAL PRIMARY KEY NOT NULL,
195
identifier VARCHAR UNIQUE NOT NULL,
213
CREATE TABLE exercise (
214
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
199
223
CREATE TABLE worksheet (
200
worksheetid SERIAL PRIMARY KEY NOT NULL,
201
subject VARCHAR NOT NULL,
202
identifier VARCHAR NOT NULL,
205
UNIQUE (subject, identifier)
208
CREATE TABLE worksheet_problem (
209
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
210
problemid INT4 REFERENCES problem (problemid) NOT NULL,
212
PRIMARY KEY (worksheetid, problemid)
215
CREATE TABLE problem_tag (
216
problemid INT4 REFERENCES problem (problemid),
217
tag VARCHAR NOT NULL,
219
standard BOOLEAN NOT NULL,
220
added_by INT4 REFERENCES login (loginid) NOT NULL,
221
date TIMESTAMP NOT NULL,
222
PRIMARY KEY (problemid,added_by,tag)
225
CREATE TABLE problem_test_case (
226
problemid INT4 REFERENCES problem (problemid) NOT NULL,
227
testcaseid SERIAL UNIQUE NOT NULL,
230
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
233
CREATE TABLE problem_test_case_tag (
234
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
235
tag VARCHAR NOT NULL,
237
standard BOOLEAN NOT NULL,
238
added_by INT4 REFERENCES login (loginid) NOT NULL,
239
date TIMESTAMP NOT NULL,
240
PRIMARY KEY (testcaseid,added_by,tag)
243
CREATE TABLE problem_attempt (
244
problemid INT4 REFERENCES problem (problemid) NOT NULL,
224
worksheetid SERIAL PRIMARY KEY,
225
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
226
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
229
assessable BOOLEAN NOT NULL,
230
seq_no INT4 NOT NULL,
231
format TEXT NOT NUll,
232
UNIQUE (offeringid, identifier)
235
CREATE TABLE worksheet_exercise (
236
ws_ex_id SERIAL PRIMARY KEY,
237
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
238
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
239
seq_no INT4 NOT NULL,
240
active BOOLEAN NOT NULL DEFAULT true,
241
optional BOOLEAN NOT NULL,
242
UNIQUE (worksheetid, exerciseid)
245
CREATE TABLE exercise_attempt (
245
246
loginid INT4 REFERENCES login (loginid) NOT NULL,
247
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
246
248
date TIMESTAMP NOT NULL,
247
attempt VARCHAR NOT NULL,
249
attempt TEXT NOT NULL,
248
250
complete BOOLEAN NOT NULL,
249
251
active BOOLEAN NOT NULL DEFAULT true,
250
PRIMARY KEY (problemid,loginid,date)
253
CREATE TABLE problem_save (
254
problemid INT4 REFERENCES problem (problemid) NOT NULL,
255
loginid INT4 REFERENCES login (loginid) NOT NULL,
256
date TIMESTAMP NOT NULL,
257
text VARCHAR NOT NULL,
258
PRIMARY KEY (problemid,loginid)
261
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
263
CREATE TABLE problem_attempt_breakdown (
264
problemid INT4 REFERENCES problem (problemid) NOT NULL,
265
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
266
loginid INT4 REFERENCES login (loginid) NOT NULL,
267
date TIMESTAMP NOT NULL,
271
CREATE TABLE problem_prerequisite (
272
parent INT4 REFERENCES problem (problemid) NOT NULL,
273
child INT4 REFERENCES problem (problemid) NOT NULL,
274
PRIMARY KEY (parent,child)
252
PRIMARY KEY (loginid, ws_ex_id, date)
255
CREATE TABLE exercise_save (
256
loginid INT4 REFERENCES login (loginid) NOT NULL,
257
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
258
date TIMESTAMP NOT NULL,
260
PRIMARY KEY (loginid, ws_ex_id)
263
CREATE TABLE test_suite (
264
suiteid SERIAL PRIMARY KEY,
265
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
272
CREATE TABLE test_case (
273
testid SERIAL PRIMARY KEY,
274
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
281
CREATE TABLE suite_variable (
282
varid SERIAL PRIMARY KEY,
283
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
286
var_type TEXT NOT NULL,
290
CREATE TABLE test_case_part (
291
partid SERIAL PRIMARY KEY,
292
testid INT4 REFERENCES test_case (testid) NOT NULL,
293
part_type TEXT NOT NULL,