Bilder als Antworten hinzugefügt

This commit is contained in:
2023-11-02 17:52:04 +01:00
parent 2b9d9d7656
commit 404e079343
8 changed files with 49 additions and 15 deletions

View File

@ -15,8 +15,10 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
@ -71,7 +73,9 @@ func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion
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
@ -89,3 +93,29 @@ func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []resultQuestion
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

@ -33,9 +33,11 @@ type questionHTMLData struct {
}
type resultAnswer struct {
ID int64
Text string
Correct bool
Chosen bool
IsImage bool // TODO: relocate to sessionStructs if possible
}
type resultQuestion struct {

View File

@ -13,7 +13,6 @@ package session
import (
"fmt"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
@ -42,17 +41,7 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session)
for s := range cs {
(*ss) = append((*ss), s)
participantChan := make(chan *BriefingParticipant)
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)
}
s.getQuestions(db)
mux.HandleFunc("/search/"+fmt.Sprint(s.ID)+"/", s.HandleSearch(db))
mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing())