feat: enforce attempt limits, time limits and one-way tests
- Service.Start resumes an open test attempt, closes expired ones at their deadline and refuses a start past max_attempts under an advisory lock; StartAttempt stays as a wrapper - answers to a test are accepted until the deadline plus golem15.quizzes.test.grace_seconds; one_way answers are final - a test covers the whole quiz and keeps a stable per-attempt order - 409 bodies carry a code; list and attempt rows carry attempts_left, question_types and seconds_left; import accepts the test keys
This commit is contained in:
@@ -19,8 +19,13 @@ var (
|
||||
ErrNotFound = errors.New("quizzes: not found")
|
||||
// ErrCompleted is a write to a finished attempt.
|
||||
ErrCompleted = errors.New("quizzes: attempt already completed")
|
||||
// ErrTimeLimit is an answer after the quiz's time limit ran out.
|
||||
// ErrTimeLimit is an answer after the quiz's time limit ran out (plus
|
||||
// the grace of a test).
|
||||
ErrTimeLimit = errors.New("quizzes: time limit exceeded")
|
||||
// ErrAttemptLimit is a test start after the user used every attempt.
|
||||
ErrAttemptLimit = errors.New("quizzes: attempt limit reached")
|
||||
// ErrAnswerLocked is a changed answer in a one_way test.
|
||||
ErrAnswerLocked = errors.New("quizzes: answer locked")
|
||||
)
|
||||
|
||||
// FieldError is an input error on one request field (a 422).
|
||||
@@ -40,6 +45,9 @@ type Service struct {
|
||||
// Shuffle reorders questions of a quiz with shuffle_questions; it
|
||||
// defaults to math/rand.
|
||||
Shuffle func(n int, swap func(i, j int))
|
||||
// Grace is how long after a test's deadline an answer is still
|
||||
// accepted; it never applies to learning quizzes. See TestGraceFrom.
|
||||
Grace time.Duration
|
||||
}
|
||||
|
||||
func (s *Service) now() time.Time {
|
||||
@@ -57,11 +65,17 @@ type QuizSummary struct {
|
||||
QuestionCount int
|
||||
BestScorePercent *float64
|
||||
AttemptsCount int
|
||||
// QuestionTypes counts the questions per type; every models.QuestionTypes
|
||||
// key is present.
|
||||
QuestionTypes map[string]int
|
||||
// AttemptsUsed counts every attempt of the user, open or completed.
|
||||
AttemptsUsed int
|
||||
}
|
||||
|
||||
// ListQuizzes returns the published quizzes, optionally only those whose
|
||||
// context equals quizContext, ordered by title. AttemptsCount counts the
|
||||
// user's completed attempts and BestScorePercent is their best result.
|
||||
// user's completed attempts, AttemptsUsed every attempt, and
|
||||
// BestScorePercent is their best result.
|
||||
func (s *Service) ListQuizzes(ctx context.Context, userID uint, quizContext string) ([]QuizSummary, error) {
|
||||
q := s.db(ctx).Where("is_published = ?", true)
|
||||
if quizContext != "" {
|
||||
@@ -81,27 +95,34 @@ func (s *Service) ListQuizzes(ctx context.Context, userID uint, quizContext stri
|
||||
}
|
||||
type countRow struct {
|
||||
QuizID uint
|
||||
Type string
|
||||
N int
|
||||
}
|
||||
var counts []countRow
|
||||
if err := s.db(ctx).Model(&models.Question{}).Select("quiz_id, COUNT(*) AS n").
|
||||
Where("quiz_id IN ?", ids).Group("quiz_id").Scan(&counts).Error; err != nil {
|
||||
if err := s.db(ctx).Model(&models.Question{}).Select("quiz_id, type, COUNT(*) AS n").
|
||||
Where("quiz_id IN ?", ids).Group("quiz_id, type").Scan(&counts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
type statRow struct {
|
||||
QuizID uint
|
||||
N int
|
||||
Used int
|
||||
Best *float64
|
||||
}
|
||||
var stats []statRow
|
||||
if err := s.db(ctx).Model(&models.Attempt{}).Select("quiz_id, COUNT(*) AS n, MAX(score_percent) AS best").
|
||||
Where("quiz_id IN ? AND user_id = ? AND completed_at IS NOT NULL", ids, userID).
|
||||
if err := s.db(ctx).Model(&models.Attempt{}).
|
||||
Select("quiz_id, COUNT(*) FILTER (WHERE completed_at IS NOT NULL) AS n, COUNT(*) AS used, "+
|
||||
"MAX(score_percent) FILTER (WHERE completed_at IS NOT NULL) AS best").
|
||||
Where("quiz_id IN ? AND user_id = ?", ids, userID).
|
||||
Group("quiz_id").Scan(&stats).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
countBy := map[uint]int{}
|
||||
typesBy := map[uint]map[string]int{}
|
||||
for _, c := range counts {
|
||||
countBy[c.QuizID] = c.N
|
||||
if typesBy[c.QuizID] == nil {
|
||||
typesBy[c.QuizID] = map[string]int{}
|
||||
}
|
||||
typesBy[c.QuizID][c.Type] += c.N
|
||||
}
|
||||
statBy := map[uint]statRow{}
|
||||
for _, st := range stats {
|
||||
@@ -109,7 +130,19 @@ func (s *Service) ListQuizzes(ctx context.Context, userID uint, quizContext stri
|
||||
}
|
||||
for _, quiz := range quizzes {
|
||||
st := statBy[quiz.ID]
|
||||
out = append(out, QuizSummary{Quiz: quiz, QuestionCount: countBy[quiz.ID], BestScorePercent: st.Best, AttemptsCount: st.N})
|
||||
types := make(map[string]int, len(models.QuestionTypes))
|
||||
for _, typ := range models.QuestionTypes {
|
||||
types[typ] = 0
|
||||
}
|
||||
total := 0
|
||||
for typ, n := range typesBy[quiz.ID] {
|
||||
types[typ] += n
|
||||
total += n
|
||||
}
|
||||
out = append(out, QuizSummary{
|
||||
Quiz: quiz, QuestionCount: total, BestScorePercent: st.Best, AttemptsCount: st.N,
|
||||
QuestionTypes: types, AttemptsUsed: st.Used,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -123,13 +156,16 @@ type QuizDetail struct {
|
||||
|
||||
// GetQuiz loads a published quiz by slug with its questions in sort order
|
||||
// (shuffled when the quiz says so) and each question's answers. A non-empty
|
||||
// category keeps only that category's questions. Categories lists every
|
||||
// category of the quiz.
|
||||
// category keeps only that category's questions; a test ignores it, as it
|
||||
// always covers the whole quiz. Categories lists every category of the quiz.
|
||||
func (s *Service) GetQuiz(ctx context.Context, slug, category string) (*QuizDetail, error) {
|
||||
quiz, err := s.publishedQuiz(ctx, slug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if IsTest(quiz) {
|
||||
category = ""
|
||||
}
|
||||
questions, err := s.questions(ctx, quiz.ID, category)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -179,38 +215,147 @@ func (s *Service) categories(ctx context.Context, quizID uint) ([]string, error)
|
||||
return cats, err
|
||||
}
|
||||
|
||||
// StartAttempt starts an attempt at a published quiz for the user. A
|
||||
// non-empty category limits the attempt to that category's questions; a
|
||||
// category without questions is a FieldError on category.
|
||||
// Started is the attempt POST /quizzes/{slug}/attempts answers with.
|
||||
type Started struct {
|
||||
Attempt *models.Attempt
|
||||
Quiz *models.Quiz
|
||||
// Resumed is true when an open test attempt was returned instead of a
|
||||
// new one.
|
||||
Resumed bool
|
||||
// AttemptsUsed counts the user's attempts on the quiz, this one
|
||||
// included; it is only counted for tests.
|
||||
AttemptsUsed int
|
||||
// AnsweredCount is the number of questions a resumed attempt answered.
|
||||
AnsweredCount int
|
||||
}
|
||||
|
||||
// StartAttempt starts an attempt at a published quiz for the user, like
|
||||
// Start without the resume and limit details.
|
||||
func (s *Service) StartAttempt(ctx context.Context, userID uint, slug, category string) (*models.Attempt, *models.Quiz, error) {
|
||||
quiz, err := s.publishedQuiz(ctx, slug)
|
||||
st, err := s.Start(ctx, userID, slug, category)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return st.Attempt, st.Quiz, nil
|
||||
}
|
||||
|
||||
// Start starts an attempt at a published quiz for the user.
|
||||
//
|
||||
// For a learning quiz a non-empty category limits the attempt to that
|
||||
// category's questions; a category without questions is a FieldError on
|
||||
// category. Every call creates a new attempt.
|
||||
//
|
||||
// A test always covers the whole quiz, so a category is a FieldError
|
||||
// (category_not_allowed). Under an advisory lock per (quiz, user) it closes
|
||||
// the user's open attempts past their deadline and grace at the deadline,
|
||||
// resumes a remaining open attempt (Resumed) and otherwise refuses with
|
||||
// ErrAttemptLimit once the user's attempts, open or completed and in any
|
||||
// mode, reach max_attempts.
|
||||
func (s *Service) Start(ctx context.Context, userID uint, slug, category string) (*Started, error) {
|
||||
quiz, err := s.publishedQuiz(ctx, slug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if IsTest(quiz) {
|
||||
if category != "" {
|
||||
return nil, &FieldError{Field: "category", Key: "category_not_allowed"}
|
||||
}
|
||||
return s.startTest(ctx, userID, quiz)
|
||||
}
|
||||
attempt := &models.Attempt{QuizID: quiz.ID, UserID: userID, StartedAt: s.now().UTC()}
|
||||
if category != "" {
|
||||
var n int64
|
||||
if err := s.db(ctx).Model(&models.Question{}).Where("quiz_id = ? AND category = ?", quiz.ID, category).Count(&n).Error; err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
if n == 0 {
|
||||
return nil, nil, &FieldError{Field: "category", Key: "unknown_category"}
|
||||
return nil, &FieldError{Field: "category", Key: "unknown_category"}
|
||||
}
|
||||
attempt.Category = &category
|
||||
}
|
||||
maxPoints, err := s.maxPoints(ctx, attempt)
|
||||
maxPoints, err := s.maxPoints(ctx, s.DB, attempt)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
attempt.MaxPoints = maxPoints
|
||||
if err := s.db(ctx).Create(attempt).Error; err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
return attempt, quiz, nil
|
||||
return &Started{Attempt: attempt, Quiz: quiz}, nil
|
||||
}
|
||||
|
||||
func (s *Service) maxPoints(ctx context.Context, a *models.Attempt) (int, error) {
|
||||
q := s.db(ctx).Model(&models.Question{}).Where("quiz_id = ?", a.QuizID)
|
||||
func (s *Service) startTest(ctx context.Context, userID uint, quiz *models.Quiz) (*Started, error) {
|
||||
var st *Started
|
||||
err := lagoon.Transaction(ctx, s.DB, func(ctx context.Context, tx *gorm.DB) error {
|
||||
tx = tx.WithContext(ctx)
|
||||
if err := tx.Exec(`SELECT pg_advisory_xact_lock(hashtext(?))`, AttemptLockKey(quiz.ID, userID)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var open []models.Attempt
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE", Table: clause.Table{Name: clause.CurrentTable}}).
|
||||
Where("quiz_id = ? AND user_id = ? AND completed_at IS NULL", quiz.ID, userID).
|
||||
Order("started_at ASC, id ASC").Find(&open).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
now := s.now()
|
||||
var resume *models.Attempt
|
||||
for i := range open {
|
||||
a := &open[i]
|
||||
a.Quiz = quiz
|
||||
if Expired(a, now, s.Grace) {
|
||||
if err := s.close(ctx, tx, a, *Deadline(a)); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
// An open attempt left from the quiz's learning days may cover
|
||||
// one category; a test only resumes a whole-quiz attempt.
|
||||
if resume == nil && a.Category == nil {
|
||||
resume = a
|
||||
}
|
||||
}
|
||||
var used int64
|
||||
if err := tx.Model(&models.Attempt{}).Where("quiz_id = ? AND user_id = ?", quiz.ID, userID).Count(&used).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if resume != nil {
|
||||
var answered int64
|
||||
if err := tx.Model(&models.AttemptAnswer{}).Where("attempt_id = ?", resume.ID).Count(&answered).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
st = &Started{Attempt: resume, Quiz: quiz, Resumed: true, AttemptsUsed: int(used), AnsweredCount: int(answered)}
|
||||
return nil
|
||||
}
|
||||
if left := AttemptsLeft(quiz, int(used)); left != nil && *left == 0 {
|
||||
return ErrAttemptLimit
|
||||
}
|
||||
attempt := &models.Attempt{QuizID: quiz.ID, UserID: userID, StartedAt: now.UTC()}
|
||||
maxPoints, err := s.maxPoints(ctx, tx, attempt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attempt.MaxPoints = maxPoints
|
||||
if err := tx.Create(attempt).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
st = &Started{Attempt: attempt, Quiz: quiz, AttemptsUsed: int(used) + 1}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
|
||||
// AttemptsUsed counts the user's attempts on the quiz, open or completed.
|
||||
func (s *Service) AttemptsUsed(ctx context.Context, quizID, userID uint) (int, error) {
|
||||
var n int64
|
||||
err := s.db(ctx).Model(&models.Attempt{}).Where("quiz_id = ? AND user_id = ?", quizID, userID).Count(&n).Error
|
||||
return int(n), err
|
||||
}
|
||||
|
||||
func (s *Service) maxPoints(ctx context.Context, db *gorm.DB, a *models.Attempt) (int, error) {
|
||||
q := db.WithContext(ctx).Model(&models.Question{}).Where("quiz_id = ?", a.QuizID)
|
||||
if a.Category != nil {
|
||||
q = q.Where("category = ?", *a.Category)
|
||||
}
|
||||
@@ -267,9 +412,12 @@ type Feedback struct {
|
||||
// Answer records or overwrites the user's answer to one question of an
|
||||
// open attempt and returns the feedback. It checks, in order: the attempt
|
||||
// belongs to the user (ErrNotFound), is not finished (ErrCompleted), is
|
||||
// within the time limit (ErrTimeLimit), the question belongs to the attempt
|
||||
// (FieldError on question_id), and the answer fits the question type
|
||||
// (FieldError on answer_ids or known).
|
||||
// within the time limit, plus the grace of a test (ErrTimeLimit), the
|
||||
// question belongs to the attempt (FieldError on question_id), a one_way
|
||||
// test has no answer to the question yet (ErrAnswerLocked; the attempt row
|
||||
// is locked, so the check is race-free), and the answer fits the question
|
||||
// type (FieldError on answer_ids or known). The feedback of an open test
|
||||
// attempt is Withheld.
|
||||
func (s *Service) Answer(ctx context.Context, userID, attemptID uint, in AnswerInput) (*Feedback, error) {
|
||||
var fb *Feedback
|
||||
err := lagoon.Transaction(ctx, s.DB, func(ctx context.Context, tx *gorm.DB) error {
|
||||
@@ -280,7 +428,7 @@ func (s *Service) Answer(ctx context.Context, userID, attemptID uint, in AnswerI
|
||||
if attempt.CompletedAt != nil {
|
||||
return ErrCompleted
|
||||
}
|
||||
if d := Deadline(attempt); d != nil && s.now().After(*d) {
|
||||
if d := AnswerDeadline(attempt, s.Grace); d != nil && s.now().After(*d) {
|
||||
return ErrTimeLimit
|
||||
}
|
||||
var question models.Question
|
||||
@@ -296,6 +444,16 @@ func (s *Service) Answer(ctx context.Context, userID, attemptID uint, in AnswerI
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if OneWay(attempt.Quiz) {
|
||||
var given int64
|
||||
if err := tx.WithContext(ctx).Model(&models.AttemptAnswer{}).
|
||||
Where("attempt_id = ? AND question_id = ?", attempt.ID, question.ID).Count(&given).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if given > 0 {
|
||||
return ErrAnswerLocked
|
||||
}
|
||||
}
|
||||
|
||||
row := models.AttemptAnswer{AttemptID: attempt.ID, QuestionID: question.ID}
|
||||
switch question.Type {
|
||||
@@ -372,24 +530,7 @@ func (s *Service) Finish(ctx context.Context, userID, attemptID uint) (*AttemptR
|
||||
if attempt.CompletedAt != nil {
|
||||
return ErrCompleted
|
||||
}
|
||||
var points []float64
|
||||
if err := tx.WithContext(ctx).Model(&models.AttemptAnswer{}).Where("attempt_id = ?", attempt.ID).
|
||||
Pluck("points", &points).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
maxPoints, err := s.maxPoints(ctx, attempt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
score, percent := Totals(points, maxPoints)
|
||||
now := s.now().UTC()
|
||||
return tx.WithContext(ctx).Model(&models.Attempt{}).Where("id = ?", attempt.ID).Updates(map[string]any{
|
||||
"completed_at": now,
|
||||
"score_points": score,
|
||||
"max_points": maxPoints,
|
||||
"score_percent": percent,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
return s.close(ctx, tx, attempt, s.now())
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -397,6 +538,31 @@ func (s *Service) Finish(ctx context.Context, userID, attemptID uint) (*AttemptR
|
||||
return s.Result(ctx, userID, attemptID)
|
||||
}
|
||||
|
||||
// close completes an attempt at the given time: it sums the recorded
|
||||
// answers, floors the total at 0 and stores the score. Finish closes at the
|
||||
// current time, an expired test closes at its deadline.
|
||||
func (s *Service) close(ctx context.Context, tx *gorm.DB, attempt *models.Attempt, at time.Time) error {
|
||||
tx = tx.WithContext(ctx)
|
||||
var points []float64
|
||||
if err := tx.Model(&models.AttemptAnswer{}).Where("attempt_id = ?", attempt.ID).Pluck("points", &points).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
maxPoints, err := s.maxPoints(ctx, tx, attempt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
score, percent := Totals(points, maxPoints)
|
||||
at = at.UTC()
|
||||
now := s.now().UTC()
|
||||
return tx.Model(&models.Attempt{}).Where("id = ?", attempt.ID).Updates(map[string]any{
|
||||
"completed_at": at,
|
||||
"score_points": score,
|
||||
"max_points": maxPoints,
|
||||
"score_percent": percent,
|
||||
"updated_at": now,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// AttemptSummary is an attempt with its quiz and answered question count.
|
||||
type AttemptSummary struct {
|
||||
Attempt models.Attempt
|
||||
@@ -417,12 +583,33 @@ type AttemptResult struct {
|
||||
}
|
||||
|
||||
// Result loads the user's attempt with every question it covers and the
|
||||
// answers given.
|
||||
// answers given. A test attempt past its deadline and grace is closed at
|
||||
// its deadline first. The questions of a shuffled test come in an order
|
||||
// seeded by the attempt, the same on every call; other attempts list them
|
||||
// in sort order.
|
||||
func (s *Service) Result(ctx context.Context, userID, attemptID uint) (*AttemptResult, error) {
|
||||
attempt, err := s.ownedAttempt(ctx, s.DB, userID, attemptID, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if Expired(attempt, s.now(), s.Grace) {
|
||||
err := lagoon.Transaction(ctx, s.DB, func(ctx context.Context, tx *gorm.DB) error {
|
||||
locked, err := s.ownedAttempt(ctx, tx, userID, attemptID, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !Expired(locked, s.now(), s.Grace) {
|
||||
return nil
|
||||
}
|
||||
return s.close(ctx, tx, locked, *Deadline(locked))
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if attempt, err = s.ownedAttempt(ctx, s.DB, userID, attemptID, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
category := ""
|
||||
if attempt.Category != nil {
|
||||
category = *attempt.Category
|
||||
@@ -431,6 +618,10 @@ func (s *Service) Result(ctx context.Context, userID, attemptID uint) (*AttemptR
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if IsTest(attempt.Quiz) && attempt.Quiz.ShuffleQuestions {
|
||||
rand.New(rand.NewPCG(uint64(attempt.ID), uint64(attempt.QuizID))).
|
||||
Shuffle(len(questions), func(i, j int) { questions[i], questions[j] = questions[j], questions[i] })
|
||||
}
|
||||
var answers []models.AttemptAnswer
|
||||
if err := s.db(ctx).Where("attempt_id = ?", attempt.ID).Find(&answers).Error; err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user