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(), Briefing: &data.Briefing{InstructorID: i.ID}, } (*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() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{SessionID: s.ID}) } } func (s *Session) HandleNewParticipant(cp chan<- *BriefingParticipant) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var err error participant := BriefingParticipant{ 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) } s.Participants = append(s.Participants, &participant) cp <- &participant data := participantHTMLData{ SessionID: s.ID, BriefingParticipant: BriefingParticipant{Login: participant.Login}, } 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 := summaryHTMLData{ SessionID: s.ID, 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 := participantHTMLData{ SessionID: session.ID, BriefingParticipant: BriefingParticipant{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 := questionHTMLData{ participantHTMLData: participantHTMLData{ SessionID: s.ID, BriefingParticipant: BriefingParticipant{Login: p.Login}, }, QuestionID: 1, Question: s.Questions[0], } 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 := questionHTMLData{ participantHTMLData: participantHTMLData{ SessionID: s.ID, BriefingParticipant: BriefingParticipant{Login: p.Login}, }, QuestionID: i + 1, Question: s.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 s.Questions { if p.GivenAnswers[i] != q.Correct { p.NoIncorrect++ } } data := resultHTMLData{ participantHTMLData: participantHTMLData{ SessionID: s.ID, BriefingParticipant: *p, }, 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 := questionHTMLData{ participantHTMLData: participantHTMLData{ SessionID: s.ID, BriefingParticipant: BriefingParticipant{Login: p.Login}, }, QuestionID: int64(*i + 1), Question: s.Questions[*i], } template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) } else { data := resultHTMLData{ participantHTMLData: participantHTMLData{ SessionID: s.ID, BriefingParticipant: *p, }, 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 := participantHTMLData{ SessionID: s.ID, 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) } }