1
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
2
4
loginid SERIAL PRIMARY KEY NOT NULL,
3
5
login VARCHAR UNIQUE NOT NULL,
5
7
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
6
'enabled', 'disabled')),
7
rolenm VARCHAR NOT NULL CHECK (rolenm in ('guest', 'student',
10
unixid INT UNIQUE NOT NULL, -- unix user id
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,
11
14
nick VARCHAR NOT NULL,
12
15
pass_exp TIMESTAMP,
13
16
acct_exp TIMESTAMP,
14
17
last_login TIMESTAMP,
16
20
fullname VARCHAR NOT NULL,
17
studentid VARCHAR -- may be 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();
20
59
CREATE TABLE offering (
21
60
offeringid SERIAL PRIMARY KEY NOT NULL,
22
subj_name VARCHAR NOT NULL,
23
subj_code VARCHAR NOT NULL,
24
year CHAR(4) NOT NULL,
25
semester CHAR(1) 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
29
77
CREATE TABLE project (
30
78
projectid SERIAL PRIMARY KEY NOT NULL,
33
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
81
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
37
85
CREATE TABLE project_group (
38
86
groupnm VARCHAR NOT NULL,
39
87
groupid SERIAL PRIMARY KEY NOT NULL,
40
offeringid INT4 REFERENCES offering (offeringid),
88
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
42
90
createdby INT4 REFERENCES login (loginid) NOT NULL,
43
91
epoch TIMESTAMP NOT NULL,
44
UNIQUE (offeringid, groupnm)
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();
47
113
CREATE TABLE group_invitation (
48
114
loginid INT4 REFERENCES login (loginid) NOT NULL,
49
115
groupid INT4 REFERENCES project_group (groupid) NOT NULL,
70
134
special_supp_result VARCHAR,
136
active BOOL NOT NULL DEFAULT true,
72
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();
75
157
CREATE TABLE assessed (
76
158
assessedid SERIAL PRIMARY KEY NOT NULL,
77
159
loginid INT4 REFERENCES login (loginid),
78
160
groupid INT4 REFERENCES project_group (groupid),
161
projectid INT4 REFERENCES project (projectid) NOT NULL,
79
162
-- exactly one of loginid and groupid must be non-null
80
163
CHECK ((loginid IS NOT NULL AND groupid IS NULL)
81
164
OR (loginid IS NULL AND groupid IS NOT NULL))
84
167
CREATE TABLE project_extension (
85
168
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
86
projectid INT4 REFERENCES project (projectid) NOT NULL,
87
169
deadline TIMESTAMP NOT NULL,
88
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
92
180
CREATE TABLE project_mark (
93
181
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
94
projectid INT4 REFERENCES project (projectid) NOT NULL,
96
183
marker INT4 REFERENCES login (loginid) NOT NULL,
103
193
CREATE TABLE problem (
104
194
problemid SERIAL PRIMARY KEY NOT NULL,
195
identifier VARCHAR UNIQUE NOT NULL,
199
CREATE TABLE worksheet (
200
worksheetid SERIAL PRIMARY KEY NOT NULL,
201
subject VARCHAR 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 INT4 REFERENCES problem (problemid) NOT NULL,
212
PRIMARY KEY (worksheetid, problemid)
108
215
CREATE TABLE problem_tag (
109
216
problemid INT4 REFERENCES problem (problemid),
110
217
tag VARCHAR NOT NULL,
139
246
date TIMESTAMP NOT NULL,
140
247
attempt VARCHAR NOT NULL,
141
248
complete BOOLEAN NOT NULL,
249
active BOOLEAN NOT NULL DEFAULT true,
142
250
PRIMARY KEY (problemid,loginid,date)
253
CREATE TABLE problem_save (
254
problemid INT4 REFERENCES problem (problemid) NOT NULL,
255
loginid INT4 REFERENCES login (loginid) NOT NULL,
256
date TIMESTAMP NOT NULL,
257
text VARCHAR NOT NULL,
258
PRIMARY KEY (problemid,loginid)
145
261
CREATE INDEX problem_attempt_index ON problem_attempt (problemid, loginid);
147
263
CREATE TABLE problem_attempt_breakdown (