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),
|
"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
|
# Choose an app-specific fragment if present, otherwise fall back to
|
||||||
# the generic fragment. Expose the chosen fragment name in the
|
# the generic fragment. Expose the chosen fragment name in the
|
||||||
# context so the full-page render can include it; return the fragment
|
# 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>
|
<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>
|
</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 #}
|
{# Responses partial: load on demand via HTMX. Button fetches partial into #responses-container #}
|
||||||
<div class="mt-3">
|
<div class="mt-3">
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
<div class="d-flex justify-content-between align-items-center">
|
||||||
@@ -116,7 +161,7 @@
|
|||||||
<button class="btn btn-sm btn-outline-primary"
|
<button class="btn btn-sm btn-outline-primary"
|
||||||
hx-get="{% url 'sbas:exam_review_question_responses' exam.pk q_index %}"
|
hx-get="{% url 'sbas:exam_review_question_responses' exam.pk q_index %}"
|
||||||
hx-target="#responses-container"
|
hx-target="#responses-container"
|
||||||
hx-swap="innerhtmlHTML">Show responses</button>
|
hx-swap="innerHTML">Show responses</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-2" id="responses-container">
|
<div class="mt-2" id="responses-container">
|
||||||
|
|||||||
@@ -3,31 +3,30 @@
|
|||||||
<h6>Responses in this exam</h6>
|
<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>
|
<div class="small text-muted mb-2">List of candidate answers recorded for this question in the current exam.</div>
|
||||||
<ul class="list-group">
|
<ul class="list-group">
|
||||||
{% for ua in question.cid_user_answers.all %}
|
{% if exam_user_answers is defined %}
|
||||||
{% if ua.exam and ua.exam.pk == exam.pk %}
|
<ul class="list-group">
|
||||||
<li class="list-group-item d-flex justify-content-between align-items-start">
|
{% for ua in exam_user_answers %}
|
||||||
<div>
|
<li class="list-group-item d-flex justify-content-between align-items-start">
|
||||||
{% if ua.user %}
|
<div>
|
||||||
<strong>{{ ua.user.get_full_name|default:ua.user.username }}</strong>
|
{% if ua.user %}
|
||||||
{% else %}
|
<strong>{{ ua.user.get_full_name|default:ua.user.username }}</strong>
|
||||||
<strong>Anonymous</strong>
|
{% else %}
|
||||||
{% endif %}
|
<strong>Anonymous</strong>
|
||||||
<div>Answer: <strong>{{ ua.answer }}</strong></div>
|
{% endif %}
|
||||||
{% if ua.created_date %}
|
<div>Answer: <strong>{{ ua.answer }}</strong></div>
|
||||||
<div class="small text-muted">Answered: {{ ua.created_date }}</div>
|
{% if ua.created_date %}
|
||||||
{% endif %}
|
<div class="small text-muted">Answered: {{ ua.created_date }}</div>
|
||||||
</div>
|
{% endif %}
|
||||||
<div>
|
</div>
|
||||||
{% if ua.answer == question.best_answer %}
|
<div>
|
||||||
<span class="badge bg-success">Correct</span>
|
{% if ua.answer == question.best_answer %}
|
||||||
{% else %}
|
<span class="badge bg-success">Correct</span>
|
||||||
<span class="badge bg-secondary">Incorrect</span>
|
{% else %}
|
||||||
{% endif %}
|
<span class="badge bg-secondary">Incorrect</span>
|
||||||
</div>
|
{% endif %}
|
||||||
</li>
|
</div>
|
||||||
{% endif %}
|
</li>
|
||||||
{% empty %}
|
{% empty %}
|
||||||
<li class="list-group-item">No responses recorded for this exam.</li>
|
<li class="list-group-item">No responses recorded for this exam.</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
|
||||||
|
|||||||
@@ -109,6 +109,14 @@ def exam_review_question_responses(request, pk: int, q_index: int):
|
|||||||
"app_name": "sbas",
|
"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)
|
return render(request, "sbas/partials/exam_review_question_responses_fragment.html", context)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user