2023-10-28 08:49:28 +02:00
|
|
|
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"
|
2023-10-18 16:22:39 +02:00
|
|
|
"time"
|
2023-10-16 18:51:52 +02:00
|
|
|
|
2023-10-19 20:05:26 +02:00
|
|
|
"github.com/google/uuid"
|
2023-10-28 08:49:28 +02:00
|
|
|
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
2023-10-05 18:08:44 +02:00
|
|
|
)
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func HandleInternalLogin(ss *[]*Session, cs chan<- *Session, db *data.DB) http.HandlerFunc {
|
2023-10-07 18:32:33 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2023-10-28 08:01:34 +02:00
|
|
|
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) {
|
2023-10-28 08:49:28 +02:00
|
|
|
session := new(Session)
|
2023-10-28 08:01:34 +02:00
|
|
|
session.ID = uuid.New()
|
2023-10-28 08:49:28 +02:00
|
|
|
session.Briefing = new(data.Briefing)
|
2023-10-28 08:01:34 +02:00
|
|
|
session.Briefing.InstructorID = i.ID
|
|
|
|
(*ss) = append((*ss), session)
|
|
|
|
cs <- session
|
|
|
|
|
|
|
|
displayTable(w, db)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
|
2023-10-07 18:32:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func HandleSearch(db *data.DB) http.HandlerFunc {
|
2023-10-08 10:36:16 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2023-10-17 18:37:31 +02:00
|
|
|
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
2023-10-08 10:36:16 +02:00
|
|
|
if err != nil {
|
2023-10-17 18:37:31 +02:00
|
|
|
http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError)
|
2023-10-08 10:36:16 +02:00
|
|
|
}
|
|
|
|
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (s *Session) HandleNewBriefing() http.HandlerFunc {
|
2023-10-05 18:08:44 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2023-10-28 08:49:28 +02:00
|
|
|
data := new(briefingHTMLData)
|
2023-10-28 08:01:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +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
|
2023-10-28 08:49:28 +02:00
|
|
|
p := new(data.Participant)
|
2023-10-28 08:01:34 +02:00
|
|
|
p.Login, err = generateLogin()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
s.Participants = append(s.Participants, p)
|
|
|
|
cp <- p
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
data := new(briefingHTMLData)
|
2023-10-26 10:05:21 +02:00
|
|
|
data.SessionID = s.ID
|
2023-10-28 08:01:34 +02:00
|
|
|
data.Login = p.Login
|
2023-10-17 18:37:31 +02:00
|
|
|
if err != nil {
|
2023-10-28 08:01:34 +02:00
|
|
|
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
2023-10-17 18:37:31 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +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) {
|
2023-10-18 16:22:39 +02:00
|
|
|
now := time.Now()
|
2023-10-26 10:05:21 +02:00
|
|
|
var err error
|
2023-10-18 16:22:39 +02:00
|
|
|
|
2023-10-28 08:01:34 +02:00
|
|
|
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
|
|
|
|
2023-10-28 08:01:34 +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-26 10:05:21 +02:00
|
|
|
displayTable(w, db)
|
2023-10-10 19:06:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func HandleExternalLogin(ss *[]*Session) http.HandlerFunc {
|
2023-10-10 20:57:53 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2023-10-28 08:01:34 +02:00
|
|
|
session, participant, loginCorrect := findCorrectLogin(r.PostFormValue("login"), ss)
|
|
|
|
if loginCorrect {
|
2023-10-28 08:49:28 +02:00
|
|
|
data := new(participantHTMLData)
|
2023-10-28 08:01:34 +02:00
|
|
|
data.SessionID = session.ID
|
|
|
|
data.Login = participant.Login
|
2023-10-16 18:51:52 +02:00
|
|
|
|
2023-10-19 20:05:26 +02:00
|
|
|
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", data)
|
2023-10-10 20:57:53 +02:00
|
|
|
} else {
|
|
|
|
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (s *Session) HandleParticipant(p *data.Participant, sq *[]data.Question, db *data.DB) http.HandlerFunc {
|
2023-10-28 08:01:34 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
data := new(questionHTMLData)
|
2023-10-28 08:01:34 +02:00
|
|
|
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 08:49:28 +02:00
|
|
|
func (s *Session) HandleAnswer(db *data.DB, p *data.Participant, sq *[]data.Question, i int64) http.HandlerFunc {
|
2023-10-28 08:01:34 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Println(i, len(*sq))
|
|
|
|
if i < int64(len(*sq)) {
|
|
|
|
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil {
|
|
|
|
http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
data := new(questionHTMLData)
|
2023-10-28 08:01:34 +02:00
|
|
|
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 {
|
|
|
|
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil {
|
|
|
|
http.Error(w, "DisplayTestResults: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
givenAnswers, err := db.GetGivenAnswers(s.Briefing.ID, p.ID, s.Questions)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "DisplayTestResults: db.GetGivenAnswers(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
data := new(resultHTMLData)
|
2023-10-28 08:01:34 +02:00
|
|
|
data.SessionID = s.ID
|
|
|
|
data.Login = p.Login
|
|
|
|
data.Incorrect = 0
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
data.Questions = make([]htmlQuestion, 0)
|
2023-10-28 08:01:34 +02:00
|
|
|
for i, q := range s.Questions {
|
2023-10-28 08:49:28 +02:00
|
|
|
question := new(htmlQuestion)
|
2023-10-28 08:01:34 +02:00
|
|
|
question.Text = q.Text
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
question.Answers = make([]htmlAnswer, 0)
|
2023-10-28 08:01:34 +02:00
|
|
|
for j, a := range q.Answers {
|
2023-10-28 08:49:28 +02:00
|
|
|
answer := new(htmlAnswer)
|
2023-10-28 08:01:34 +02:00
|
|
|
answer.Text = a.Text
|
|
|
|
|
|
|
|
if j+1 == q.Correct {
|
|
|
|
answer.Correct = true
|
|
|
|
} else {
|
|
|
|
answer.Correct = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if j+1 == givenAnswers[i] {
|
|
|
|
answer.Chosen = true
|
|
|
|
} else {
|
|
|
|
answer.Chosen = false
|
|
|
|
}
|
|
|
|
question.Answers = append(question.Answers, *answer)
|
|
|
|
}
|
|
|
|
data.Questions = append(data.Questions, *question)
|
|
|
|
|
|
|
|
if givenAnswers[i] != q.Correct {
|
|
|
|
data.Incorrect++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
|
2023-10-28 08:01:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question) http.HandlerFunc {
|
2023-10-28 08:01:34 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2023-10-28 08:49:28 +02:00
|
|
|
data := new(questionHTMLData)
|
2023-10-28 08:01:34 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|