diff --git a/packages/data/dbFuncs.go b/packages/data/dbFuncs.go index 34d75e7..26b004c 100644 --- a/packages/data/dbFuncs.go +++ b/packages/data/dbFuncs.go @@ -68,21 +68,23 @@ func (db *DB) WriteParticipant(p *Participant) error { return nil } -func (db *DB) WriteGivenAnswer(b *Briefing, p *Participant, q *Question, g int) error { - _, err := db.Exec(` - INSERT INTO given_answers - (briefing_id, participant_id, question_id, given_answer) - VALUES - (?, ?, ?, ?) - `, b.ID, p.ID, q.ID, g) - if err != nil { - return fmt.Errorf("*DB.writeGivenAnswers: db.Exec(): %v\n", err) +func (db *DB) WriteGivenAnswers(b Briefing, p Participant, sq []Question, givenAnswers []int) error { + for i, q := range sq { + _, err := db.Exec(` + INSERT INTO given_answers + (briefing_id, participant_id, question_id, given_answer) + VALUES + (?, ?, ?, ?) + `, b.ID, p.ID, q.ID, givenAnswers[i]) + if err != nil { + return fmt.Errorf("*DB.WriteGivenAnswers: db.Exec(): %v\n", err) + } } return nil } -func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) { +func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) { rows, err := db.Query(` SELECT i.first_name, @@ -115,7 +117,7 @@ func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) { } defer rows.Close() - data := make([]*OverviewTableData, 0) + data := make([]OverviewTableData, 0) for rows.Next() { otd := new(OverviewTableData) @@ -135,7 +137,7 @@ func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) { return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err) } - data = append(data, otd) + data = append(data, *otd) } return data, nil diff --git a/packages/session/handlerFuncs.go b/packages/session/handlerFuncs.go index 7a0763f..e22df0a 100644 --- a/packages/session/handlerFuncs.go +++ b/packages/session/handlerFuncs.go @@ -28,7 +28,15 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H (*ss) = append((*ss), session) cs <- session - displayTable(w, db) + 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 } } @@ -41,6 +49,7 @@ func HandleSearch(db *data.DB) http.HandlerFunc { bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search")) if err != nil { http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError) + log.Panicln(err) } template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs) } @@ -61,6 +70,7 @@ func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.Handler p.Login, err = generateLogin() if err != nil { http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError) + log.Panicln(err) } s.Participants = append(s.Participants, p) cp <- p @@ -70,6 +80,7 @@ func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.Handler data.Login = p.Login if err != nil { http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError) + log.Panicln(err) } template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data) @@ -93,7 +104,13 @@ func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc { log.Panicln(err) } - displayTable(w, db) + 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) } } @@ -134,11 +151,10 @@ func (s *Session) HandleParticipant(db *data.DB, p *data.Participant, sq *[]data } } -func (s *Session) HandleAnswer(db *data.DB, p *data.Participant, sq *[]data.Question, i int64) http.HandlerFunc { +func (s *Session) HandleAnswer(db *data.DB, p *SessionParticipant, sq *[]data.Question, i int64) http.HandlerFunc { 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 { + if err := handleGivenAnswer(s, p, i-1, r); err != nil { http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError) log.Panicln(err) } @@ -151,64 +167,44 @@ func (s *Session) HandleAnswer(db *data.DB, p *data.Participant, sq *[]data.Ques template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) } else { - if err := handleGivenAnswer(s, p, i-1, r, db); err != nil { + if err := handleGivenAnswer(s, p, i-1, r); 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) - } + log.Println(p.GivenAnswers) data := new(resultHTMLData) data.SessionID = s.ID data.Login = p.Login data.Incorrect = 0 - - data.Questions = make([]htmlQuestion, 0) - for i, q := range s.Questions { - question := new(htmlQuestion) - question.Text = q.Text - - question.Answers = make([]htmlAnswer, 0) - for j, a := range q.Answers { - answer := new(htmlAnswer) - 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.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers) + for i, q := range *sq { + if p.GivenAnswers[i] != q.Correct { data.Incorrect++ } } + 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) } } } -func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question) http.HandlerFunc { +func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question, i *int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + (*i) = 0 + data := new(questionHTMLData) data.SessionID = s.ID data.Login = p.Login - data.Question = (*sq)[0] - data.QuestionID = 1 + data.Question = (*sq)[*i] + data.QuestionID = int64(*i + 1) template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) } diff --git a/packages/session/helperFuncs.go b/packages/session/helperFuncs.go index 5e033bf..d53bfcf 100644 --- a/packages/session/helperFuncs.go +++ b/packages/session/helperFuncs.go @@ -4,19 +4,25 @@ import ( "crypto/rand" "encoding/hex" "fmt" - "html/template" "net/http" "strconv" "streifling.com/jason/sicherheitsunterweisung/packages/data" ) -func displayTable(w http.ResponseWriter, db *data.DB) { - bs, err := db.GetAllOverviewTableData() - if err != nil { - http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError) +func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) { + for p := range cp { + sessionParticipant := new(SessionParticipant) + sessionParticipant.Participant = p + sessionParticipant.GivenAnswers = make([]int, len(s.Questions)) + + mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p, &s.Questions)) + 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, sessionParticipant, &s.Questions, int64(i+1))) + } + mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &s.Questions, &i)) } - template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs) } func generateLogin() (string, error) { @@ -52,15 +58,42 @@ func newParticipant(l string) (*data.Participant, error) { return p, nil } -func handleGivenAnswer(s *Session, p *data.Participant, i int64, r *http.Request, db *data.DB) error { +func handleGivenAnswer(s *Session, p *SessionParticipant, i int64, r *http.Request) error { answer, err := strconv.Atoi(r.PostFormValue("answer")) if err != nil { return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err) } - if err := db.WriteGivenAnswer(s.Briefing, p, &s.Questions[i], answer); err != nil { - return fmt.Errorf("handleGivenAnswer: db.WriteGivenAnswer(): %v\n", err) - } - + p.GivenAnswers[i] = answer return nil } + +func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []htmlQuestion { + questions := make([]htmlQuestion, 0) + for i, q := range sq { + question := new(htmlQuestion) + question.Text = q.Text + + question.Answers = make([]htmlAnswer, 0) + for j, a := range q.Answers { + answer := new(htmlAnswer) + 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) + } + questions = append(questions, *question) + } + + return questions +} diff --git a/packages/session/htmlStructs.go b/packages/session/htmlStructs.go index e77919f..3829230 100644 --- a/packages/session/htmlStructs.go +++ b/packages/session/htmlStructs.go @@ -5,11 +5,9 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/data" ) -type Session struct { - ID uuid.UUID - *data.Briefing - Participants []*data.Participant - Questions []data.Question +type tableHTMLData struct { + SessionID uuid.UUID + OTD []data.OverviewTableData } type briefingHTMLData struct { diff --git a/packages/session/mux.go b/packages/session/muxFuncs.go similarity index 54% rename from packages/session/mux.go rename to packages/session/muxFuncs.go index 0f272c5..bb616ac 100644 --- a/packages/session/mux.go +++ b/packages/session/muxFuncs.go @@ -8,26 +8,12 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/data" ) -type Mux struct { - *http.ServeMux -} - func NewMux() *Mux { mux := new(Mux) mux.ServeMux = http.NewServeMux() return mux } -func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) { - for participant := range cp { - mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleParticipant(db, participant, &s.Questions)) - for i := range s.Questions { - mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, participant, &s.Questions, int64(i+1))) - } - mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleRetry(participant, &s.Questions)) - } -} - func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) { for s := range cs { (*ss) = append(*ss, s) @@ -44,7 +30,7 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) log.Fatalln(err) } - mux.HandleFunc("/new-briefing/", s.HandleNewBriefing()) + 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)) diff --git a/packages/session/sessionStructs.go b/packages/session/sessionStructs.go new file mode 100644 index 0000000..e7a548c --- /dev/null +++ b/packages/session/sessionStructs.go @@ -0,0 +1,23 @@ +package session + +import ( + "github.com/google/uuid" + "net/http" + "streifling.com/jason/sicherheitsunterweisung/packages/data" +) + +type Mux struct { + *http.ServeMux +} + +type Session struct { + ID uuid.UUID + *data.Briefing + Participants []*data.Participant + Questions []data.Question +} + +type SessionParticipant struct { + *data.Participant + GivenAnswers []int +} diff --git a/templates/briefing.html b/templates/briefing.html index 87861bd..cf3c67d 100644 --- a/templates/briefing.html +++ b/templates/briefing.html @@ -1,21 +1,21 @@ -{{ define "add-buttons" }} +{{define "add-buttons"}}
- -
-{{ end }} +{{end}} -{{ define "new" }} -{{ template "add-buttons" . }} -

{{ .Login }}

-{{ end }} +{{define "new"}} +{{template "add-buttons" .}} +

{{.Login}}

+{{end}} -{{ define "content" }} +{{define "content"}}
@@ -32,6 +32,6 @@
- {{ template "add-buttons" . }} + {{template "add-buttons" .}}
-{{ end }} +{{end}} diff --git a/templates/index.html b/templates/index.html index c15d010..ecfab87 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,7 +12,7 @@

Sicherheitsunterweisung

- {{ template "content" . }} + {{template "content" .}}
diff --git a/templates/login.html b/templates/login.html index 4d27908..80f3db8 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,4 +1,4 @@ -{{ define "content" }} +{{define "content"}}

Anmeldung

@@ -14,4 +14,4 @@
-{{ end }} +{{end}} diff --git a/templates/participant.html b/templates/participant.html index 93f51df..b374209 100644 --- a/templates/participant.html +++ b/templates/participant.html @@ -1,16 +1,16 @@ -{{ define "content" }} +{{define "content"}}
- - + + - - + + - - + + -
-{{ end }} +{{end}} diff --git a/templates/table.html b/templates/table.html index a6acecc..cc68b67 100644 --- a/templates/table.html +++ b/templates/table.html @@ -1,29 +1,28 @@ -{{ define "rows" }} -{{ range . }} +{{define "rows"}} +{{range .OTD}} - {{ .InstructorFirstName }} - {{ .InstructorLastName }} - {{ .BriefingDate }} - {{ .BriefingTime }} - {{ .BriefingLocation }} - {{ .BriefingDocumentName }} - {{ .BriefingAsOf }} - {{ .ParticipantFirstName }} - {{ .ParticipantLastName }} - {{ .ParticipantCompany }} + {{.InstructorFirstName}} + {{.InstructorLastName}} + {{.BriefingDate}} + {{.BriefingTime}} + {{.BriefingLocation}} + {{.BriefingDocumentName}} + {{.BriefingAsOf}} + {{.ParticipantFirstName}} + {{.ParticipantLastName}} + {{.ParticipantCompany}} -{{ end }} -{{ end }} +{{end}} +{{end}} -{{ define "content" }} +{{define "content"}}
- +
-
@@ -43,7 +42,7 @@ - {{ template "rows" . }} + {{template "rows" .}} -{{ end }} +{{end}}