From: Richard Mansfield Date: Thu, 7 Aug 2014 05:12:17 +0000 (+1200) Subject: Display the number of questions answered so far & the number remaining X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=ba9932eadbfca173f2f374c25c55c2180c6cd042;p=django_quiz.git Display the number of questions answered so far & the number remaining --- diff --git a/quiz/models.py b/quiz/models.py index 672cd43..bf9759d 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -485,6 +485,15 @@ class Sitting(models.Model): def get_max_score(self): return len(self._question_ids()) + def progress(self): + """ + Returns the number of questions answered so far and the total number of + questions. + """ + answered = len(json.loads(self.user_answers)) + total = self.get_max_score + return (answered, total) + class Question(models.Model): """ diff --git a/quiz/templates/question.html b/quiz/templates/question.html index 273c63d..18d4a09 100644 --- a/quiz/templates/question.html +++ b/quiz/templates/question.html @@ -41,6 +41,12 @@ {% if question %} +{% if progress %} +
+Question {{ progress.0|add:1 }} of {{ progress.1 }} +
+{% endif %} +

Question category: {{ question.category }} diff --git a/quiz/views.py b/quiz/views.py index fa736ec..9d42bcb 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -134,8 +134,10 @@ class QuizTake(FormView): def get_form(self, form_class): if self.logged_in_user: self.question = self.sitting.get_first_question() + self.progress = self.sitting.progress() else: self.question = self.anon_next_question() + self.progress = self.anon_sitting_progress() if self.question.__class__ is Essay_Question: form_class = EssayForm @@ -165,6 +167,8 @@ class QuizTake(FormView): context['quiz'] = self.quiz if hasattr(self, 'previous'): context['previous'] = self.previous + if hasattr(self, 'progress'): + context['progress'] = self.progress return context def form_valid_user(self, form): @@ -257,6 +261,11 @@ class QuizTake(FormView): next_question_id = self.request.session[self.quiz.anon_q_list()][0] return Question.objects.get_subclass(id=next_question_id) + def anon_sitting_progress(self): + total = len(self.request.session[self.quiz.anon_q_data()]['order']) + answered = total - len(self.request.session[self.quiz.anon_q_list()]) + return (answered, total) + def form_valid_anon(self, form): guess = form.cleaned_data['answers'] is_correct = self.question.check_if_correct(guess)