models

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoMigrate

func AutoMigrate(db *gorm.DB) error

func ValidEmail

func ValidEmail(email string) bool

Types

type Question

type Question struct {
	gorm.Model
	Index          int // used for sorting in the final quiz
	SessionEmail   string
	Session        Session      `gorm:"foreignKey:SessionEmail"`
	Text           string       `yaml:"text,omitempty"`
	Difficulty     int          `yaml:"difficulty,omitempty"`
	Type           QuestionType `yaml:"type,omitempty"`
	RightAnswer    int          `yaml:"rightAnswer,omitempty"`
	UserAnswer     int          `yaml:"userAnswer,omitempty"`
	Answers        Answers      `yaml:"answers,omitempty" gorm:"type:VARCHAR(255)"`
	AllowedSeconds int          `yaml:"allowedSeconds,omitempty"`
	StartedAt      time.Time
}

func (Question) Expired

func (q Question) Expired() bool

func (Question) Valid

func (q Question) Valid() bool

type QuestionList

type QuestionList []Question

func (QuestionList) InDifficultyRange

func (ql QuestionList) InDifficultyRange(min, max int) QuestionList

func (QuestionList) Limit

func (ql QuestionList) Limit(limit int) QuestionList

Limit return exactly `limit` number of questions. If the total questions are more than the requests, it will remove the additional ones, with preference to the questions with difficulty that is already represented. For example If there are 5 total questions like this: - 2 with difficulty 1 - 1 with difficulty 2 - 2 with difficutly 3 when limit is "3" the method will return one questions from each difficulty (instead for example: 2 with difficulty 1 and 1 with difficulty 2). In other words, it tries to have questions from all difficulties if possible. Desired questions are picked starting from lower to higher levels but when a difficulty level runs out of questions, one from a higher level is picked instead.

func (QuestionList) OrderedByDifficulty

func (ql QuestionList) OrderedByDifficulty() QuestionList

func (QuestionList) Score

func (ql QuestionList) Score() float64

func (QuestionList) Suffled

func (ql QuestionList) Suffled() QuestionList

func (QuestionList) Valid

func (ql QuestionList) Valid() QuestionList

type QuestionPool

type QuestionPool struct {
	Questions QuestionList `yaml:"questions,omitempty"`
}

func NewQuestionPool

func NewQuestionPool(template string) (QuestionPool, error)

func NewQuestionPoolFromFile

func NewQuestionPoolFromFile(filePath string) (QuestionPool, error)

type Quiz

type Quiz struct {
	Questions QuestionList `yaml:"questions,omitempty"`
}

Quiz is the collection of questions from a QuestionPool based on QuizOptions. It's an intermidiate model used to prepare the Questions that will be stored in the database.

func NewQuizWithOpts

func NewQuizWithOpts(opts QuizOptions) (Quiz, error)

func (Quiz) PersistForSessionEmail

func (quiz Quiz) PersistForSessionEmail(db *gorm.DB, email string) error

type QuizOptions

type QuizOptions struct {
	TotalQuestions     int
	MinDifficulty      int
	MaxDifficulty      int
	QuestionTimeoutSec int
	AvailableQuestions QuestionList
}

type Session

type Session struct {
	gorm.Model
	Email     string
	Nickname  string
	Score     int
	Complete  bool
	Questions []Question `gorm:"foreignKey:SessionEmail;references:Email"`
}

func NewSession added in v0.0.2

func NewSession(db *gorm.DB, email, nickname string) (Session, error)

func SessionForEmail

func SessionForEmail(db *gorm.DB, email string) (Session, error)

func (Session) CurrentQuestion

func (s Session) CurrentQuestion() (Question, error)

CurrentQuestion returns the first non-expired question If the session has an expired question, this method returns an error (there is no "CurrentQuestion" since the whole Session is expired)

func (Session) EmailObfuscated added in v0.0.2

func (s Session) EmailObfuscated() string

EmailObfuscated obfuscates an email address by replacing characters with dots, except for the first and last characters of the username and domain parts.

func (Session) HasExpiredQuestions

func (s Session) HasExpiredQuestions() bool

HasExpiredQuestions returns `true` if there is at least one expired Question

func (*Session) UpdateCacheColumns added in v0.0.2

func (s *Session) UpdateCacheColumns()

UpdateCacheColumns calculates the current "Score" value based only on answered and expired questions. Expired questions with no answer are considered "wrong". It also calculated the value of the "Completed" column. A session is complete when all questions are answered or expired.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL