diff --git a/packages/session/handlerFuncs.go b/packages/session/handlerFuncs.go index 02169c2..51d5eca 100644 --- a/packages/session/handlerFuncs.go +++ b/packages/session/handlerFuncs.go @@ -33,8 +33,9 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H for _, i := range instructors { if r.PostFormValue("login") == fmt.Sprint(i.ID) { session := Session{ - ID: uuid.New(), - Briefing: &data.Briefing{InstructorID: i.ID}, + ID: uuid.New(), + Instructor: *i, + Briefings: make([]*Briefing, 0), } (*ss) = append((*ss), &session) cs <- &session @@ -69,16 +70,20 @@ func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc { } } -func (s *Session) HandleNewBriefing() http.HandlerFunc { +func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{SessionID: s.ID}) + 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 (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.HandlerFunc { +func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var err error - participant := BriefingParticipant{ + participant := Participant{ Participant: new(data.Participant), NoIncorrect: -1, AllowRetry: false, @@ -89,38 +94,38 @@ func (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.Hand http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } - s.Participants = append(s.Participants, &participant) + b.Participants = append(b.Participants, &participant) cp <- &participant data := participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: BriefingParticipant{Login: participant.Login}, + BriefingID: b.ID, + Participant: Participant{Login: participant.Login}, } template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data) } } -func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc { +func (b *Briefing) HandleBriefingForm(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - s.DateTime = time.Now().Format("2006-01-02 15:04:05") - s.Location = r.PostFormValue("location") - s.Document = r.PostFormValue("document") - s.AsOf = r.PostFormValue("as-of") + 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(s.Briefing) + err := db.WriteBriefing(b.Briefing) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } data := summaryHTMLData{ - SessionID: s.ID, - ParticipantsData: make([]participantHTMLData, len(s.Participants)), + BriefingID: b.ID, + ParticipantsData: make([]participantHTMLData, len(b.Participants)), } - for i, p := range s.Participants { - data.ParticipantsData[i].SessionID = s.ID - data.ParticipantsData[i].BriefingParticipant = *p + 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) @@ -129,11 +134,11 @@ func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc { func HandleExternalLogin(ss *[]*Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss) + briefing, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss) if loginCorrect { data := participantHTMLData{ - SessionID: session.ID, - BriefingParticipant: BriefingParticipant{Login: participant.Login}, + BriefingID: briefing.ID, + Participant: Participant{Login: participant.Login}, } template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data) @@ -143,7 +148,7 @@ func HandleExternalLogin(ss *[]*Session) http.HandlerFunc { } } -func (s *Session) HandleParticipant(db *data.DB, p *BriefingParticipant) http.HandlerFunc { +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)) @@ -156,17 +161,17 @@ func (s *Session) HandleParticipant(db *data.DB, p *BriefingParticipant) http.Ha } data := participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: *p, + BriefingID: b.ID, + Participant: *p, } template.Must(template.ParseFiles("templates/video.html")).ExecuteTemplate(w, "content", data) } } -func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) http.HandlerFunc { +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(s.Questions)) { + 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) @@ -174,11 +179,11 @@ func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) htt data := questionHTMLData{ participantHTMLData: participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: BriefingParticipant{Login: p.Login}, + BriefingID: b.ID, + Participant: Participant{Login: p.Login}, }, QuestionID: i + 1, - Question: s.Questions[i], + Question: b.Questions[i], } template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) @@ -189,7 +194,7 @@ func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) htt } p.NoIncorrect = 0 - for i, q := range s.Questions { + for i, q := range b.Questions { if p.GivenAnswers[i] != q.Correct { p.NoIncorrect++ } @@ -197,14 +202,14 @@ func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) htt data := resultHTMLData{ participantHTMLData: participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: *p, + BriefingID: b.ID, + Participant: *p, }, - Questions: makeResultQuestions(s.Questions, p.GivenAnswers), + Questions: makeResultQuestions(b.Questions, p.GivenAnswers), } if data.NoIncorrect == 0 { - if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.Questions, p.GivenAnswers); err != nil { + if err := db.WriteGivenAnswers(*b.Briefing, *p.Participant, b.Questions, p.GivenAnswers); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Panicln(err) } @@ -215,14 +220,14 @@ func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) htt } } -func (s *Session) HandleAllowRetry(p *BriefingParticipant) http.HandlerFunc { +func (p *Participant) HandleAllowRetry() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { p.NoIncorrect = -1 p.AllowRetry = true } } -func (s *Session) HandleRetry(p *BriefingParticipant, i *int) http.HandlerFunc { +func (b *Briefing) HandleRetry(p *Participant, i *int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if p.AllowRetry { p.AllowRetry = false @@ -230,21 +235,21 @@ func (s *Session) HandleRetry(p *BriefingParticipant, i *int) http.HandlerFunc { data := questionHTMLData{ participantHTMLData: participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: BriefingParticipant{Login: p.Login}, + BriefingID: b.ID, + Participant: Participant{Login: p.Login}, }, QuestionID: int64(*i + 1), - Question: s.Questions[*i], + Question: b.Questions[*i], } template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) } else { data := resultHTMLData{ participantHTMLData: participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: *p, + BriefingID: b.ID, + Participant: *p, }, - Questions: makeResultQuestions(s.Questions, p.GivenAnswers), + Questions: makeResultQuestions(b.Questions, p.GivenAnswers), } template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data) @@ -252,32 +257,41 @@ func (s *Session) HandleRetry(p *BriefingParticipant, i *int) http.HandlerFunc { } } -func (s *Session) HandleRefresh(p *BriefingParticipant) http.HandlerFunc { +func (b *Briefing) HandleRefresh(p *Participant) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { data := participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: *p, + BriefingID: b.ID, + Participant: *p, } template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data) } } -func (s *Session) HandleBriefingDone() http.HandlerFunc { +func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil) + 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 (s *Session) HandleEndOfVideo(p *BriefingParticipant) http.HandlerFunc { +func (b *Briefing) HandleEndOfVideo(p *Participant) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { data := questionHTMLData{ participantHTMLData: participantHTMLData{ - SessionID: s.ID, - BriefingParticipant: BriefingParticipant{Login: p.Login}, + BriefingID: b.ID, + Participant: Participant{Login: p.Login}, }, QuestionID: 1, - Question: s.Questions[0], + 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 index 29841e4..115fa11 100644 --- a/packages/session/helperFuncs.go +++ b/packages/session/helperFuncs.go @@ -33,11 +33,13 @@ func generateLogin() (string, error) { return hex.EncodeToString(bs), nil } -func findCorrectLogin(l string, ss *[]*Session) (*Session, *BriefingParticipant, bool) { - for _, session := range *ss { - for _, p := range session.Participants { - if l == p.Login { - return session, p, true +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 + } } } } @@ -56,7 +58,7 @@ func newParticipant(l string) (*data.Participant, error) { return p, nil } -func handleGivenAnswer(p *BriefingParticipant, i int64, r *http.Request) error { +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) @@ -94,7 +96,7 @@ func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestio return questions } -func (s *Session) getQuestions(db *data.DB) { +func (b *Briefing) getQuestions(db *data.DB) { questionIDs := make([]string, 4) for i := 0; i < len(questionIDs); i++ { @@ -102,19 +104,19 @@ func (s *Session) getQuestions(db *data.DB) { } var err error - s.Questions, err = db.GetQuestions(questionIDs) + b.Questions, err = db.GetQuestions(questionIDs) if err != nil { log.Fatalln(err) } // TODO: ggf. weniger komplex durch Pointer machen - for i := range s.Questions { - for j := range s.Questions[i].Answers { - if parts := strings.Split(s.Questions[i].Answers[j].Text, ":/"); parts[0] == "file" { - s.Questions[i].Answers[j].Text = parts[1] - s.Questions[i].Answers[j].IsImage = true + 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 { - s.Questions[i].Answers[j].IsImage = false + b.Questions[i].Answers[j].IsImage = false } } } diff --git a/packages/session/htmlStructs.go b/packages/session/htmlStructs.go index 69ad8ec..c28ccd6 100644 --- a/packages/session/htmlStructs.go +++ b/packages/session/htmlStructs.go @@ -22,8 +22,8 @@ type tableHTMLData struct { } type participantHTMLData struct { - SessionID uuid.UUID - BriefingParticipant + BriefingID int64 + Participant } type questionHTMLData struct { @@ -51,6 +51,6 @@ type resultHTMLData struct { } type summaryHTMLData struct { - SessionID uuid.UUID + BriefingID int64 ParticipantsData []participantHTMLData } diff --git a/packages/session/muxFuncs.go b/packages/session/muxFuncs.go index ff27418..d2572c9 100644 --- a/packages/session/muxFuncs.go +++ b/packages/session/muxFuncs.go @@ -18,38 +18,47 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/data" ) -func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *BriefingParticipant, s *Session) { - for p := range cp { - p.GivenAnswers = make([]int, len(s.Questions)) - - mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p)) - mux.HandleFunc("/end-video/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleEndOfVideo(p)) - var i int - for i = range s.Questions { - mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, p, int64(i+1))) - } - mux.HandleFunc("/allow-retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleAllowRetry(p)) - mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &i)) - mux.HandleFunc("/refresh-summary/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRefresh(p)) - } -} - func NewMux() *Mux { return &Mux{ServeMux: http.NewServeMux()} } -func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) { - for s := range cs { - (*ss) = append((*ss), s) - participantChan := make(chan *BriefingParticipant) - s.getQuestions(db) +func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *Participant, b *Briefing) { + for p := range cp { + p.GivenAnswers = make([]int, len(b.Questions)) - mux.HandleFunc("/search/"+fmt.Sprint(s.ID)+"/", s.HandleSearch(db)) - mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing()) - mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan)) - mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db)) - mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingDone()) - - go mux.handleParticipants(db, participantChan, s) + mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.ID)+"/"+fmt.Sprint(p.Login)+"/", b.HandleParticipant(db, p)) + mux.HandleFunc("/end-video/"+fmt.Sprint(b.ID)+"/"+fmt.Sprint(p.Login)+"/", b.HandleEndOfVideo(p)) + var i int + for i = range b.Questions { + mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", b.HandleAnswer(db, p, int64(i+1))) + } + mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.ID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleAllowRetry()) + mux.HandleFunc("/retry/"+fmt.Sprint(b.ID)+"/"+fmt.Sprint(p.Login)+"/", b.HandleRetry(p, &i)) + mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.ID)+"/"+fmt.Sprint(p.Login)+"/", b.HandleRefresh(p)) + } +} + +func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *Briefing) { + participantChan := make(chan *Participant) + for b := range cb { + b.getQuestions(db) + + mux.HandleFunc("/new-participant/"+fmt.Sprint(b.ID)+"/", b.HandleNewParticipant(participantChan)) + mux.HandleFunc("/submit-form/"+fmt.Sprint(b.ID)+"/", b.HandleBriefingForm(db)) + + go mux.handleParticipants(db, participantChan, b) + } +} + +func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) { + briefingChan := make(chan *Briefing) + for s := range cs { + (*ss) = append((*ss), s) + + mux.HandleFunc("/search/"+fmt.Sprint(s.ID)+"/", s.HandleSearch(db)) + mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing(briefingChan)) + mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingDone(db)) + + go mux.handleBriefings(db, briefingChan) } } diff --git a/packages/session/sessionStructs.go b/packages/session/sessionStructs.go index 97822ad..17fc8f8 100644 --- a/packages/session/sessionStructs.go +++ b/packages/session/sessionStructs.go @@ -22,7 +22,7 @@ type Mux struct { *http.ServeMux } -type BriefingParticipant struct { +type Participant struct { *data.Participant Login string GivenAnswers []int @@ -30,9 +30,15 @@ type BriefingParticipant struct { AllowRetry bool } -type Session struct { - ID uuid.UUID +type Briefing struct { + ID int64 *data.Briefing - Participants []*BriefingParticipant + Participants []*Participant Questions []data.Question } + +type Session struct { + ID uuid.UUID + Instructor data.Instructor + Briefings []*Briefing +} diff --git a/templates/briefing.html b/templates/briefing.html index a8c76f7..fbc7109 100644 --- a/templates/briefing.html +++ b/templates/briefing.html @@ -11,11 +11,11 @@ {{define "add-buttons"}}
- -
diff --git a/templates/participant.html b/templates/participant.html index 9909de0..7f2f120 100644 --- a/templates/participant.html +++ b/templates/participant.html @@ -20,7 +20,7 @@ - diff --git a/templates/question.html b/templates/question.html index 1af1ef6..c6f64cc 100644 --- a/templates/question.html +++ b/templates/question.html @@ -29,7 +29,7 @@
{{template "answers" .}} -
diff --git a/templates/result.html b/templates/result.html index a2d5816..9dbf753 100644 --- a/templates/result.html +++ b/templates/result.html @@ -28,7 +28,7 @@

{{.BriefingParticipant.NoIncorrect}} Fehler

{{if gt .BriefingParticipant.NoIncorrect 0}}

Bitte nachschulen lassen und anschließend wiederholen.

- + {{end}} {{range .Questions}}

{{.Text}}

diff --git a/templates/summary.html b/templates/summary.html index 2e414a3..94f960a 100644 --- a/templates/summary.html +++ b/templates/summary.html @@ -10,12 +10,12 @@ --> {{define "refresh"}} - {{end}} {{define "retry"}} - + {{end}} {{define "participant"}} @@ -40,7 +40,7 @@ {{end}} {{define "content"}} - + {{range .ParticipantsData}} {{template "participant" .}} {{end}} diff --git a/templates/video.html b/templates/video.html index f81c2c9..2e505c3 100644 --- a/templates/video.html +++ b/templates/video.html @@ -1,6 +1,6 @@ {{define "content"}} - {{end}}