Implement HTMX-based loading of exam question responses and add corresponding view and URL routing

This commit is contained in:
Ross
2025-11-09 22:01:57 +00:00
parent c89c14b89e
commit ae720662f7
4 changed files with 75 additions and 32 deletions
@@ -109,39 +109,19 @@
<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>
{# Show responses given during this exam for the question #}
{# Responses partial: load on demand via HTMX. Button fetches partial into #responses-container #}
<div class="mt-3">
<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 %}
<li class="list-group-item d-flex justify-content-between align-items-start">
<div>
{% if ua.user %}
<strong>{{ ua.user.get_full_name|default:ua.user.username }}</strong>
{% else %}
<strong>Anonymous</strong>
{% endif %}
<div>Answer: <strong>{{ ua.answer }}</strong></div>
{% if ua.created_date %}
<div class="small text-muted">Answered: {{ ua.created_date }}</div>
{% endif %}
</div>
<div>
{% if ua.answer == question.best_answer %}
<span class="badge bg-success">Correct</span>
{% else %}
<span class="badge bg-secondary">Incorrect</span>
{% endif %}
</div>
</li>
{% endif %}
{% empty %}
<li class="list-group-item">No responses recorded for this exam.</li>
{% endfor %}
</ul>
</div>
<div class="d-flex justify-content-between align-items-center">
<div>
<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>
</div>
</div>
<div class="mt-2" id="responses-container">
<div class="text-muted small">Responses are hidden — click “Show responses” to load.</div>
</div>
<div class="d-flex justify-content-between mt-3">
{% if prev_index is not None %}
@@ -0,0 +1,33 @@
{# Partial: responses for a question within an exam. Rendered server-side or via HTMX. #}
<div id="responses-container">
<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 %}
<li class="list-group-item d-flex justify-content-between align-items-start">
<div>
{% if ua.user %}
<strong>{{ ua.user.get_full_name|default:ua.user.username }}</strong>
{% else %}
<strong>Anonymous</strong>
{% endif %}
<div>Answer: <strong>{{ ua.answer }}</strong></div>
{% if ua.created_date %}
<div class="small text-muted">Answered: {{ ua.created_date }}</div>
{% endif %}
</div>
<div>
{% if ua.answer == question.best_answer %}
<span class="badge bg-success">Correct</span>
{% else %}
<span class="badge bg-secondary">Incorrect</span>
{% endif %}
</div>
</li>
{% endif %}
{% empty %}
<li class="list-group-item">No responses recorded for this exam.</li>
{% endfor %}
</ul>
</div>
+5
View File
@@ -117,6 +117,11 @@ urlpatterns = [
),
path("search/", views.question_search_page, name="question_search_page"),
path("search/questions/", views.question_search_partial, name="question_search_partial"),
path(
"exam/<int:pk>/review/<int:q_index>/responses",
views.exam_review_question_responses,
name="exam_review_question_responses",
),
path("question/<int:pk>/toggle_frcr/", views.toggle_frcr_appropriate, name="toggle_frcr"),
path("category/merge/", views.merge_category, name="merge_category"),
#path(
+25
View File
@@ -87,6 +87,31 @@ from .forms import (
)
@login_required
def exam_review_question_responses(request, pk: int, q_index: int):
"""Return the responses partial for a given exam question (HTMX-friendly).
pk: exam pk
q_index: question index within the exam
"""
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": "sbas",
}
return render(request, "sbas/partials/exam_review_question_responses_fragment.html", context)
class AuthorOrCheckerRequiredMixin(object):
def get_object(self, *args, **kwargs):
obj = super().get_object(*args, **kwargs)