Add aggregated response summary and user answer filtering in exam review
This commit is contained in:
@@ -1216,6 +1216,53 @@ class ExamViews(View, LoginRequiredMixin):
|
||||
"can_edit": self.check_user_edit_access(request.user, exam_id=pk),
|
||||
}
|
||||
|
||||
# Compute aggregated response summary for this question within the current exam.
|
||||
# Add counts and percentages to the context so fragments (e.g. SBAS) can display them.
|
||||
try:
|
||||
if hasattr(question, "cid_user_answers"):
|
||||
ua_qs = question.cid_user_answers.filter(exam=exam)
|
||||
total_responses = ua_qs.count()
|
||||
counts = {}
|
||||
for ans in ua_qs.values_list("answer", flat=True):
|
||||
counts[ans] = counts.get(ans, 0) + 1
|
||||
|
||||
# Compute percentages per answer key
|
||||
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
|
||||
|
||||
# If the question exposes a best_answer, compute correct count
|
||||
correct_count = None
|
||||
if hasattr(question, "best_answer") and question.best_answer:
|
||||
correct_count = ua_qs.filter(answer=question.best_answer).count()
|
||||
else:
|
||||
correct_count = None
|
||||
|
||||
# percent correct
|
||||
correct_pct = None
|
||||
if correct_count is not None and total_responses:
|
||||
try:
|
||||
correct_pct = round(100.0 * correct_count / total_responses, 1)
|
||||
except Exception:
|
||||
correct_pct = None
|
||||
|
||||
context.update(
|
||||
{
|
||||
"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,
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
# Best-effort: don't fail the review page if aggregation errors occur
|
||||
pass
|
||||
|
||||
# Choose an app-specific fragment if present, otherwise fall back to
|
||||
# the generic fragment. Expose the chosen fragment name in the
|
||||
# context so the full-page render can include it; return the fragment
|
||||
|
||||
@@ -109,6 +109,51 @@
|
||||
<a class="btn btn-sm btn-outline-secondary" href="{% url app_name|add:':question_detail' question.pk %}" target="_blank">View full question details</a>
|
||||
</p>
|
||||
|
||||
{# Aggregated summary: always shown if aggregation context is available #}
|
||||
<div class="mt-3">
|
||||
<h6>Response summary</h6>
|
||||
{% if exam_response_total %}
|
||||
<div class="small text-muted mb-2">Total responses: {{ exam_response_total }}</div>
|
||||
<div class="row mb-2">
|
||||
<div class="col-auto">Correct:</div>
|
||||
<div class="col">
|
||||
{% if exam_response_correct_count is not None %}
|
||||
<strong>{{ exam_response_correct_count }}</strong>
|
||||
{% if exam_response_correct_pct is not None %}
|
||||
<span class="text-muted">({{ exam_response_correct_pct }}%)</span>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<span class="text-muted">N/A</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="list-group">
|
||||
<div class="list-group-item d-flex justify-content-between align-items-center">
|
||||
<div>A</div>
|
||||
<div><span class="me-2">{{ exam_response_counts.a|default:0 }}</span><small class="text-muted">{{ exam_response_pcts.a|default:0 }}%</small></div>
|
||||
</div>
|
||||
<div class="list-group-item d-flex justify-content-between align-items-center">
|
||||
<div>B</div>
|
||||
<div><span class="me-2">{{ exam_response_counts.b|default:0 }}</span><small class="text-muted">{{ exam_response_pcts.b|default:0 }}%</small></div>
|
||||
</div>
|
||||
<div class="list-group-item d-flex justify-content-between align-items-center">
|
||||
<div>C</div>
|
||||
<div><span class="me-2">{{ exam_response_counts.c|default:0 }}</span><small class="text-muted">{{ exam_response_pcts.c|default:0 }}%</small></div>
|
||||
</div>
|
||||
<div class="list-group-item d-flex justify-content-between align-items-center">
|
||||
<div>D</div>
|
||||
<div><span class="me-2">{{ exam_response_counts.d|default:0 }}</span><small class="text-muted">{{ exam_response_pcts.d|default:0 }}%</small></div>
|
||||
</div>
|
||||
<div class="list-group-item d-flex justify-content-between align-items-center">
|
||||
<div>E</div>
|
||||
<div><span class="me-2">{{ exam_response_counts.e|default:0 }}</span><small class="text-muted">{{ exam_response_pcts.e|default:0 }}%</small></div>
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="small text-muted">No responses recorded for this question in the exam yet.</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{# Responses partial: load on demand via HTMX. Button fetches partial into #responses-container #}
|
||||
<div class="mt-3">
|
||||
<div class="d-flex justify-content-between align-items-center">
|
||||
@@ -116,7 +161,7 @@
|
||||
<button class="btn btn-sm btn-outline-primary"
|
||||
hx-get="{% url 'sbas:exam_review_question_responses' exam.pk q_index %}"
|
||||
hx-target="#responses-container"
|
||||
hx-swap="innerhtmlHTML">Show responses</button>
|
||||
hx-swap="innerHTML">Show responses</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-2" id="responses-container">
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
<h6>Responses in this exam</h6>
|
||||
<div class="small text-muted mb-2">List of candidate answers recorded for this question in the current exam.</div>
|
||||
<ul class="list-group">
|
||||
{% for ua in question.cid_user_answers.all %}
|
||||
{% if ua.exam and ua.exam.pk == exam.pk %}
|
||||
{% if exam_user_answers is defined %}
|
||||
<ul class="list-group">
|
||||
{% for ua in exam_user_answers %}
|
||||
<li class="list-group-item d-flex justify-content-between align-items-start">
|
||||
<div>
|
||||
{% if ua.user %}
|
||||
@@ -25,9 +26,7 @@
|
||||
{% endif %}
|
||||
</div>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% empty %}
|
||||
<li class="list-group-item">No responses recorded for this exam.</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@@ -109,6 +109,14 @@ def exam_review_question_responses(request, pk: int, q_index: int):
|
||||
"app_name": "sbas",
|
||||
}
|
||||
|
||||
# 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()
|
||||
|
||||
context["exam_user_answers"] = exam_user_answers
|
||||
|
||||
return render(request, "sbas/partials/exam_review_question_responses_fragment.html", context)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user