3
-- Check that the provided name is sane for use in URLs.
4
CREATE OR REPLACE FUNCTION valid_url_name(name text) RETURNS boolean AS
7
RETURN name ~ E'^[a-z0-9][a-z0-9_\+\.\-]*$';
11
-- Just like valid_url_name, except that @ is permitted (so we can use a
12
-- reasonable subset of email addresses as usernames).
13
CREATE OR REPLACE FUNCTION valid_login_name(name text) RETURNS boolean AS
16
RETURN name ~ E'^[a-z0-9][a-z0-9@_\+\.\-]*$';
18
$$ LANGUAGE 'plpgsql';
2
20
CREATE SEQUENCE login_unixid_seq MINVALUE 1000 MAXVALUE 29999 START WITH 5000;
4
22
CREATE TABLE login (
5
23
loginid SERIAL PRIMARY KEY NOT NULL,
6
login VARCHAR UNIQUE NOT NULL,
24
login VARCHAR UNIQUE NOT NULL CHECK (valid_login_name(login)),
8
26
state VARCHAR NOT NULL CHECK (state in ('no_agreement', 'pending',
9
27
'enabled', 'disabled'))
10
28
DEFAULT 'no_agreement',
11
rolenm VARCHAR NOT NULL CHECK (rolenm in ('anyone', 'student',
13
'lecturer', 'admin')),
29
admin BOOLEAN NOT NULL DEFAULT false,
14
30
unixid INT UNIQUE DEFAULT nextval('login_unixid_seq') NOT NULL,
15
31
nick VARCHAR NOT NULL,
16
32
pass_exp TIMESTAMP,
30
46
subjectid SERIAL PRIMARY KEY NOT NULL,
31
47
subj_code VARCHAR UNIQUE NOT NULL,
32
48
subj_name VARCHAR NOT NULL,
33
subj_short_name VARCHAR UNIQUE NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
37
52
CREATE TABLE semester (
38
53
semesterid SERIAL PRIMARY KEY NOT NULL,
39
year CHAR(4) NOT NULL,
40
semester CHAR(1) NOT NULL,
42
UNIQUE (year, semester)
54
year TEXT NOT NULL CHECK (valid_url_name(year)),
55
url_name TEXT NOT NULL CHECK (valid_url_name(url_name)),
57
display_name TEXT NOT NULL,
58
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
59
'current', 'future')) DEFAULT 'current',
60
UNIQUE (year, url_name),
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
64
CREATE TABLE offering (
61
65
offeringid SERIAL PRIMARY KEY NOT NULL,
62
66
subject INT4 REFERENCES subject (subjectid) NOT NULL,
63
67
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
70
show_worksheet_marks BOOLEAN NOT NULL DEFAULT false,
71
worksheet_cutoff TIMESTAMP,
64
72
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
65
73
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
66
74
UNIQUE (subject, semesterid)
72
80
CREATE TABLE project_set (
73
81
projectsetid SERIAL PRIMARY KEY NOT NULL,
74
82
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
75
max_students_per_group INTEGER NOT NULL DEFAULT 4
83
max_students_per_group INTEGER
78
86
CREATE TABLE project (
79
87
projectid SERIAL PRIMARY KEY NOT NULL,
88
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
82
92
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
93
deadline TIMESTAMP NOT NULL
96
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
101
IF TG_OP = ''UPDATE'' THEN
102
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
106
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
107
PERFORM 1 FROM project, project_set
108
WHERE project_set.offeringid = oid AND
109
project.projectsetid = project_set.projectsetid AND
110
project.short_name = NEW.short_name;
112
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
116
' LANGUAGE 'plpgsql';
118
CREATE TRIGGER check_project_namespacing
119
BEFORE INSERT OR UPDATE ON project
120
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
86
122
CREATE TABLE project_group (
87
groupnm VARCHAR NOT NULL,
123
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
88
124
groupid SERIAL PRIMARY KEY NOT NULL,
89
125
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
129
165
CREATE TABLE enrolment (
130
166
loginid INT4 REFERENCES login (loginid),
131
167
offeringid INT4 REFERENCES offering (offeringid),
168
role TEXT NOT NULL CHECK (role IN ('student', 'tutor',
169
'lecturer')) DEFAULT 'student',
133
171
special_result VARCHAR,
138
176
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
179
CREATE TABLE assessed (
159
180
assessedid SERIAL PRIMARY KEY NOT NULL,
160
181
loginid INT4 REFERENCES login (loginid),
165
186
OR (loginid IS NULL AND groupid IS NOT NULL))
189
-- We must use conditional constraints here -- NULL != NULL.
190
CREATE UNIQUE INDEX assessed_loginid_key ON assessed(loginid, projectid) WHERE loginid IS NOT NULL;
191
CREATE UNIQUE INDEX assessed_groupid_key ON assessed(groupid, projectid) WHERE groupid IS NOT NULL;
168
193
CREATE TABLE project_extension (
194
extensionid SERIAL PRIMARY KEY,
169
195
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
170
deadline TIMESTAMP NOT NULL,
171
197
approver INT4 REFERENCES login (loginid) NOT NULL,
175
201
CREATE TABLE project_submission (
202
submissionid SERIAL PRIMARY KEY,
176
203
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
177
204
path VARCHAR NOT NULL,
178
revision INT4 NOT NULL
205
revision INT4 NOT NULL,
206
date_submitted TIMESTAMP NOT NULL,
207
submitter INT4 REFERENCES login (loginid) NOT NULL
181
210
CREATE TABLE project_mark (
193
222
CREATE TABLE exercise (
194
identifier TEXT PRIMARY KEY,
223
identifier TEXT PRIMARY KEY CHECK (valid_url_name(identifier)),
196
225
description TEXT,
226
description_xhtml_cache TEXT,
203
233
CREATE TABLE worksheet (
204
234
worksheetid SERIAL PRIMARY KEY,
205
235
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
206
identifier TEXT NOT NULL,
236
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
207
237
name TEXT NOT NULL,
208
238
data TEXT NOT NULL,
239
data_xhtml_cache TEXT,
209
240
assessable BOOLEAN NOT NULL,
241
published BOOLEAN NOT NULL DEFAULT true,
210
242
seq_no INT4 NOT NULL,
211
243
format TEXT NOT NUll,
212
244
UNIQUE (offeringid, identifier)