-
Notifications
You must be signed in to change notification settings - Fork 0
/
creator.go
209 lines (158 loc) · 5.19 KB
/
creator.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
package controllers
import (
"database/sql"
"time"
"turm/app/models"
"github.com/revel/revel"
)
/*Activate a course draft.
- Roles: creator of the course */
func (c Creator) Activate(ID int) revel.Result {
c.Log.Debug("activate course draft", "ID", ID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
course := models.Course{ID: ID}
invalid, users, err := course.Activate(c.Validation)
if err != nil {
return flashError(errDB, err, "", c.Controller, "")
} else if invalid {
return flashError(errValidation, nil, "", c.Controller, "")
}
//send notification e-mail to editors/instructors
for _, data := range users {
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("creator.course.activated",
course.Title,
course.ID,
))
return c.Redirect(Manage.Active)
}
/*Delete a course (draft).
- Roles: creator of the course */
func (c Creator) Delete(ID int) revel.Result {
c.Log.Debug("delete course (draft)", "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 valid, err := course.Delete(); err != nil {
return flashError(errDB, err, "", c.Controller, "")
} else if !valid {
c.Validation.ErrorKey("validation.invalid.delete.course")
return flashError(errValidation, nil, "", c.Controller, "")
}
c.Flash.Success(c.Message("creator.course.deleted", ID))
if course.Active {
return c.Redirect(Manage.Expired)
}
return c.Redirect(Manage.Drafts)
}
/*Duplicate a course draft.
- Roles: creator of the course */
func (c Creator) Duplicate(ID int, title string) revel.Result {
c.Log.Debug("duplicate course draft", "ID", ID, "title", title)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
//get the course creator
creatorID, err := getIntFromSession(c.Controller, "userID")
if err != nil {
return flashError(errTypeConv, err, "", c.Controller, "")
}
course := models.Course{ID: ID, Title: title,
Creator: sql.NullInt32{
Int32: int32(creatorID),
Valid: true,
}}
if err := course.Duplicate(); err != nil {
return flashError(errDB, err, "", c.Controller, "")
}
c.Flash.Success(c.Message("creator.course.duplicated",
course.Title,
course.ID))
return c.Redirect(Manage.Drafts)
}
/*Expire an active course.
- Roles: creator of the course */
func (c Creator) Expire(ID int) revel.Result {
c.Log.Debug("expire course", "ID", ID)
c.Session["lastURL"] = c.Request.URL.String()
//NOTE: the interceptor assures that the course ID is valid
now := time.Now().Add(-time.Minute * 1).Format(revel.TimeFormats[0])
course := models.Course{ID: ID}
if err := course.Update(nil, "expiration_date", now, nil); err != nil {
return flashError(errDB, err, "", c.Controller, "")
}
c.Flash.Success(c.Message("creator.course.expired", course.ID))
return c.Redirect(Manage.Expired)
}
/*New creates a new inactive course according to the specified parameters.
- Roles: creator */
func (c Creator) New(param models.NewCourseParam, file []byte) revel.Result {
c.Log.Debug("create a new course", "param", param, "file", string(file))
c.Session["lastURL"] = c.Request.URL.String()
param.JSON = file
var course models.Course
param.Validate(c.Validation, &course)
if c.Validation.HasErrors() {
return flashError(errValidation, nil, "", c.Controller, "")
}
//get the course creator
creatorID, err := getIntFromSession(c.Controller, "userID")
if err != nil {
return flashError(errTypeConv, err, "", c.Controller, "")
}
course.ID = param.CourseID
course.Creator = sql.NullInt32{
Int32: int32(creatorID),
Valid: true,
}
course.Title = param.Title
if param.Option == models.BLANK {
c.Log.Debug("insert blank course")
err = course.NewBlank()
} else if param.Option == models.DRAFT {
c.Log.Debug("insert draft")
err = course.InsertFromDraft(c.Validation)
if c.Validation.HasErrors() {
return flashError(errValidation, nil, "", c.Controller, "")
}
} else {
c.Log.Debug("insert uploaded course")
err = course.InsertUploadedCourse()
}
if err != nil {
return flashError(errDB, err, "", c.Controller, "")
}
c.Flash.Success(c.Message("creator.opened.new.course",
course.Title,
course.ID))
return c.Redirect(Edit.Open, course.ID)
}
/*Search returns all courses matching a search value for which the user has edit rights. */
func (c Creator) Search(value string) revel.Result {
c.Log.Debug("search courses", "value", value)
c.Session["lastURL"] = c.Request.URL.String()
models.ValidateLength(&value, "validation.invalid.searchValue",
1, 127, c.Validation)
if c.Validation.HasErrors() {
c.Validation.Keep()
return c.Render()
}
//get the userID
userID, err := getIntFromSession(c.Controller, "userID")
if err != nil {
return flashError(errTypeConv, err, "", c.Controller, "")
}
var courses models.CourseList
err = courses.SearchForDrafts(value, userID, c.Session["role"].(string))
if err != nil {
renderQuietError(errDB, err, c.Controller)
return c.Render()
}
return c.Render(courses)
}