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;
1
-- We need a users database to do authorization, manage groups, &c
6
DROP TABLE group_members;
11
login VARCHAR UNIQUE NOT NULL,
20
12
loginid SERIAL PRIMARY KEY NOT NULL,
21
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
23
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,
28
nick VARCHAR NOT NULL,
34
fullname VARCHAR NOT NULL,
35
15
studentid VARCHAR, -- may be null
40
CREATE TABLE subject (
41
subjectid SERIAL PRIMARY KEY NOT NULL,
42
subj_code VARCHAR UNIQUE NOT NULL,
43
subj_name VARCHAR NOT NULL,
44
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
47
CREATE TABLE semester (
48
semesterid SERIAL PRIMARY KEY NOT NULL,
49
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
50
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
51
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
52
'current', 'future')) DEFAULT 'current',
53
UNIQUE (year, semester)
56
CREATE TABLE offering (
57
offeringid SERIAL PRIMARY KEY NOT NULL,
58
subject INT4 REFERENCES subject (subjectid) NOT NULL,
59
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
62
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
63
worksheet_cutoff TIMESTAMP,
64
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
65
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
66
UNIQUE (subject, semesterid)
70
CREATE TABLE project_set (
71
projectsetid SERIAL PRIMARY KEY NOT NULL,
72
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
73
max_students_per_group INTEGER
76
CREATE TABLE project (
77
projectid SERIAL PRIMARY KEY NOT NULL,
78
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
82
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
83
deadline TIMESTAMP NOT NULL
86
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
91
IF TG_OP = ''UPDATE'' THEN
92
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
96
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
97
PERFORM 1 FROM project, project_set
98
WHERE project_set.offeringid = oid AND
99
project.projectsetid = project_set.projectsetid AND
100
project.short_name = NEW.short_name;
102
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
106
' LANGUAGE 'plpgsql';
108
CREATE TRIGGER check_project_namespacing
109
BEFORE INSERT OR UPDATE ON project
110
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
112
CREATE TABLE project_group (
113
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
19
group VARCHAR NOT NULL,
114
20
groupid SERIAL PRIMARY KEY NOT NULL,
115
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
21
offeringid INT4 REFERENCES offerings (offeringid),
117
createdby INT4 REFERENCES login (loginid) NOT NULL,
118
epoch TIMESTAMP NOT NULL,
119
UNIQUE (projectsetid, groupnm)
23
UNIQUE (offeringid, group)
122
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
127
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
128
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;
130
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
134
' LANGUAGE 'plpgsql';
136
CREATE TRIGGER check_group_namespacing
137
BEFORE INSERT OR UPDATE ON project_group
138
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
140
CREATE TABLE group_invitation (
141
loginid INT4 REFERENCES login (loginid) NOT NULL,
142
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
143
inviter INT4 REFERENCES login (loginid) NOT NULL,
144
invited TIMESTAMP NOT NULL,
26
CREATE TABLE group_invitations (
27
loginid INT4 REFERENCES users (loginid),
28
groupid INT4 REFERENCES groups (groupid),
146
29
UNIQUE (loginid,groupid)
149
CREATE TABLE group_member (
150
loginid INT4 REFERENCES login (loginid),
151
groupid INT4 REFERENCES project_group (groupid),
32
CREATE TABLE group_members (
33
loginid INT4 REFERENCES users (loginid),
34
groupid INT4 REFERENCES groups (groupid),
35
projectid INT4 REFERENCES projects (projectid),
36
UNIQUE (loginid,projectid),
152
37
PRIMARY KEY (loginid,groupid)
155
40
CREATE TABLE enrolment (
156
loginid INT4 REFERENCES login (loginid),
157
offeringid INT4 REFERENCES offering (offeringid),
158
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
159
'lecturer')) DEFAULT 'student',
41
loginid INT4 REFERENCES users (loginid),
42
offeringid INT4 REFERENCES offerings (offeringid),
161
special_result VARCHAR,
163
special_supp_result VARCHAR,
165
active BOOL NOT NULL DEFAULT true,
166
46
PRIMARY KEY (loginid,offeringid)
169
CREATE TABLE assessed (
170
assessedid SERIAL PRIMARY KEY NOT NULL,
171
loginid INT4 REFERENCES login (loginid),
172
groupid INT4 REFERENCES project_group (groupid),
173
projectid INT4 REFERENCES project (projectid) NOT NULL,
174
-- exactly one of loginid and groupid must be non-null
175
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
176
OR (loginid IS NULL AND groupid IS NOT NULL))
50
loginid INT4 PRIMARY KEY REFERENCES users (loginid),
54
CREATE TABLE projects (
55
projectid SERIAL PRIMARY KEY NOT NULL,
58
offeringid INT4 REFERENCES offerings (offeringid) NOT NULL,
179
62
CREATE TABLE project_extension (
180
extensionid SERIAL PRIMARY KEY,
181
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
64
projectid INT4 REFERENCES projects (projectid) NOT NULL,
182
65
deadline TIMESTAMP NOT NULL,
183
approver INT4 REFERENCES login (loginid) NOT NULL,
66
approver INT4 REFERENCES users (loginid) NOT NULL,
187
CREATE TABLE project_submission (
188
submissionid SERIAL PRIMARY KEY,
189
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
190
path VARCHAR NOT NULL,
191
revision INT4 NOT NULL,
192
date_submitted TIMESTAMP NOT NULL,
193
submitter INT4 REFERENCES login (loginid) NOT NULL
196
70
CREATE TABLE project_mark (
197
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
72
projectid INT4 REFERENCES projects (projectid) NOT NULL,
199
marker INT4 REFERENCES login (loginid) NOT NULL,
74
marker INT4 REFERENCES users (loginid) NOT NULL,
206
CREATE TABLE exercise (
207
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
210
description_xhtml_cache TEXT,
217
CREATE TABLE worksheet (
218
worksheetid SERIAL PRIMARY KEY,
219
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
220
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
223
data_xhtml_cache TEXT,
224
assessable BOOLEAN NOT NULL,
225
published BOOLEAN NOT NULL DEFAULT true,
226
seq_no INT4 NOT NULL,
227
format TEXT NOT NUll,
228
UNIQUE (offeringid, identifier)
231
CREATE TABLE worksheet_exercise (
232
ws_ex_id SERIAL PRIMARY KEY,
233
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
234
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
235
seq_no INT4 NOT NULL,
236
active BOOLEAN NOT NULL DEFAULT true,
237
optional BOOLEAN NOT NULL,
238
UNIQUE (worksheetid, exerciseid)
241
CREATE TABLE exercise_attempt (
242
loginid INT4 REFERENCES login (loginid) NOT NULL,
243
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
244
date TIMESTAMP NOT NULL,
245
attempt TEXT NOT NULL,
79
PRIMARY KEY (loginid/groupid, projectid, componentid)
82
CREATE TABLE problem (
83
problemid SERIAL PRIMARY KEY NOT NULL,
87
CREATE TABLE problem_tags (
88
problemid INT4 REFERENCES tutorial_problem (problemid),
90
added_by INT4 REFERENCES users (loginid) NOT NULL,
91
when TIMESTAMP NOT NULL,
92
PRIMARY KEY (problemid,added_by,tag)
95
CREATE TABLE problem_test_case (
96
problemid INT4 REFERENCES problem (problemid) NOT NULL,
97
testcaseid SERIAL UNIQUE NOT NULL,
100
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
103
CREATE TABLE problem_test_case_tags (
104
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
105
tag VARCHAR NOT NULL,
107
added_by INT4 REFERENCES users (loginid) NOT NULL,
108
when TIMESTAMP NOT NULL,
109
PRIMARY KEY (testcaseid,added_by,tag)
112
CREATE TABLE problem_attempt (
113
problemid INT4 REFERENCES problem (problemid) NOT NULL,
114
loginid INT4 REFERENCES users (loginid) NOT NULL,
115
when TIMESTAMP NOT NULL,
116
attempt VARCHAR NOT NULL,
246
117
complete BOOLEAN NOT NULL,
247
active BOOLEAN NOT NULL DEFAULT true,
248
PRIMARY KEY (loginid, ws_ex_id, date)
251
CREATE TABLE exercise_save (
252
loginid INT4 REFERENCES login (loginid) NOT NULL,
253
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
254
date TIMESTAMP NOT NULL,
256
PRIMARY KEY (loginid, ws_ex_id)
259
CREATE TABLE test_suite (
260
suiteid SERIAL PRIMARY KEY,
261
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
268
CREATE TABLE test_case (
269
testid SERIAL PRIMARY KEY,
270
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
277
CREATE TABLE suite_variable (
278
varid SERIAL PRIMARY KEY,
279
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
282
var_type TEXT NOT NULL,
286
CREATE TABLE test_case_part (
287
partid SERIAL PRIMARY KEY,
288
testid INT4 REFERENCES test_case (testid) NOT NULL,
289
part_type TEXT NOT NULL,
118
PRIMARY KEY (problemid,loginid,when)
121
CREATE INDEX indexname ON problem_attempt (problemid, login);
123
CREATE TABLE problem_attempt_breakdown (
124
problemid INT4 REFERENCES problem (problemid) NOT NULL,
125
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
126
loginid INT4 REFERENCES users (loginid) NOT NULL,
127
when TIMESTAMP NOT NULL,
131
CREATE TABLE problem_prerequisites (
132
parent INT4 REFERENCES problem (problemid) NOT NULL,
133
child INT4 REFERENCES problem (problemid) NOT NULL,
134
PRIMARY KEY (parent,child)