probo/sessionmanager/score.go
2023-12-17 18:56:20 +01:00

52 lines
980 B
Go

package sessionmanager
import (
"fmt"
"log"
"git.andreafazzi.eu/andrea/probo/models"
"git.andreafazzi.eu/andrea/probo/store/file"
)
type Score struct {
Exam *models.Exam
Score float32
}
type Scores []*Score
func NewScores(responseFileStore *file.ResponseFileStore, session *models.Session) (Scores, error) {
var scores Scores
for _, exam := range session.Exams {
response, err := responseFileStore.Read(exam.ID)
if err != nil {
log.Println(err)
continue
}
scores = append(scores, CalcScore(response, exam))
}
return scores, nil
}
func (ss Scores) String() string {
var result string
for _, s := range ss {
result += fmt.Sprintf("%v\t%f\n", s.Exam.Participant, s.Score)
}
return result
}
func CalcScore(response *models.Response, exam *models.Exam) *Score {
var score float32
for _, q := range exam.Quizzes {
answerId := response.Questions[q.Question.ID]
if answerId == q.Correct.ID {
score++
}
}
return &Score{exam, score}
}