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';
2
20
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
4
22
CREATE TABLE login (
5
23
loginid SERIAL PRIMARY KEY NOT NULL,
6
login VARCHAR UNIQUE NOT NULL,
24
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
8
26
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
9
27
'enabled', 'disabled'))
10
28
DEFAULT 'no_agreement',
11
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
13
'lecturer', 'admin')),
29
admin BOOLEAN NOT NULL DEFAULT false,
14
30
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
15
31
nick VARCHAR NOT NULL,
16
32
pass_exp TIMESTAMP,
30
46
subjectid SERIAL PRIMARY KEY NOT NULL,
31
47
subj_code VARCHAR UNIQUE NOT NULL,
32
48
subj_name VARCHAR NOT NULL,
33
subj_short_name VARCHAR UNIQUE NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
37
52
CREATE TABLE semester (
38
53
semesterid SERIAL PRIMARY KEY NOT NULL,
39
year CHAR(4) NOT NULL,
40
semester CHAR(1) 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',
42
58
UNIQUE (year, semester)
45
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
48
IF OLD.active = true AND NEW.active = false THEN
49
UPDATE enrolment SET active=false WHERE offeringid IN (
50
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
56
CREATE TRIGGER deactivate_semester_enrolments
57
AFTER UPDATE ON semester
58
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
60
61
CREATE TABLE offering (
61
62
offeringid SERIAL PRIMARY KEY NOT NULL,
62
63
subject INT4 REFERENCES subject (subjectid) NOT NULL,
63
64
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
67
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
68
worksheet_cutoff TIMESTAMP,
64
69
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
65
70
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
66
71
UNIQUE (subject, semesterid)
72
77
CREATE TABLE project_set (
73
78
projectsetid SERIAL PRIMARY KEY NOT NULL,
74
79
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
75
max_students_per_group INTEGER NOT NULL DEFAULT 4
80
max_students_per_group INTEGER
78
83
CREATE TABLE project (
79
84
projectid SERIAL PRIMARY KEY NOT NULL,
85
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
82
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();
86
119
CREATE TABLE project_group (
87
groupnm VARCHAR NOT NULL,
120
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
88
121
groupid SERIAL PRIMARY KEY NOT NULL,
89
122
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
138
173
PRIMARY KEY (loginid,offeringid)
141
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
146
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
147
IF NOT active AND NEW.active = true THEN
148
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
152
' LANGUAGE 'plpgsql';
154
CREATE TRIGGER confirm_active_semester
155
BEFORE INSERT OR UPDATE ON enrolment
156
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
158
176
CREATE TABLE assessed (
159
177
assessedid SERIAL PRIMARY KEY NOT NULL,
160
178
loginid INT4 REFERENCES login (loginid),
165
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;
168
190
CREATE TABLE project_extension (
191
extensionid SERIAL PRIMARY KEY,
169
192
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
170
deadline TIMESTAMP NOT NULL,
171
194
approver INT4 REFERENCES login (loginid) NOT NULL,
175
198
CREATE TABLE project_submission (
199
submissionid SERIAL PRIMARY KEY,
176
200
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
177
201
path VARCHAR NOT NULL,
178
revision INT4 NOT NULL
202
revision INT4 NOT NULL,
203
date_submitted TIMESTAMP NOT NULL,
204
submitter INT4 REFERENCES login (loginid) NOT NULL
181
207
CREATE TABLE project_mark (
203
230
CREATE TABLE worksheet (
204
231
worksheetid SERIAL PRIMARY KEY,
205
232
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
206
identifier TEXT NOT NULL,
233
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
207
234
name TEXT NOT NULL,
208
235
data TEXT NOT NULL,
236
data_xhtml_cache TEXT,
209
237
assessable BOOLEAN NOT NULL,
238
published BOOLEAN NOT NULL DEFAULT true,
210
239
seq_no INT4 NOT NULL,
211
240
format TEXT NOT NUll,
212
241
UNIQUE (offeringid, identifier)
215
CREATE TABLE worksheet_problem (
216
ws_prob_id SERIAL PRIMARY KEY,
244
CREATE TABLE worksheet_exercise (
245
ws_ex_id SERIAL PRIMARY KEY,
217
246
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
218
problemid TEXT REFERENCES problem (identifier) NOT NULL,
247
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
219
248
seq_no INT4 NOT NULL,
220
249
active BOOLEAN NOT NULL DEFAULT true,
221
250
optional BOOLEAN NOT NULL,
222
UNIQUE (worksheetid, problemid)
251
UNIQUE (worksheetid, exerciseid)
225
CREATE TABLE problem_attempt (
254
CREATE TABLE exercise_attempt (
226
255
loginid INT4 REFERENCES login (loginid) NOT NULL,
227
ws_prob_id INT4 REFERENCES worksheet_problem (ws_prob_id) NOT NULL,
256
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
228
257
date TIMESTAMP NOT NULL,
229
258
attempt TEXT NOT NULL,
230
259
complete BOOLEAN NOT NULL,
231
260
active BOOLEAN NOT NULL DEFAULT true,
232
PRIMARY KEY (loginid, ws_prob_id, date)
261
PRIMARY KEY (loginid, ws_ex_id, date)
235
CREATE TABLE problem_save (
264
CREATE TABLE exercise_save (
236
265
loginid INT4 REFERENCES login (loginid) NOT NULL,
237
ws_prob_id INT4 REFERENCES worksheet_problem (ws_prob_id) NOT NULL,
266
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
238
267
date TIMESTAMP NOT NULL,
239
268
text TEXT NOT NULL,
240
PRIMARY KEY (loginid, ws_prob_id)
269
PRIMARY KEY (loginid, ws_ex_id)
243
272
CREATE TABLE test_suite (
244
273
suiteid SERIAL PRIMARY KEY,
245
problemid TEXT REFERENCES problem (identifier) NOT NULL,
274
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
246
275
description TEXT,