diff --git a/packages/session/briefing.go b/packages/session/briefing.go
new file mode 100644
index 0000000..b5b2aea
--- /dev/null
+++ b/packages/session/briefing.go
@@ -0,0 +1,95 @@
+/*
+ * Sicherheitsunterweisung
+ * Copyright (C) 2023 Jason Streifling
+ *
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with this program. If not, see .
+ */
+
+package session
+
+import (
+ "crypto/rand"
+ "encoding/hex"
+ "fmt"
+ "html/template"
+ "log"
+ "net/http"
+ "time"
+
+ "github.com/google/uuid"
+ "streifling.com/jason/sicherheitsunterweisung/packages/data"
+)
+
+type Briefing struct {
+ *data.Briefing
+ UUID uuid.UUID
+ Participants []*Participant
+ Questions []data.Question
+}
+
+func generateLogin() (string, error) {
+ bs := make([]byte, 4)
+
+ if _, err := rand.Read(bs); err != nil {
+ return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err)
+ }
+
+ return hex.EncodeToString(bs), nil
+}
+
+func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ var err error
+ participant := Participant{
+ Participant: new(data.Participant),
+ NoIncorrect: -1,
+ AllowRetry: false,
+ }
+
+ participant.Login, err = generateLogin()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+ b.Participants = append(b.Participants, &participant)
+ cp <- &participant
+
+ data := participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: Participant{Login: participant.Login},
+ }
+
+ template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
+ }
+}
+
+func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ b.DateTime = time.Now().Format("2006-01-02 15:04:05")
+ b.Location = r.PostFormValue("location")
+ b.Document = r.PostFormValue("document")
+ b.AsOf = r.PostFormValue("as-of")
+
+ err := db.WriteBriefing(b.Briefing)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+
+ data := summaryHTMLData{
+ SessionID: s.UUID,
+ BriefingID: b.UUID,
+ ParticipantsData: make([]participantHTMLData, len(b.Participants)),
+ }
+ for i, p := range b.Participants {
+ data.ParticipantsData[i].BriefingID = b.UUID
+ data.ParticipantsData[i].Participant = *p
+ }
+
+ template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data)
+ }
+}
diff --git a/packages/session/handlerFuncs.go b/packages/session/handlerFuncs.go
deleted file mode 100644
index 51d5eca..0000000
--- a/packages/session/handlerFuncs.go
+++ /dev/null
@@ -1,299 +0,0 @@
-/*
- * Sicherheitsunterweisung
- * Copyright (C) 2023 Jason Streifling
- *
- * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with this program. If not, see .
- */
-
-package session
-
-import (
- "fmt"
- "html/template"
- "log"
- "net/http"
- "time"
-
- "github.com/google/uuid"
- "streifling.com/jason/sicherheitsunterweisung/packages/data"
-)
-
-func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- instructors, err := db.GetInstructors()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
-
- for _, i := range instructors {
- if r.PostFormValue("login") == fmt.Sprint(i.ID) {
- session := Session{
- ID: uuid.New(),
- Instructor: *i,
- Briefings: make([]*Briefing, 0),
- }
- (*ss) = append((*ss), &session)
- cs <- &session
-
- data := tableHTMLData{SessionID: session.ID}
- data.OTD, err = db.GetAllOverviewTableData()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
-
- template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", data)
- return
- }
- }
- template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
- }
-}
-
-func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- var err error
- data := tableHTMLData{}
-
- data.SessionID = s.ID
- data.OTD, err = db.GetOverviewTableDataByName(r.PostFormValue("search"))
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
- template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", data)
- }
-}
-
-func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- briefing := Briefing{Briefing: &data.Briefing{InstructorID: s.Instructor.ID}}
- s.Briefings = append(s.Briefings, &briefing)
- cb <- &briefing
-
- template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{BriefingID: briefing.ID})
- }
-}
-
-func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- var err error
- participant := Participant{
- Participant: new(data.Participant),
- NoIncorrect: -1,
- AllowRetry: false,
- }
-
- participant.Login, err = generateLogin()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
- b.Participants = append(b.Participants, &participant)
- cp <- &participant
-
- data := participantHTMLData{
- BriefingID: b.ID,
- Participant: Participant{Login: participant.Login},
- }
-
- template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
- }
-}
-
-func (b *Briefing) HandleBriefingForm(db *data.DB) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- b.DateTime = time.Now().Format("2006-01-02 15:04:05")
- b.Location = r.PostFormValue("location")
- b.Document = r.PostFormValue("document")
- b.AsOf = r.PostFormValue("as-of")
-
- err := db.WriteBriefing(b.Briefing)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
-
- data := summaryHTMLData{
- BriefingID: b.ID,
- ParticipantsData: make([]participantHTMLData, len(b.Participants)),
- }
- for i, p := range b.Participants {
- data.ParticipantsData[i].BriefingID = b.ID
- data.ParticipantsData[i].Participant = *p
- }
-
- template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data)
- }
-}
-
-func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- briefing, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
- if loginCorrect {
- data := participantHTMLData{
- BriefingID: briefing.ID,
- Participant: Participant{Login: participant.Login},
- }
-
- template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
- } else {
- template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
- }
- }
-}
-
-func (b *Briefing) HandleParticipant(db *data.DB, p *Participant) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login))
- p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login))
- p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login))
-
- err := db.WriteParticipant(p.Participant)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
-
- data := participantHTMLData{
- BriefingID: b.ID,
- Participant: *p,
- }
-
- template.Must(template.ParseFiles("templates/video.html")).ExecuteTemplate(w, "content", data)
- }
-}
-
-func (b *Briefing) HandleAnswer(db *data.DB, p *Participant, i int64) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- if i < int64(len(b.Questions)) {
- if err := handleGivenAnswer(p, i-1, r); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
-
- data := questionHTMLData{
- participantHTMLData: participantHTMLData{
- BriefingID: b.ID,
- Participant: Participant{Login: p.Login},
- },
- QuestionID: i + 1,
- Question: b.Questions[i],
- }
-
- template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
- } else {
- if err := handleGivenAnswer(p, i-1, r); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
-
- p.NoIncorrect = 0
- for i, q := range b.Questions {
- if p.GivenAnswers[i] != q.Correct {
- p.NoIncorrect++
- }
- }
-
- data := resultHTMLData{
- participantHTMLData: participantHTMLData{
- BriefingID: b.ID,
- Participant: *p,
- },
- Questions: makeResultQuestions(b.Questions, p.GivenAnswers),
- }
-
- if data.NoIncorrect == 0 {
- if err := db.WriteGivenAnswers(*b.Briefing, *p.Participant, b.Questions, p.GivenAnswers); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
- }
-
- template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
- }
- }
-}
-
-func (p *Participant) HandleAllowRetry() http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- p.NoIncorrect = -1
- p.AllowRetry = true
- }
-}
-
-func (b *Briefing) HandleRetry(p *Participant, i *int) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- if p.AllowRetry {
- p.AllowRetry = false
- (*i) = 0
-
- data := questionHTMLData{
- participantHTMLData: participantHTMLData{
- BriefingID: b.ID,
- Participant: Participant{Login: p.Login},
- },
- QuestionID: int64(*i + 1),
- Question: b.Questions[*i],
- }
-
- template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
- } else {
- data := resultHTMLData{
- participantHTMLData: participantHTMLData{
- BriefingID: b.ID,
- Participant: *p,
- },
- Questions: makeResultQuestions(b.Questions, p.GivenAnswers),
- }
-
- template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
- }
- }
-}
-
-func (b *Briefing) HandleRefresh(p *Participant) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- data := participantHTMLData{
- BriefingID: b.ID,
- Participant: *p,
- }
-
- template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
- }
-}
-
-func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- data := tableHTMLData{SessionID: s.ID}
- var err error
-
- data.OTD, err = db.GetAllOverviewTableData()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- log.Panicln(err)
- }
-
- template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", data)
- }
-}
-
-func (b *Briefing) HandleEndOfVideo(p *Participant) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- data := questionHTMLData{
- participantHTMLData: participantHTMLData{
- BriefingID: b.ID,
- Participant: Participant{Login: p.Login},
- },
- QuestionID: 1,
- Question: b.Questions[0],
- }
-
- template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
- }
-}
diff --git a/packages/session/helperFuncs.go b/packages/session/helperFuncs.go
deleted file mode 100644
index 115fa11..0000000
--- a/packages/session/helperFuncs.go
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Sicherheitsunterweisung
- * Copyright (C) 2023 Jason Streifling
- *
- * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with this program. If not, see .
- */
-
-package session
-
-import (
- "crypto/rand"
- "encoding/hex"
- "fmt"
- "log"
- "net/http"
- "strconv"
- "strings"
-
- "streifling.com/jason/sicherheitsunterweisung/packages/data"
-)
-
-func generateLogin() (string, error) {
- bs := make([]byte, 4)
-
- if _, err := rand.Read(bs); err != nil {
- return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err)
- }
-
- return hex.EncodeToString(bs), nil
-}
-
-func findCorrectLogin(l string, ss *[]*Session) (*Briefing, *Participant, bool) {
- for _, s := range *ss {
- for _, b := range s.Briefings {
- for _, p := range b.Participants {
- if l == p.Login {
- return b, p, true
- }
- }
- }
- }
- return nil, nil, false
-}
-
-func newParticipant(l string) (*data.Participant, error) {
- var err error
- p := new(data.Participant)
-
- p.ID, err = strconv.ParseInt(l, 10, 64)
- if err != nil {
- return nil, fmt.Errorf("error: newParticipant: strconv.Atoi(): %v", err)
- }
-
- return p, nil
-}
-
-func handleGivenAnswer(p *Participant, i int64, r *http.Request) error {
- answer, err := strconv.Atoi(r.PostFormValue("answer"))
- if err != nil {
- return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
- }
-
- p.GivenAnswers[i] = answer
- return nil
-}
-
-func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
- questions := make([]resultQuestion, len(sq))
- for i, q := range sq {
- questions[i].Text = q.Text
- questions[i].Answers = make([]resultAnswer, len(q.Answers))
-
- for j := range q.Answers {
- questions[i].Answers[j].ID = q.Answers[j].ID
- questions[i].Answers[j].Text = q.Answers[j].Text
- questions[i].Answers[j].IsImage = q.Answers[j].IsImage
-
- if j+1 == q.Correct {
- questions[i].Answers[j].Correct = true
- } else {
- questions[i].Answers[j].Correct = false
- }
-
- if j+1 == givenAnswers[i] {
- questions[i].Answers[j].Chosen = true
- } else {
- questions[i].Answers[j].Chosen = false
- }
- }
- }
-
- return questions
-}
-
-func (b *Briefing) getQuestions(db *data.DB) {
- questionIDs := make([]string, 4)
-
- for i := 0; i < len(questionIDs); i++ {
- questionIDs[i] = fmt.Sprint(i + 1)
- }
-
- var err error
- b.Questions, err = db.GetQuestions(questionIDs)
- if err != nil {
- log.Fatalln(err)
- }
-
- // TODO: ggf. weniger komplex durch Pointer machen
- for i := range b.Questions {
- for j := range b.Questions[i].Answers {
- if parts := strings.Split(b.Questions[i].Answers[j].Text, ":/"); parts[0] == "file" {
- b.Questions[i].Answers[j].Text = parts[1]
- b.Questions[i].Answers[j].IsImage = true
- } else {
- b.Questions[i].Answers[j].IsImage = false
- }
- }
- }
-}
diff --git a/packages/session/login.go b/packages/session/login.go
new file mode 100644
index 0000000..a599b25
--- /dev/null
+++ b/packages/session/login.go
@@ -0,0 +1,84 @@
+/*
+ * Sicherheitsunterweisung
+ * Copyright (C) 2023 Jason Streifling
+ *
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with this program. If not, see .
+ */
+
+package session
+
+import (
+ "fmt"
+ "html/template"
+ "log"
+ "net/http"
+
+ "github.com/google/uuid"
+ "streifling.com/jason/sicherheitsunterweisung/packages/data"
+)
+
+func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ instructors, err := db.GetInstructors()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+
+ for _, i := range instructors {
+ if r.PostFormValue("login") == fmt.Sprint(i.ID) {
+ session := Session{
+ UUID: uuid.New(),
+ Instructor: *i,
+ Briefings: make([]*Briefing, 0),
+ }
+ (*ss) = append((*ss), &session)
+ cs <- &session
+
+ data := tableHTMLData{SessionID: session.UUID}
+ data.OTD, err = db.GetAllOverviewTableData()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+
+ template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", data)
+ return
+ }
+ }
+ template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
+ }
+}
+
+func findCorrectLogin(l string, ss *[]*Session) (*Briefing, *Participant, bool) {
+ for _, s := range *ss {
+ for _, b := range s.Briefings {
+ for _, p := range b.Participants {
+ if l == p.Login {
+ return b, p, true
+ }
+ }
+ }
+ }
+ return nil, nil, false
+}
+
+func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ briefing, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
+ if loginCorrect {
+ data := participantHTMLData{
+ BriefingID: briefing.UUID,
+ Participant: Participant{Login: participant.Login},
+ }
+
+ template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
+ } else {
+ template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
+ }
+ }
+}
diff --git a/packages/session/muxFuncs.go b/packages/session/mux.go
similarity index 57%
rename from packages/session/muxFuncs.go
rename to packages/session/mux.go
index d287a77..e70bbb4 100644
--- a/packages/session/muxFuncs.go
+++ b/packages/session/mux.go
@@ -13,11 +13,17 @@ package session
import (
"fmt"
+ "log"
"net/http"
+ "strings"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
+type Mux struct {
+ *http.ServeMux
+}
+
func NewMux() *Mux {
return &Mux{ServeMux: http.NewServeMux()}
}
@@ -26,25 +32,50 @@ func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *Participant, b *Brief
for p := range cp {
p.GivenAnswers = make([]int, len(b.Questions))
- mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", HandleParticipant(db, b, p))
- mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", HandleEndOfVideo(b, p))
+ mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleParticipant(db, b))
+ mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleEndOfVideo(b))
var i int
for i = range b.Questions {
- mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", HandleAnswer(db, b, p, int64(i+1)))
+ mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.HandleAnswer(db, b, int64(i+1)))
+ }
+ mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleAllowRetry())
+ mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRetry(b, &i))
+ mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRefresh(b))
+ }
+}
+
+func getQuestions(db *data.DB, b *Briefing) {
+ questionIDs := make([]string, 4)
+
+ for i := 0; i < len(questionIDs); i++ {
+ questionIDs[i] = fmt.Sprint(i + 1)
+ }
+
+ var err error
+ b.Questions, err = db.GetQuestions(questionIDs)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ for i := range b.Questions {
+ for j := range b.Questions[i].Answers {
+ if parts := strings.Split(b.Questions[i].Answers[j].Text, ":/"); parts[0] == "file" {
+ b.Questions[i].Answers[j].Text = parts[1]
+ b.Questions[i].Answers[j].IsImage = true
+ } else {
+ b.Questions[i].Answers[j].IsImage = false
+ }
}
- mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", HandleAllowRetry(p))
- mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", HandleRetry(b, p, &i))
- mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", HandleRefresh(b, p))
}
}
func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *Briefing, s *Session) {
participantChan := make(chan *Participant)
for b := range cb {
- b.getQuestions(db)
+ getQuestions(db, b)
- mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", HandleNewParticipant(b, participantChan))
- mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", HandleBriefingForm(db, s, b))
+ mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.HandleNewParticipant(participantChan))
+ mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.HandleBriefingForm(db, s))
go mux.handleParticipants(db, participantChan, b)
}
@@ -55,9 +86,9 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session)
for s := range cs {
(*ss) = append((*ss), s)
- mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", HandleSearch(db, s))
- mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", HandleNewBriefing(s, briefingChan))
- mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", HandleBriefingDone(db, s))
+ mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.HandleSearch(db))
+ mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.HandleNewBriefing(briefingChan))
+ mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.HandleBriefingDone(db))
go mux.handleBriefings(db, briefingChan, s)
}
diff --git a/packages/session/participant.go b/packages/session/participant.go
new file mode 100644
index 0000000..1318328
--- /dev/null
+++ b/packages/session/participant.go
@@ -0,0 +1,203 @@
+/*
+ * Sicherheitsunterweisung
+ * Copyright (C) 2023 Jason Streifling
+ *
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with this program. If not, see .
+ */
+
+package session
+
+import (
+ "fmt"
+ "html/template"
+ "log"
+ "net/http"
+ "strconv"
+
+ "streifling.com/jason/sicherheitsunterweisung/packages/data"
+)
+
+type Participant struct {
+ *data.Participant
+ Login string
+ GivenAnswers []int
+ NoIncorrect int
+ AllowRetry bool
+}
+
+func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login))
+ p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login))
+ p.Company = r.PostFormValue("company-" + fmt.Sprint(p.Login))
+
+ err := db.WriteParticipant(p.Participant)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+
+ data := participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: *p,
+ }
+
+ template.Must(template.ParseFiles("templates/video.html")).ExecuteTemplate(w, "content", data)
+ }
+}
+
+func handleGivenAnswer(p *Participant, i int64, r *http.Request) error {
+ answer, err := strconv.Atoi(r.PostFormValue("answer"))
+ if err != nil {
+ return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
+ }
+
+ p.GivenAnswers[i] = answer
+ return nil
+}
+
+func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
+ questions := make([]resultQuestion, len(sq))
+ for i, q := range sq {
+ questions[i].Text = q.Text
+ questions[i].Answers = make([]resultAnswer, len(q.Answers))
+
+ for j := range q.Answers {
+ questions[i].Answers[j].ID = q.Answers[j].ID
+ questions[i].Answers[j].Text = q.Answers[j].Text
+ questions[i].Answers[j].IsImage = q.Answers[j].IsImage
+
+ if j+1 == q.Correct {
+ questions[i].Answers[j].Correct = true
+ } else {
+ questions[i].Answers[j].Correct = false
+ }
+
+ if j+1 == givenAnswers[i] {
+ questions[i].Answers[j].Chosen = true
+ } else {
+ questions[i].Answers[j].Chosen = false
+ }
+ }
+ }
+
+ return questions
+}
+
+func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ if i < int64(len(b.Questions)) {
+ if err := handleGivenAnswer(p, i-1, r); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+
+ data := questionHTMLData{
+ participantHTMLData: participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: Participant{Login: p.Login},
+ },
+ QuestionID: i + 1,
+ Question: b.Questions[i],
+ }
+
+ template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
+ } else {
+ if err := handleGivenAnswer(p, i-1, r); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+
+ p.NoIncorrect = 0
+ for i, q := range b.Questions {
+ if p.GivenAnswers[i] != q.Correct {
+ p.NoIncorrect++
+ }
+ }
+
+ data := resultHTMLData{
+ participantHTMLData: participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: *p,
+ },
+ Questions: makeResultQuestions(b.Questions, p.GivenAnswers),
+ }
+
+ if data.NoIncorrect == 0 {
+ if err := db.WriteGivenAnswers(*b.Briefing, *p.Participant, b.Questions, p.GivenAnswers); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+ }
+
+ template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
+ }
+ }
+}
+
+func (p *Participant) HandleAllowRetry() http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ p.NoIncorrect = -1
+ p.AllowRetry = true
+ }
+}
+
+func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ if p.AllowRetry {
+ p.AllowRetry = false
+ (*i) = 0
+
+ data := questionHTMLData{
+ participantHTMLData: participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: Participant{Login: p.Login},
+ },
+ QuestionID: int64(*i + 1),
+ Question: b.Questions[*i],
+ }
+
+ template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
+ } else {
+ data := resultHTMLData{
+ participantHTMLData: participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: *p,
+ },
+ Questions: makeResultQuestions(b.Questions, p.GivenAnswers),
+ }
+
+ template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
+ }
+ }
+}
+
+func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ data := participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: *p,
+ }
+
+ template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
+ }
+}
+
+func (p *Participant) HandleEndOfVideo(b *Briefing) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ data := questionHTMLData{
+ participantHTMLData: participantHTMLData{
+ BriefingID: b.UUID,
+ Participant: Participant{Login: p.Login},
+ },
+ QuestionID: 1,
+ Question: b.Questions[0],
+ }
+
+ template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
+ }
+}
diff --git a/packages/session/session.go b/packages/session/session.go
new file mode 100644
index 0000000..47b178d
--- /dev/null
+++ b/packages/session/session.go
@@ -0,0 +1,67 @@
+/*
+ * Sicherheitsunterweisung
+ * Copyright (C) 2023 Jason Streifling
+ *
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with this program. If not, see .
+ */
+
+package session
+
+import (
+ "html/template"
+ "log"
+ "net/http"
+
+ "github.com/google/uuid"
+ "streifling.com/jason/sicherheitsunterweisung/packages/data"
+)
+
+type Session struct {
+ UUID uuid.UUID
+ Instructor data.Instructor
+ Briefings []*Briefing
+}
+
+func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ var err error
+ data := tableHTMLData{}
+
+ data.SessionID = s.UUID
+ data.OTD, err = db.GetOverviewTableDataByName(r.PostFormValue("search"))
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+ template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", data)
+ }
+}
+
+func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ briefing := Briefing{Briefing: &data.Briefing{InstructorID: s.Instructor.ID}, UUID: uuid.New()}
+ s.Briefings = append(s.Briefings, &briefing)
+ cb <- &briefing
+
+ template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{BriefingID: briefing.UUID})
+ }
+}
+
+func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ data := tableHTMLData{SessionID: s.UUID}
+ var err error
+
+ data.OTD, err = db.GetAllOverviewTableData()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ log.Panicln(err)
+ }
+
+ template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", data)
+ }
+}
diff --git a/packages/session/sessionStructs.go b/packages/session/sessionStructs.go
deleted file mode 100644
index 11c1602..0000000
--- a/packages/session/sessionStructs.go
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Sicherheitsunterweisung
- * Copyright (C) 2023 Jason Streifling
- *
- * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with this program. If not, see .
- */
-
-package session
-
-import (
- "net/http"
-
- "github.com/google/uuid"
- "streifling.com/jason/sicherheitsunterweisung/packages/data"
-)
-
-type Mux struct {
- *http.ServeMux
-}
-
-type Participant struct {
- *data.Participant
- Login string
- GivenAnswers []int
- NoIncorrect int
- AllowRetry bool
-}
-
-type Briefing struct {
- *data.Briefing
- UUID uuid.UUID
- Participants []*Participant
- Questions []data.Question
-}
-
-type Session struct {
- UUID uuid.UUID
- Instructor data.Instructor
- Briefings []*Briefing
-}