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
1
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
19
3
CREATE TABLE login (
20
4
loginid SERIAL PRIMARY KEY NOT NULL,
21
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
5
login VARCHAR UNIQUE NOT NULL,
23
7
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
24
8
'enabled', 'disabled'))
25
9
DEFAULT 'no_agreement',
26
admin BOOLEAN NOT NULL DEFAULT false,
10
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
12
'lecturer', 'admin')),
27
13
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
28
14
nick VARCHAR NOT NULL,
29
15
pass_exp TIMESTAMP,
46
29
subjectid SERIAL PRIMARY KEY NOT NULL,
47
30
subj_code VARCHAR UNIQUE NOT NULL,
48
31
subj_name VARCHAR NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
32
subj_short_name VARCHAR UNIQUE NOT NULL,
52
36
CREATE TABLE semester (
53
37
semesterid SERIAL PRIMARY KEY 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',
38
year CHAR(4) NOT NULL,
39
semester CHAR(1) NOT NULL,
58
41
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();
61
59
CREATE TABLE offering (
62
60
offeringid SERIAL PRIMARY KEY NOT NULL,
63
61
subject INT4 REFERENCES subject (subjectid) NOT NULL,
64
62
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
67
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
68
worksheet_cutoff TIMESTAMP,
69
63
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
70
64
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
71
65
UNIQUE (subject, semesterid)
77
71
CREATE TABLE project_set (
78
72
projectsetid SERIAL PRIMARY KEY NOT NULL,
79
73
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
80
max_students_per_group INTEGER
74
max_students_per_group INTEGER NOT NULL DEFAULT 4
83
77
CREATE TABLE project (
84
78
projectid SERIAL PRIMARY KEY NOT NULL,
85
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
89
81
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();
119
85
CREATE TABLE project_group (
120
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
86
groupnm VARCHAR NOT NULL,
121
87
groupid SERIAL PRIMARY KEY NOT NULL,
122
88
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
173
137
PRIMARY KEY (loginid,offeringid)
140
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
145
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
146
IF NOT active AND NEW.active = true THEN
147
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
151
' LANGUAGE 'plpgsql';
153
CREATE TRIGGER confirm_active_semester
154
BEFORE INSERT OR UPDATE ON enrolment
155
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
176
157
CREATE TABLE assessed (
177
158
assessedid SERIAL PRIMARY KEY NOT NULL,
178
159
loginid INT4 REFERENCES login (loginid),
219
CREATE TABLE exercise (
220
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
192
--TODO: Add in a field for the user-friendly identifier
193
CREATE TABLE problem (
194
identifier VARCHAR PRIMARY KEY NOT NULL,
222
196
description TEXT,
223
description_xhtml_cache TEXT,
203
--TODO: Link worksheets to offerings
230
204
CREATE TABLE worksheet (
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 (
205
worksheetid SERIAL PRIMARY KEY NOT NULL,
206
subject VARCHAR NOT NULL,
207
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
208
identifier VARCHAR NOT NULL,
211
UNIQUE (subject, identifier)
214
CREATE TABLE worksheet_problem (
215
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
216
problemid TEXT REFERENCES problem (identifier) NOT NULL,
218
PRIMARY KEY (worksheetid, problemid)
221
CREATE TABLE problem_attempt (
222
problemid VARCHAR REFERENCES problem (identifier) NOT NULL,
255
223
loginid INT4 REFERENCES login (loginid) NOT NULL,
256
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
224
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
257
225
date TIMESTAMP NOT NULL,
258
attempt TEXT NOT NULL,
226
attempt VARCHAR NOT NULL,
259
227
complete BOOLEAN NOT NULL,
260
228
active BOOLEAN NOT NULL DEFAULT true,
261
PRIMARY KEY (loginid, ws_ex_id, date)
229
PRIMARY KEY (problemid,loginid,date)
264
CREATE TABLE exercise_save (
232
CREATE TABLE problem_save (
233
problemid INT4 REFERENCES problem (problemid) NOT NULL,
265
234
loginid INT4 REFERENCES login (loginid) NOT NULL,
266
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
235
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
267
236
date TIMESTAMP NOT NULL,
269
PRIMARY KEY (loginid, ws_ex_id)
237
text VARCHAR NOT NULL,
238
PRIMARY KEY (problemid,loginid)
241
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
243
-- TABLES FOR EXERCISES IN DATABASE --
272
244
CREATE TABLE test_suite (
273
suiteid SERIAL PRIMARY KEY,
274
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
245
suiteid SERIAL NOT NULL,
246
problemid TEXT REFERENCES problem (identifier) NOT NULL,
275
247
description TEXT,
249
PRIMARY KEY (problemid, suiteid)
281
252
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,
253
testid SERIAL NOT NULL,
292
254
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,
262
PRIMARY KEY (testid, suiteid)