30
28
subjectid SERIAL PRIMARY KEY NOT NULL,
31
29
subj_code VARCHAR UNIQUE NOT NULL,
32
30
subj_name VARCHAR NOT NULL,
33
subj_short_name VARCHAR UNIQUE NOT NULL,
31
subj_short_name VARCHAR UNIQUE NOT NULL
37
34
CREATE TABLE semester (
38
35
semesterid SERIAL PRIMARY KEY NOT NULL,
39
36
year CHAR(4) NOT NULL,
40
37
semester CHAR(1) NOT NULL,
38
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
39
'current', 'future')) DEFAULT 'current',
42
40
UNIQUE (year, semester)
45
CREATE OR REPLACE FUNCTION deactivate_semester_enrolments_update()
48
IF OLD.active = true AND NEW.active = false THEN
49
UPDATE enrolment SET active=false WHERE offeringid IN (
50
SELECT offeringid FROM offering WHERE offering.semesterid = NEW.semesterid);
56
CREATE TRIGGER deactivate_semester_enrolments
57
AFTER UPDATE ON semester
58
FOR EACH ROW EXECUTE PROCEDURE deactivate_semester_enrolments_update();
60
43
CREATE TABLE offering (
61
44
offeringid SERIAL PRIMARY KEY NOT NULL,
62
45
subject INT4 REFERENCES subject (subjectid) NOT NULL,
63
46
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
64
49
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
65
50
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
66
51
UNIQUE (subject, semesterid)
72
57
CREATE TABLE project_set (
73
58
projectsetid SERIAL PRIMARY KEY NOT NULL,
74
59
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
75
max_students_per_group INTEGER NOT NULL DEFAULT 4
60
max_students_per_group INTEGER
78
63
CREATE TABLE project (
79
64
projectid SERIAL PRIMARY KEY NOT NULL,
65
short_name TEXT NOT NULL,
82
69
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
70
deadline TIMESTAMP NOT NULL
73
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
78
IF TG_OP = ''UPDATE'' THEN
79
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
83
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
84
PERFORM 1 FROM project, project_set
85
WHERE project_set.offeringid = oid AND
86
project.projectsetid = project_set.projectsetid AND
87
project.short_name = NEW.short_name;
89
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
95
CREATE TRIGGER check_project_namespacing
96
BEFORE INSERT OR UPDATE ON project
97
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
86
99
CREATE TABLE project_group (
87
100
groupnm VARCHAR NOT NULL,
88
101
groupid SERIAL PRIMARY KEY NOT NULL,
138
153
PRIMARY KEY (loginid,offeringid)
141
CREATE OR REPLACE FUNCTION confirm_active_semester_insertupdate()
146
SELECT semester.active INTO active FROM offering, semester WHERE offeringid=NEW.offeringid AND semester.semesterid = offering.semesterid;
147
IF NOT active AND NEW.active = true THEN
148
RAISE EXCEPTION ''cannot have active enrolment for % in offering %, as the semester is inactive'', NEW.loginid, NEW.offeringid;
152
' LANGUAGE 'plpgsql';
154
CREATE TRIGGER confirm_active_semester
155
BEFORE INSERT OR UPDATE ON enrolment
156
FOR EACH ROW EXECUTE PROCEDURE confirm_active_semester_insertupdate();
158
156
CREATE TABLE assessed (
159
157
assessedid SERIAL PRIMARY KEY NOT NULL,
160
158
loginid INT4 REFERENCES login (loginid),
203
205
CREATE TABLE worksheet (
204
206
worksheetid SERIAL PRIMARY KEY,
205
207
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
206
identifier VARCHAR NOT NULL,
208
identifier TEXT NOT NULL,
211
assessable BOOLEAN NOT NULL,
212
seq_no INT4 NOT NULL,
213
format TEXT NOT NUll,
209
214
UNIQUE (offeringid, identifier)
212
CREATE TABLE worksheet_problem (
213
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
214
problemid TEXT REFERENCES problem (identifier) NOT NULL,
216
PRIMARY KEY (worksheetid, problemid)
217
CREATE TABLE worksheet_exercise (
218
ws_ex_id SERIAL PRIMARY KEY,
219
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
220
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
221
seq_no INT4 NOT NULL,
222
active BOOLEAN NOT NULL DEFAULT true,
223
optional BOOLEAN NOT NULL,
224
UNIQUE (worksheetid, exerciseid)
219
CREATE TABLE problem_attempt (
220
problemid TEXT REFERENCES problem (identifier) NOT NULL,
227
CREATE TABLE exercise_attempt (
221
228
loginid INT4 REFERENCES login (loginid) NOT NULL,
222
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
229
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
223
230
date TIMESTAMP NOT NULL,
224
attempt VARCHAR NOT NULL,
231
attempt TEXT NOT NULL,
225
232
complete BOOLEAN NOT NULL,
226
233
active BOOLEAN NOT NULL DEFAULT true,
227
PRIMARY KEY (problemid,loginid,worksheetid,date)
234
PRIMARY KEY (loginid, ws_ex_id, date)
230
CREATE TABLE problem_save (
231
problemid TEXT REFERENCES problem (identifier) NOT NULL,
237
CREATE TABLE exercise_save (
232
238
loginid INT4 REFERENCES login (loginid) NOT NULL,
233
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
239
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
234
240
date TIMESTAMP NOT NULL,
235
241
text TEXT NOT NULL,
236
PRIMARY KEY (problemid,loginid, worksheetid)
242
PRIMARY KEY (loginid, ws_ex_id)
239
245
CREATE TABLE test_suite (
240
246
suiteid SERIAL PRIMARY KEY,
241
problemid TEXT REFERENCES problem (identifier) NOT NULL,
247
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
242
248
description TEXT,