-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
703 lines (552 loc) · 20.3 KB
/
edit.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
package controllers
import (
"bufio"
"database/sql"
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"turm/app/models"
"github.com/revel/revel"
)
/*Open an already existing course for modification, etc.
- Roles: creator and editors of this course */
func (c Edit) Open(ID int) revel.Result {
c.Log.Debug("open course", "ID", ID)
//NOTE: the interceptor assures that the course ID is valid
//get the course data
course := models.Course{ID: ID}
if err := course.Get(nil, true, 0); err != nil {
renderQuietError(errDB, err, c.Controller)
return c.Render()
}
if course.Expired && course.Active {
c.Flash.Error(c.Message("intercept.invalid.action"))
return c.Redirect(c.Session["callPath"].(string))
}
//only set these after the course is loaded - TODO: why?
c.Session["callPath"] = c.Request.URL.String()
c.Session["currPath"] = c.Request.URL.String()
c.Session["lastURL"] = c.Request.URL.String()
c.ViewArgs["tab"] = c.Message("creator.tab")
return c.Render(course)
}
/*Download a course as JSON.
- Roles: creator of the course */
func (c Edit) Download(ID int, filename string) revel.Result {
c.Log.Debug("download course", "ID", ID, "filename", filename)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
course := models.Course{ID: ID}
if err := course.Get(nil, true, 0); err != nil {
return flashError(
errDB, err, "", c.Controller, "")
}
//reset some values
course.CreatorData = models.User{}
course.Path = models.Groups{}
//marshal the course data into json format
json, err := json.Marshal(course)
if err != nil {
return flashError(
errTypeConv, err, "", c.Controller, "")
}
jsonString := string(json[:])
//if the user did not provide a custom file name
if filename == "" {
now := time.Now().Format(revel.TimeFormats[1])
filename = now + " " + course.Title
filename = strings.ReplaceAll(filename, "/", " ")
}
//filepath
filepath := filepath.Join("/tmp", filename+".json")
//create the file
file, err := os.Create(filepath)
if err != nil {
return flashError(
errContent, err, "", c.Controller, "")
}
defer file.Close()
//write data to the file
writer := bufio.NewWriter(file)
_, err = writer.WriteString(jsonString)
if err != nil {
return flashError(
errContent, err, "", c.Controller, "")
}
defer writer.Flush()
//render the file
return c.RenderFileName(filepath, revel.Attachment)
}
/*Validate all course data.
- Roles: creator and editors of this course */
func (c Edit) Validate(ID int) revel.Result {
c.Log.Debug("validate course", "ID", ID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
course := models.Course{ID: ID}
if err := course.Get(nil, true, 0); err != nil {
return flashError(
errDB, err, "", c.Controller, "")
}
course.Validate(c.Validation)
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "", c.Controller, "")
}
c.Flash.Success(c.Message("creator.course.valid"))
return c.Redirect(c.Session["currPath"])
}
/*NewEvent creates a new blank event in a course.
- Roles: creator and editors of this course. */
func (c Edit) NewEvent(ID int, value, eventType string,
conf models.EditEMailConfig) revel.Result {
c.Log.Debug("create a new event", "ID", ID, "value", value,
"eventType", eventType, "conf", conf)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
models.ValidateLength(&value, "validation.invalid.text.short",
3, 255, c.Validation)
if eventType != models.EventTypeNormal && eventType != models.EventTypeCalendar {
c.Validation.ErrorKey("validation.invalid.params")
}
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/events?ID="+strconv.Itoa(ID),
c.Controller, "")
}
//normal event
if eventType == models.EventTypeNormal {
conf.ID = ID
event := models.Event{CourseID: ID, Title: value}
if err := event.NewBlank(&conf); err != nil {
return flashError(
errDB, err, "/course/events?ID="+strconv.Itoa(ID),
c.Controller, "")
}
//if the course is active, send notification e-mail
conf.Field = "email.edit.new.event"
if err := sendEMailsEdit(c.Controller, &conf); err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errEMail.String())})
}
c.Flash.Success(c.Message("event.new.success", event.Title, event.ID))
} else { //calendar event
event := models.CalendarEvent{CourseID: ID, Title: value}
if err := event.NewBlank(); err != nil {
return flashError(
errDB, err, "/course/calendarEvents?ID="+strconv.Itoa(ID),
c.Controller, "")
}
c.Flash.Success(c.Message("event.new.calendar.success", event.Title, event.ID))
}
//reload correct content
if eventType == models.EventTypeNormal {
return c.Redirect(Course.Events, ID)
}
return c.Redirect(Course.CalendarEvents, ID)
}
/*ChangeTimestamp changes the specified timestamp.
- Roles: creator and editors of the course */
func (c Edit) ChangeTimestamp(ID int, fieldID, date, time string,
conf models.EditEMailConfig) revel.Result {
c.Log.Debug("change timestamp", "ID", ID, "fieldID", fieldID, "date", date,
"time", time, "conf", conf)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
timestamp := date + " " + time
valid := (timestamp != " ")
if valid || fieldID != models.ColUnsubscribeEnd { //only the unsubscribeend can be null
c.Validation.Required(date).
MessageKey("validation.invalid.date")
c.Validation.Required(time).
MessageKey("validation.invalid.time")
}
if c.Validation.HasErrors() {
return c.RenderJSON(
response{Status: INVALID, Msg: getErrorString(c.Validation.Errors)})
}
if fieldID != models.ColEnrollmentStart && fieldID != models.ColEnrollmentEnd &&
fieldID != models.ColUnsubscribeEnd && fieldID != models.ColExpirationDate {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
t, err := getTimestamp(timestamp, c.Controller, valid, fieldID)
if err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
course := models.Course{ID: ID}
conf.ID = ID
if err = course.UpdateTimestamp(c.Validation, &conf, fieldID,
t, valid); 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)})
}
//if the course is active, send notification e-mail
if fieldID != models.ColExpirationDate {
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("course."+fieldID+".delete.success", course.ID)
if valid {
msg = c.Message("course."+fieldID+".change.success", timestamp, course.ID)
}
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: fieldID, Value: strings.TrimSpace(timestamp)})
}
/*ChangeUserList adds a user to the user list of a course.
- Roles: creator and editors of the course */
func (c Edit) ChangeUserList(ID, userID int, listType string) revel.Result {
c.Log.Debug("add user to user list", "ID", ID, "userID", userID,
"listType", listType)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
c.Validation.Required(userID).
MessageKey("validation.missing.userID")
if listType != models.TableBlocklists && listType != models.TableAllowlists &&
listType != models.TableInstructors && listType != models.TableEditors {
c.Validation.ErrorKey("validation.invalid.params")
}
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/editorInstructorList?ID="+strconv.Itoa(ID),
c.Controller, "")
}
entry := models.UserListEntry{UserID: userID, CourseID: ID}
active, data, err := entry.Insert(listType)
if err != nil {
return flashError(
errDB, err, "/course/editorInstructorList?ID="+strconv.Itoa(ID),
c.Controller, "")
}
//if the course is active, the user gets a notification e-mail
if active {
err = sendEMail(c.Controller, &data,
"email.subject.new.course.role",
"newCourseRole")
if err != nil {
return flashError(errEMail, err, "", c.Controller, data.User.EMail)
}
}
c.Flash.Success(c.Message("course."+listType+".change.success",
entry.EMail,
entry.CourseID,
))
if listType == models.TableInstructors || listType == models.TableEditors {
return c.Redirect(Course.EditorInstructorList, ID)
} else if listType == models.TableAllowlists {
return c.Redirect(Course.Allowlist, ID)
}
return c.Redirect(Course.Blocklist, ID)
}
/*DeleteFromUserList removes a user from the user list of a course.
- Roles: creator and editors of the course */
func (c Edit) DeleteFromUserList(ID, userID int, listType string) revel.Result {
c.Log.Debug("delete user from user list", "ID", ID, "userID", userID,
"listType", listType)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
c.Validation.Required(userID).
MessageKey("validation.missing.userID")
if listType != models.TableBlocklists && listType != models.TableAllowlists &&
listType != models.TableInstructors && listType != models.TableEditors {
c.Validation.ErrorKey("validation.invalid.params")
}
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/editorInstructorList?ID="+strconv.Itoa(ID),
c.Controller, "")
}
entry := models.UserListEntry{UserID: userID, CourseID: ID}
active, data, err := entry.Delete(listType)
if err != nil {
return flashError(
errDB, err, "/course/editorInstructorList?ID="+strconv.Itoa(ID),
c.Controller, "")
}
//if the course is active, the user gets a notification e-mail
if active {
err = sendEMail(c.Controller, &data,
"email.subject.course.role.deleted",
"deleteCourseRole")
if err != nil {
return flashError(errEMail, err, "", c.Controller, data.User.EMail)
}
}
c.Flash.Success(c.Message("course."+listType+".delete.success", ID))
if listType == models.TableInstructors || listType == models.TableEditors {
return c.Redirect(Course.EditorInstructorList, ID)
} else if listType == models.TableAllowlists {
return c.Redirect(Course.Allowlist, ID)
}
return c.Redirect(Course.Blocklist, ID)
}
/*ChangeViewMatrNr toggles the matriculation number restrictions of an editor/instructor.
- Roles: creator and editors of the course */
func (c Edit) ChangeViewMatrNr(ID, userID int, listType string, option bool) revel.Result {
c.Log.Debug("update user in user list", "ID", ID, "userID", userID,
"listType", listType, "option", option)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
c.Validation.Required(userID).
MessageKey("validation.missing.userID")
if listType != models.TableInstructors && listType != models.TableEditors {
c.Validation.ErrorKey("validation.invalid.params")
}
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/editorInstructorList?ID="+strconv.Itoa(ID),
c.Controller, "")
}
entry := models.UserListEntry{UserID: userID, CourseID: ID, ViewMatrNr: option}
active, data, err := entry.Update(listType)
if err != nil {
return flashError(
errDB, err, "/course/editorInstructorList?ID="+strconv.Itoa(ID),
c.Controller, "")
}
//if the course is active, the user gets a notification e-mail
if active {
err = sendEMail(c.Controller, &data,
"email.subject.course.role.authorization",
"changeViewMatrNr")
if err != nil {
return flashError(errEMail, err, "", c.Controller, data.User.EMail)
}
}
c.Flash.Success(c.Message("course.matr.nr.change.success", entry.EMail, entry.CourseID))
return c.Redirect(Course.EditorInstructorList, ID)
}
/*ChangeBool toggles the provided boolean value of a course.
- Roles: creator and editors of the course */
func (c Edit) 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 course ID is valid
if listType != models.ColVisible && listType != models.ColOnlyLDAP {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
course := models.Course{ID: ID}
if err := course.Update(nil, listType, option, nil); err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errDB.String())})
}
msg := c.Message("course."+listType+".change.success", course.ID)
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: listType, Valid: option})
}
/*ChangeText changes the text of the provided column.
- Roles: creator and editors of the course */
func (c Edit) 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()
//NOTE: the interceptor assures that the course ID is valid
value = strings.TrimSpace(value)
valid := (value != "")
if valid || fieldID == models.ColTitle {
if fieldID == models.ColTitle || fieldID == models.ColSubtitle {
models.ValidateLength(&value, "validation.invalid.text",
3, 511, c.Validation)
} else if fieldID == models.ColFee {
c.Validation.Match(value, models.FeePattern).
MessageKey("validation.invalid.fee")
} else {
models.ValidateLength(&value, "validation.invalid.text.area",
3, 50000, c.Validation)
}
if c.Validation.HasErrors() {
return c.RenderJSON(
response{Status: INVALID, Msg: getErrorString(c.Validation.Errors)})
}
}
if fieldID != models.ColDescription && fieldID != models.ColCustomEMail &&
fieldID != models.ColSpeaker && fieldID != models.ColTitle &&
fieldID != models.ColSubtitle && fieldID != models.ColFee {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
course := models.Course{ID: ID}
conf.ID = ID
var err error
if fieldID == models.ColFee && valid {
value = strings.ReplaceAll(value, ",", ".")
var fee float64
fee, err = strconv.ParseFloat(value, 64)
if err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
err = course.Update(nil, fieldID, sql.NullFloat64{
Float64: fee,
Valid: valid,
}, &conf)
} else {
err = course.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("course."+fieldID+".delete.success", course.ID)
if valid {
if fieldID == models.ColTitle || fieldID == models.ColSubtitle || fieldID == models.ColFee {
msg = c.Message("course."+fieldID+".change.success", value, course.ID)
} else {
msg = c.Message("course."+fieldID+".change.success", course.ID)
}
}
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: fieldID, Value: value})
}
/*ChangeGroup of a course.
- Roles: creator and editors of the course */
func (c Edit) ChangeGroup(ID, parentID int, conf models.EditEMailConfig) revel.Result {
c.Log.Debug("change group", "ID", ID, "parentID", parentID,
"conf", conf)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
c.Validation.Required(parentID).
MessageKey("validation.invalid.params")
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/path?ID="+strconv.Itoa(ID),
c.Controller, "")
}
course := models.Course{
ID: ID,
ParentID: sql.NullInt32{Int32: int32(parentID), Valid: true},
}
if err := course.Update(nil, "parent_id", course.ParentID, &conf); err != nil {
return flashError(
errDB, err, "/course/path?ID="+strconv.Itoa(ID),
c.Controller, "")
}
c.Flash.Success(c.Message("course.group.change.success", course.ID))
return c.Redirect(Course.Path, ID)
}
/*ChangeEnrollLimit changes the enrollment limit of a course.
- Roles: creator and editors of the course */
func (c Edit) ChangeEnrollLimit(ID int, fieldID string, value int) revel.Result {
c.Log.Debug("change enrollment limit", "ID", ID, "fieldID", fieldID, "value", value)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
c.Validation.Check(value,
revel.Min{0},
revel.Max{1000000},
).MessageKey("validation.invalid.int")
if c.Validation.HasErrors() {
return c.RenderJSON(
response{Status: INVALID, Msg: getErrorString(c.Validation.Errors)})
}
valid := (value != 0)
if fieldID != "enroll_limit_events" {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message("error.undefined")})
}
course := models.Course{ID: ID}
err := course.Update(nil, fieldID, sql.NullInt32{
Int32: int32(value),
Valid: valid,
}, nil)
if err != nil {
return c.RenderJSON(
response{Status: ERROR, Msg: c.Message(errDB.String())})
}
msg := c.Message("course."+fieldID+".delete.success", course.ID)
if valid {
msg = c.Message("course."+fieldID+".change.success", value, course.ID)
}
return c.RenderJSON(
response{Status: SUCCESS, Msg: msg, FieldID: fieldID, Value: strconv.Itoa(value)})
}
/*ChangeRestriction adds/edits a degree/course of study/semester restriction of a course.
- Roles: creator and editors of the course */
func (c Edit) ChangeRestriction(ID int, restriction models.Restriction) revel.Result {
c.Log.Debug("change enrollment restriction", "ID", ID, "restriction", restriction)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
restriction.CourseID = ID
restriction.Validate(c.Validation)
if c.Validation.HasErrors() {
return flashError(
errValidation, nil, "/course/restrictions?ID="+strconv.Itoa(ID),
c.Controller, "")
}
if restriction.ID == 0 { //insert
if err := restriction.Insert(nil, 0); err != nil {
return flashError(
errDB, err, "/course/restrictions?ID="+strconv.Itoa(ID),
c.Controller, "")
}
} else { //update
if err := restriction.Update(); err != nil {
return flashError(
errDB, err, "/course/restrictions?ID="+strconv.Itoa(ID),
c.Controller, "")
}
}
c.Flash.Success(c.Message("course.restriction.change.success",
restriction.CourseID,
))
return c.Redirect(Course.Restrictions, ID)
}
/*DeleteRestriction of a course.
- Roles: creator and editors of this course */
func (c Edit) DeleteRestriction(ID, restrictionID int) revel.Result {
c.Log.Debug("delete enrollment restriction", "ID", ID, "restrictionID", restrictionID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
restriction := models.Restriction{ID: restrictionID}
if err := restriction.Delete(); err != nil {
return flashError(
errDB, err, "/course/restrictions?ID="+strconv.Itoa(ID),
c.Controller, "")
}
c.Flash.Success(c.Message("course.restriction.delete.success", ID))
return c.Redirect(Course.Restrictions, ID)
}
/*SearchUser searches for users for the different user lists.
- Roles: creator and editors of the course */
func (c Edit) SearchUser(ID int, value, listType string, searchInactive bool) revel.Result {
c.Log.Debug("search users", "ID", ID, "value", value, "listType", listType,
"searchInactive", searchInactive)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
models.ValidateLength(&value, "validation.invalid.searchValue",
3, 127, c.Validation)
if listType != models.TableBlocklists && listType != models.TableAllowlists &&
listType != models.TableInstructors && listType != models.TableEditors {
c.Validation.ErrorKey("validation.invalid.params")
}
if c.Validation.HasErrors() {
c.Validation.Keep()
return c.Render()
}
var users models.UserList
if err := users.Search(&value, &listType, &searchInactive, &ID); err != nil {
renderQuietError(errDB, err, c.Controller)
return c.Render()
}
//TODO: do not search by matriculation numbers if the user is not allowed to see them
return c.Render(users, listType)
}