exam.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package models
  2. import (
  3. "crypto/sha256"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. )
  8. type Exam struct {
  9. Meta
  10. Participant *Participant `json:"participant"`
  11. Quizzes []*Quiz `json:"quizzes"`
  12. }
  13. func (e *Exam) String() string {
  14. return fmt.Sprintf("Exam ID %v with %v quizzes.", e.ID, len(e.Quizzes))
  15. }
  16. func (e *Exam) GetHash() string {
  17. qHashes := ""
  18. for _, q := range e.Quizzes {
  19. qHashes += q.GetHash()
  20. }
  21. return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join([]string{e.Participant.GetHash(), qHashes}, ""))))
  22. }
  23. func (e *Exam) Marshal() ([]byte, error) {
  24. return json.Marshal(e)
  25. }
  26. func (e *Exam) Unmarshal(data []byte) error {
  27. return json.Unmarshal(data, e)
  28. }
  29. func (e *Exam) ToMarkdown() (string, error) {
  30. result := ""
  31. for _, quiz := range e.Quizzes {
  32. quizMD, err := QuizToMarkdown(quiz)
  33. if err != nil {
  34. return "", err
  35. }
  36. result += fmt.Sprintf(quizMD)
  37. result += "\n"
  38. }
  39. return strings.TrimRight(fmt.Sprintf("# %s %s \n %s", e.Participant.Lastname, e.Participant.Firstname, result), "\n"), nil
  40. }