4
DROP TABLE group_members;
9
login varchar(80) PRIMARY KEY, -- login id
14
groupid varchar(18) PRIMARY KEY, -- group name Y^4-S^9-G^3
15
nick varchar(80), -- group nickname
16
subject varchar(9), -- subject code
17
year varchar(4) -- when
20
CREATE TABLE group_members (
21
login varchar(80) REFERENCES users (login),
22
groupid varchar(18) REFERENCES groups (groupid)
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
28
CREATE TABLE subject (
29
subjectid SERIAL PRIMARY KEY NOT NULL,
30
subj_code VARCHAR UNIQUE NOT NULL,
31
subj_name VARCHAR NOT NULL,
32
subj_short_name VARCHAR UNIQUE NOT NULL,
36
CREATE TABLE semester (
37
semesterid SERIAL PRIMARY KEY NOT NULL,
38
year CHAR(4) NOT NULL,
39
semester CHAR(1) NOT NULL,
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();
59
CREATE TABLE offering (
60
offeringid SERIAL PRIMARY KEY NOT NULL,
61
subject INT4 REFERENCES subject (subjectid) NOT NULL,
62
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
63
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
64
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
65
UNIQUE (subject, semesterid)
68
-- Projects and groups
69
-- -------------------
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 NOT NULL DEFAULT 4
77
CREATE TABLE project (
78
projectid SERIAL PRIMARY KEY NOT NULL,
81
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
85
CREATE TABLE project_group (
86
groupnm VARCHAR NOT NULL,
87
groupid SERIAL PRIMARY KEY NOT NULL,
88
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
90
createdby INT4 REFERENCES login (loginid) NOT NULL,
91
epoch TIMESTAMP NOT NULL,
92
UNIQUE (projectsetid, groupnm)
95
CREATE OR REPLACE FUNCTION check_group_namespacing_insertupdate()
100
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
101
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;
103
RAISE EXCEPTION ''a project group named % already exists in offering ID %'', NEW.groupnm, oid;
107
' LANGUAGE 'plpgsql';
109
CREATE TRIGGER check_group_namespacing
110
BEFORE INSERT OR UPDATE ON project_group
111
FOR EACH ROW EXECUTE PROCEDURE check_group_namespacing_insertupdate();
113
CREATE TABLE group_invitation (
114
loginid INT4 REFERENCES login (loginid) NOT NULL,
115
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
116
inviter INT4 REFERENCES login (loginid) NOT NULL,
117
invited TIMESTAMP NOT NULL,
119
UNIQUE (loginid,groupid)
122
CREATE TABLE group_member (
123
loginid INT4 REFERENCES login (loginid),
124
groupid INT4 REFERENCES project_group (groupid),
125
PRIMARY KEY (loginid,groupid)
25
128
CREATE TABLE enrolment (
26
login varchar(80) REFERENCES users (login),
32
login varchar(80) REFERENCES users (login),
36
INSERT INTO users (login,nick) values ('conway', 'Tom');
37
INSERT INTO roles (login,role) values ('conway', 'student');
38
INSERT INTO users (login,nick) values ('apeel', 'Andrew');
39
INSERT INTO roles (login,role) values ('apeel', 'student');
40
INSERT INTO users (login,nick) values ('mgiuca', 'Matt');
41
INSERT INTO roles (login,role) values ('mgiuca', 'tutor');
42
INSERT INTO users (login,nick) values ('sb', 'Steven');
43
INSERT INTO roles (login,role) values ('sb', 'lecturer');
44
INSERT INTO users (login,nick) values ('mpp', 'Mike');
45
INSERT INTO roles (login,role) values ('mpp', 'student');
46
INSERT INTO users (login,nick) values ('ivo', 'Ivo');
47
INSERT INTO roles (login,role) values ('ivo', 'admin');
49
INSERT INTO groups (groupid, nick, subject, year) values ('2007-INFO10001-321', 'Purple Alert', 'INFO10001', '2008');
50
INSERT INTO groups (groupid, nick, subject, year) values ('2007-INFO10001-322', 'Blind Illuminati', 'INFO10001', '2008');
52
INSERT INTO group_members (login,groupid) values ('conway', '2007-INFO10001-321');
53
INSERT INTO group_members (login,groupid) values ('apeel', '2007-INFO10001-321');
54
INSERT INTO group_members (login,groupid) values ('mgiuca', '2007-INFO10001-321');
55
INSERT INTO group_members (login,groupid) values ('sb', '2007-INFO10001-321');
56
INSERT INTO group_members (login,groupid) values ('mpp', '2007-INFO10001-322');
57
INSERT INTO group_members (login,groupid) values ('ivo', '2007-INFO10001-322');
59
INSERT INTO enrolment (login,subject,year) values ('conway' , 'INFO10001', '2008');
60
INSERT INTO enrolment (login,subject,year) values ('apeel' , 'INFO10001', '2008');
61
INSERT INTO enrolment (login,subject,year) values ('mgiuca' , 'INFO10001', '2008');
62
INSERT INTO enrolment (login,subject,year) values ('sb' , 'INFO10001', '2008');
63
INSERT INTO enrolment (login,subject,year) values ('mpp' , 'INFO10001', '2008');
64
INSERT INTO enrolment (login,subject,year) values ('ivo' , 'INFO10001', '2008');
129
loginid INT4 REFERENCES login (loginid),
130
offeringid INT4 REFERENCES offering (offeringid),
132
special_result VARCHAR,
134
special_supp_result VARCHAR,
136
active BOOL NOT NULL DEFAULT true,
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();
157
CREATE TABLE assessed (
158
assessedid SERIAL PRIMARY KEY NOT NULL,
159
loginid INT4 REFERENCES login (loginid),
160
groupid INT4 REFERENCES project_group (groupid),
161
projectid INT4 REFERENCES project (projectid) NOT NULL,
162
-- exactly one of loginid and groupid must be non-null
163
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
164
OR (loginid IS NULL AND groupid IS NOT NULL))
167
CREATE TABLE project_extension (
168
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
169
deadline TIMESTAMP NOT NULL,
170
approver INT4 REFERENCES login (loginid) NOT NULL,
174
CREATE TABLE project_submission (
175
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
176
path VARCHAR NOT NULL,
177
revision INT4 NOT NULL
180
CREATE TABLE project_mark (
181
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
183
marker INT4 REFERENCES login (loginid) NOT NULL,
193
CREATE TABLE problem (
194
problemid SERIAL PRIMARY KEY NOT NULL,
195
identifier VARCHAR UNIQUE NOT NULL,
199
CREATE TABLE worksheet (
200
worksheetid SERIAL PRIMARY KEY NOT NULL,
201
subject VARCHAR NOT NULL,
202
identifier VARCHAR NOT NULL,
205
UNIQUE (subject, identifier)
208
CREATE TABLE worksheet_problem (
209
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
210
problemid INT4 REFERENCES problem (problemid) NOT NULL,
212
PRIMARY KEY (worksheetid, problemid)
215
CREATE TABLE problem_tag (
216
problemid INT4 REFERENCES problem (problemid),
217
tag VARCHAR NOT NULL,
219
standard BOOLEAN NOT NULL,
220
added_by INT4 REFERENCES login (loginid) NOT NULL,
221
date TIMESTAMP NOT NULL,
222
PRIMARY KEY (problemid,added_by,tag)
225
CREATE TABLE problem_test_case (
226
problemid INT4 REFERENCES problem (problemid) NOT NULL,
227
testcaseid SERIAL UNIQUE NOT NULL,
230
visibility VARCHAR CHECK (visibility in ('public', 'protected', 'private'))
233
CREATE TABLE problem_test_case_tag (
234
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
235
tag VARCHAR NOT NULL,
237
standard BOOLEAN NOT NULL,
238
added_by INT4 REFERENCES login (loginid) NOT NULL,
239
date TIMESTAMP NOT NULL,
240
PRIMARY KEY (testcaseid,added_by,tag)
243
CREATE TABLE problem_attempt (
244
problemid INT4 REFERENCES problem (problemid) NOT NULL,
245
loginid INT4 REFERENCES login (loginid) NOT NULL,
246
date TIMESTAMP NOT NULL,
247
attempt VARCHAR NOT NULL,
248
complete BOOLEAN NOT NULL,
249
active BOOLEAN NOT NULL DEFAULT true,
250
PRIMARY KEY (problemid,loginid,date)
253
CREATE TABLE problem_save (
254
problemid INT4 REFERENCES problem (problemid) NOT NULL,
255
loginid INT4 REFERENCES login (loginid) NOT NULL,
256
date TIMESTAMP NOT NULL,
257
text VARCHAR NOT NULL,
258
PRIMARY KEY (problemid,loginid)
261
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
263
CREATE TABLE problem_attempt_breakdown (
264
problemid INT4 REFERENCES problem (problemid) NOT NULL,
265
testcaseid INT4 REFERENCES problem_test_case (testcaseid) NOT NULL,
266
loginid INT4 REFERENCES login (loginid) NOT NULL,
267
date TIMESTAMP NOT NULL,
271
CREATE TABLE problem_prerequisite (
272
parent INT4 REFERENCES problem (problemid) NOT NULL,
273
child INT4 REFERENCES problem (problemid) NOT NULL,
274
PRIMARY KEY (parent,child)