feat: add open questions with a pending grading state

- migration: open question type, rubric, max_length, quiz grader and
  the grading and review columns of attempt answers
- open answers are checked, stored as pending and never scored on
  answer; a changed text resets the grading state
- student JSON gains text, grading_status, grader_feedback,
  review_comment, max_length, needs_review and open_answers; the
  rubric is never serialized
This commit is contained in:
Jakub Zych
2026-11-09 17:48:11 +01:00
parent ef38b81fd9
commit 297780cb8e
19 changed files with 893 additions and 48 deletions

View File

@@ -227,6 +227,10 @@ type Started struct {
AttemptsUsed int
// AnsweredCount is the number of questions a resumed attempt answered.
AnsweredCount int
// OpenAnswers and Unreviewed count the open answers of a resumed
// attempt, as in AttemptSummary.
OpenAnswers int
Unreviewed int
}
// StartAttempt starts an attempt at a published quiz for the user, like
@@ -322,11 +326,15 @@ func (s *Service) startTest(ctx context.Context, userID uint, quiz *models.Quiz)
return err
}
if resume != nil {
var answered int64
if err := tx.Model(&models.AttemptAnswer{}).Where("attempt_id = ?", resume.ID).Count(&answered).Error; err != nil {
counts, err := answerCounts(ctx, tx, []uint{resume.ID})
if err != nil {
return err
}
st = &Started{Attempt: resume, Quiz: quiz, Resumed: true, AttemptsUsed: int(used), AnsweredCount: int(answered)}
c := counts[resume.ID]
st = &Started{
Attempt: resume, Quiz: quiz, Resumed: true, AttemptsUsed: int(used),
AnsweredCount: c.N, OpenAnswers: c.OpenN, Unreviewed: c.UnreviewedN,
}
return nil
}
if left := AttemptsLeft(quiz, int(used)); left != nil && *left == 0 {
@@ -403,6 +411,8 @@ type AnswerInput struct {
QuestionID uint
AnswerIDs []uint
Known *bool
// Text is the answer to an open question.
Text *string
}
// Feedback is what the user learns after answering one question.
@@ -414,6 +424,9 @@ type Feedback struct {
// and the serializer must drop Result, CorrectAnswerIDs and every
// solution field of Question.
Withheld bool
// Answer is the stored row of an open question, with its text and
// grading state; it is nil for every other type.
Answer *models.AttemptAnswer
}
// Answer records or overwrites the user's answer to one question of an
@@ -423,8 +436,11 @@ type Feedback struct {
// 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.
// type (FieldError on answer_ids, known or text). The feedback of an open
// test attempt is Withheld.
//
// An open question stores its text as pending (see answerOpen) and is never
// scored here: a grader or the owner scores it later.
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 {
@@ -464,6 +480,20 @@ func (s *Service) Answer(ctx context.Context, userID, attemptID uint, in AnswerI
row := models.AttemptAnswer{AttemptID: attempt.ID, QuestionID: question.ID}
switch question.Type {
case models.TypeOpen:
if in.Text == nil {
return &FieldError{Field: "text", Key: "text_required"}
}
text, err := CheckOpenText(*in.Text, question)
if err != nil {
return err
}
stored, err := s.answerOpen(ctx, tx, attempt, question, text)
if err != nil {
return err
}
fb = &Feedback{Question: question, Answer: stored, Withheld: Withheld(attempt)}
return nil
case models.TypeSelfCheck:
if in.Known == nil {
return &FieldError{Field: "known", Key: "known_required"}
@@ -512,6 +542,78 @@ func (s *Service) Answer(ctx context.Context, userID, attemptID uint, in AnswerI
return fb, nil
}
// answerOpen stores a cleaned open answer in a transaction that holds the
// attempt row lock, so reading the existing row first is race-free. The
// same text again changes nothing. Another text is a new answer (Pitfall 1):
// the row goes back to pending with the next grading_seq, and every grade,
// error, review and point of the old text is cleared, so a learning
// re-answer replaces an earlier grade or review. A first answer creates the
// row as pending with seq 1. A new or changed text is handed to
// enqueueGrading.
func (s *Service) answerOpen(ctx context.Context, tx *gorm.DB, attempt *models.Attempt, question models.Question, text string) (*models.AttemptAnswer, error) {
tx = tx.WithContext(ctx)
now := s.now().UTC()
var existing models.AttemptAnswer
err := tx.Where("attempt_id = ? AND question_id = ?", attempt.ID, question.ID).Take(&existing).Error
switch {
case err == nil:
if existing.TextAnswer != nil && *existing.TextAnswer == text && existing.GradingStatus != nil {
return &existing, nil
}
seq := existing.GradingSeq + 1
if err := tx.Model(&models.AttemptAnswer{}).Where("id = ?", existing.ID).Updates(map[string]any{
"text_answer": text,
"answer_ids": nil,
"self_check_known": nil,
"grading_status": models.GradingPending,
"grading_seq": seq,
"grading_attempts": 0,
"grader": nil,
"grader_points": nil,
"grader_feedback": nil,
"grading_error": nil,
"graded_at": nil,
"review_comment": nil,
"reviewed_at": nil,
"reviewed_by_user_id": nil,
"reviewed_by_admin_id": nil,
"points": 0,
"is_correct": false,
"updated_at": now,
}).Error; err != nil {
return nil, err
}
if err := tx.Where("id = ?", existing.ID).Take(&existing).Error; err != nil {
return nil, err
}
if err := s.enqueueGrading(ctx, tx, existing.ID, seq, attempt); err != nil {
return nil, err
}
return &existing, nil
case errors.Is(err, gorm.ErrRecordNotFound):
default:
return nil, err
}
pending := models.GradingPending
row := &models.AttemptAnswer{
AttemptID: attempt.ID, QuestionID: question.ID,
TextAnswer: &text, GradingStatus: &pending, GradingSeq: 1,
CreatedAt: now, UpdatedAt: now,
}
if err := tx.Create(row).Error; err != nil {
return nil, err
}
if err := s.enqueueGrading(ctx, tx, row.ID, row.GradingSeq, attempt); err != nil {
return nil, err
}
return row, nil
}
// enqueueGrading hands a new or changed open answer to the quiz's grader.
func (s *Service) enqueueGrading(ctx context.Context, tx *gorm.DB, answerID uint, seq int, attempt *models.Attempt) error {
return nil
}
// CorrectAnswerIDs lists the IDs of a question's correct answers in sort
// order; it is empty for a self_check question.
func CorrectAnswerIDs(q models.Question) []uint {
@@ -574,6 +676,50 @@ func (s *Service) close(ctx context.Context, tx *gorm.DB, attempt *models.Attemp
type AttemptSummary struct {
Attempt models.Attempt
AnsweredCount int
// OpenAnswers counts the attempt's answers to open questions (rows
// with a grading status).
OpenAnswers int
// Unreviewed counts the open answers the owner has not reviewed yet:
// pending, graded or failed.
Unreviewed int
}
// answerCountRow is the per-attempt answer counts answerCounts reads.
type answerCountRow struct {
AttemptID uint
N int
OpenN int
UnreviewedN int
}
// answerCounts counts, per attempt, the answers, the open answers and the
// open answers not reviewed yet.
func answerCounts(ctx context.Context, db *gorm.DB, attemptIDs []uint) (map[uint]answerCountRow, error) {
var rows []answerCountRow
if err := db.WithContext(ctx).Model(&models.AttemptAnswer{}).
Select("attempt_id, COUNT(*) AS n, "+
"COUNT(*) FILTER (WHERE grading_status IS NOT NULL) AS open_n, "+
"COUNT(*) FILTER (WHERE grading_status IN ('pending', 'graded', 'failed')) AS unreviewed_n").
Where("attempt_id IN ?", attemptIDs).Group("attempt_id").Scan(&rows).Error; err != nil {
return nil, err
}
out := make(map[uint]answerCountRow, len(rows))
for _, r := range rows {
out[r.AttemptID] = r
}
return out, nil
}
// unreviewed reports whether an open answer still waits for the owner.
func unreviewed(a *models.AttemptAnswer) bool {
if a == nil || a.GradingStatus == nil {
return false
}
switch *a.GradingStatus {
case models.GradingPending, models.GradingGraded, models.GradingFailed:
return true
}
return false
}
// QuestionResult is one question of an attempt with the user's answer.
@@ -638,6 +784,14 @@ func (s *Service) Result(ctx context.Context, userID, attemptID uint) (*AttemptR
byQuestion[answers[i].QuestionID] = &answers[i]
}
res := &AttemptResult{AttemptSummary: AttemptSummary{Attempt: *attempt, AnsweredCount: len(answers)}}
for i := range answers {
if answers[i].GradingStatus != nil {
res.OpenAnswers++
}
if unreviewed(&answers[i]) {
res.Unreviewed++
}
}
for _, q := range questions {
res.Questions = append(res.Questions, QuestionResult{Question: q, Answer: byQuestion[q.ID]})
}
@@ -668,21 +822,13 @@ func (s *Service) History(ctx context.Context, userID uint, quizSlug string, pag
for i, a := range attempts {
ids[i] = a.ID
}
type countRow struct {
AttemptID uint
N int
}
var counts []countRow
if err := s.db(ctx).Model(&models.AttemptAnswer{}).Select("attempt_id, COUNT(*) AS n").
Where("attempt_id IN ?", ids).Group("attempt_id").Scan(&counts).Error; err != nil {
countBy, err := answerCounts(ctx, s.DB, ids)
if err != nil {
return nil, 0, err
}
countBy := map[uint]int{}
for _, c := range counts {
countBy[c.AttemptID] = c.N
}
for _, a := range attempts {
out = append(out, AttemptSummary{Attempt: a, AnsweredCount: countBy[a.ID]})
c := countBy[a.ID]
out = append(out, AttemptSummary{Attempt: a, AnsweredCount: c.N, OpenAnswers: c.OpenN, Unreviewed: c.UnreviewedN})
}
return out, total, nil
}