/
session.go
387 lines (359 loc) · 8.54 KB
/
session.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
package course
import (
"database/sql"
"net/http"
"github.com/OpenCoursePlatform/OpenCoursePlatform-Go/helpers"
"github.com/gorilla/mux"
)
/*
Session struct contains session data both
for rendering and for logic such as deciding
the session type.
*/
type Session struct {
ID int
Name string
Slug string
SessionType int
}
/*
TextData is used for
rendering a text session.
Should not be used by other structs.
*/
type TextData struct {
Title string
Paths []helpers.Path
Course Course
Module Module
Session Session
Sessions []Session
Text string
}
/*
YoutubeData is used for
rendering a YouTube session.
Should not be used by other structs.
*/
type YoutubeData struct {
Title string
Paths []helpers.Path
Course Course
Module Module
Session Session
Sessions []Session
Text string
Youtube string
}
/*
Option struct contains question data
for rendering.
*/
type Option struct {
Text string `json:"text"`
IsCorrect bool `json:"is_correct"`
Checked bool
}
/*
Question struct contains question data
for rendering.
*/
type Question struct {
ID int
Text string
Options []Option
}
/*
MultipleChoiceData is used for
rendering a Multiple-choice session.
Should not be used by other structs.
*/
type MultipleChoiceData struct {
Title string
Paths []helpers.Path
Course Course
Module Module
Session Session
Sessions []Session
Questions []Question
}
/*
GetSessionsText gets the session text for
the session id. Should only ever be one
possible value unless there's a bug or
someone does a manual insert.
*/
func GetSessionsText(db *sql.DB, sessionID int) (string, error) {
var pageText string
query := `
SELECT session_text.text
FROM session_text
WHERE session_text.session_id = ?
`
err := db.QueryRow(query, sessionID).Scan(&pageText)
if err != nil {
return "", err
}
return pageText, nil
}
/*
GetSessionsByModuleID gets all of the
sessions associated with one module.
*/
func GetSessionsByModuleID(db *sql.DB, moduleID int) ([]Session, error) {
rows, err := db.Query(`
SELECT name, slug
FROM session
WHERE module_id = ?
`, moduleID) // check err
if err != nil {
return nil, err
}
defer rows.Close()
var sessions []Session
for rows.Next() {
var session Session
err = rows.Scan(&session.Name, &session.Slug) // check err
if err != nil {
return sessions, err
}
sessions = append(sessions, session)
}
err = rows.Err() // check err
if err != nil {
return sessions, err
}
return sessions, nil
}
/*
GetSession gets the session data by the
session slug.
*/
func GetSession(db *sql.DB, sessionSlug string) (Session, int, error) {
var session Session
var moduleID int
query := `
SELECT id, name, session_type, module_id
FROM session
WHERE slug = ?
`
err := db.QueryRow(query, sessionSlug).Scan(&session.ID, &session.Name, &session.SessionType, &moduleID)
if err != nil {
return session, 0, err
}
return session, moduleID, nil
}
/*
GetSessionsYoutube gets the data for
a YouTube session by the session id.
*/
func GetSessionsYoutube(db *sql.DB, sessionID int) (string, string, error) {
var pageText string
var youtube string
query := `
SELECT text, youtube_id
FROM session_youtube
WHERE session_id = ?
`
err := db.QueryRow(query, sessionID).Scan(&pageText, &youtube)
if err != nil {
return "", "", err
}
return pageText, youtube, nil
}
/*
GetSessionsMultipleChoice gets the data for
a Multiple-choice session by the session id.
*/
func GetSessionsMultipleChoice(db *sql.DB, sessionID int) ([]Question, error) {
rows, err := db.Query(`
SELECT id, question
FROM session_multiple_choice
WHERE session_id = ?
`, sessionID) // check err
if err != nil {
return nil, err
}
defer rows.Close()
var questions []Question
for rows.Next() {
var question Question
err = rows.Scan(&question.ID, &question.Text) // check err
if err != nil {
return questions, err
}
questions = append(questions, question)
}
err = rows.Err() // check err
if err != nil {
return questions, err
}
for index := range questions {
rows, err := db.Query(`
SELECT answer, is_correct
FROM session_multiple_choice_answers
WHERE multiple_choice_id = ?
`, questions[index].ID) // check err
if err != nil {
return nil, err
}
defer rows.Close()
var options []Option
for rows.Next() {
var option Option
err = rows.Scan(&option.Text, &option.IsCorrect) // check err
if err != nil {
return questions, err
}
options = append(options, option)
}
err = rows.Err() // check err
if err != nil {
return questions, err
}
questions[index].Options = options
}
return questions, nil
}
/*
SessionPage endpoint returns the actual
session data. Could be any type
of session.
*/
func SessionPage(w http.ResponseWriter, r *http.Request) {
db, err := helpers.CreateDBHandler()
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
defer db.Close()
course, err := GetCourse(db, mux.Vars(r)["course"])
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
module, err := GetModule(db, mux.Vars(r)["module"])
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
session, moduleID, err := GetSession(db, mux.Vars(r)["session"])
if err != nil {
helpers.HandleError(err)
http.NotFound(w, r)
return
}
sessions, err := GetSessionsByModuleID(db, moduleID)
if err != nil {
helpers.HandleError(err)
http.NotFound(w, r)
return
}
if session.SessionType == 1 {
coursePage := TextData{
Title: session.Name,
Paths: []helpers.Path{{Name: "Courses", Link: "/"}, {Name: course.Name, Link: "/courses/" + course.Slug}, {Name: module.Name, Link: "/courses/" + course.Slug + "/" + module.Slug}},
Course: course,
Module: module,
Session: session,
Sessions: sessions,
}
coursePageText, err := GetSessionsText(db, session.ID)
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
coursePage.Text = coursePageText
helpers.RenderTemplate(r, w, "text", coursePage)
} else if session.SessionType == 2 {
coursePage := YoutubeData{
Title: session.Name,
Paths: []helpers.Path{{Name: "Courses", Link: "/"}, {Name: course.Name, Link: "/courses/" + course.Slug}, {Name: module.Name, Link: "/courses/" + course.Slug + "/" + module.Slug}},
Course: course,
Module: module,
Session: session,
Sessions: sessions,
}
text, youtube, err := GetSessionsYoutube(db, session.ID)
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
coursePage.Text = text
coursePage.Youtube = youtube
helpers.RenderTemplate(r, w, "youtube", coursePage)
} else if session.SessionType == 3 {
questions, err := GetSessionsMultipleChoice(db, session.ID)
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
coursePage := MultipleChoiceData{
Title: session.Name,
Paths: []helpers.Path{{Name: "Courses", Link: "/"}, {Name: course.Name, Link: "/courses/" + course.Slug}, {Name: module.Name, Link: "/courses/" + course.Slug + "/" + module.Slug}},
Course: course,
Module: module,
Session: session,
Sessions: sessions,
Questions: questions,
}
helpers.RenderTemplate(r, w, "multiple_choice", coursePage)
}
}
// SessionAnswer ...
func SessionAnswer(w http.ResponseWriter, r *http.Request) {
db, err := helpers.CreateDBHandler()
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
defer db.Close()
session, moduleID, err := GetSession(db, mux.Vars(r)["session"])
if err != nil {
helpers.HandleError(err)
http.NotFound(w, r)
return
}
sessions, err := GetSessionsByModuleID(db, moduleID)
if err != nil {
helpers.HandleError(err)
http.NotFound(w, r)
return
}
if session.SessionType == 3 {
r.ParseForm()
questions, err := GetSessionsMultipleChoice(db, session.ID)
if err != nil {
helpers.HandleError(err)
helpers.InternalServerError(w)
return
}
for i := range questions {
for j := range questions[i].Options {
answer := questions[i].Options[j].Text
for k := range r.Form[questions[i].Text] {
if answer == r.Form[questions[i].Text][k] {
questions[i].Options[j].Checked = true
}
}
}
}
coursePage := MultipleChoiceData{
Title: session.Name,
Paths: []helpers.Path{{Name: "Introduction to Python", Link: "/introduction-to-python"}},
Sessions: sessions,
Questions: questions,
}
helpers.RenderTemplate(r, w, "multiple_choice_answered", coursePage)
} else {
http.NotFound(w, r)
return
}
}