36
37
semesterid SERIAL PRIMARY KEY NOT NULL,
37
38
year CHAR(4) NOT NULL,
38
39
semester CHAR(1) NOT NULL,
39
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
40
'current', 'future')) DEFAULT 'current',
41
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();
44
59
CREATE TABLE offering (
45
60
offeringid SERIAL PRIMARY KEY NOT NULL,
46
61
subject INT4 REFERENCES subject (subjectid) NOT NULL,
124
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();
127
157
CREATE TABLE assessed (
128
158
assessedid SERIAL PRIMARY KEY NOT NULL,
129
159
loginid INT4 REFERENCES login (loginid),
203
--TODO: Link worksheets to offerings
172
204
CREATE TABLE worksheet (
173
worksheetid SERIAL PRIMARY KEY,
174
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
175
identifier TEXT NOT NULL,
178
assessable BOOLEAN NOT NULL,
179
seq_no INT4 NOT NULL,
180
format TEXT NOT NUll,
181
UNIQUE (offeringid, identifier)
184
CREATE TABLE worksheet_exercise (
185
ws_ex_id SERIAL PRIMARY KEY,
186
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
187
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
188
seq_no INT4 NOT NULL,
189
active BOOLEAN NOT NULL DEFAULT true,
190
optional BOOLEAN NOT NULL,
191
UNIQUE (worksheetid, exerciseid)
194
CREATE TABLE exercise_attempt (
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,
195
223
loginid INT4 REFERENCES login (loginid) NOT NULL,
196
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
224
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
197
225
date TIMESTAMP NOT NULL,
198
attempt TEXT NOT NULL,
226
attempt VARCHAR NOT NULL,
199
227
complete BOOLEAN NOT NULL,
200
228
active BOOLEAN NOT NULL DEFAULT true,
201
PRIMARY KEY (loginid, ws_ex_id, date)
229
PRIMARY KEY (problemid,loginid,date)
204
CREATE TABLE exercise_save (
232
CREATE TABLE problem_save (
233
problemid INT4 REFERENCES problem (problemid) NOT NULL,
205
234
loginid INT4 REFERENCES login (loginid) NOT NULL,
206
ws_ex_id INT4 REFERENCES worksheet_exercise (ws_ex_id) NOT NULL,
235
worksheetid INT4 REFERENCES worksheet (worksheetid) NOT NULL,
207
236
date TIMESTAMP NOT NULL,
209
PRIMARY KEY (loginid, ws_ex_id)
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 --
212
244
CREATE TABLE test_suite (
213
suiteid SERIAL PRIMARY KEY,
214
exerciseid TEXT REFERENCES exercise (identifier) NOT NULL,
245
suiteid SERIAL NOT NULL,
246
problemid TEXT REFERENCES problem (identifier) NOT NULL,
215
247
description TEXT,
249
PRIMARY KEY (problemid, suiteid)
221
252
CREATE TABLE test_case (
222
testid SERIAL PRIMARY KEY,
223
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
230
CREATE TABLE suite_variable (
231
varid SERIAL PRIMARY KEY,
253
testid SERIAL NOT NULL,
232
254
suiteid INT4 REFERENCES test_suite (suiteid) NOT NULL,
235
var_type TEXT NOT NULL,
239
CREATE TABLE test_case_part (
240
partid SERIAL PRIMARY KEY,
241
testid INT4 REFERENCES test_case (testid) NOT NULL,
242
part_type TEXT NOT NULL,
262
PRIMARY KEY (testid, suiteid)