Refactor exam review templates and views to improve response summary display and handle unanswered responses

This commit is contained in:
Ross
2025-11-10 09:58:02 +00:00
parent 3d6ca8be54
commit e078b5a98f
4 changed files with 71 additions and 11 deletions
+24 -2
View File
@@ -138,13 +138,24 @@ def exam_review_question_summary(request, pk: int, q_index: int):
ua_qs = question.cid_user_answers.filter(exam=exam)
total_responses = ua_qs.count()
# Track counts for the five canonical choices and aggregate any
# empty / unexpected answers into an 'unanswered' bucket so the
# template can show them explicitly and percentages use the same
# denominator (total_responses).
counts = {"a": 0, "b": 0, "c": 0, "d": 0, "e": 0}
other_count = 0
for ans in ua_qs.values_list("answer", flat=True):
# Normalize None/empty strings to be considered 'unanswered'
if not ans:
other_count += 1
continue
if ans in counts:
counts[ans] += 1
else:
# Unexpected answer labels — store them as-is (rare)
counts[ans] = counts.get(ans, 0) + 1
# Unexpected answer labels — treat as 'other/unanswered'
other_count += 1
# Expose 'unanswered' so templates can report it
counts['unanswered'] = other_count
pcts = {}
if total_responses:
@@ -161,6 +172,16 @@ def exam_review_question_summary(request, pk: int, q_index: int):
if total_responses:
correct_pct = round(100.0 * correct_count / total_responses, 1)
# Provide the actual answer text for each canonical choice so the
# summary template can display the full answer alongside the letter.
exam_response_texts = {
"a": (getattr(question, "a_answer", None) or "") ,
"b": (getattr(question, "b_answer", None) or "") ,
"c": (getattr(question, "c_answer", None) or "") ,
"d": (getattr(question, "d_answer", None) or "") ,
"e": (getattr(question, "e_answer", None) or "") ,
}
context = {
"exam": exam,
"question": question,
@@ -171,6 +192,7 @@ def exam_review_question_summary(request, pk: int, q_index: int):
"exam_response_total": total_responses,
"exam_response_correct_count": correct_count,
"exam_response_correct_pct": correct_pct,
"exam_response_texts": exam_response_texts,
}
return render(request, "sbas/partials/exam_review_question_summary_fragment.html", context)