probo/models/quiz.go

212 lines
4.2 KiB
Go
Raw Normal View History

2022-06-23 11:25:35 +02:00
package models
2023-11-13 21:01:12 +01:00
import (
"crypto/sha256"
"errors"
"fmt"
"io"
"sort"
"strings"
"gopkg.in/yaml.v2"
)
2022-06-23 11:25:35 +02:00
type Quiz struct {
2023-09-22 10:29:10 +02:00
Meta
2023-10-28 20:50:06 +02:00
2023-11-13 21:01:12 +01:00
Hash string `json:"hash"`
Question *Question `json:"question" gorm:"foreignKey:ID"`
Answers []*Answer `json:"answers" gorm:"many2many:quiz_answers"`
Tags []*Tag `json:"tags" yaml:"-" gorm:"-"`
Correct *Answer `json:"correct" gorm:"foreignKey:ID"`
CorrectPos uint `gorm:"-"` // Position of the correct answer during quiz creation
Type int `json:"type"`
}
2023-11-20 14:14:09 +01:00
func MarkdownToQuiz(quiz *Quiz, markdown string) error {
2023-11-13 21:01:12 +01:00
meta, remainingMarkdown, err := ParseMetaHeaderFromMarkdown(markdown)
if err != nil {
2023-11-20 14:14:09 +01:00
return err
2023-11-13 21:01:12 +01:00
}
lines := strings.Split(remainingMarkdown, "\n")
questionText := ""
answers := []*Answer{}
for _, line := range lines {
if strings.HasPrefix(line, "*") {
answerText := strings.TrimPrefix(line, "* ")
answer := &Answer{Text: answerText}
answers = append(answers, answer)
} else {
if questionText != "" {
questionText += "\n"
}
questionText += line
}
}
questionText = strings.TrimRight(questionText, "\n")
if questionText == "" {
2023-11-20 14:14:09 +01:00
return fmt.Errorf("Question text should not be empty.")
2023-11-13 21:01:12 +01:00
}
if len(answers) < 2 {
2023-11-20 14:14:09 +01:00
return fmt.Errorf("Number of answers should be at least 2 but parsed answers are %d.", len(answers))
2023-11-13 21:01:12 +01:00
}
question := &Question{Text: questionText}
2023-11-20 14:14:09 +01:00
quiz.Question = question
quiz.Answers = answers
//quiz = &Quiz{Question: question, Answers: answers, CorrectPos: 0}
2023-11-13 21:01:12 +01:00
if meta != nil {
quiz.Meta = *meta
}
2023-11-20 14:14:09 +01:00
return nil
2023-11-13 21:01:12 +01:00
}
func QuizToMarkdown(quiz *Quiz) (string, error) {
if quiz.Question == nil {
return "", errors.New("Quiz should contain a question but it wasn't provided.")
}
if len(quiz.Answers) < 2 {
return "", errors.New("Quiz should contain at least 2 answers but none was provided.")
}
quiz.Correct = quiz.Answers[quiz.CorrectPos]
if quiz.Correct == nil {
return "", errors.New("Quiz should contain a correct answer but not was provided.")
}
correctAnswer := "* " + quiz.Correct.Text
var otherAnswers string
for pos, answer := range quiz.Answers {
if quiz.CorrectPos != uint(pos) {
otherAnswers += "* " + answer.Text + "\n"
}
}
markdown := quiz.Question.Text + "\n\n" + correctAnswer + "\n" + otherAnswers
return markdown, nil
}
func (q *Quiz) GetID() string {
return q.ID
}
func (q *Quiz) SetID(id string) {
q.ID = id
}
func (q *Quiz) GetHash() string {
return q.calculateHash()
}
2023-11-20 14:14:09 +01:00
func (q *Quiz) Marshal() ([]byte, error) {
result, err := QuizToMarkdown(q)
return []byte(result), err
}
func (q *Quiz) Unmarshal(data []byte) error {
return MarkdownToQuiz(q, string(data))
}
2023-11-13 21:01:12 +01:00
func (q *Quiz) calculateHash() string {
result := make([]string, 0)
result = append(result, q.Question.GetHash())
for _, a := range q.Answers {
result = append(result, a.GetHash())
}
orderedHashes := make([]string, len(result))
copy(orderedHashes, result)
sort.Strings(orderedHashes)
return fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(orderedHashes, ""))))
}
func ParseMetaHeaderFromMarkdown(markdown string) (*Meta, string, error) {
reader := strings.NewReader(markdown)
var sb strings.Builder
var line string
var err error
for {
line, err = readLine(reader)
if err != nil {
if err == io.EOF {
break
}
return nil, "", err
}
if strings.TrimSpace(line) == "---" {
break
}
}
for {
line, err = readLine(reader)
if err != nil {
if err == io.EOF {
break
}
return nil, "", err
}
if strings.TrimSpace(line) == "---" {
break
}
sb.WriteString(line)
}
if sb.String() == "" {
return nil, markdown, nil
}
var meta Meta
err = yaml.Unmarshal([]byte(sb.String()), &meta)
if err != nil {
return nil, markdown, err
}
remainingMarkdown := markdown[strings.Index(markdown, "---\n"+sb.String()+"---\n")+len("---\n"+sb.String()+"---\n"):]
return &meta, remainingMarkdown, nil
}
func readLine(reader *strings.Reader) (string, error) {
var sb strings.Builder
for {
r, _, err := reader.ReadRune()
if err != nil {
if err == io.EOF {
return sb.String(), io.EOF
}
return "", err
}
sb.WriteRune(r)
if r == '\n' {
break
}
}
return sb.String(), nil
2022-06-23 11:25:35 +02:00
}