3
CREATE OR REPLACE FUNCTION valid_url_name(name text) RETURNS boolean AS
6
RETURN name ~ E'^[a-z0-9][a-z0-9_\+\.\-]*$';
10
CREATE OR REPLACE FUNCTION valid_login_name(name text) RETURNS boolean AS
13
RETURN name ~ E'^[a-z0-9][a-z0-9@_\+\.\-]*$';
15
$$ LANGUAGE 'plpgsql';
17
2
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
19
4
CREATE TABLE login (
20
5
loginid SERIAL PRIMARY KEY NOT NULL,
21
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
6
login VARCHAR UNIQUE NOT NULL,
23
8
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
24
9
'enabled', 'disabled'))
25
10
DEFAULT 'no_agreement',
26
admin BOOLEAN NOT NULL DEFAULT false,
11
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
13
'lecturer', 'admin')),
27
14
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
28
15
nick VARCHAR NOT NULL,
29
16
pass_exp TIMESTAMP,
46
30
subjectid SERIAL PRIMARY KEY NOT NULL,
47
31
subj_code VARCHAR UNIQUE NOT NULL,
48
32
subj_name VARCHAR NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
33
subj_short_name VARCHAR UNIQUE NOT NULL,
52
37
CREATE TABLE semester (
53
38
semesterid SERIAL PRIMARY KEY NOT NULL,
54
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
55
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
56
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
57
'current', 'future')) DEFAULT 'current',
39
year CHAR(4) NOT NULL,
40
semester CHAR(1) NOT NULL,
58
42
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();
61
60
CREATE TABLE offering (
62
61
offeringid SERIAL PRIMARY KEY NOT NULL,
63
62
subject INT4 REFERENCES subject (subjectid) NOT NULL,
64
63
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
67
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
68
worksheet_cutoff TIMESTAMP,
69
64
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
70
65
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
71
66
UNIQUE (subject, semesterid)
77
72
CREATE TABLE project_set (
78
73
projectsetid SERIAL PRIMARY KEY NOT NULL,
79
74
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
80
max_students_per_group INTEGER
75
max_students_per_group INTEGER NOT NULL DEFAULT 4
83
78
CREATE TABLE project (
84
79
projectid SERIAL PRIMARY KEY NOT NULL,
85
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
89
82
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
90
deadline TIMESTAMP NOT NULL
93
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
98
IF TG_OP = ''UPDATE'' THEN
99
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
103
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
104
PERFORM 1 FROM project, project_set
105
WHERE project_set.offeringid = oid AND
106
project.projectsetid = project_set.projectsetid AND
107
project.short_name = NEW.short_name;
109
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
113
' LANGUAGE 'plpgsql';
115
CREATE TRIGGER check_project_namespacing
116
BEFORE INSERT OR UPDATE ON project
117
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
119
86
CREATE TABLE project_group (
120
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
87
groupnm VARCHAR NOT NULL,
121
88
groupid SERIAL PRIMARY KEY NOT NULL,
122
89
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
162
129
CREATE TABLE enrolment (
163
130
loginid INT4 REFERENCES login (loginid),
164
131
offeringid INT4 REFERENCES offering (offeringid),
165
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
166
'lecturer')) DEFAULT 'student',
168
133
special_result VARCHAR,
173
138
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();
176
158
CREATE TABLE assessed (
177
159
assessedid SERIAL PRIMARY KEY NOT NULL,
178
160
loginid INT4 REFERENCES login (loginid),
183
165
OR (loginid IS NULL AND groupid IS NOT NULL))
186
CREATE UNIQUE INDEX assessed_loginid_key ON assessed(loginid, projectid) WHERE loginid IS NOT NULL;
187
CREATE UNIQUE INDEX assessed_groupid_key ON assessed(groupid, projectid) WHERE groupid IS NOT NULL;
189
168
CREATE TABLE project_extension (
190
extensionid SERIAL PRIMARY KEY,
191
169
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
192
170
deadline TIMESTAMP NOT NULL,
193
171
approver INT4 REFERENCES login (loginid) NOT NULL,
198
175
CREATE TABLE project_submission (
199
submissionid SERIAL PRIMARY KEY,
200
176
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
201
177
path VARCHAR NOT NULL,
202
revision INT4 NOT NULL,
203
date_submitted TIMESTAMP NOT NULL,
204
submitter INT4 REFERENCES login (loginid) NOT NULL
178
revision INT4 NOT NULL
207
181
CREATE TABLE project_mark (
219
193
CREATE TABLE exercise (
220
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
194
identifier TEXT PRIMARY KEY,
222
196
description TEXT,
223
description_xhtml_cache TEXT,
230
203
CREATE TABLE worksheet (
231
204
worksheetid SERIAL PRIMARY KEY,
232
205
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
233
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
206
identifier TEXT NOT NULL,
234
207
name TEXT NOT NULL,
235
208
data TEXT NOT NULL,
236
data_xhtml_cache TEXT,
237
209
assessable BOOLEAN NOT NULL,
238
published BOOLEAN NOT NULL DEFAULT true,
239
210
seq_no INT4 NOT NULL,
240
211
format TEXT NOT NUll,
241
212
UNIQUE (offeringid, identifier)