1
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
2
login VARCHAR UNIQUE NOT NULL,
4
3
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,
8
studentid VARCHAR -- may be null
11
CREATE TABLE offering (
12
offeringid SERIAL PRIMARY KEY NOT NULL,
13
subj_name VARCHAR NOT NULL,
14
subj_code VARCHAR NOT NULL,
35
15
year CHAR(4) NOT NULL,
36
16
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
20
CREATE TABLE project (
73
21
projectid SERIAL PRIMARY KEY NOT NULL,
76
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
24
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
80
28
CREATE TABLE project_group (
81
29
groupnm VARCHAR NOT NULL,
82
30
groupid SERIAL PRIMARY KEY NOT NULL,
83
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
31
offeringid INT4 REFERENCES offering (offeringid),
85
33
createdby INT4 REFERENCES login (loginid) NOT NULL,
86
34
epoch TIMESTAMP NOT NULL,
87
UNIQUE (projectsetid, groupnm)
35
UNIQUE (offeringid, 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_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
38
CREATE TABLE group_invitation (
109
39
loginid INT4 REFERENCES login (loginid) NOT NULL,
110
40
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
133
61
special_supp_result VARCHAR,
135
active BOOL NOT NULL DEFAULT true,
136
63
PRIMARY KEY (loginid,offeringid)
139
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
144
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
145
IF NOT active AND NEW.active = true THEN
146
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
150
' LANGUAGE 'plpgsql';
152
CREATE TRIGGER confirm_active_semester
153
BEFORE INSERT OR UPDATE ON enrolment
154
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
156
66
CREATE TABLE assessed (
157
67
assessedid SERIAL PRIMARY KEY NOT NULL,
158
68
loginid INT4 REFERENCES login (loginid),
159
69
groupid INT4 REFERENCES project_group (groupid),
160
projectid INT4 REFERENCES project (projectid) NOT NULL,
161
70
-- exactly one of loginid and groupid must be non-null
162
71
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
163
72
OR (loginid IS NULL AND groupid IS NOT NULL))
166
75
CREATE TABLE project_extension (
167
76
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
77
projectid INT4 REFERENCES project (projectid) NOT NULL,
168
78
deadline TIMESTAMP NOT NULL,
169
79
approver INT4 REFERENCES login (loginid) NOT NULL,
173
CREATE TABLE project_submission (
174
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
175
path VARCHAR NOT NULL,
176
revision INT4 NOT NULL
179
83
CREATE TABLE project_mark (
180
84
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
85
projectid INT4 REFERENCES project (projectid) NOT NULL,
182
87
marker INT4 REFERENCES login (loginid) NOT NULL,
190
94
CREATE TABLE problem (
191
95
problemid SERIAL PRIMARY KEY NOT NULL,
192
identifier VARCHAR UNIQUE NOT NULL,
196
CREATE TABLE worksheet (
197
worksheetid SERIAL PRIMARY KEY NOT NULL,
198
subject VARCHAR NOT NULL,
199
identifier VARCHAR NOT NULL,
202
UNIQUE (subject, identifier)
205
CREATE TABLE worksheet_problem (
206
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
207
problemid INT4 REFERENCES problem (problemid) NOT NULL,
209
PRIMARY KEY (worksheetid, problemid)
212
99
CREATE TABLE problem_tag (
213
100
problemid INT4 REFERENCES problem (problemid),
214
101
tag VARCHAR NOT NULL,
245
130
date TIMESTAMP NOT NULL,
246
131
attempt VARCHAR NOT NULL,
247
132
complete BOOLEAN NOT NULL,
248
active BOOLEAN NOT NULL DEFAULT true,
249
133
PRIMARY KEY (problemid,loginid,date)
252
CREATE TABLE problem_save (
253
problemid INT4 REFERENCES problem (problemid) NOT NULL,
254
loginid INT4 REFERENCES login (loginid) NOT NULL,
255
date TIMESTAMP NOT NULL,
256
text VARCHAR NOT NULL,
257
PRIMARY KEY (problemid,loginid)
260
136
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
262
138
CREATE TABLE problem_attempt_breakdown (