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
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
19
1
CREATE TABLE login (
20
2
loginid SERIAL PRIMARY KEY NOT NULL,
21
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
3
login VARCHAR UNIQUE NOT NULL,
23
5
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
24
'enabled', 'disabled'))
25
DEFAULT 'no_agreement',
26
admin BOOLEAN NOT NULL DEFAULT false,
27
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
6
'enabled', 'disabled')),
7
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
10
unixid INT UNIQUE NOT NULL, -- unix user id
28
11
nick VARCHAR NOT NULL,
29
12
pass_exp TIMESTAMP,
30
13
acct_exp TIMESTAMP,
43
CREATE TABLE subject (
44
subjectid SERIAL PRIMARY KEY NOT NULL,
45
subj_code VARCHAR UNIQUE NOT NULL,
46
subj_name VARCHAR NOT NULL,
47
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
50
CREATE TABLE semester (
51
semesterid SERIAL PRIMARY KEY NOT NULL,
52
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
53
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
54
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
55
'current', 'future')) DEFAULT 'current',
56
UNIQUE (year, semester)
59
22
CREATE TABLE offering (
60
23
offeringid SERIAL PRIMARY KEY NOT NULL,
61
subject INT4 REFERENCES subject (subjectid) NOT NULL,
62
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
65
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
66
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
67
UNIQUE (subject, semesterid)
71
CREATE TABLE project_set (
72
projectsetid SERIAL PRIMARY KEY NOT NULL,
73
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
74
max_students_per_group INTEGER
24
subj_name VARCHAR NOT NULL,
25
subj_code VARCHAR NOT NULL,
26
year CHAR(4) NOT NULL,
27
semester CHAR(1) NOT NULL,
77
31
CREATE TABLE project (
78
32
projectid SERIAL PRIMARY KEY NOT NULL,
79
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
83
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
84
deadline TIMESTAMP NOT NULL
35
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
87
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
92
IF TG_OP = ''UPDATE'' THEN
93
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
97
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
98
PERFORM 1 FROM project, project_set
99
WHERE project_set.offeringid = oid AND
100
project.projectsetid = project_set.projectsetid AND
101
project.short_name = NEW.short_name;
103
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
107
' LANGUAGE 'plpgsql';
109
CREATE TRIGGER check_project_namespacing
110
BEFORE INSERT OR UPDATE ON project
111
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
113
39
CREATE TABLE project_group (
114
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
40
groupnm VARCHAR NOT NULL,
115
41
groupid SERIAL PRIMARY KEY NOT NULL,
116
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
42
offeringid INT4 REFERENCES offering (offeringid),
118
44
createdby INT4 REFERENCES login (loginid) NOT NULL,
119
45
epoch TIMESTAMP NOT NULL,
120
UNIQUE (projectsetid, groupnm)
46
UNIQUE (offeringid, groupnm)
123
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
128
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
129
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;
131
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
135
' LANGUAGE 'plpgsql';
137
CREATE TRIGGER check_group_namespacing
138
BEFORE INSERT OR UPDATE ON project_group
139
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
141
49
CREATE TABLE group_invitation (
142
50
loginid INT4 REFERENCES login (loginid) NOT NULL,
143
51
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
154
58
CREATE TABLE group_member (
155
59
loginid INT4 REFERENCES login (loginid),
156
60
groupid INT4 REFERENCES project_group (groupid),
61
projectid INT4 REFERENCES project (projectid),
62
UNIQUE (loginid,projectid),
157
63
PRIMARY KEY (loginid,groupid)
160
66
CREATE TABLE enrolment (
161
67
loginid INT4 REFERENCES login (loginid),
162
68
offeringid INT4 REFERENCES offering (offeringid),
163
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
164
'lecturer')) DEFAULT 'student',
166
70
special_result VARCHAR,
168
72
special_supp_result VARCHAR,
170
active BOOL NOT NULL DEFAULT true,
171
74
PRIMARY KEY (loginid,offeringid)
175
78
assessedid SERIAL PRIMARY KEY NOT NULL,
176
79
loginid INT4 REFERENCES login (loginid),
177
80
groupid INT4 REFERENCES project_group (groupid),
178
projectid INT4 REFERENCES project (projectid) NOT NULL,
179
81
-- exactly one of loginid and groupid must be non-null
180
82
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
181
83
OR (loginid IS NULL AND groupid IS NOT NULL))
184
86
CREATE TABLE project_extension (
185
extensionid SERIAL PRIMARY KEY,
186
87
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
88
projectid INT4 REFERENCES project (projectid) NOT NULL,
187
89
deadline TIMESTAMP NOT NULL,
188
90
approver INT4 REFERENCES login (loginid) NOT NULL,
192
94
CREATE TABLE project_submission (
193
submissionid SERIAL PRIMARY KEY,
194
95
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
96
projectid INT4 REFERENCES project (projectid) NOT NULL,
195
97
path VARCHAR NOT NULL,
196
revision INT4 NOT NULL,
197
date_submitted TIMESTAMP NOT NULL,
198
submitter INT4 REFERENCES login (loginid) NOT NULL
98
revision INT4 NOT NULL
201
101
CREATE TABLE project_mark (
202
102
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
103
projectid INT4 REFERENCES project (projectid) NOT NULL,
203
104
componentid INT4,
204
105
marker INT4 REFERENCES login (loginid) NOT NULL,
211
CREATE TABLE exercise (
212
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
112
CREATE TABLE problem (
113
problemid SERIAL PRIMARY KEY NOT NULL,
114
identifier VARCHAR UNIQUE NOT NULL,
221
118
CREATE TABLE worksheet (
222
worksheetid SERIAL PRIMARY KEY,
223
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
224
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
227
assessable BOOLEAN NOT NULL,
228
seq_no INT4 NOT NULL,
229
format TEXT NOT NUll,
230
UNIQUE (offeringid, identifier)
233
CREATE TABLE worksheet_exercise (
234
ws_ex_id SERIAL PRIMARY KEY,
235
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
236
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
237
seq_no INT4 NOT NULL,
238
active BOOLEAN NOT NULL DEFAULT true,
239
optional BOOLEAN NOT NULL,
240
UNIQUE (worksheetid, exerciseid)
243
CREATE TABLE exercise_attempt (
119
worksheetid SERIAL PRIMARY KEY NOT NULL,
120
subject VARCHAR NOT NULL,
121
identifier VARCHAR NOT NULL,
124
UNIQUE (subject, identifier)
127
CREATE TABLE worksheet_problem (
128
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
129
problemid INT4 REFERENCES problem (problemid) NOT NULL,
131
PRIMARY KEY (worksheetid, problemid)
134
CREATE TABLE problem_tag (
135
problemid INT4 REFERENCES problem (problemid),
136
tag VARCHAR NOT NULL,
138
standard BOOLEAN NOT NULL,
139
added_by INT4 REFERENCES login (loginid) NOT NULL,
140
date TIMESTAMP NOT NULL,
141
PRIMARY KEY (problemid,added_by,tag)
144
CREATE TABLE problem_test_case (
145
problemid INT4 REFERENCES problem (problemid) NOT NULL,
146
testcaseid SERIAL UNIQUE NOT NULL,
149
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
152
CREATE TABLE problem_test_case_tag (
153
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
154
tag VARCHAR NOT NULL,
156
standard BOOLEAN NOT NULL,
157
added_by INT4 REFERENCES login (loginid) NOT NULL,
158
date TIMESTAMP NOT NULL,
159
PRIMARY KEY (testcaseid,added_by,tag)
162
CREATE TABLE problem_attempt (
163
problemid INT4 REFERENCES problem (problemid) NOT NULL,
244
164
loginid INT4 REFERENCES login (loginid) NOT NULL,
245
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
246
165
date TIMESTAMP NOT NULL,
247
attempt TEXT NOT NULL,
166
attempt VARCHAR NOT NULL,
248
167
complete BOOLEAN NOT NULL,
249
active BOOLEAN NOT NULL DEFAULT true,
250
PRIMARY KEY (loginid, ws_ex_id, date)
253
CREATE TABLE exercise_save (
254
loginid INT4 REFERENCES login (loginid) NOT NULL,
255
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
256
date TIMESTAMP NOT NULL,
258
PRIMARY KEY (loginid, ws_ex_id)
261
CREATE TABLE test_suite (
262
suiteid SERIAL PRIMARY KEY,
263
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
270
CREATE TABLE test_case (
271
testid SERIAL PRIMARY KEY,
272
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
279
CREATE TABLE suite_variable (
280
varid SERIAL PRIMARY KEY,
281
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
284
var_type TEXT NOT NULL,
288
CREATE TABLE test_case_part (
289
partid SERIAL PRIMARY KEY,
290
testid INT4 REFERENCES test_case (testid) NOT NULL,
291
part_type TEXT NOT NULL,
168
PRIMARY KEY (problemid,loginid,date)
171
CREATE TABLE problem_save (
172
problemid INT4 REFERENCES problem (problemid) NOT NULL,
173
loginid INT4 REFERENCES login (loginid) NOT NULL,
174
date TIMESTAMP NOT NULL,
175
text VARCHAR NOT NULL,
176
PRIMARY KEY (problemid,loginid)
179
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
181
CREATE TABLE problem_attempt_breakdown (
182
problemid INT4 REFERENCES problem (problemid) NOT NULL,
183
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
184
loginid INT4 REFERENCES login (loginid) NOT NULL,
185
date TIMESTAMP NOT NULL,
189
CREATE TABLE problem_prerequisite (
190
parent INT4 REFERENCES problem (problemid) NOT NULL,
191
child INT4 REFERENCES problem (problemid) NOT NULL,
192
PRIMARY KEY (parent,child)