probo/pkg/models/exam.go

51 lines
1 KiB
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
2024-04-22 14:24:29 +02:00
Participant *Participant `json:"participant"`
Quizzes []*Quiz `json:"quizzes"`
2023-11-28 16:19:49 +01:00
}
func (e *Exam) String() string {
2024-05-12 10:15:48 +02:00
return fmt.Sprintf("%v's exam with %v quizzes.", e.Participant, len(e.Quizzes))
2023-11-28 16:19:49 +01:00
}
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
}
2024-04-22 14:24:29 +02:00
func (e *Exam) ToMarkdown() (string, error) {
result := ""
for _, quiz := range e.Quizzes {
quizMD, err := QuizToMarkdown(quiz)
if err != nil {
return "", err
}
result += fmt.Sprintf(quizMD)
result += "\n"
}
return strings.TrimRight(fmt.Sprintf("# %s %s \n %s", e.Participant.Lastname, e.Participant.Firstname, result), "\n"), nil
}