Add exam review question responses and summary views with HTMX support
This commit is contained in:
@@ -414,3 +414,134 @@ class UserAnswerDelete(SuperuserRequiredMixin, DeleteView):
|
||||
model = UserAnswer
|
||||
template_name = "user_answer_delete.html"
|
||||
success_url = reverse_lazy("physics:user_answer_table_view")
|
||||
|
||||
|
||||
@login_required
|
||||
def exam_review_question_responses(request, pk: int, q_index: int):
|
||||
"""Return the responses partial for a given exam question (HTMX-friendly)."""
|
||||
exam = get_object_or_404(Exam, pk=pk)
|
||||
|
||||
questions = exam.get_questions()
|
||||
try:
|
||||
question = questions[q_index]
|
||||
except Exception:
|
||||
raise Http404("Question not found in exam")
|
||||
|
||||
context = {
|
||||
"exam": exam,
|
||||
"question": question,
|
||||
"q_index": q_index,
|
||||
"app_name": "physics",
|
||||
}
|
||||
|
||||
# Prefilter user answers for this exam to avoid template-side filtering
|
||||
try:
|
||||
exam_user_answers = question.cid_user_answers.filter(exam=exam).select_related("user")
|
||||
except Exception:
|
||||
exam_user_answers = question.cid_user_answers.all()
|
||||
|
||||
# Default: anonymise unless reveal=1 is provided
|
||||
reveal_flag = str(request.GET.get("reveal", "")).lower() in ("1", "true", "on")
|
||||
context["reveal_respondents"] = reveal_flag
|
||||
context["exam_user_answers"] = exam_user_answers
|
||||
|
||||
return render(request, "physics/partials/exam_review_question_responses_fragment.html", context)
|
||||
|
||||
|
||||
@login_required
|
||||
def exam_review_question_summary(request, pk: int, q_index: int):
|
||||
"""Return the aggregated response summary partial for a given exam question (HTMX-friendly)."""
|
||||
exam = get_object_or_404(Exam, pk=pk)
|
||||
|
||||
questions = exam.get_questions()
|
||||
try:
|
||||
question = questions[q_index]
|
||||
except Exception:
|
||||
raise Http404("Question not found in exam")
|
||||
|
||||
# Aggregate user answers for this question & exam
|
||||
ua_qs = question.cid_user_answers.filter(exam=exam)
|
||||
total_responses = ua_qs.count()
|
||||
|
||||
# Physics answers may be stored as tuples/lists or named fields; attempt to aggregate
|
||||
counts = defaultdict(int)
|
||||
other_count = 0
|
||||
for ans in ua_qs.values_list("answer", flat=True):
|
||||
if not ans:
|
||||
other_count += 1
|
||||
continue
|
||||
# normalize iterable answers to a string key
|
||||
if isinstance(ans, (list, tuple)):
|
||||
key = ", ".join([str(x) for x in ans])
|
||||
else:
|
||||
key = str(ans)
|
||||
counts[key] += 1
|
||||
|
||||
counts["unanswered"] = other_count
|
||||
|
||||
pcts = {}
|
||||
if total_responses:
|
||||
for k, v in counts.items():
|
||||
pcts[k] = round(100.0 * v / total_responses, 1)
|
||||
else:
|
||||
for k in counts.keys():
|
||||
pcts[k] = 0.0
|
||||
|
||||
# Compute percent correct using per-answer scoring
|
||||
correct_count = None
|
||||
correct_pct = None
|
||||
try:
|
||||
# per-question max for physics is 5 (see generic.get_max_score mapping)
|
||||
per_question_max = 5
|
||||
if total_responses:
|
||||
cnt = 0
|
||||
for ua in ua_qs:
|
||||
try:
|
||||
score = ua.get_answer_score()
|
||||
except Exception:
|
||||
continue
|
||||
if isinstance(score, (list, tuple)):
|
||||
try:
|
||||
ssum = sum([s for s in score if isinstance(s, (int, float))])
|
||||
except Exception:
|
||||
continue
|
||||
if ssum == per_question_max:
|
||||
cnt += 1
|
||||
else:
|
||||
if isinstance(score, (int, float)) and score == per_question_max:
|
||||
cnt += 1
|
||||
correct_count = cnt
|
||||
correct_pct = round(100.0 * correct_count / total_responses, 1) if total_responses else 0.0
|
||||
else:
|
||||
correct_count = 0
|
||||
correct_pct = 0.0
|
||||
except Exception:
|
||||
correct_count = None
|
||||
correct_pct = None
|
||||
|
||||
# Provide a best-effort correct answer display
|
||||
correct_answer = None
|
||||
try:
|
||||
correct_answer = question.get_answers()
|
||||
if isinstance(correct_answer, (list, tuple)):
|
||||
correct_answer = ", ".join([str(x) for x in correct_answer])
|
||||
else:
|
||||
correct_answer = str(correct_answer)
|
||||
except Exception:
|
||||
correct_answer = None
|
||||
|
||||
context = {
|
||||
"exam": exam,
|
||||
"question": question,
|
||||
"q_index": q_index,
|
||||
"app_name": "physics",
|
||||
"exam_response_counts": counts,
|
||||
"exam_response_pcts": pcts,
|
||||
"exam_response_total": total_responses,
|
||||
"exam_response_correct_count": correct_count,
|
||||
"exam_response_correct_pct": correct_pct,
|
||||
"exam_response_texts": {},
|
||||
"correct_answer": correct_answer,
|
||||
}
|
||||
|
||||
return render(request, "physics/partials/exam_review_question_summary_fragment.html", context)
|
||||
|
||||
Reference in New Issue
Block a user