exam.go 658 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package models
  2. import (
  3. "crypto/sha256"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. )
  8. type Exam struct {
  9. Meta
  10. SessionID string
  11. Participant *Participant
  12. Quizzes []*Quiz
  13. }
  14. func (e *Exam) String() string {
  15. return fmt.Sprintf("Exam ID %v with %v quizzes.", e.ID, len(e.Quizzes))
  16. }
  17. func (e *Exam) GetHash() string {
  18. qHashes := ""
  19. for _, q := range e.Quizzes {
  20. qHashes += q.GetHash()
  21. }
  22. return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join([]string{e.Participant.GetHash(), qHashes}, ""))))
  23. }
  24. func (e *Exam) Marshal() ([]byte, error) {
  25. return json.Marshal(e)
  26. }
  27. func (e *Exam) Unmarshal(data []byte) error {
  28. return json.Unmarshal(data, e)
  29. }