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 quiz_question(request, session_id): """Get current question in quiz mode""" session = get_object_or_404(QuizSession, id=session_id, user=request.quiz_user) # Get all questions for this session all_questions = get_session_questions(session) # Get answered questions answered_ids = QuizResult.objects.filter( user=request.quiz_user, quiz_session=session ).values_list('question_id', flat=True) # Get unanswered questions unanswered = all_questions.exclude(id__in=answered_ids) # Default to first unanswered question, or first question if all answered if unanswered.exists(): question = unanswered.first() show_answer = False else: # All answered, show first question question = all_questions.first() if question: result = QuizResult.objects.filter( user=request.quiz_user, quiz_session=session, question=question ).first() show_answer = result is not None else: return render(request, 'partials/complete.html', {'session': session}) # Calculate navigation all_q_ids = list(all_questions.values_list('id', flat=True)) current_index = all_q_ids.index(question.id) if question.id in all_q_ids else 0 current_number = current_index + 1 # 1-based numbering context = { 'question': question, 'session': session, 'show_answer': show_answer, 'has_previous': current_index > 0, 'has_next': current_index < len(all_q_ids) - 1, 'current_number': current_number, 'total_questions': len(all_q_ids), } if show_answer: result = QuizResult.objects.get( user=request.quiz_user, quiz_session=session, question=question ) context['is_correct'] = result.is_correct return render(request, 'partials/quiz_question.html', context)