diff --git a/.gitignore b/.gitignore index 4de3d5d..9d6f7ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ tmp test.sql +static/test.jpg diff --git a/create_tables.sql b/create_tables.sql index fc6dd8b..2e8c2aa 100644 --- a/create_tables.sql +++ b/create_tables.sql @@ -97,7 +97,7 @@ INSERT INTO questions ( ( 'Was ist 1+1?', '1', '2', '3', '4', '2' ), ( 'Was ist 1+2?', '1', '2', '3', '4', '3' ), ( 'Was ist 2+2?', '1', '2', '3', '4', '4' ), - ( 'Was ist 0+1?', '1', '2', '3', '4', '1' ); + ( 'Was ist 0+1?', '1', '2', '3', 'file://static/test.jpg', '1' ); -- --INSERT INTO given_answers ( -- briefing_id, participant_id, question_id, given_answer diff --git a/packages/data/dataStructs.go b/packages/data/dataStructs.go index 93d2299..5eb8768 100644 --- a/packages/data/dataStructs.go +++ b/packages/data/dataStructs.go @@ -40,8 +40,9 @@ type Briefing struct { } type Answer struct { - ID int64 - Text string + ID int64 + Text string + IsImage bool // TODO: relocate to sessionStructs if possible } type Question struct { diff --git a/packages/session/helperFuncs.go b/packages/session/helperFuncs.go index aeb6a68..efa6e2d 100644 --- a/packages/session/helperFuncs.go +++ b/packages/session/helperFuncs.go @@ -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 + } + } + } +} diff --git a/packages/session/htmlStructs.go b/packages/session/htmlStructs.go index d480a37..69ad8ec 100644 --- a/packages/session/htmlStructs.go +++ b/packages/session/htmlStructs.go @@ -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 { diff --git a/packages/session/muxFuncs.go b/packages/session/muxFuncs.go index d85f379..86ed91a 100644 --- a/packages/session/muxFuncs.go +++ b/packages/session/muxFuncs.go @@ -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()) diff --git a/templates/question.html b/templates/question.html index cc6eb68..1af1ef6 100644 --- a/templates/question.html +++ b/templates/question.html @@ -13,7 +13,11 @@ {{range .Question.Answers}}
+ {{if .IsImage}} + {{.Text}} + {{else}} + {{end}}
{{end}} {{end}} diff --git a/templates/result.html b/templates/result.html index 9f25a89..a2d5816 100644 --- a/templates/result.html +++ b/templates/result.html @@ -11,11 +11,18 @@ {{define "answers"}} {{range .Answers}} +{{if .IsImage}} + + {{.ID}}. + +{{.Text}} +{{else}}

{{.Text}}

{{end}} {{end}} +{{end}} {{define "content"}}

{{.BriefingParticipant.NoIncorrect}} Fehler