122 lines
3.2 KiB
Go

/*
* 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 makeHTMLQuestions(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
}
}
}
}