52 lines
988 B
Go
52 lines
988 B
Go
package sessionmanager
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.andreafazzi.eu/andrea/probo/pkg/models"
|
|
"git.andreafazzi.eu/andrea/probo/pkg/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}
|
|
}
|