1
0
Files
medical-notes/stroma/quiz/views/navigate_question_view.py
Johan Dahlin 50366b9b9c
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
vault backup: 2025-12-26 02:09:22
2025-12-26 02:09:22 +01:00

58 lines
1.7 KiB
Python

from django.shortcuts import render, get_object_or_404
from quiz.models import QuizSession, QuizResult
from .get_session_questions_view import get_session_questions
def navigate_question(request, session_id, direction):
"""Navigate to previous/next question"""
session = get_object_or_404(QuizSession, id=session_id, user=request.quiz_user)
# Get current question from session or query params
current_q_id = request.GET.get('q')
all_questions = get_session_questions(session)
all_q_ids = list(all_questions.values_list('id', flat=True))
if current_q_id:
try:
current_index = all_q_ids.index(int(current_q_id))
except (ValueError, IndexError):
current_index = 0
else:
current_index = 0
# Navigate
if direction == 'previous' and current_index > 0:
new_index = current_index - 1
elif direction == 'next' and current_index < len(all_q_ids) - 1:
new_index = current_index + 1
else:
new_index = current_index
question = all_questions.filter(id=all_q_ids[new_index]).first()
# Check if answered
result = QuizResult.objects.filter(
user=request.quiz_user,
quiz_session=session,
question=question
).first()
current_number = new_index + 1 # 1-based numbering
context = {
'question': question,
'session': session,
'show_answer': result is not None,
'has_previous': new_index > 0,
'has_next': new_index < len(all_q_ids) - 1,
'current_number': current_number,
'total_questions': len(all_q_ids),
}
if result:
context['is_correct'] = result.is_correct
return render(request, 'partials/quiz_question.html', context)