1
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
4
loginid SERIAL PRIMARY KEY NOT NULL,
5
login VARCHAR UNIQUE NOT NULL,
7
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
8
'enabled', 'disabled')),
9
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
11
'lecturer', 'admin')),
12
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
13
nick VARCHAR NOT NULL,
19
fullname VARCHAR NOT NULL,
20
studentid VARCHAR, -- may be null
25
CREATE TABLE subject (
26
subjectid SERIAL PRIMARY KEY NOT NULL,
27
subj_code VARCHAR UNIQUE NOT NULL,
28
subj_name VARCHAR NOT NULL,
29
subj_short_name VARCHAR UNIQUE NOT NULL,
33
CREATE TABLE semester (
34
semesterid SERIAL PRIMARY KEY NOT NULL,
35
year CHAR(4) NOT NULL,
36
semester CHAR(1) NOT NULL,
38
UNIQUE (year, semester)
41
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
44
IF OLD.active = true AND NEW.active = false THEN
45
UPDATE enrolment SET active=false WHERE offeringid IN (
46
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
52
CREATE TRIGGER deactivate_semester_enrolments
53
AFTER UPDATE ON semester
54
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
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,
60
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
61
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
62
UNIQUE (subject, semesterid)
66
CREATE TABLE project_set (
67
projectsetid SERIAL PRIMARY KEY NOT NULL,
68
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
69
max_students_per_group INTEGER NOT NULL DEFAULT 4
72
CREATE TABLE project (
73
projectid SERIAL PRIMARY KEY NOT NULL,
76
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
80
CREATE TABLE project_group (
81
groupnm VARCHAR NOT NULL,
82
groupid SERIAL PRIMARY KEY NOT NULL,
83
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
85
createdby INT4 REFERENCES login (loginid) NOT NULL,
86
epoch TIMESTAMP NOT NULL,
87
UNIQUE (projectsetid, groupnm)
90
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
95
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
96
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;
98
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
102
' LANGUAGE 'plpgsql';
104
CREATE TRIGGER check_group_namespacing
105
BEFORE INSERT OR UPDATE ON project_group
106
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
108
CREATE TABLE group_invitation (
109
loginid INT4 REFERENCES login (loginid) NOT NULL,
110
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
111
inviter INT4 REFERENCES login (loginid) NOT NULL,
112
invited TIMESTAMP NOT NULL,
114
UNIQUE (loginid,groupid)
117
CREATE TABLE group_member (
118
loginid INT4 REFERENCES login (loginid),
119
groupid INT4 REFERENCES project_group (groupid),
120
PRIMARY KEY (loginid,groupid)
1
-- We need a users database to do authorization, manage groups, &c
6
DROP TABLE group_members;
11
login varchar(80) PRIMARY KEY, -- login id
16
groupid varchar(18) PRIMARY KEY, -- group name Y^4-S^9-G^3
17
nick varchar(80), -- group nickname
18
subject varchar(9), -- subject code
19
year varchar(4) -- when
22
CREATE TABLE group_members (
23
login varchar(80) REFERENCES users (login),
24
groupid varchar(18) REFERENCES groups (groupid)
123
27
CREATE TABLE enrolment (
124
loginid INT4 REFERENCES login (loginid),
125
offeringid INT4 REFERENCES offering (offeringid),
127
special_result VARCHAR,
129
special_supp_result VARCHAR,
131
active BOOL NOT NULL DEFAULT true,
132
PRIMARY KEY (loginid,offeringid)
135
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
140
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
141
IF NOT active AND NEW.active = true THEN
142
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
146
' LANGUAGE 'plpgsql';
148
CREATE TRIGGER confirm_active_semester
149
BEFORE INSERT OR UPDATE ON enrolment
150
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
152
CREATE TABLE assessed (
153
assessedid SERIAL PRIMARY KEY NOT NULL,
154
loginid INT4 REFERENCES login (loginid),
155
groupid INT4 REFERENCES project_group (groupid),
156
projectid INT4 REFERENCES project (projectid) NOT NULL,
157
-- exactly one of loginid and groupid must be non-null
158
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
159
OR (loginid IS NULL AND groupid IS NOT NULL))
162
CREATE TABLE project_extension (
163
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
164
deadline TIMESTAMP NOT NULL,
165
approver INT4 REFERENCES login (loginid) NOT NULL,
169
CREATE TABLE project_submission (
170
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
171
path VARCHAR NOT NULL,
172
revision INT4 NOT NULL
175
CREATE TABLE project_mark (
176
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
178
marker INT4 REFERENCES login (loginid) NOT NULL,
186
CREATE TABLE problem (
187
problemid SERIAL PRIMARY KEY NOT NULL,
188
identifier VARCHAR UNIQUE NOT NULL,
192
CREATE TABLE worksheet (
193
worksheetid SERIAL PRIMARY KEY NOT NULL,
194
subject VARCHAR NOT NULL,
195
identifier VARCHAR NOT NULL,
198
UNIQUE (subject, identifier)
201
CREATE TABLE worksheet_problem (
202
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
203
problemid INT4 REFERENCES problem (problemid) NOT NULL,
205
PRIMARY KEY (worksheetid, problemid)
208
CREATE TABLE problem_tag (
209
problemid INT4 REFERENCES problem (problemid),
210
tag VARCHAR NOT NULL,
212
standard BOOLEAN NOT NULL,
213
added_by INT4 REFERENCES login (loginid) NOT NULL,
214
date TIMESTAMP NOT NULL,
215
PRIMARY KEY (problemid,added_by,tag)
218
CREATE TABLE problem_test_case (
219
problemid INT4 REFERENCES problem (problemid) NOT NULL,
220
testcaseid SERIAL UNIQUE NOT NULL,
223
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
226
CREATE TABLE problem_test_case_tag (
227
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
228
tag VARCHAR NOT NULL,
230
standard BOOLEAN NOT NULL,
231
added_by INT4 REFERENCES login (loginid) NOT NULL,
232
date TIMESTAMP NOT NULL,
233
PRIMARY KEY (testcaseid,added_by,tag)
236
CREATE TABLE problem_attempt (
237
problemid INT4 REFERENCES problem (problemid) NOT NULL,
238
loginid INT4 REFERENCES login (loginid) NOT NULL,
239
date TIMESTAMP NOT NULL,
240
attempt VARCHAR NOT NULL,
241
complete BOOLEAN NOT NULL,
242
active BOOLEAN NOT NULL DEFAULT true,
243
PRIMARY KEY (problemid,loginid,date)
246
CREATE TABLE problem_save (
247
problemid INT4 REFERENCES problem (problemid) NOT NULL,
248
loginid INT4 REFERENCES login (loginid) NOT NULL,
249
date TIMESTAMP NOT NULL,
250
text VARCHAR NOT NULL,
251
PRIMARY KEY (problemid,loginid)
254
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
256
CREATE TABLE problem_attempt_breakdown (
257
problemid INT4 REFERENCES problem (problemid) NOT NULL,
258
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
259
loginid INT4 REFERENCES login (loginid) NOT NULL,
260
date TIMESTAMP NOT NULL,
264
CREATE TABLE problem_prerequisite (
265
parent INT4 REFERENCES problem (problemid) NOT NULL,
266
child INT4 REFERENCES problem (problemid) NOT NULL,
267
PRIMARY KEY (parent,child)
28
login varchar(80) REFERENCES users (login),
34
login varchar(80) REFERENCES users (login),
38
INSERT INTO users (login,nick) values ('conway', 'Tom');
39
INSERT INTO roles (login,role) values ('conway', 'student');
40
INSERT INTO users (login,nick) values ('apeel', 'Andrew');
41
INSERT INTO roles (login,role) values ('apeel', 'student');
42
INSERT INTO users (login,nick) values ('mgiuca', 'Matt');
43
INSERT INTO roles (login,role) values ('mgiuca', 'tutor');
44
INSERT INTO users (login,nick) values ('sb', 'Steven');
45
INSERT INTO roles (login,role) values ('sb', 'lecturer');
46
INSERT INTO users (login,nick) values ('mpp', 'Mike');
47
INSERT INTO roles (login,role) values ('mpp', 'student');
48
INSERT INTO users (login,nick) values ('ivo', 'Ivo');
49
INSERT INTO roles (login,role) values ('ivo', 'admin');
51
INSERT INTO groups (groupid, nick, subject, year) values ('2007-INFO10001-321', 'Purple Alert', 'INFO10001', '2008');
52
INSERT INTO groups (groupid, nick, subject, year) values ('2007-INFO10001-322', 'Blind Illuminati', 'INFO10001', '2008');
54
INSERT INTO group_members (login,groupid) values ('conway', '2007-INFO10001-321');
55
INSERT INTO group_members (login,groupid) values ('apeel', '2007-INFO10001-321');
56
INSERT INTO group_members (login,groupid) values ('mgiuca', '2007-INFO10001-321');
57
INSERT INTO group_members (login,groupid) values ('sb', '2007-INFO10001-321');
58
INSERT INTO group_members (login,groupid) values ('mpp', '2007-INFO10001-322');
59
INSERT INTO group_members (login,groupid) values ('ivo', '2007-INFO10001-322');
61
INSERT INTO enrolment (login,subject,year) values ('conway' , 'INFO10001', '2008');
62
INSERT INTO enrolment (login,subject,year) values ('apeel' , 'INFO10001', '2008');
63
INSERT INTO enrolment (login,subject,year) values ('mgiuca' , 'INFO10001', '2008');
64
INSERT INTO enrolment (login,subject,year) values ('sb' , 'INFO10001', '2008');
65
INSERT INTO enrolment (login,subject,year) values ('mpp' , 'INFO10001', '2008');
66
INSERT INTO enrolment (login,subject,year) values ('ivo' , 'INFO10001', '2008');