All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
from django.db.models import Q
|
|
from quiz.models import QuizSession, Question, QuizResult
|
|
|
|
|
|
def get_next_question(request, session_id):
|
|
session = get_object_or_404(QuizSession, id=session_id, user=request.quiz_user)
|
|
|
|
answered_ids = QuizResult.objects.filter(
|
|
user=request.quiz_user,
|
|
quiz_session=session
|
|
).values_list('question_id', flat=True)
|
|
|
|
questions = Question.objects.exclude(id__in=answered_ids)
|
|
|
|
# Apply filters from session
|
|
if session.course:
|
|
questions = questions.filter(exam__course=session.course)
|
|
|
|
if session.tags.exists():
|
|
questions = questions.filter(tags__in=session.tags.all())
|
|
|
|
if session.exams.exists():
|
|
questions = questions.filter(exam__in=session.exams.all())
|
|
|
|
if session.question_types:
|
|
q_objs = Q()
|
|
if 'single' in session.question_types:
|
|
q_objs |= ~Q(correct_answer__contains=',')
|
|
if 'multi' in session.question_types:
|
|
q_objs |= Q(correct_answer__contains=',')
|
|
|
|
if q_objs:
|
|
questions = questions.filter(q_objs)
|
|
|
|
questions = questions.distinct()
|
|
next_question = questions.first()
|
|
|
|
if not next_question:
|
|
return render(request, 'partials/complete.html', {'session': session})
|
|
|
|
return render(request, 'partials/question.html', {
|
|
'question': next_question,
|
|
'session': session
|
|
})
|
|
|