-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_event.go
302 lines (235 loc) · 8.78 KB
/
edit_event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package controllers
import (
"database/sql"
"strconv"
"strings"
"turm/app/models"
"github.com/revel/revel"
)
/*Delete event.
- Roles: creator and editors of the course of the event */
func (c EditEvent) Delete(ID, courseID int) revel.Result {
c.Log.Debug("delete event", "ID", ID, "courseID", courseID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the event ID is valid
event := models.Event{ID: ID}
if err := event.Delete(c.Validation); err != nil {
return flashError(
errDB, err, "/course/events?ID="+strconv.Itoa(courseID),
c.Controller, "")
} else if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/events?ID="+strconv.Itoa(courseID),
c.Controller, "")
}
c.Flash.Success(c.Message("event.delete.success", ID))
return c.Redirect(Course.Events, courseID)
}
/*Duplicate event.
- Roles: creator and editors of the course of the event */
func (c EditEvent) Duplicate(ID, courseID int) revel.Result {
c.Log.Debug("duplicate event", "ID", ID, "courseID", courseID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the event ID is valid
event := models.Event{ID: ID, CourseID: courseID}
if err := event.Duplicate(nil); err != nil {
return flashError(
errDB, err, "/course/events?ID="+strconv.Itoa(courseID),
c.Controller, "")
}
c.Flash.Success(c.Message("event.duplicate.success", ID))
return c.Redirect(Course.Events, courseID)
}
/*NewMeeting creates a new blank meeting that is part of an event.
- Roles: creator and editors of the course of the event */
func (c EditEvent) NewMeeting(ID int, option models.MeetingInterval,
conf models.EditEMailConfig) revel.Result {
c.Log.Debug("create a new meeting", "ID", ID, "option", option,
"conf", conf)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the event ID is valid
if option > models.ODD || option < models.SINGLE {
c.Validation.ErrorKey("validation.invalid.option")
}
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/meetings?ID="+strconv.Itoa(ID),
c.Controller, "")
}
conf.ID = ID
conf.IsEvent = true
meeting := models.Meeting{EventID: ID, MeetingInterval: option}
if err := meeting.NewBlank(&conf); err != nil {
return flashError(
errDB, err, "/course/meetings?ID="+strconv.Itoa(ID),
c.Controller, "")
}
//if the course is active, send notification e-mail
conf.Field = "email.edit.new.meeting"
if err := sendEMailsEdit(c.Controller, &conf); err != nil {
return flashError(errEMail, err, "", c.Controller, "")
}
c.Flash.Success(c.Message("meeting.new.success", meeting.ID))
return c.Redirect(Course.Meetings, ID)
}
/*ChangeCapacity changes the capacity of an event.
- Roles: creator and editors of the course of the event */
func (c EditEvent) ChangeCapacity(ID int, fieldID string, value int) revel.Result {
c.Log.Debug("change capacity", "ID", ID, "fieldID", fieldID, "value", value)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the event ID is valid
c.Validation.Check(value,
revel.Min{1},
revel.Max{1000000},
).MessageKey("validation.invalid.int")
if c.Validation.HasErrors() {
return c.RenderJSON(
response{Status: INVALID, Msg: getErrorString(c.Validation.Errors)})
}
if fieldID != "capacity" {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
event := models.Event{ID: ID}
users, err := event.Update(nil, fieldID, value, nil)
if err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errDB.String())})
}
//auto enroll users from wait list if the capacity is changed
for _, user := range users {
err = sendEMail(c.Controller, &user,
"email.subject.from.wait.list",
"fromWaitlist")
if err != nil {
return flashError(errEMail, err, "", c.Controller, user.User.EMail)
}
}
msg := c.Message("event.capacity.change.success", event.Capacity)
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: fieldID,
Value: strconv.Itoa(value), ID: ID, Fullness: strconv.Itoa(event.Fullness)})
}
/*ChangeText changes the text of the provided column.
- Roles: creator and editors of the course of the event */
func (c EditEvent) ChangeText(ID int, fieldID, value string,
conf models.EditEMailConfig) revel.Result {
c.Log.Debug("change text value", "ID", ID, "fieldID", fieldID, "value", value,
"conf", conf)
c.Session["lastURL"] = c.Request.URL.String()
value = strings.TrimSpace(value)
valid := (value != "")
//NOTE: the interceptor assures that the event ID is valid
if value != "" || fieldID == models.ColTitle {
models.ValidateLength(&value, "validation.invalid.text.short",
3, 255, c.Validation)
if c.Validation.HasErrors() {
return c.RenderJSON(
response{Status: INVALID, Msg: getErrorString(c.Validation.Errors)})
}
}
if fieldID != models.ColTitle && fieldID != models.ColAnnotation {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
conf.ID = ID
conf.IsEvent = true
event := models.Event{ID: ID}
_, err := event.Update(nil, fieldID, sql.NullString{
String: value,
Valid: valid,
}, &conf)
if err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errDB.String())})
}
//if the course is active, send notification e-mail
conf.Field = "email.edit." + fieldID
if err = sendEMailsEdit(c.Controller, &conf); err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errEMail.String())})
}
msg := c.Message("event." + fieldID + ".delete.success")
if valid {
msg = c.Message("event."+fieldID+".change.success", value)
}
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: fieldID, Value: value, ID: ID})
}
/*ChangeBool toggles the provided boolean value of an event.
- Roles: creator and editors of the course of the event */
func (c EditEvent) ChangeBool(ID int, listType string, option bool) revel.Result {
c.Log.Debug("update bool", "ID", ID, "listType", listType, "option", option)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the event ID is valid
if listType != models.ColHasWaitlist && listType != models.ColHasComments {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
event := models.Event{ID: ID}
var err error
if listType == models.ColHasWaitlist {
err = event.UpdateWaitlist(option, c.Validation)
} else if listType == models.ColHasComments {
err = event.UpdateComments(option, c.Validation)
}
if err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errDB.String())})
} else if c.Validation.HasErrors() {
return c.RenderJSON(
response{Status: INVALID, Msg: getErrorString(c.Validation.Errors),
FieldID: listType, Valid: option, ID: ID})
}
msg := c.Message("event.waitlist.change.success")
if listType == models.ColHasComments {
msg = c.Message("event.has.comments.change.success")
}
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: listType, Valid: option, ID: ID})
}
/*ChangeEnrollmentKey sets an enrollment key.
- Roles: creator and editors of the course of the event */
func (c EditEvent) ChangeEnrollmentKey(ID int, key1, key2, fieldID string) revel.Result {
c.Log.Debug("change enrollment key", "ID", ID, "key1", key1, "key2", key2, "fieldID", fieldID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the event ID is valid
key1 = strings.TrimSpace(key1)
key2 = strings.TrimSpace(key2)
if key1 == key2 {
models.ValidateLength(&key1, "validation.invalid.keys",
3, 511, c.Validation)
} else {
c.Validation.ErrorKey("validation.invalid.keys")
}
if c.Validation.HasErrors() {
return c.RenderJSON(
response{Status: INVALID, Msg: getErrorString(c.Validation.Errors)})
}
event := models.Event{ID: ID, EnrollmentKey: sql.NullString{Valid: true, String: key1}}
if err := event.UpdateKey(); err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errDB.String())})
}
msg := c.Message("event.key.change.success")
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: fieldID, Value: "key", ID: ID})
}
/*DeleteEnrollmentKey of an event.
- Roles: creator and editors of the course of the event */
func (c EditEvent) DeleteEnrollmentKey(ID int) revel.Result {
c.Log.Debug("delete enrollment key", "ID", ID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the event ID is valid
event := models.Event{ID: ID}
_, err := event.Update(nil, "enrollment_key", sql.NullString{
Valid: false,
}, nil)
if err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errDB.String())})
}
msg := c.Message("event.key.delete.success")
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: "enrollment_key", ID: ID})
}