204 lines
5.7 KiB
Go

/*
* 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 <http://www.gnu.org/licenses/>.
*/
package server
import (
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type participant struct {
*data.Participant
login string
givenAnswers []int
numberIncorrect int
allowRetry bool
}
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) 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 (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.numberIncorrect = 0
for i, q := range b.questions {
if p.givenAnswers[i] != q.Correct {
p.numberIncorrect++
}
}
data := resultHTMLData{
participantHTMLData: participantHTMLData{
briefingID: b.uuid,
participant: *p,
},
questions: makeResultQuestions(b.questions, p.givenAnswers),
}
if data.numberIncorrect == 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.numberIncorrect = -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)
}
}