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,
192
--TODO: Add in a field for the user-friendly identifier
193
CREATE TABLE problem (
194
identifier VARCHAR PRIMARY KEY NOT NULL,
203
--TODO: Link worksheets to offerings
204
CREATE TABLE worksheet (
205
worksheetid SERIAL PRIMARY KEY NOT NULL,
206
subject VARCHAR NOT NULL,
207
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
208
identifier VARCHAR NOT NULL,
211
UNIQUE (subject, identifier)
214
CREATE TABLE worksheet_problem (
215
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
216
problemid TEXT REFERENCES problem (identifier) NOT NULL,
218
PRIMARY KEY (worksheetid, problemid)
221
CREATE TABLE problem_attempt (
222
problemid VARCHAR REFERENCES problem (identifier) NOT NULL,
223
loginid INT4 REFERENCES login (loginid) NOT NULL,
224
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
225
date TIMESTAMP NOT NULL,
226
attempt VARCHAR NOT NULL,
227
complete BOOLEAN NOT NULL,
228
active BOOLEAN NOT NULL DEFAULT true,
229
PRIMARY KEY (problemid,loginid,date)
232
CREATE TABLE problem_save (
233
problemid INT4 REFERENCES problem (problemid) NOT NULL,
234
loginid INT4 REFERENCES login (loginid) NOT NULL,
235
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
236
date TIMESTAMP NOT NULL,
237
text VARCHAR NOT NULL,
238
PRIMARY KEY (problemid,loginid)
241
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
243
-- TABLES FOR EXERCISES IN DATABASE --
244
CREATE TABLE test_suite (
245
suiteid SERIAL NOT NULL,
246
problemid TEXT REFERENCES problem (identifier) NOT NULL,
249
PRIMARY KEY (problemid, suiteid)
252
CREATE TABLE test_case (
253
testid SERIAL NOT NULL,
254
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
262
PRIMARY KEY (testid, suiteid)