probo/pkg/models/exam.go

50 lines
1 KiB
Go

package models
import (
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
)
type Exam struct {
Meta
Participant *Participant `json:"participant"`
Quizzes []*Quiz `json:"quizzes"`
}
func (e *Exam) String() string {
return fmt.Sprintf("Exam ID %v with %v quizzes.", e.ID, len(e.Quizzes))
}
func (e *Exam) GetHash() string {
qHashes := ""
for _, q := range e.Quizzes {
qHashes += q.GetHash()
}
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join([]string{e.Participant.GetHash(), qHashes}, ""))))
}
func (e *Exam) Marshal() ([]byte, error) {
return json.Marshal(e)
}
func (e *Exam) Unmarshal(data []byte) error {
return json.Unmarshal(data, e)
}
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
}