204 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			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 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)
 | |
| 	}
 | |
| }
 |