Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion quiz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class Quiz(models.Model):
" taken by users who can edit"
" quizzes."))

time_limit = models.SmallIntegerField(
blank=True, default=0,
verbose_name=_("Time Limit"),
help_text=_("In minutes."),
validators=[MaxValueValidator(600)])

def save(self, force_insert=False, force_update=False, *args, **kwargs):
self.url = re.sub('\s+', '-', self.url).lower()

Expand Down Expand Up @@ -507,7 +513,11 @@ def get_questions(self, with_answers=False):
if with_answers:
user_answers = json.loads(self.user_answers)
for question in questions:
question.user_answer = user_answers[str(question.id)]
try:
question.user_answer = user_answers[str(question.id)]
except KeyError:
# quiz time has elapsed
pass

return questions

Expand Down
8 changes: 8 additions & 0 deletions quiz/templates/result.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
{% block description %} {% trans "Exam Results for" %} {{ quiz.title }} {% endblock %}

{% block content %}

{% if quiz.time_limit > 0 and elapsed > quiz.time_limit %}
<p>Exceeded maximum allowed {{ quiz.time_limit }} minutes.</p>
{% endif %}

{% if previous.answers %}

Expand All @@ -18,10 +22,12 @@
</strong>
</p>
{% include 'correct_answer.html' %}
{% if previous.previous_question.explanation %}
<p><strong>{% trans "Explanation" %}:</strong></p>
<div class="well " style="background-color: #fcf8e3;">
<p>{{ previous.previous_question.explanation }}</p>
</div>
{% endif %}
<hr>

{% endif %}
Expand Down Expand Up @@ -84,10 +90,12 @@ <h2>{% trans "Exam results" %}</h2>
<p>{% trans "Your answer" %}: {{ question|answer_choice_to_string:question.user_answer }}</p>
{% endif %}

{% if question.explanation %}
<p><strong>{% trans "Explanation" %}:</strong></p>
<div class="well " style="background-color: #fcf8e3;">
<p>{{ question.explanation|safe }}</p>
</div>
{% endif %}

<hr>

Expand Down
21 changes: 15 additions & 6 deletions quiz/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import datetime

from django.contrib.auth.decorators import login_required, permission_required
from django.core.exceptions import PermissionDenied
Expand Down Expand Up @@ -56,7 +57,7 @@ class CategoriesListView(ListView):

class ViewQuizListByCategory(ListView):
model = Quiz
template_name = 'view_quiz_category.html'
template_name = 'quiz/view_quiz_category.html'

def dispatch(self, request, *args, **kwargs):
self.category = get_object_or_404(
Expand All @@ -80,7 +81,7 @@ def get_queryset(self):


class QuizUserProgressView(TemplateView):
template_name = 'progress.html'
template_name = 'quiz/progress.html'

@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
Expand Down Expand Up @@ -134,7 +135,7 @@ def get_context_data(self, **kwargs):

class QuizTake(FormView):
form_class = QuestionForm
template_name = 'question.html'
template_name = 'quiz/question.html'

def dispatch(self, request, *args, **kwargs):
self.quiz = get_object_or_404(Quiz, url=self.kwargs['quiz_name'])
Expand All @@ -150,7 +151,7 @@ def dispatch(self, request, *args, **kwargs):
self.sitting = self.anon_load_sitting()

if self.sitting is False:
return render(request, 'single_complete.html')
return render(request, 'quiz/single_complete.html')

return super(QuizTake, self).dispatch(request, *args, **kwargs)

Expand All @@ -177,6 +178,10 @@ def form_valid(self, form):
self.form_valid_user(form)
if self.sitting.get_first_question() is False:
return self.final_result_user()
if self.quiz.time_limit > 0:
elapsed = datetime.datetime.now() - self.sitting.start
if elapsed.total_seconds() > self.quiz.time_limit*60:
return self.final_result_user()
else:
self.form_valid_anon(form)
if not self.request.session[self.quiz.anon_q_list()]:
Expand Down Expand Up @@ -222,13 +227,17 @@ def form_valid_user(self, form):
self.sitting.remove_first_question()

def final_result_user(self):
elapsed = datetime.datetime.now() - self.sitting.start
elapsed = int(elapsed.total_seconds()) // 60

results = {
'quiz': self.quiz,
'score': self.sitting.get_current_score,
'max_score': self.sitting.get_max_score,
'percent': self.sitting.get_percent_correct,
'sitting': self.sitting,
'previous': self.previous,
'elapsed': elapsed,
}

self.sitting.mark_quiz_complete()
Expand All @@ -242,7 +251,7 @@ def final_result_user(self):
if self.quiz.exam_paper is False:
self.sitting.delete()

return render(self.request, 'result.html', results)
return render(self.request, 'quiz/result.html', results)

def anon_load_sitting(self):
if self.quiz.single_attempt is True:
Expand Down Expand Up @@ -351,7 +360,7 @@ def final_result_anon(self):

del self.request.session[self.quiz.anon_q_data()]

return render(self.request, 'result.html', results)
return render(self.request, 'quiz/result.html', results)


def anon_session_score(session, to_add=0, possible=0):
Expand Down