Bilder als Antworten hinzugefügt

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

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
tmp
test.sql
static/test.jpg

View File

@ -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

View File

@ -42,6 +42,7 @@ type Briefing struct {
type Answer struct {
ID int64
Text string
IsImage bool // TODO: relocate to sessionStructs if possible
}
type Question struct {

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())

View File

@ -13,7 +13,11 @@
{{range .Question.Answers}}
<div>
<input type="radio" name="answer" id="answer-{{.ID}}" value="{{.ID}}" />
{{if .IsImage}}
<img src="{{.Text}}" alt="{{.Text}}" />
{{else}}
<label for="answer-{{.ID}}">{{.Text}}</label>
{{end}}
</div>
{{end}}
{{end}}

View File

@ -11,11 +11,18 @@
{{define "answers"}}
{{range .Answers}}
{{if .IsImage}}
<span class="{{if and .Chosen .Correct}} correct {{else if and .Chosen (not .Correct)}} incorrect {{end}}">
{{.ID}}.
</span>
<img src="{{.Text}}" alt="{{.Text}}" />
{{else}}
<p class="{{if and .Chosen .Correct}} correct {{else if and .Chosen (not .Correct)}} incorrect {{end}}">
{{.Text}}
</p>
{{end}}
{{end}}
{{end}}
{{define "content"}}
<p>{{.BriefingParticipant.NoIncorrect}} Fehler</p>