Compare commits

..

No commits in common. "c1fa004384fb79d3939014233edee80792f0380f" and "2d07991c0099584eebaa63c16340b9d4b90bba09" have entirely different histories.

16 changed files with 509 additions and 555 deletions

View File

@ -1,95 +0,0 @@
/*
* 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 (
"crypto/rand"
"encoding/hex"
"fmt"
"html/template"
"log"
"net/http"
"time"
"github.com/google/uuid"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type Briefing struct {
*data.Briefing
UUID uuid.UUID
Participants []*Participant
Questions []data.Question
}
func generateLogin() (string, error) {
bs := make([]byte, 4)
if _, err := rand.Read(bs); err != nil {
return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err)
}
return hex.EncodeToString(bs), nil
}
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.UUID,
Participant: Participant{Login: participant.Login},
}
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
}
}
func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) 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{
SessionID: s.UUID,
BriefingID: b.UUID,
ParticipantsData: make([]participantHTMLData, len(b.Participants)),
}
for i, p := range b.Participants {
data.ParticipantsData[i].BriefingID = b.UUID
data.ParticipantsData[i].Participant = *p
}
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "content", data)
}
}

View File

@ -0,0 +1,285 @@
/*
* 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(),
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) {
s.DateTime = time.Now().Format("2006-01-02 15:04:05")
s.Location = r.PostFormValue("location")
s.Document = 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 := participantHTMLData{
SessionID: s.ID,
BriefingParticipant: *p,
}
template.Must(template.ParseFiles("templates/video.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: makeResultQuestions(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: makeResultQuestions(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)
}
}
func (s *Session) HandleEndOfVideo(p *BriefingParticipant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
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)
}
}

View File

@ -0,0 +1,121 @@
/*
* 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 (
"crypto/rand"
"encoding/hex"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
func generateLogin() (string, error) {
bs := make([]byte, 4)
if _, err := rand.Read(bs); err != nil {
return "", fmt.Errorf("error: generateLogin: rand.Read(bs): %v", err)
}
return hex.EncodeToString(bs), nil
}
func findCorrectLogin(l string, ss *[]*Session) (*Session, *BriefingParticipant, bool) {
for _, session := range *ss {
for _, p := range session.Participants {
if l == p.Login {
return session, p, true
}
}
}
return nil, nil, false
}
func newParticipant(l string) (*data.Participant, error) {
var err error
p := new(data.Participant)
p.ID, err = strconv.ParseInt(l, 10, 64)
if err != nil {
return nil, fmt.Errorf("error: newParticipant: strconv.Atoi(): %v", err)
}
return p, nil
}
func handleGivenAnswer(p *BriefingParticipant, i int64, r *http.Request) error {
answer, err := strconv.Atoi(r.PostFormValue("answer"))
if err != nil {
return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
}
p.GivenAnswers[i] = answer
return nil
}
func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
questions := make([]resultQuestion, len(sq))
for i, q := range sq {
questions[i].Text = q.Text
questions[i].Answers = make([]resultAnswer, len(q.Answers))
for j := range q.Answers {
questions[i].Answers[j].ID = q.Answers[j].ID
questions[i].Answers[j].Text = q.Answers[j].Text
questions[i].Answers[j].IsImage = q.Answers[j].IsImage
if j+1 == q.Correct {
questions[i].Answers[j].Correct = true
} else {
questions[i].Answers[j].Correct = false
}
if j+1 == givenAnswers[i] {
questions[i].Answers[j].Chosen = true
} else {
questions[i].Answers[j].Chosen = false
}
}
}
return questions
}
func (s *Session) getQuestions(db *data.DB) {
questionIDs := make([]string, 4)
for i := 0; i < len(questionIDs); i++ {
questionIDs[i] = fmt.Sprint(i + 1)
}
var err error
s.Questions, err = db.GetQuestions(questionIDs)
if err != nil {
log.Fatalln(err)
}
// TODO: ggf. weniger komplex durch Pointer machen
for i := range s.Questions {
for j := range s.Questions[i].Answers {
if parts := strings.Split(s.Questions[i].Answers[j].Text, ":/"); parts[0] == "file" {
s.Questions[i].Answers[j].Text = parts[1]
s.Questions[i].Answers[j].IsImage = true
} else {
s.Questions[i].Answers[j].IsImage = false
}
}
}
}

View File

@ -22,8 +22,8 @@ type tableHTMLData struct {
} }
type participantHTMLData struct { type participantHTMLData struct {
BriefingID uuid.UUID SessionID uuid.UUID
Participant BriefingParticipant
} }
type questionHTMLData struct { type questionHTMLData struct {
@ -52,6 +52,5 @@ type resultHTMLData struct {
type summaryHTMLData struct { type summaryHTMLData struct {
SessionID uuid.UUID SessionID uuid.UUID
BriefingID uuid.UUID
ParticipantsData []participantHTMLData ParticipantsData []participantHTMLData
} }

View File

@ -1,84 +0,0 @@
/*
* 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"
"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{
UUID: uuid.New(),
Instructor: *i,
Briefings: make([]*Briefing, 0),
}
(*ss) = append((*ss), &session)
cs <- &session
data := tableHTMLData{SessionID: session.UUID}
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 findCorrectLogin(l string, ss *[]*Session) (*Briefing, *Participant, bool) {
for _, s := range *ss {
for _, b := range s.Briefings {
for _, p := range b.Participants {
if l == p.Login {
return b, p, true
}
}
}
}
return nil, nil, false
}
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.UUID,
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)
}
}
}

View File

@ -1,95 +0,0 @@
/*
* 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"
"log"
"net/http"
"strings"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type Mux struct {
*http.ServeMux
}
func NewMux() *Mux {
return &Mux{ServeMux: http.NewServeMux()}
}
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *Participant, b *Briefing) {
for p := range cp {
p.GivenAnswers = make([]int, len(b.Questions))
mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleParticipant(db, b))
mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleEndOfVideo(b))
var i int
for i = range b.Questions {
mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.HandleAnswer(db, b, int64(i+1)))
}
mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleAllowRetry())
mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRetry(b, &i))
mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRefresh(b))
}
}
func getQuestions(db *data.DB, b *Briefing) {
questionIDs := make([]string, 4)
for i := 0; i < len(questionIDs); i++ {
questionIDs[i] = fmt.Sprint(i + 1)
}
var err error
b.Questions, err = db.GetQuestions(questionIDs)
if err != nil {
log.Fatalln(err)
}
for i := range b.Questions {
for j := range b.Questions[i].Answers {
if parts := strings.Split(b.Questions[i].Answers[j].Text, ":/"); parts[0] == "file" {
b.Questions[i].Answers[j].Text = parts[1]
b.Questions[i].Answers[j].IsImage = true
} else {
b.Questions[i].Answers[j].IsImage = false
}
}
}
}
func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *Briefing, s *Session) {
participantChan := make(chan *Participant)
for b := range cb {
getQuestions(db, b)
mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.HandleNewParticipant(participantChan))
mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.HandleBriefingForm(db, s))
go mux.handleParticipants(db, participantChan, b)
}
}
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
briefingChan := make(chan *Briefing)
for s := range cs {
(*ss) = append((*ss), s)
mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.HandleSearch(db))
mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.HandleNewBriefing(briefingChan))
mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.HandleBriefingDone(db))
go mux.handleBriefings(db, briefingChan, s)
}
}

View File

@ -0,0 +1,55 @@
/*
* 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"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *BriefingParticipant, s *Session) {
for p := range cp {
p.GivenAnswers = make([]int, len(s.Questions))
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p))
mux.HandleFunc("/end-video/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleEndOfVideo(p))
var i int
for i = range s.Questions {
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, p, int64(i+1)))
}
mux.HandleFunc("/allow-retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleAllowRetry(p))
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &i))
mux.HandleFunc("/refresh-summary/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRefresh(p))
}
}
func NewMux() *Mux {
return &Mux{ServeMux: http.NewServeMux()}
}
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
for s := range cs {
(*ss) = append((*ss), s)
participantChan := make(chan *BriefingParticipant)
s.getQuestions(db)
mux.HandleFunc("/search/"+fmt.Sprint(s.ID)+"/", s.HandleSearch(db))
mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing())
mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan))
mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db))
mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingDone())
go mux.handleParticipants(db, participantChan, s)
}
}

View File

@ -1,203 +0,0 @@
/*
* 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"
"strconv"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type Participant struct {
*data.Participant
Login string
GivenAnswers []int
NoIncorrect int
AllowRetry bool
}
func handleGivenAnswer(p *Participant, i int64, r *http.Request) error {
answer, err := strconv.Atoi(r.PostFormValue("answer"))
if err != nil {
return fmt.Errorf("error: handleGivenAnswer: strconv.Atoi(): %v", err)
}
p.GivenAnswers[i] = answer
return nil
}
func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestion {
questions := make([]resultQuestion, len(sq))
for i, q := range sq {
questions[i].Text = q.Text
questions[i].Answers = make([]resultAnswer, len(q.Answers))
for j := range q.Answers {
questions[i].Answers[j].ID = q.Answers[j].ID
questions[i].Answers[j].Text = q.Answers[j].Text
questions[i].Answers[j].IsImage = q.Answers[j].IsImage
if j+1 == q.Correct {
questions[i].Answers[j].Correct = true
} else {
questions[i].Answers[j].Correct = false
}
if j+1 == givenAnswers[i] {
questions[i].Answers[j].Chosen = true
} else {
questions[i].Answers[j].Chosen = false
}
}
}
return questions
}
func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) 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.UUID,
Participant: *p,
}
template.Must(template.ParseFiles("templates/video.html")).ExecuteTemplate(w, "content", data)
}
}
func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, 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.UUID,
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.UUID,
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 (p *Participant) HandleRetry(b *Briefing, 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.UUID,
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.UUID,
Participant: *p,
},
Questions: makeResultQuestions(b.Questions, p.GivenAnswers),
}
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
}
}
}
func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := participantHTMLData{
BriefingID: b.UUID,
Participant: *p,
}
template.Must(template.ParseFiles("templates/summary.html")).ExecuteTemplate(w, "participant", data)
}
}
func (p *Participant) HandleEndOfVideo(b *Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := questionHTMLData{
participantHTMLData: participantHTMLData{
BriefingID: b.UUID,
Participant: Participant{Login: p.Login},
},
QuestionID: 1,
Question: b.Questions[0],
}
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
}
}

View File

@ -1,67 +0,0 @@
/*
* 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 (
"html/template"
"log"
"net/http"
"github.com/google/uuid"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type Session struct {
UUID uuid.UUID
Instructor data.Instructor
Briefings []*Briefing
}
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.UUID
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}, UUID: uuid.New()}
s.Briefings = append(s.Briefings, &briefing)
cb <- &briefing
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", participantHTMLData{BriefingID: briefing.UUID})
}
}
func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := tableHTMLData{SessionID: s.UUID}
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)
}
}

View File

@ -0,0 +1,38 @@
/*
* 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 (
"net/http"
"github.com/google/uuid"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type Mux struct {
*http.ServeMux
}
type BriefingParticipant struct {
*data.Participant
Login string
GivenAnswers []int
NoIncorrect int
AllowRetry bool
}
type Session struct {
ID uuid.UUID
*data.Briefing
Participants []*BriefingParticipant
Questions []data.Question
}

View File

@ -11,11 +11,11 @@
{{define "add-buttons"}} {{define "add-buttons"}}
<div id="briefing-buttons"> <div id="briefing-buttons">
<button type="button" hx-post="/new-participant/{{.BriefingID}}/" hx-target="#briefing-buttons" hx-swap="outerHTML"> <button type="button" hx-post="/new-participant/{{.SessionID}}/" hx-target="#briefing-buttons" hx-swap="outerHTML">
Neuer Teilnehmer Neuer Teilnehmer
</button> </button>
<button type="submit" hx-post="/submit-form/{{.BriefingID}}/" hx-target="#content"> <button type="submit" hx-post="/submit-form/{{.SessionID}}/" hx-target="#content">
Weiter Weiter
</button> </button>
</div> </div>

View File

@ -20,7 +20,7 @@
<label for="company-{{.Login}}">Firma</label> <label for="company-{{.Login}}">Firma</label>
<input type="text" name="company-{{.Login}}" id="company-{{.Login}}" /> <input type="text" name="company-{{.Login}}" id="company-{{.Login}}" />
<button type="button" hx-post="/submit-participant/{{.BriefingID}}/{{.Login}}/" hx-target="#content"> <button type="button" hx-post="/submit-participant/{{.SessionID}}/{{.Login}}/" hx-target="#content">
Fertig Fertig
</button> </button>
</form> </form>

View File

@ -29,7 +29,7 @@
<form> <form>
{{template "answers" .}} {{template "answers" .}}
<button hx-post="/submit-answer/{{.BriefingID}}/{{.Login}}/{{.QuestionID}}/" hx-target="#content" type="submit"> <button hx-post="/submit-answer/{{.SessionID}}/{{.Login}}/{{.QuestionID}}/" hx-target="#content" type="submit">
Weiter Weiter
</button> </button>
</form> </form>

View File

@ -28,7 +28,7 @@
<p>{{.BriefingParticipant.NoIncorrect}} Fehler</p> <p>{{.BriefingParticipant.NoIncorrect}} Fehler</p>
{{if gt .BriefingParticipant.NoIncorrect 0}} {{if gt .BriefingParticipant.NoIncorrect 0}}
<p>Bitte nachschulen lassen und anschließend wiederholen.</p> <p>Bitte nachschulen lassen und anschließend wiederholen.</p>
<button hx-post="/retry/{{.BriefingID}}/{{.Login}}/" hx-target="#content" type="submit">Wiederholen</button> <button hx-post="/retry/{{.SessionID}}/{{.Login}}/" hx-target="#content" type="submit">Wiederholen</button>
{{end}} {{end}}
{{range .Questions}} {{range .Questions}}
<p>{{.Text}}</p> <p>{{.Text}}</p>

View File

@ -10,12 +10,12 @@
--> -->
{{define "refresh"}} {{define "refresh"}}
<button hx-post="/refresh-summary/{{.BriefingID}}/{{.Login}}/" hx-target="#id-{{.Login}}" <button hx-post="/refresh-summary/{{.SessionID}}/{{.Login}}/" hx-target="#id-{{.Login}}"
type="button">Aktualisieren</button> type="button">Aktualisieren</button>
{{end}} {{end}}
{{define "retry"}} {{define "retry"}}
<button hx-post="/allow-retry/{{.BriefingID}}/{{.Login}}/" hx-swap="delete" type="button">Wiederholen erlauben</button> <button hx-post="/allow-retry/{{.SessionID}}/{{.Login}}/" hx-swap="delete" type="button">Wiederholen erlauben</button>
{{end}} {{end}}
{{define "participant"}} {{define "participant"}}

View File

@ -1,6 +1,6 @@
{{define "content"}} {{define "content"}}
<video src="/static/test.mp4">Test</video> <video src="/static/test.mp4">Test</video>
<button hx-post="/end-video/{{.BriefingID}}/{{.Login}}/" hx-target="#content" type="button"> <button hx-post="/end-video/{{.SessionID}}/{{.Login}}/" hx-target="#content" type="button">
Weiter Weiter
</button> </button>
{{end}} {{end}}