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,
186
--TODO: Add in a field for the user-friendly identifier
187
CREATE TABLE problem (
188
identifier VARCHAR PRIMARY KEY NOT NULL,
197
--TODO: Link worksheets to offerings
198
CREATE TABLE worksheet (
199
worksheetid SERIAL PRIMARY KEY NOT NULL,
200
subject VARCHAR NOT NULL,
201
offeringid INT4 REFERENCES offering (offeringid) 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 TEXT REFERENCES problem (identifier) NOT NULL,
212
PRIMARY KEY (worksheetid, problemid)
215
CREATE TABLE problem_attempt (
216
problemid VARCHAR REFERENCES problem (identifier) NOT NULL,
217
loginid INT4 REFERENCES login (loginid) NOT NULL,
218
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
219
date TIMESTAMP NOT NULL,
220
attempt VARCHAR NOT NULL,
221
complete BOOLEAN NOT NULL,
222
active BOOLEAN NOT NULL DEFAULT true,
223
PRIMARY KEY (problemid,loginid,date)
226
CREATE TABLE problem_save (
227
problemid INT4 REFERENCES problem (problemid) NOT NULL,
228
loginid INT4 REFERENCES login (loginid) NOT NULL,
229
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
230
date TIMESTAMP NOT NULL,
231
text VARCHAR NOT NULL,
232
PRIMARY KEY (problemid,loginid)
235
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
237
CREATE TABLE test_suite (
238
suiteid SERIAL NOT NULL,
239
problemid TEXT REFERENCES problem (identifier) NOT NULL,
242
PRIMARY KEY (problemid, suiteid)
245
CREATE TABLE test_case (
246
testid SERIAL NOT NULL,
247
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
255
PRIMARY KEY (testid, suiteid)
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');