212 lines
6.7 KiB
Go
Raw Normal View History

package session
2023-10-05 18:08:44 +02:00
import (
"fmt"
"html/template"
2023-10-20 16:33:00 +02:00
"log"
2023-10-05 18:08:44 +02:00
"net/http"
"time"
"github.com/google/uuid"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
2023-10-05 18:08:44 +02:00
)
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, "HandleInternalLogin: db.GetInstructors(): "+fmt.Sprint(err), 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.Briefing.InstructorID = i.ID
(*ss) = append((*ss), session)
cs <- session
2023-10-28 10:52:06 +02:00
data := new(tableHTMLData)
data.SessionID = session.ID
data.OTD, err = db.GetAllOverviewTableData()
if err != nil {
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), 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 HandleSearch(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
if err != nil {
http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError)
2023-10-28 10:52:06 +02:00
log.Panicln(err)
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
}
}
func (s *Session) HandleNewBriefing() http.HandlerFunc {
2023-10-05 18:08:44 +02:00
return func(w http.ResponseWriter, r *http.Request) {
data := new(briefingHTMLData)
data.SessionID = s.ID
2023-10-20 16:33:00 +02:00
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", data)
2023-10-05 18:08:44 +02:00
}
}
func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.HandlerFunc {
2023-10-05 18:08:44 +02:00
return func(w http.ResponseWriter, r *http.Request) {
2023-10-26 10:05:21 +02:00
var err error
p := new(data.Participant)
p.Login, err = generateLogin()
if err != nil {
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
2023-10-28 10:52:06 +02:00
log.Panicln(err)
}
s.Participants = append(s.Participants, p)
cp <- p
data := new(briefingHTMLData)
2023-10-26 10:05:21 +02:00
data.SessionID = s.ID
data.Login = p.Login
if err != nil {
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
2023-10-28 10:52:06 +02:00
log.Panicln(err)
}
2023-10-26 10:05:21 +02:00
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
2023-10-05 18:08:44 +02:00
}
}
func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc {
2023-10-05 18:08:44 +02:00
return func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
2023-10-26 10:05:21 +02:00
var err error
s.Briefing.Date = now.Format("2006-01-02")
s.Briefing.Time = now.Format("15:04:05")
s.Briefing.Location = r.PostFormValue("location")
s.Briefing.DocumentName = r.PostFormValue("document")
s.Briefing.AsOf = r.PostFormValue("as-of")
2023-10-26 10:05:21 +02:00
err = db.WriteBriefing(s.Briefing)
2023-10-26 10:05:21 +02:00
if err != nil {
http.Error(w, "SubmitBriefingForm: db.WriteBriefing(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
2023-10-19 15:22:45 +02:00
2023-10-28 10:52:06 +02:00
bs, err := db.GetAllOverviewTableData()
if err != nil {
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
}
}
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 *data.Participant, sq *[]data.Question) 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)
if err != nil {
http.Error(w, "DisplayQuestion: db.WriteParticipant(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = (*sq)[0]
data.QuestionID = 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}
2023-10-28 10:52:06 +02:00
func (s *Session) HandleAnswer(db *data.DB, p *SessionParticipant, sq *[]data.Question, i int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if i < int64(len(*sq)) {
2023-10-28 10:52:06 +02:00
if err := handleGivenAnswer(s, p, i-1, r); err != nil {
http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Question = (*sq)[i]
data.QuestionID = i + 1
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} else {
2023-10-28 10:52:06 +02:00
if err := handleGivenAnswer(s, p, i-1, r); err != nil {
http.Error(w, "DisplayTestResults: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
2023-10-28 10:52:06 +02:00
log.Println(p.GivenAnswers)
data := new(resultHTMLData)
data.SessionID = s.ID
data.Login = p.Login
data.Incorrect = 0
2023-10-28 10:52:06 +02:00
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
for i, q := range *sq {
if p.GivenAnswers[i] != q.Correct {
data.Incorrect++
}
2023-10-28 10:52:06 +02:00
}
2023-10-28 10:52:06 +02:00
if data.Incorrect == 0 {
if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.Questions, p.GivenAnswers); err != nil {
http.Error(w, "HandleAnswer: db.WriteGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
}
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
}
}
}
2023-10-28 10:52:06 +02:00
func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question, i *int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2023-10-28 10:52:06 +02:00
(*i) = 0
data := new(questionHTMLData)
data.SessionID = s.ID
data.Login = p.Login
2023-10-28 10:52:06 +02:00
data.Question = (*sq)[*i]
data.QuestionID = int64(*i + 1)
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}