probo/pkg/models/exam.go

38 lines
658 B
Go
Raw Normal View History

2023-10-18 13:40:21 +02:00
package models
2023-10-28 20:50:06 +02:00
import (
2023-12-17 18:56:20 +01:00
"crypto/sha256"
2023-11-28 16:19:49 +01:00
"encoding/json"
"fmt"
2023-12-17 18:56:20 +01:00
"strings"
2023-10-28 20:50:06 +02:00
)
2023-10-18 13:40:21 +02:00
type Exam struct {
2023-11-28 16:19:49 +01:00
Meta
2023-12-17 18:56:20 +01:00
SessionID string
2023-11-28 16:19:49 +01:00
Participant *Participant
Quizzes []*Quiz
}
func (e *Exam) String() string {
return fmt.Sprintf("Exam ID %v with %v quizzes.", e.ID, len(e.Quizzes))
}
func (e *Exam) GetHash() string {
2023-12-17 18:56:20 +01:00
qHashes := ""
for _, q := range e.Quizzes {
qHashes += q.GetHash()
}
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join([]string{e.Participant.GetHash(), qHashes}, ""))))
2023-11-28 16:19:49 +01:00
}
2023-10-18 13:40:21 +02:00
2023-11-28 16:19:49 +01:00
func (e *Exam) Marshal() ([]byte, error) {
return json.Marshal(e)
}
2023-10-18 13:40:21 +02:00
2023-11-28 16:19:49 +01:00
func (e *Exam) Unmarshal(data []byte) error {
return json.Unmarshal(data, e)
2023-10-18 13:40:21 +02:00
}