-
Notifications
You must be signed in to change notification settings - Fork 0
/
enrollment.go
263 lines (215 loc) · 6.95 KB
/
enrollment.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
package controllers
import (
"database/sql"
"net/url"
"strconv"
"strings"
"time"
"turm/app"
"turm/app/models"
"github.com/revel/revel"
)
/*Enroll a user in an event.
- Roles: logged in and activated users */
func (c Enrollment) Enroll(ID int, key, comment string) revel.Result {
c.Log.Debug("enroll a user in an event", "ID", ID, "key", key, "comment", comment)
c.Session["lastURL"] = c.Request.URL.String()
userID, err := getIntFromSession(c.Controller, "userID")
if err != nil {
return flashError(errTypeConv, err, "", c.Controller, "")
}
//enroll user
enrolled := models.Enrolled{
EventID: ID,
UserID: userID,
Comment: sql.NullString{String: comment, Valid: (len(comment) != 0)}}
data, waitList, _, msg, err := enrolled.EnrollOrUnsubscribe(models.ENROLL, key)
if err != nil {
return flashError(errDB, err, "", c.Controller, "")
} else if msg != "" {
c.Validation.ErrorKey(msg)
return flashError(errValidation, nil, "", c.Controller, "")
}
//send e-mail to the user
if waitList {
err = sendEMail(c.Controller, &data,
"email.subject.wait.list",
"waitlist")
} else {
err = sendEMail(c.Controller, &data,
"email.subject.enroll",
"enroll")
}
if err != nil {
return flashError(errEMail, err, "", c.Controller, data.User.EMail)
}
c.Flash.Success(c.Message("event.enroll.success"))
return c.Redirect(c.Session["currPath"])
}
/*Unsubscribe a user from an event.
- Roles: logged in and activated users */
func (c Enrollment) Unsubscribe(ID int) revel.Result {
c.Log.Debug("unsubscribe a user from an event", "ID", ID)
c.Session["lastURL"] = c.Request.URL.String()
userID, err := getIntFromSession(c.Controller, "userID")
if err != nil {
return flashError(errTypeConv, err, "", c.Controller, "")
}
//unsubscribe user
enrolled := models.Enrolled{EventID: ID, UserID: userID}
data, waitList, users, msg, err := enrolled.EnrollOrUnsubscribe(models.UNSUBSCRIBE, "")
if err != nil {
return flashError(errDB, err, "", c.Controller, "")
} else if msg != "" {
c.Validation.ErrorKey(msg)
return flashError(errValidation, nil, "", c.Controller, "")
}
//send e-mail to the user who unsubscribed
if waitList {
err = sendEMail(c.Controller, &data,
"email.subject.unsub.wait.list",
"unsubWaitlist")
} else {
err = sendEMail(c.Controller, &data,
"email.subject.unsubscribe",
"unsubscribe")
}
if err != nil {
return flashError(errEMail, err, "", c.Controller, data.User.EMail)
}
//send e-mail to each auto enrolled user
for _, user := range users {
mailData := models.EMailData{
User: user,
CourseTitle: data.CourseTitle,
EventTitle: data.EventTitle,
CourseID: data.CourseID,
}
err = sendEMail(c.Controller, &mailData,
"email.subject.from.wait.list",
"fromWaitlist")
if err != nil {
return flashError(errEMail, err, "", c.Controller, mailData.User.EMail)
}
}
c.Flash.Success(c.Message("event.unsubscribe.success"))
return c.Redirect(c.Session["currPath"])
}
/*EnrollInSlot enrolls a user in a time slot of a day in a calendar event.
- Roles: logged in and activated users */
func (c Enrollment) EnrollInSlot(ID, courseID, year, day int, startTime, endTime,
date string, monday string) revel.Result {
c.Log.Debug("enroll a user in a slot", "ID", ID, "courseID", courseID,
"year", year, "day", day, "startTime", startTime, "endTime", endTime, "date", date,
"monday", monday)
c.Session["lastURL"] = c.Request.URL.String()
//path in case of an error
path := "/course/calendarEvent?ID=" + url.QueryEscape(strconv.Itoa(ID)) +
"&courseID=" + url.QueryEscape(strconv.Itoa(courseID)) +
"&shift=0" + "&monday=" + url.QueryEscape(monday) + "&day=0"
//get user
userID, err := getIntFromSession(c.Controller, "userID")
if err != nil {
return flashError(errTypeConv, err, path, c.Controller, "")
}
//get start and end time
start, end, err := getStartEndForSlot(startTime, endTime, date, year)
if err != nil {
return flashError(errTypeConv, err, path, c.Controller, "")
}
slot := models.Slot{
UserID: userID,
Start: start,
End: end,
}
//enroll user
data, err := slot.Insert(c.Validation, ID, false)
if err != nil {
return flashError(errDB, err, path, c.Controller, "")
} else if c.Validation.HasErrors() {
return flashError(errValidation, err, path, c.Controller, "")
}
//send e-mail to the user who enrolled
err = sendEMail(c.Controller, &data,
"email.subject.enroll.slot",
"enrollToSlot")
if err != nil {
return flashError(errEMail, err, path, c.Controller, data.User.EMail)
}
c.Flash.Success(c.Message("event.enroll.success"))
return c.Redirect(Course.CalendarEvent, ID, courseID, 0, monday, day)
}
/*UnsubscribeFromSlot deletes a slot of an user. */
func (c Enrollment) UnsubscribeFromSlot(slotID, eventID, courseID, day int,
monday string) revel.Result {
c.Log.Debug("unsubscribe a user from a slot", "slotID", slotID,
"eventID", eventID, "courseID", courseID, "day", day, "monday", monday)
c.Session["lastURL"] = c.Request.URL.String()
//path in case of an error
path := "/course/calendarEvent?ID=" + url.QueryEscape(strconv.Itoa(eventID)) +
"&courseID=" + url.QueryEscape(strconv.Itoa(courseID)) +
"&shift=0" + "&monday=" + url.QueryEscape(monday) + "&day=0"
//get user
userID, err := getIntFromSession(c.Controller, "userID")
if err != nil {
return flashError(errTypeConv, err, path, c.Controller, "")
}
//delete slot
slot := models.Slot{ID: slotID, UserID: userID}
data, err := slot.Delete(c.Validation)
if err != nil {
return flashError(errDB, err, path, c.Controller, "")
} else if c.Validation.HasErrors() {
return flashError(errValidation, nil, path, c.Controller, "")
}
//send e-mail to the user who enrolled
err = sendEMail(c.Controller, &data,
"email.subject.unsubscribe.from.slot",
"unsubscribeFromSlot")
if err != nil {
return flashError(errEMail, err, path, c.Controller, data.User.EMail)
}
c.Flash.Success(c.Message("event.unsubscribe.success"))
return c.Redirect(Course.CalendarEvent, eventID, courseID, 0, monday, day)
}
//getStartEndForSlot formats the startTime and endTime of a slot as received
//by the controller
func getStartEndForSlot(startTime, endTime, date string, year int) (start,
end time.Time, err error) {
location, err := time.LoadLocation(app.TimeZone)
if err != nil {
return
}
splitDate := strings.Split(date, ".")
month, err := strconv.Atoi(splitDate[1])
if err != nil {
return
}
day, err := strconv.Atoi(splitDate[0])
if err != nil {
return
}
//set start time
splitTime := strings.Split(startTime, ":")
hour, err := strconv.Atoi(splitTime[0])
if err != nil {
return
}
min, err := strconv.Atoi(splitTime[1])
if err != nil {
return
}
start = time.Date(year, time.Month(month), day, hour, min, 0, 0, location)
//set end time
splitTime = strings.Split(endTime, ":")
hour, err = strconv.Atoi(splitTime[0])
if err != nil {
return
}
min, err = strconv.Atoi(splitTime[1])
if err != nil {
return
}
end = time.Date(year, time.Month(month), day, hour, min, 0, 0, location)
return
}