-
Notifications
You must be signed in to change notification settings - Fork 3
/
client-handlers.go
1219 lines (1024 loc) · 29.8 KB
/
client-handlers.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package clienthandlers
import (
"encoding/json"
"fmt"
"github.com/tsawler/goblender/client/clienthandlers/clientmodels"
"github.com/tsawler/goblender/pkg/forms"
"github.com/tsawler/goblender/pkg/handlers"
"github.com/tsawler/goblender/pkg/helpers"
"github.com/tsawler/goblender/pkg/models"
"github.com/tsawler/goblender/pkg/templates"
"html/template"
"net/http"
"strconv"
"time"
)
// AllCourses lists all active courses with link to overview
func AllCourses(w http.ResponseWriter, r *http.Request) {
pg, err := repo.DB.GetPageBySlug("courses")
if err != nil {
helpers.ServerError(w, err)
return
}
courses, err := dbModel.AllActiveSections()
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets := make(map[string]interface{})
rowSets["courses"] = courses
helpers.Render(w, r, "courses.page.tmpl", &templates.TemplateData{
Page: pg,
RowSets: rowSets,
})
}
// ShowCourse shows one course
func ShowCourse(w http.ResponseWriter, r *http.Request) {
courseID, err := strconv.Atoi(r.URL.Query().Get(":ID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
section, err := dbModel.GetCourseSection(courseID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets := make(map[string]interface{})
rowSets["section"] = section
pg := models.Page{
ID: section.ID,
AccessLevel: 2,
Active: 1,
SEOImage: 0,
MenuId: 0,
MenuColor: "navbar-light",
HasSlider: 0,
Immutable: 1,
Content: section.Course.Description,
PageTitle: section.Course.CourseName,
}
helpers.Render(w, r, "course.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
Page: pg,
})
}
// ShowLecture shows one lecture
func ShowLecture(w http.ResponseWriter, r *http.Request) {
lectureID, err := strconv.Atoi(r.URL.Query().Get(":ID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
sectionID, err := strconv.Atoi(r.URL.Query().Get(":SectionID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
var lecture clientmodels.Lecture
if sectionID > 0 {
l, err := dbModel.GetLectureForSection(lectureID, sectionID)
if err != nil {
errorLog.Println("Error getting section:", err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
lecture = l
} else {
l, err := dbModel.GetLecture(lectureID)
if err != nil {
errorLog.Println("Error getting section:", err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
lecture = l
}
lecture.SectionID = sectionID
course, err := dbModel.GetCourse(lecture.CourseID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets := make(map[string]interface{})
rowSets["lecture"] = lecture
rowSets["course"] = course
pg := models.Page{
ID: lecture.ID,
AccessLevel: 2,
Active: 1,
SEOImage: 0,
MenuId: 0,
MenuColor: "navbar-light",
HasSlider: 0,
Immutable: 1,
Content: lecture.Notes,
PageTitle: lecture.LectureName,
}
next, prev, err := dbModel.GetNextPreviousLectures(course.ID, lecture.ID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
intMap := make(map[string]int)
intMap["next"] = next
intMap["previous"] = prev
helpers.Render(w, r, "lecture.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
Page: pg,
IntMap: intMap,
})
}
// AdminAllCourses shows list of all courses for admin
func AdminAllCourses(w http.ResponseWriter, r *http.Request) {
courses, err := dbModel.AllCourses()
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets := make(map[string]interface{})
rowSets["courses"] = courses
helpers.Render(w, r, "courses-all-admin.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
})
}
// AdminCourse shows course for add/edit
func AdminCourse(w http.ResponseWriter, r *http.Request) {
courseID, err := strconv.Atoi(r.URL.Query().Get(":ID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
var course clientmodels.Course
if courseID > 0 {
c, err := dbModel.GetCourse(courseID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
course = c
}
rowSets := make(map[string]interface{})
rowSets["course"] = course
helpers.Render(w, r, "courses-admin.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
Form: forms.New(nil),
})
}
// SortOrder is type for sorting
type SortOrder struct {
ID string `json:"id"`
Order int `json:"order"`
}
// PostAdminCourse updates or adds a course
func PostAdminCourse(w http.ResponseWriter, r *http.Request) {
courseID, err := strconv.Atoi(r.URL.Query().Get(":ID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
var course clientmodels.Course
if courseID > 0 {
c, err := dbModel.GetCourse(courseID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
course = c
course.CourseName = r.Form.Get("course_name")
active, _ := strconv.Atoi(r.Form.Get("active"))
course.Active = active
err = dbModel.UpdateCourse(course)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
} else {
// inserting course
course.CourseName = r.Form.Get("course_name")
active, _ := strconv.Atoi(r.Form.Get("active"))
course.Active = active
course.Description = ""
newID, err := dbModel.InsertCourse(course)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
course.ID = newID
}
//now do sort order
var sorted []SortOrder
sortList := r.Form.Get("sort_list")
err = json.Unmarshal([]byte(sortList), &sorted)
if err != nil {
app.ErrorLog.Println(err)
}
for _, v := range sorted {
lectureID, _ := strconv.Atoi(v.ID)
err := dbModel.UpdateLectureSortOrder(lectureID, v.Order)
if err != nil {
app.ErrorLog.Println(err)
}
}
action, _ := strconv.Atoi(r.Form.Get("action"))
session.Put(r.Context(), "flash", "Changes saved")
if action == 1 {
http.Redirect(w, r, "/admin/courses/all", http.StatusSeeOther)
return
}
http.Redirect(w, r, fmt.Sprintf("/admin/courses/%d", course.ID), http.StatusSeeOther)
}
// AdminLecture shows form for lecture
func AdminLecture(w http.ResponseWriter, r *http.Request) {
courseID, err := strconv.Atoi(r.URL.Query().Get(":courseID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
lectureID, err := strconv.Atoi(r.URL.Query().Get(":ID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
course, err := dbModel.GetCourse(courseID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
var lecture clientmodels.Lecture
if lectureID > 0 {
lecture, err = dbModel.GetLecture(lectureID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
} else {
lecture.CourseID = courseID
lecture.PostedDate = time.Now()
}
rowSets := make(map[string]interface{})
rowSets["course"] = course
rowSets["lecture"] = lecture
helpers.Render(w, r, "lecture-admin.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
Form: forms.New(nil),
})
}
// PostAdminLecture posts a lecture
func PostAdminLecture(w http.ResponseWriter, r *http.Request) {
courseID, err := strconv.Atoi(r.URL.Query().Get(":courseID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
lectureID, err := strconv.Atoi(r.URL.Query().Get(":ID"))
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
videoID, _ := strconv.Atoi(r.Form.Get("video_id"))
active, _ := strconv.Atoi(r.Form.Get("active"))
lectureName := r.Form.Get("lecture_name")
pd := r.Form.Get("posted_date")
layout := "2006-01-02 15:04"
t, err := time.Parse(layout, pd)
if err != nil {
fmt.Println(err)
}
var lecture clientmodels.Lecture
if lectureID > 0 {
lecture, err = dbModel.GetLecture(lectureID)
if err != nil {
errorLog.Print(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
}
lecture.CourseID = courseID
lecture.LectureName = lectureName
lecture.Active = active
lecture.VideoID = videoID
lecture.PostedDate = t
if lectureID == 0 {
lecture.Notes = ""
_, err := dbModel.InsertLecture(lecture)
if err != nil {
errorLog.Print(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
} else {
err := dbModel.UpdateLecture(lecture)
if err != nil {
errorLog.Print(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
}
session.Put(r.Context(), "flash", "Changes saved")
http.Redirect(w, r, fmt.Sprintf("/admin/courses/%d", courseID), http.StatusSeeOther)
}
// GetLectureContentJSON gets html (notes) for lecture on edit page
func GetLectureContentJSON(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get(":ID"))
lecture, err := dbModel.GetLecture(id)
if err != nil {
errorLog.Println(err)
return
}
theData := handlers.PageContentJSON{
OK: true,
Content: template.HTML(lecture.Notes),
}
out, err := json.MarshalIndent(theData, "", " ")
if err != nil {
helpers.ServerError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
app.ErrorLog.Println(err)
}
}
// SaveLecture saves lecture html (notes)
func SaveLecture(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
id, _ := strconv.Atoi(r.Form.Get("page_id"))
sectionID, _ := strconv.Atoi(r.Form.Get("section_id"))
pageContent := r.Form.Get("thedata")
lecture, err := dbModel.GetLecture(id)
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
lecture.Notes = pageContent
err = dbModel.UpdateLectureContent(lecture)
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
app.Session.Put(r.Context(), "flash", "Lecture successfully updated!")
http.Redirect(w, r, fmt.Sprintf("/courses/lecture/%d/%d", sectionID, id), http.StatusSeeOther)
}
// GetCourseContentJSON gets html (description) for course on edit page
func GetCourseContentJSON(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get(":ID"))
course, err := dbModel.GetCourse(id)
if err != nil {
errorLog.Println(err)
return
}
theData := handlers.PageContentJSON{
OK: true,
Content: template.HTML(course.Description),
}
out, err := json.MarshalIndent(theData, "", " ")
if err != nil {
helpers.ServerError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
app.ErrorLog.Println(err)
}
}
// SaveCourse saves course html (description)
func SaveCourse(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
id, _ := strconv.Atoi(r.Form.Get("page_id"))
pageContent := r.Form.Get("thedata")
course, err := dbModel.GetCourse(id)
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
course.Description = pageContent
err = dbModel.UpdateCourseContent(course)
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
app.Session.Put(r.Context(), "flash", "Lecture successfully updated!")
http.Redirect(w, r, fmt.Sprintf("/courses/overview/%d", id), http.StatusSeeOther)
}
// SubmitAssignment displays page to submit an assignment
func SubmitAssignment(w http.ResponseWriter, r *http.Request) {
pg, err := repo.DB.GetPageBySlug("submit-assignment")
if err == models.ErrNoRecord {
helpers.NotFound(w)
return
} else if err != nil {
helpers.ServerError(w, err)
return
}
userID := session.GetInt(r.Context(), "userID")
sections, err := dbModel.AllActiveSectionsForStudentID(userID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets := make(map[string]interface{})
rowSets["sections"] = sections
helpers.Render(w, r, "submit-assignment.page.tmpl", &templates.TemplateData{
Page: pg,
Form: forms.New(nil),
RowSets: rowSets,
})
}
// PostSubmitAssignment handles assignment submission
func PostSubmitAssignment(w http.ResponseWriter, r *http.Request) {
userID := app.Session.GetInt(r.Context(), "userID")
sectionID, _ := strconv.Atoi(r.Form.Get("section_id"))
description := r.Form.Get("description")
helpers.CreateDirIfNotExist("./ui/static/site-content/assignments/")
helpers.CreateDirIfNotExist(fmt.Sprintf("./ui/static/site-content/assignments/%d", userID))
fileName, displayName, err := helpers.UploadOneFileReturnSlugName(r, fmt.Sprintf("./ui/static/site-content/assignments/%d/", userID))
if err != nil {
errorLog.Println(err)
}
assignment := clientmodels.Assignment{
FileNameDisplay: displayName,
FileName: fileName,
Description: description,
UserID: userID,
SectionID: sectionID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
_, err = dbModel.InsertAssignment(assignment)
if err != nil {
session.Put(r.Context(), "error", "Error: assignment NOT received!")
http.Redirect(w, r, "/courses/assignments/submit-an-assignment", http.StatusNotAcceptable)
return
}
session.Put(r.Context(), "flash", "Assignment received!")
http.Redirect(w, r, "/courses/assignments/submit-an-assignment", http.StatusSeeOther)
}
// Assignments displays assignments in admin tool
func Assignments(w http.ResponseWriter, r *http.Request) {
a, err := dbModel.AllAssignments(0)
if err != nil {
errorLog.Print(err)
}
rowSets := make(map[string]interface{})
rowSets["assignments"] = a
helpers.Render(w, r, "assignments-admin.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
})
}
// Assignment displays assignment in admin tool
func Assignment(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get(":ID"))
a, err := dbModel.GetAssignment(id)
if err != nil {
errorLog.Print(err)
}
intMap := make(map[string]int)
if r.URL.Query().Get("src") != "" {
intMap["student_id"], _ = strconv.Atoi(r.URL.Query().Get("src"))
}
rowSets := make(map[string]interface{})
rowSets["assignment"] = a
helpers.Render(w, r, "assignment-admin.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
Form: forms.New(nil),
IntMap: intMap,
})
}
// DownloadGradeAssignment downloads a graded assignment
func DownloadGradeAssignment(w http.ResponseWriter, r *http.Request) {
userID, _ := strconv.Atoi(r.URL.Query().Get(":UserID"))
id, _ := strconv.Atoi(r.URL.Query().Get(":ID"))
// get assignment
a, err := dbModel.GetAssignment(id)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusNotFound)
return
}
path := fmt.Sprintf("./ui/static/site-content/assignments/%d/graded/%d/", userID, a.ID)
helpers.DownloadStaticFile(w, r, path, a.GradedFile, a.GradedFileDisplayName)
}
// DownloadGradeAssignmentForStudent downloads a graded assignment for a student (validating access rights)
func DownloadGradeAssignmentForStudent(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get(":ID"))
studentID := app.Session.GetInt(r.Context(), "userID")
// get assignment
a, err := dbModel.GetAssignment(id)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusNotFound)
return
}
if studentID != a.UserID {
errorLog.Println(err)
helpers.ClientError(w, http.StatusForbidden)
return
}
path := fmt.Sprintf("./ui/static/site-content/assignments/%d/graded/%d/", a.UserID, a.ID)
helpers.DownloadStaticFile(w, r, path, a.GradedFile, a.GradedFileDisplayName)
}
// GradeAssignment grades an assignment
func GradeAssignment(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get(":ID"))
a, err := dbModel.GetAssignment(id)
if err != nil {
errorLog.Print(err)
}
form := forms.New(r.PostForm)
hasGradedFile := form.HasFile("graded", r)
var fileName string
var displayName string
if hasGradedFile {
// uploading a graded file
_ = helpers.CreateDirIfNotExist("./ui/static/site-content/assignments/")
_ = helpers.CreateDirIfNotExist(fmt.Sprintf("./ui/static/site-content/assignments/%d", a.UserID))
_ = helpers.CreateDirIfNotExist(fmt.Sprintf("./ui/static/site-content/assignments/%d/graded", a.UserID))
_ = helpers.CreateDirIfNotExist(fmt.Sprintf("./ui/static/site-content/assignments/%d/graded/%d", a.UserID, a.ID))
f, d, err := helpers.UploadOneFileReturnSlugName(r, fmt.Sprintf("./ui/static/site-content/assignments/%d/graded/%d/", a.UserID, a.ID))
if err != nil {
errorLog.Println(err)
}
fileName = f
displayName = d
}
a.Mark, _ = strconv.Atoi(r.Form.Get("mark"))
a.TotalValue, _ = strconv.Atoi(r.Form.Get("total_value"))
a.GradedFileDisplayName = displayName
a.GradedFile = fileName
_ = dbModel.GradeAssignment(a)
app.Session.Put(r.Context(), "flash", "Changes saved")
fromMember, _ := strconv.Atoi(r.Form.Get("from_member"))
if fromMember > 0 {
http.Redirect(w, r, fmt.Sprintf("/admin/members/%d", fromMember), http.StatusSeeOther)
return
}
http.Redirect(w, r, "/admin/assignments/assignments", http.StatusSeeOther)
}
// StudentProfile shows profile page
func StudentProfile(w http.ResponseWriter, r *http.Request) {
id := app.Session.GetInt(r.Context(), "userID")
user, err := repo.DB.GetUserById(id)
if err != nil {
app.ErrorLog.Println(err)
return
}
a, err := dbModel.AllAssignments(id)
if err != nil {
errorLog.Print(err)
}
rowSets := make(map[string]interface{})
rowSets["assignments"] = a
courses, err := dbModel.AllActiveSectionsForStudentID(id)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets["courses"] = courses
ca, err := dbModel.CourseAccessHistoryForStudent(id)
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets["access"] = ca
helpers.Render(w, r, "profile.page.tmpl", &templates.TemplateData{
Form: forms.New(nil),
AdminUser: user,
RowSets: rowSets,
})
}
// StudentAssignments displays assignments in admin tool for a given student
func StudentAssignments(w http.ResponseWriter, r *http.Request) {
userID := app.Session.GetInt(r.Context(), "userID")
a, err := dbModel.AllAssignments(userID)
if err != nil {
errorLog.Print(err)
}
rowSets := make(map[string]interface{})
rowSets["assignments"] = a
helpers.Render(w, r, "student-assignments.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
})
}
// StudentLeftLecture records student leaving lecture
func StudentLeftLecture(w http.ResponseWriter, r *http.Request) {
lectureID, _ := strconv.Atoi(r.Form.Get("lecture_id"))
sectionID, _ := strconv.Atoi(r.Form.Get("section_id"))
duration, _ := strconv.Atoi(r.Form.Get("duration"))
userID := app.Session.GetInt(r.Context(), "userID")
// only record if 1 second or longer
if duration > 0 {
access := clientmodels.CourseAccess{
UserID: userID,
LectureID: lectureID,
Duration: duration,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
SectionID: sectionID,
}
_ = dbModel.RecordCourseAccess(access)
}
}
// CourseSectionAccessHistory shows history of student access to course
func CourseSectionAccessHistory(w http.ResponseWriter, r *http.Request) {
sectionID, _ := strconv.Atoi(r.URL.Query().Get(":ID"))
accesses, _ := dbModel.CourseSectionAccessHistory(sectionID)
rowSets := make(map[string]interface{})
rowSets["access"] = accesses
intMap := make(map[string]int)
intMap["section_id"] = sectionID
helpers.Render(w, r, "course-access-admin.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
IntMap: intMap,
})
}
// MemberEdit displays the user for add/edit
func MemberEdit(w http.ResponseWriter, r *http.Request) {
var u models.User
id, err := strconv.Atoi(r.URL.Query().Get(":id"))
if err != nil {
app.ErrorLog.Println(err)
return
}
if id > 0 {
u, err = repo.DB.GetUserById(id)
if err != nil {
app.ErrorLog.Println(err)
return
}
}
src := "/admin/members/all"
srcSlice := r.URL.Query()["src"]
if len(srcSlice) > 0 {
src = srcSlice[0]
}
stringMap := make(map[string]string)
stringMap["src"] = src
ca, err := dbModel.CourseAccessHistoryForStudent(id)
if err != nil {
app.ErrorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets := make(map[string]interface{})
rowSets["access"] = ca
var assignments []clientmodels.Assignment
if id > 0 {
assignments, _ = dbModel.AllAssignments(id)
}
rowSets["assignments"] = assignments
courses, err := dbModel.AllActiveSectionsForStudentID(id)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets["courses"] = courses
helpers.Render(w, r, "member.page.tmpl", &templates.TemplateData{
Form: forms.New(nil),
AdminUser: u,
RowSets: rowSets,
StringMap: stringMap,
})
}
// MembersAll overrides default function to include access time
func MembersAll(w http.ResponseWriter, r *http.Request) {
users, err := dbModel.AllStudents()
if err != nil {
app.ErrorLog.Println(err)
return
}
myMap := make(map[string]interface{})
myMap["users"] = users
helpers.Render(w, r, "members-all.page.tmpl", &templates.TemplateData{
RowSets: myMap,
})
}
type jsonResponse struct {
OK bool `json:"ok"`
Message string `json:"message"`
Content string `json:"content"`
ID int `json:"id"`
}
// SaveLectureSortOrder saves lecture sort order on drag/drop
func SaveLectureSortOrder(w http.ResponseWriter, r *http.Request) {
//Save sort order
var sorted []SortOrder
sortList := r.Form.Get("sort_list")
var resp jsonResponse
err := json.Unmarshal([]byte(sortList), &sorted)
if err != nil {
app.ErrorLog.Println(err)
resp.OK = false
}
ok := true
for _, v := range sorted {
lectureID, _ := strconv.Atoi(v.ID)
err := dbModel.UpdateLectureSortOrder(lectureID, v.Order)
if err != nil {
app.ErrorLog.Println(err)
ok = false
}
}
resp.OK = ok
out, err := json.MarshalIndent(resp, "", " ")
if err != nil {
helpers.ServerError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
app.ErrorLog.Println(err)
}
}
// CourseTraffic displays a chart of course traffic (views)
func CourseTraffic(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get(":id"))
rowSets := make(map[string]interface{})
intMap := make(map[string]int)
intMap["course_id"] = id
courses, err := dbModel.AllActiveSections()
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
rowSets["courses"] = courses
helpers.Render(w, r, "course-traffic.page.tmpl", &templates.TemplateData{
RowSets: rowSets,
})
}
// CourseTrafficData sends data for chart
func CourseTrafficData(w http.ResponseWriter, r *http.Request) {
sectionID, err := strconv.Atoi(r.URL.Query().Get("section_id"))
if err != nil {
errorLog.Println(err)
}
traffic, err := dbModel.GetTrafficForCourseSection(sectionID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
out, err := json.MarshalIndent(traffic, "", " ")
if err != nil {
helpers.ServerError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
app.ErrorLog.Println(err)
}
}
// CourseTrafficDataForStudent sends data for chart
func CourseTrafficDataForStudent(w http.ResponseWriter, r *http.Request) {
courseID, err := strconv.Atoi(r.URL.Query().Get("course_id"))
if err != nil {
errorLog.Println(err)
}
userID := app.Session.GetInt(r.Context(), "userID")
traffic, err := dbModel.GetTrafficForCourseForStudent(courseID, userID)
if err != nil {
errorLog.Println(err)
helpers.ClientError(w, http.StatusBadRequest)
return
}
out, err := json.MarshalIndent(traffic, "", " ")
if err != nil {
helpers.ServerError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
app.ErrorLog.Println(err)
}
}
// CourseTrafficDataForStudentAdmin sends data for chart
func CourseTrafficDataForStudentAdmin(w http.ResponseWriter, r *http.Request) {
sectionID, err := strconv.Atoi(r.URL.Query().Get("section_id"))
if err != nil {
errorLog.Println(err)
}