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
DEFAULT 'no_agreement',
10
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
12
'lecturer', 'admin')),
13
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
14
nick VARCHAR NOT NULL,
20
fullname VARCHAR NOT NULL,
21
studentid VARCHAR, -- may be null
26
CREATE TABLE subject (
27
subjectid SERIAL PRIMARY KEY NOT NULL,
28
subj_code VARCHAR UNIQUE NOT NULL,
29
subj_name VARCHAR NOT NULL,
30
subj_short_name VARCHAR UNIQUE NOT NULL,
34
CREATE TABLE semester (
35
semesterid SERIAL PRIMARY KEY NOT NULL,
36
year CHAR(4) NOT NULL,
37
semester CHAR(1) NOT NULL,
39
UNIQUE (year, semester)
42
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
45
IF OLD.active = true AND NEW.active = false THEN
46
UPDATE enrolment SET active=false WHERE offeringid IN (
47
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
53
CREATE TRIGGER deactivate_semester_enrolments
54
AFTER UPDATE ON semester
55
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
57
CREATE TABLE offering (
58
offeringid SERIAL PRIMARY KEY NOT NULL,
59
subject INT4 REFERENCES subject (subjectid) NOT NULL,
60
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
61
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
62
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
63
UNIQUE (subject, semesterid)
67
CREATE TABLE project_set (
68
projectsetid SERIAL PRIMARY KEY NOT NULL,
69
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
70
max_students_per_group INTEGER NOT NULL DEFAULT 4
73
CREATE TABLE project (
74
projectid SERIAL PRIMARY KEY NOT NULL,
77
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
81
CREATE TABLE project_group (
82
groupnm VARCHAR NOT NULL,
83
groupid SERIAL PRIMARY KEY NOT NULL,
84
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
86
createdby INT4 REFERENCES login (loginid) NOT NULL,
87
epoch TIMESTAMP NOT NULL,
88
UNIQUE (projectsetid, groupnm)
91
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
96
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
97
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;
99
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
103
' LANGUAGE 'plpgsql';
105
CREATE TRIGGER check_group_namespacing
106
BEFORE INSERT OR UPDATE ON project_group
107
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
109
CREATE TABLE group_invitation (
110
loginid INT4 REFERENCES login (loginid) NOT NULL,
111
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
112
inviter INT4 REFERENCES login (loginid) NOT NULL,
113
invited TIMESTAMP NOT NULL,
115
UNIQUE (loginid,groupid)
118
CREATE TABLE group_member (
119
loginid INT4 REFERENCES login (loginid),
120
groupid INT4 REFERENCES project_group (groupid),
121
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)
124
27
CREATE TABLE enrolment (
125
loginid INT4 REFERENCES login (loginid),
126
offeringid INT4 REFERENCES offering (offeringid),
128
special_result VARCHAR,
130
special_supp_result VARCHAR,
132
active BOOL NOT NULL DEFAULT true,
133
PRIMARY KEY (loginid,offeringid)
136
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
141
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
142
IF NOT active AND NEW.active = true THEN
143
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
147
' LANGUAGE 'plpgsql';
149
CREATE TRIGGER confirm_active_semester
150
BEFORE INSERT OR UPDATE ON enrolment
151
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
153
CREATE TABLE assessed (
154
assessedid SERIAL PRIMARY KEY NOT NULL,
155
loginid INT4 REFERENCES login (loginid),
156
groupid INT4 REFERENCES project_group (groupid),
157
projectid INT4 REFERENCES project (projectid) NOT NULL,
158
-- exactly one of loginid and groupid must be non-null
159
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
160
OR (loginid IS NULL AND groupid IS NOT NULL))
163
CREATE TABLE project_extension (
164
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
165
deadline TIMESTAMP NOT NULL,
166
approver INT4 REFERENCES login (loginid) NOT NULL,
170
CREATE TABLE project_submission (
171
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
172
path VARCHAR NOT NULL,
173
revision INT4 NOT NULL
176
CREATE TABLE project_mark (
177
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
179
marker INT4 REFERENCES login (loginid) NOT NULL,
187
CREATE TABLE problem (
188
problemid SERIAL PRIMARY KEY NOT NULL,
189
identifier VARCHAR UNIQUE NOT NULL,
193
CREATE TABLE worksheet (
194
worksheetid SERIAL PRIMARY KEY NOT NULL,
195
subject VARCHAR NOT NULL,
196
identifier VARCHAR NOT NULL,
199
UNIQUE (subject, identifier)
202
CREATE TABLE worksheet_problem (
203
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
204
problemid INT4 REFERENCES problem (problemid) NOT NULL,
206
PRIMARY KEY (worksheetid, problemid)
209
CREATE TABLE problem_tag (
210
problemid INT4 REFERENCES problem (problemid),
211
tag VARCHAR NOT NULL,
213
standard BOOLEAN NOT NULL,
214
added_by INT4 REFERENCES login (loginid) NOT NULL,
215
date TIMESTAMP NOT NULL,
216
PRIMARY KEY (problemid,added_by,tag)
219
CREATE TABLE problem_test_case (
220
problemid INT4 REFERENCES problem (problemid) NOT NULL,
221
testcaseid SERIAL UNIQUE NOT NULL,
224
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
227
CREATE TABLE problem_test_case_tag (
228
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
229
tag VARCHAR NOT NULL,
231
standard BOOLEAN NOT NULL,
232
added_by INT4 REFERENCES login (loginid) NOT NULL,
233
date TIMESTAMP NOT NULL,
234
PRIMARY KEY (testcaseid,added_by,tag)
237
CREATE TABLE problem_attempt (
238
problemid INT4 REFERENCES problem (problemid) NOT NULL,
239
loginid INT4 REFERENCES login (loginid) NOT NULL,
240
date TIMESTAMP NOT NULL,
241
attempt VARCHAR NOT NULL,
242
complete BOOLEAN NOT NULL,
243
active BOOLEAN NOT NULL DEFAULT true,
244
PRIMARY KEY (problemid,loginid,date)
247
CREATE TABLE problem_save (
248
problemid INT4 REFERENCES problem (problemid) NOT NULL,
249
loginid INT4 REFERENCES login (loginid) NOT NULL,
250
date TIMESTAMP NOT NULL,
251
text VARCHAR NOT NULL,
252
PRIMARY KEY (problemid,loginid)
255
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
257
CREATE TABLE problem_attempt_breakdown (
258
problemid INT4 REFERENCES problem (problemid) NOT NULL,
259
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
260
loginid INT4 REFERENCES login (loginid) NOT NULL,
261
date TIMESTAMP NOT NULL,
265
CREATE TABLE problem_prerequisite (
266
parent INT4 REFERENCES problem (problemid) NOT NULL,
267
child INT4 REFERENCES problem (problemid) NOT NULL,
268
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');