]> git.parisson.com Git - django_quiz.git/commitdiff
Display the number of questions answered so far & the number remaining
authorRichard Mansfield <richard@dragonfly.co.nz>
Thu, 7 Aug 2014 05:12:17 +0000 (17:12 +1200)
committerRichard Mansfield <richard@dragonfly.co.nz>
Thu, 7 Aug 2014 05:12:17 +0000 (17:12 +1200)
quiz/models.py
quiz/templates/question.html
quiz/views.py

index 672cd43ae0e0539c8b37a6d677bbb816f077b4c3..bf9759d6aae9b2a9ce99c99044194afa59598347 100644 (file)
@@ -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):
     """
index 273c63d9aa34d27e4ad626b69c3fbddb5e4cb029..18d4a099a1ed09a3a404c7bf07ae5432edf47b0a 100644 (file)
 
 {% if question %}
 
+{% if progress %}
+<div style="float: right;">
+Question {{ progress.0|add:1 }} of {{ progress.1 }}
+</div>
+{% endif %}
+
 <p>
   <small class="muted">Question category:</small>
   <strong>{{ question.category }}</strong>
index fa736ecf7ebe688b068979cde2260a5ceb584ea1..9d42bcb61cfbf6806441289202f1460dd10f64c1 100644 (file)
@@ -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)