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'))
28
46
subjectid SERIAL PRIMARY KEY NOT NULL,
29
47
subj_code VARCHAR UNIQUE NOT NULL,
30
48
subj_name VARCHAR NOT NULL,
31
subj_short_name VARCHAR UNIQUE NOT NULL,
49
subj_short_name VARCHAR UNIQUE NOT NULL CHECK (valid_url_name(subj_short_name))
35
52
CREATE TABLE semester (
36
53
semesterid SERIAL PRIMARY KEY NOT NULL,
37
year CHAR(4) NOT NULL,
38
semester CHAR(1) NOT NULL,
54
year CHAR(4) NOT NULL CHECK (valid_url_name(year)),
55
semester CHAR(1) NOT NULL CHECK (valid_url_name(semester)),
39
56
state TEXT NOT NULL CHECK (state IN ('disabled', 'past',
40
57
'current', 'future')) DEFAULT 'current',
41
58
UNIQUE (year, semester)
45
62
offeringid SERIAL PRIMARY KEY NOT NULL,
46
63
subject INT4 REFERENCES subject (subjectid) NOT NULL,
47
64
semesterid INTEGER REFERENCES semester (semesterid) NOT NULL,
48
67
groups_student_permissions VARCHAR NOT NULL DEFAULT 'none',
49
68
CHECK (groups_student_permissions in ('none', 'invite', 'create')),
50
69
UNIQUE (subject, semesterid)
56
75
CREATE TABLE project_set (
57
76
projectsetid SERIAL PRIMARY KEY NOT NULL,
58
77
offeringid INTEGER REFERENCES offering (offeringid) NOT NULL,
59
max_students_per_group INTEGER NOT NULL DEFAULT 4
78
max_students_per_group INTEGER
62
81
CREATE TABLE project (
63
82
projectid SERIAL PRIMARY KEY NOT NULL,
83
short_name TEXT NOT NULL CHECK (valid_url_name(short_name)),
66
87
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
88
deadline TIMESTAMP NOT NULL
91
CREATE OR REPLACE FUNCTION check_project_namespacing_insertupdate()
96
IF TG_OP = ''UPDATE'' THEN
97
IF NEW.projectsetid = OLD.projectsetid AND NEW.short_name = OLD.short_name THEN
101
SELECT offeringid INTO oid FROM project_set WHERE project_set.projectsetid = NEW.projectsetid;
102
PERFORM 1 FROM project, project_set
103
WHERE project_set.offeringid = oid AND
104
project.projectsetid = project_set.projectsetid AND
105
project.short_name = NEW.short_name;
107
RAISE EXCEPTION ''a project named % already exists in offering ID %'', NEW.short_name, oid;
111
' LANGUAGE 'plpgsql';
113
CREATE TRIGGER check_project_namespacing
114
BEFORE INSERT OR UPDATE ON project
115
FOR EACH ROW EXECUTE PROCEDURE check_project_namespacing_insertupdate();
70
117
CREATE TABLE project_group (
71
groupnm VARCHAR NOT NULL,
118
groupnm VARCHAR NOT NULL CHECK (valid_url_name(groupnm)),
72
119
groupid SERIAL PRIMARY KEY NOT NULL,
73
120
projectsetid INTEGER REFERENCES project_set (projectsetid) NOT NULL,
137
184
CREATE TABLE project_extension (
185
extensionid SERIAL PRIMARY KEY,
138
186
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
139
187
deadline TIMESTAMP NOT NULL,
140
188
approver INT4 REFERENCES login (loginid) NOT NULL,
144
192
CREATE TABLE project_submission (
193
submissionid SERIAL PRIMARY KEY,
145
194
assessedid INT4 REFERENCES assessed (assessedid) NOT NULL,
146
195
path VARCHAR NOT NULL,
147
revision INT4 NOT NULL
196
revision INT4 NOT NULL,
197
date_submitted TIMESTAMP NOT NULL,
198
submitter INT4 REFERENCES login (loginid) NOT NULL
150
201
CREATE TABLE project_mark (
172
223
CREATE TABLE worksheet (
173
224
worksheetid SERIAL PRIMARY KEY,
174
225
offeringid INT4 REFERENCES offering (offeringid) NOT NULL,
175
identifier TEXT NOT NULL,
226
identifier TEXT NOT NULL CHECK (valid_url_name(identifier)),
176
227
name TEXT NOT NULL,
177
228
data TEXT NOT NULL,
178
229
assessable BOOLEAN NOT NULL,