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

@@ -16,7 +16,11 @@ import (
func fixtureQuestion(id uint, typ string) models.Question {
expl, model, why := "question explanation", "model answer", "answer explanation"
q := models.Question{ID: id, Text: "Q", Type: typ, Points: 2, Explanation: &expl, AnswerText: &model}
if typ != models.TypeSelfCheck {
if typ == models.TypeOpen {
rubric := "R-SECRET"
q.Rubric = &rubric
}
if isChoice(typ) {
q.Answers = []models.Answer{
{ID: id*10 + 1, QuestionID: id, Text: "right", IsCorrect: true, Explanation: &why},
{ID: id*10 + 2, QuestionID: id, Text: "wrong", Explanation: &why},
@@ -25,6 +29,28 @@ func fixtureQuestion(id uint, typ string) models.Question {
return q
}
func isChoice(typ string) bool { return typ == models.TypeSingle || typ == models.TypeMultiple }
func strPtr(s string) *string { return &s }
// noRubric fails when a student shape names or contains the rubric or the
// grading error.
func noRubric(t *testing.T, where, raw string) {
t.Helper()
if strings.Contains(raw, `"rubric"`) || strings.Contains(raw, "R-SECRET") || strings.Contains(raw, "grading_error") || strings.Contains(raw, "provider exploded") {
t.Errorf("%s leaks the rubric or the grading error: %s", where, raw)
}
}
// openRow is a stored open answer in the given grading status.
func openRow(questionID uint, status string) *models.AttemptAnswer {
return &models.AttemptAnswer{
AttemptID: 5, QuestionID: questionID,
TextAnswer: strPtr("moja odpowiedź"), GradingStatus: strPtr(status), GradingSeq: 1,
GradingError: strPtr("provider exploded"),
}
}
func decode(t *testing.T, v any) (map[string]any, string) {
t.Helper()
b, err := json.Marshal(v)
@@ -83,21 +109,43 @@ func TestWithheldSerializers(t *testing.T) {
CorrectAnswerIDs: classes.CorrectAnswerIDs(answered),
Withheld: true,
}
// gradedOpen is an open answer with every grading field set, so
// a serializer that forgets to withhold one shows it.
gradedOpen := func() *models.AttemptAnswer {
r := openRow(1, models.GradingGraded)
two := 2.0
r.IsCorrect, r.Points, r.GraderPoints = true, 2, &two
r.GraderFeedback, r.ReviewComment = strPtr("feedback"), strPtr("comment")
return r
}
if typ == models.TypeOpen {
fb.Answer = gradedOpen()
}
m, raw := decode(t, feedback(fb))
if m["withheld"] != true {
t.Errorf("feedback withheld = %v, want true", m["withheld"])
}
noSolution(t, "feedback", m, "is_correct", "points", "correct_answer_ids", "explanation", "answer_text")
noSolution(t, "feedback", m, "is_correct", "points", "correct_answer_ids", "explanation", "answer_text",
"grading_status", "grader_feedback", "review_comment")
noCorrectness(t, "feedback", raw)
if typ != models.TypeSelfCheck && len(m["answers"].([]any)) != 2 {
noRubric(t, "feedback", raw)
if isChoice(typ) && len(m["answers"].([]any)) != 2 {
t.Errorf("feedback answers = %v, want both answers", m["answers"])
}
if typ == models.TypeOpen && m["text"] != "moja odpowiedź" {
t.Errorf("withheld open feedback lost the student's text: %v", m["text"])
} else if typ != models.TypeOpen && m["text"] != nil {
t.Errorf("%s feedback text = %v, want null", typ, m["text"])
}
known := true
row := &models.AttemptAnswer{AttemptID: 5, QuestionID: 1, IsCorrect: true, Points: 2}
if typ == models.TypeSelfCheck {
switch typ {
case models.TypeSelfCheck:
row.SelfCheckKnown = &known
} else {
case models.TypeOpen:
row = gradedOpen()
default:
row.AnswerIDs = lagoon.Jsonable[[]uint]{Data: []uint{11}, Valid: true}
}
res := &classes.AttemptResult{
@@ -109,24 +157,33 @@ func TestWithheldSerializers(t *testing.T) {
t.Errorf("result solutions_withheld = %v quiz_mode = %v", m["solutions_withheld"], m["quiz_mode"])
}
noCorrectness(t, "result", raw)
noRubric(t, "result", raw)
questions := m["questions"].([]any)
if len(questions) != 2 {
t.Fatalf("result questions = %d, want 2", len(questions))
}
for i, raw := range questions {
q := raw.(map[string]any)
noSolution(t, "result question "+string(rune('0'+i)), q, "is_correct", "points_earned", "correct_answer_ids", "explanation", "answer_text")
noSolution(t, "result question "+string(rune('0'+i)), q, "is_correct", "points_earned", "correct_answer_ids", "explanation", "answer_text",
"grading_status", "grader_feedback", "review_comment")
}
first := questions[0].(map[string]any)
if first["answered"] != true {
t.Errorf("answered question lost answered: %v", first)
}
if typ == models.TypeSelfCheck {
switch typ {
case models.TypeSelfCheck:
if first["known"] != true {
t.Errorf("self_check lost the student's known: %v", first)
}
} else if ids, _ := first["answer_ids"].([]any); len(ids) != 1 || ids[0] != float64(11) {
t.Errorf("answer_ids = %v, want the student's [11]", first["answer_ids"])
case models.TypeOpen:
if first["text_answer"] != "moja odpowiedź" {
t.Errorf("open lost the student's text: %v", first)
}
default:
if ids, _ := first["answer_ids"].([]any); len(ids) != 1 || ids[0] != float64(11) {
t.Errorf("answer_ids = %v, want the student's [11]", first["answer_ids"])
}
}
done := time.Now()
@@ -207,15 +264,15 @@ func TestSummaryAndAttemptFields(t *testing.T) {
t.Errorf("test summary = %v", m)
}
types, _ := m["question_types"].(map[string]any)
if len(types) != 3 || types["single"] != float64(3) || types["multiple"] != float64(0) || types["self_check"] != float64(0) {
t.Errorf("question_types = %v, want all three keys", m["question_types"])
if len(types) != 4 || types["single"] != float64(3) || types["multiple"] != float64(0) || types["self_check"] != float64(0) || types["open"] != float64(0) {
t.Errorf("question_types = %v, want all four keys", m["question_types"])
}
m, _ = decode(t, quizSummary(classes.QuizSummary{Quiz: models.Quiz{ID: 4, Slug: "l"}, AttemptsUsed: 9}))
if v, ok := m["attempts_left"]; !ok || v != nil {
t.Errorf("learning attempts_left = %v (present %v), want null", v, ok)
}
if types, _ := m["question_types"].(map[string]any); len(types) != 3 {
t.Errorf("learning question_types = %v, want all three keys", m["question_types"])
if types, _ := m["question_types"].(map[string]any); len(types) != 4 {
t.Errorf("learning question_types = %v, want all four keys", m["question_types"])
}
if m["mode"] != models.ModeLearning || m["navigation"] != models.NavigationFree {
t.Errorf("zero-value quiz mode / navigation = %v / %v", m["mode"], m["navigation"])
@@ -241,3 +298,119 @@ func TestSummaryAndAttemptFields(t *testing.T) {
t.Errorf("start json = %v", m)
}
}
func TestOpenSerializers(t *testing.T) {
q := fixtureQuestion(1, models.TypeOpen)
q.AnswerText = strPtr("MODEL")
learning := &models.Quiz{Slug: "l", PassingScore: 50}
// Feedback of a pending learning answer: the model answer and the
// student's text, no score yet, never the rubric.
m, raw := decode(t, feedback(&classes.Feedback{Question: q, Answer: openRow(1, models.GradingPending)}))
noRubric(t, "pending feedback", raw)
if m["answer_text"] != "MODEL" || m["text"] != "moja odpowiedź" || m["grading_status"] != models.GradingPending {
t.Errorf("pending feedback = %s", raw)
}
if m["is_correct"] != nil || m["points"] != nil {
t.Errorf("pending feedback has a score: %s", raw)
}
if a, ok := m["answers"].([]any); !ok || len(a) != 0 {
t.Errorf("open feedback answers = %v, want []", m["answers"])
}
if ids, ok := m["correct_answer_ids"].([]any); !ok || len(ids) != 0 {
t.Errorf("open feedback correct_answer_ids = %v, want []", m["correct_answer_ids"])
}
m, raw = decode(t, feedback(&classes.Feedback{Question: q, Answer: openRow(1, models.GradingFailed)}))
noRubric(t, "failed feedback", raw)
if m["grading_status"] != models.GradingFailed || m["is_correct"] != nil || m["points"] != nil {
t.Errorf("failed feedback = %s", raw)
}
graded := openRow(1, models.GradingGraded)
graded.Points, graded.IsCorrect, graded.GraderFeedback = 1.5, false, strPtr("Brakuje przykładu.")
m, raw = decode(t, feedback(&classes.Feedback{Question: q, Answer: graded}))
noRubric(t, "graded feedback", raw)
if m["points"] != 1.5 || m["is_correct"] != false || m["grader_feedback"] != "Brakuje przykładu." || m["review_comment"] != nil {
t.Errorf("graded feedback = %s", raw)
}
reviewed := openRow(1, models.GradingReviewed)
reviewed.Points, reviewed.IsCorrect, reviewed.ReviewComment = 2, true, strPtr("Dobrze.")
m, raw = decode(t, feedback(&classes.Feedback{Question: q, Answer: reviewed}))
if m["points"] != float64(2) || m["is_correct"] != true || m["review_comment"] != "Dobrze." || m["grading_status"] != models.GradingReviewed {
t.Errorf("reviewed feedback = %s", raw)
}
// The question before answering: max_length, no model answer, no rubric.
m, raw = decode(t, question(q))
noRubric(t, "open question", raw)
if m["max_length"] != float64(models.DefaultMaxLength) {
t.Errorf("open question max_length = %v, want %d", m["max_length"], models.DefaultMaxLength)
}
if _, ok := m["answer_text"]; ok {
t.Errorf("open question leaks its model answer: %s", raw)
}
five := 5
q5 := q
q5.MaxLength = &five
if m, _ = decode(t, question(q5)); m["max_length"] != float64(5) {
t.Errorf("open question max_length = %v, want 5", m["max_length"])
}
if _, raw = decode(t, question(fixtureQuestion(2, models.TypeSingle))); strings.Contains(raw, "max_length") {
t.Errorf("single question names max_length: %s", raw)
}
_, raw = decode(t, quizDetail(&classes.QuizDetail{Quiz: *learning, Questions: []models.Question{q}}))
noRubric(t, "quiz detail", raw)
// The result of a learning attempt with a pending open answer.
res := &classes.AttemptResult{
AttemptSummary: classes.AttemptSummary{
Attempt: models.Attempt{ID: 5, StartedAt: time.Now(), Quiz: learning},
AnsweredCount: 1, OpenAnswers: 1, Unreviewed: 1,
},
Questions: []classes.QuestionResult{{Question: q, Answer: openRow(1, models.GradingPending)}, {Question: fixtureQuestion(2, models.TypeSingle)}},
}
m, raw = decode(t, result(res))
noRubric(t, "result", raw)
if m["needs_review"] != true || m["open_answers"] != float64(1) {
t.Errorf("result needs_review / open_answers = %v / %v", m["needs_review"], m["open_answers"])
}
row := m["questions"].([]any)[0].(map[string]any)
if row["points_earned"] != nil || row["is_correct"] != nil {
t.Errorf("pending open result has a score: %v", row)
}
if row["answer_text"] != "MODEL" || row["text_answer"] != "moja odpowiedź" || row["text"] != "Q" || row["grading_status"] != models.GradingPending {
t.Errorf("pending open result = %v", row)
}
if row["max_length"] != float64(models.DefaultMaxLength) {
t.Errorf("open result max_length = %v", row["max_length"])
}
choice := m["questions"].([]any)[1].(map[string]any)
for _, k := range []string{"text_answer", "grading_status", "grader_feedback", "review_comment"} {
if v, ok := choice[k]; !ok || v != nil {
t.Errorf("single result %s = %v (present %v), want null", k, v, ok)
}
}
if _, ok := choice["max_length"]; ok {
t.Errorf("single result names max_length: %v", choice)
}
res.Questions[0].Answer = reviewed
res.Unreviewed = 0
m, _ = decode(t, result(res))
row = m["questions"].([]any)[0].(map[string]any)
if row["points_earned"] != float64(2) || row["is_correct"] != true || row["review_comment"] != "Dobrze." || m["needs_review"] != false {
t.Errorf("reviewed open result = %v (needs_review %v)", row, m["needs_review"])
}
// Every attempt row carries the two review keys.
m, _ = decode(t, attempt(classes.AttemptSummary{Attempt: models.Attempt{ID: 1, StartedAt: time.Now(), Quiz: learning}}))
if m["needs_review"] != false || m["open_answers"] != float64(0) {
t.Errorf("attempt without open answers = %v / %v", m["needs_review"], m["open_answers"])
}
m, _ = decode(t, start(&classes.Started{Attempt: &models.Attempt{ID: 2, StartedAt: time.Now()}, Quiz: learning, OpenAnswers: 2, Unreviewed: 1}))
if m["needs_review"] != true || m["open_answers"] != float64(2) {
t.Errorf("start needs_review / open_answers = %v / %v", m["needs_review"], m["open_answers"])
}
}