254 lines
7.5 KiB
Go

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 := new(Session)
session.ID = uuid.New()
session.Briefing = new(data.Briefing)
session.InstructorID = i.ID
(*ss) = append((*ss), session)
cs <- session
data := new(tableHTMLData)
data.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() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := new(participantHTMLData)
data.SessionID = s.ID
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
}
}
func (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err error
p := new(BriefingParticipant)
p.Participant = new(data.Participant)
p.NoIncorrect = -1
p.AllowRetry = false
p.Login, err = generateLogin()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
s.Participants = append(s.Participants, p)
cp <- p
data := new(participantHTMLData)
data.SessionID = s.ID
data.Login = p.Login
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
}
}
func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
s.Date = now.Format("2006-01-02")
s.Time = now.Format("15:04:05")
s.Location = r.PostFormValue("location")
s.DocumentName = r.PostFormValue("document")
s.AsOf = r.PostFormValue("as-of")
err := db.WriteBriefing(s.Briefing)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(summaryHTMLData)
data.SessionID = s.ID
data.ParticipantsData = make([]participantHTMLData, len(s.Participants))
for i, p := range s.Participants {
data.ParticipantsData[i].SessionID = s.ID
data.ParticipantsData[i].BriefingParticipant = *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) {
session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
if loginCorrect {
data := new(participantHTMLData)
data.SessionID = session.ID
data.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 (s *Session) HandleParticipant(db *data.DB, p *BriefingParticipant) 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 := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = s.Questions[0]
data.QuestionID = 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}
func (s *Session) HandleAnswer(db *data.DB, p *BriefingParticipant, i int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if i < int64(len(s.Questions)) {
if err := handleGivenAnswer(p, i-1, r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = s.Questions[i]
data.QuestionID = i + 1
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 s.Questions {
if p.GivenAnswers[i] != q.Correct {
p.NoIncorrect++
}
}
data := new(resultHTMLData)
data.SessionID = s.ID
data.BriefingParticipant = *p
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
if data.NoIncorrect == 0 {
if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.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 (s *Session) HandleAllowRetry(p *BriefingParticipant) 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 {
return func(w http.ResponseWriter, r *http.Request) {
if p.AllowRetry {
p.AllowRetry = false
(*i) = 0
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = s.Questions[*i]
data.QuestionID = int64(*i + 1)
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} else {
data := new(resultHTMLData)
data.SessionID = s.ID
data.BriefingParticipant = *p
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
}
}
}
func (s *Session) HandleRefresh(p *BriefingParticipant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := new(participantHTMLData)
data.SessionID = s.ID
data.BriefingParticipant = *p
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
}
}
func (s *Session) HandleBriefingDone() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
}
}