300 lines
8.9 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"
"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)
}
}