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
2
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
19
4
CREATE TABLE login (
20
5
loginid SERIAL PRIMARY KEY NOT NULL,
21
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
6
login VARCHAR UNIQUE NOT NULL,
23
8
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
24
9
'enabled', 'disabled'))
25
10
DEFAULT 'no_agreement',
26
admin BOOLEAN NOT NULL DEFAULT false,
11
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
13
'lecturer', 'admin')),
27
14
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
28
15
nick VARCHAR NOT NULL,
29
16
pass_exp TIMESTAMP,
46
30
subjectid SERIAL PRIMARY KEY NOT NULL,
47
31
subj_code VARCHAR UNIQUE NOT NULL,
48
32
subj_name VARCHAR NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
33
subj_short_name VARCHAR UNIQUE NOT NULL,
52
37
CREATE TABLE semester (
53
38
semesterid SERIAL PRIMARY KEY NOT NULL,
54
year TEXT NOT NULL CHECK (valid_url_name(year)),
55
url_name TEXT NOT NULL CHECK (valid_url_name(url_name)),
57
display_name TEXT NOT NULL,
58
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
59
'current', 'future')) DEFAULT 'current',
60
UNIQUE (year, url_name),
39
year CHAR(4) NOT NULL,
40
semester CHAR(1) NOT NULL,
42
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();
64
60
CREATE TABLE offering (
65
61
offeringid SERIAL PRIMARY KEY NOT NULL,
66
62
subject INT4 REFERENCES subject (subjectid) NOT NULL,
67
63
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
70
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
71
worksheet_cutoff TIMESTAMP,
72
64
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
73
65
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
74
66
UNIQUE (subject, semesterid)
80
72
CREATE TABLE project_set (
81
73
projectsetid SERIAL PRIMARY KEY NOT NULL,
82
74
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
83
max_students_per_group INTEGER
75
max_students_per_group INTEGER NOT NULL DEFAULT 4
86
78
CREATE TABLE project (
87
79
projectid SERIAL PRIMARY KEY NOT NULL,
88
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
92
82
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
93
deadline TIMESTAMP NOT NULL
96
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
101
IF TG_OP = ''UPDATE'' THEN
102
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
106
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
107
PERFORM 1 FROM project, project_set
108
WHERE project_set.offeringid = oid AND
109
project.projectsetid = project_set.projectsetid AND
110
project.short_name = NEW.short_name;
112
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
116
' LANGUAGE 'plpgsql';
118
CREATE TRIGGER check_project_namespacing
119
BEFORE INSERT OR UPDATE ON project
120
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
122
86
CREATE TABLE project_group (
123
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
87
groupnm VARCHAR NOT NULL,
124
88
groupid SERIAL PRIMARY KEY NOT NULL,
125
89
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
176
138
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();
179
158
CREATE TABLE assessed (
180
159
assessedid SERIAL PRIMARY KEY NOT NULL,
181
160
loginid INT4 REFERENCES login (loginid),
186
165
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
168
CREATE TABLE project_extension (
193
extensionid SERIAL PRIMARY KEY,
194
169
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
170
deadline TIMESTAMP NOT NULL,
196
171
approver INT4 REFERENCES login (loginid) NOT NULL,
200
175
CREATE TABLE project_submission (
201
submissionid SERIAL PRIMARY KEY,
202
176
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
203
177
path VARCHAR NOT NULL,
204
revision INT4 NOT NULL,
205
date_submitted TIMESTAMP NOT NULL,
206
submitter INT4 REFERENCES login (loginid) NOT NULL
178
revision INT4 NOT NULL
209
181
CREATE TABLE project_mark (
233
203
CREATE TABLE worksheet (
234
204
worksheetid SERIAL PRIMARY KEY,
235
205
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
236
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
206
identifier TEXT NOT NULL,
237
207
name TEXT NOT NULL,
238
208
data TEXT NOT NULL,
239
data_xhtml_cache TEXT,
240
209
assessable BOOLEAN NOT NULL,
241
published BOOLEAN NOT NULL DEFAULT true,
242
210
seq_no INT4 NOT NULL,
243
211
format TEXT NOT NUll,
244
212
UNIQUE (offeringid, identifier)
247
CREATE TABLE worksheet_exercise (
248
ws_ex_id SERIAL PRIMARY KEY,
215
CREATE TABLE worksheet_problem (
216
ws_prob_id SERIAL PRIMARY KEY,
249
217
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
250
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
218
problemid TEXT REFERENCES problem (identifier) NOT NULL,
251
219
seq_no INT4 NOT NULL,
252
220
active BOOLEAN NOT NULL DEFAULT true,
253
221
optional BOOLEAN NOT NULL,
254
UNIQUE (worksheetid, exerciseid)
222
UNIQUE (worksheetid, problemid)
257
CREATE TABLE exercise_attempt (
225
CREATE TABLE problem_attempt (
258
226
loginid INT4 REFERENCES login (loginid) NOT NULL,
259
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
227
ws_prob_id INT4 REFERENCES worksheet_problem (ws_prob_id) NOT NULL,
260
228
date TIMESTAMP NOT NULL,
261
229
attempt TEXT NOT NULL,
262
230
complete BOOLEAN NOT NULL,
263
231
active BOOLEAN NOT NULL DEFAULT true,
264
PRIMARY KEY (loginid, ws_ex_id, date)
232
PRIMARY KEY (loginid, ws_prob_id, date)
267
CREATE TABLE exercise_save (
235
CREATE TABLE problem_save (
268
236
loginid INT4 REFERENCES login (loginid) NOT NULL,
269
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
237
ws_prob_id INT4 REFERENCES worksheet_problem (ws_prob_id) NOT NULL,
270
238
date TIMESTAMP NOT NULL,
271
239
text TEXT NOT NULL,
272
PRIMARY KEY (loginid, ws_ex_id)
240
PRIMARY KEY (loginid, ws_prob_id)
275
243
CREATE TABLE test_suite (
276
244
suiteid SERIAL PRIMARY KEY,
277
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
245
problemid TEXT REFERENCES problem (identifier) NOT NULL,
278
246
description TEXT,