]> git.parisson.com Git - django_quiz.git/commitdiff
worked through views, template tags and templates for true/false question type. Works...
authorTom Walker <tomwalker0472@gmail.com>
Tue, 17 Jun 2014 21:15:55 +0000 (22:15 +0100)
committerTom Walker <tomwalker0472@gmail.com>
Tue, 17 Jun 2014 21:15:55 +0000 (22:15 +0100)
multichoice/models.py
quiz/models.py
quiz/templatetags/quiz_tags.py
quiz/views.py
templates/quiz/answers_for_mc_question.html [new file with mode: 0644]
templates/quiz/answers_for_question.html
templates/quiz/answers_for_tf_question.html [new file with mode: 0644]
templates/quiz/question.html
true_false/models.py

index 7b82a712be0b83773854031aa17e5336605c9e76..e80d42c5e59113ba7dbe0e6489060026bb85b63c 100644 (file)
@@ -4,6 +4,14 @@ from quiz.models import Quiz, Category, Question
 
 class MCQuestion(Question):
 
+    def check_if_correct(self, guess):
+        answer = Answer.objects.get(id = guess)
+
+        if answer.correct == True:
+            return True
+        else:
+            return False
+
     class Meta:
         verbose_name = "Multiple Choice Question"
         verbose_name_plural = "Multiple Choice Questions"
index 13c5dca5f68d263ebc3eb3a65e2e06f0abec413c..d505fc88c5a09cc901a3fa5e547f5f85a85d9aeb 100644 (file)
@@ -245,8 +245,8 @@ class Progress(models.Model):
 
     def update_score(self, category_queried, score_to_add, possible_to_add):
         """
-        Pass in category, amount to increase score and max possible
-        increase if all were correct.
+        Pass in category, amount to increase score and max possible.
+        Increase if all were correct.
 
         Does not return anything.
         """
@@ -372,7 +372,7 @@ class Sitting(models.Model):
             self.save()
 
     def add_to_score(self, points):
-        present_score = self.get_current_score()
+        present_score = int(self.get_current_score())
         updated_score = present_score + int(points)
         self.current_score = updated_score
         self.save()
index bf8a9411484816e6e696b273ea254887b4bb91a9..0492f959be410a1a06609e114f5b679d8a08f184 100644 (file)
@@ -3,13 +3,10 @@ from multichoice.models import Answer
 
 register = template.Library()
 
-@register.inclusion_tag('answers_for_question.html', takes_context=True)
-def answers_for_question(context, question, quiz):
-    """
-    Displays the possible answers to a question
-    """
+@register.inclusion_tag('answers_for_mc_question.html', takes_context=True)
+def answers_for_mc_question(context, question, quiz):
     answers = Answer.objects.filter(question__id=question.id).order_by('?')
-    return {'answers': answers, 'quiz': quiz}
+    return {'answers': answers,}
 
 @register.inclusion_tag('correct_answer.html', takes_context=True)
 def correct_answer(context, previous):
index 97064b0b4196ef88cfe4955c6c08020035071904..cf7dff7b2e6ee4cd3b261e1d54186b24a3017807 100644 (file)
@@ -109,8 +109,8 @@ def load_anon_next_question(request, quiz):
         #  if there has been a previous question
         #  returns a dictionary with previous question details
         previous = question_check_anon(request, quiz)
-
-        request.session[str(quiz.id)+ "_q_list"] = question_list[1:]
+        question_list = question_list[1:]
+        request.session[str(quiz.id)+ "_q_list"] = question_list
         request.session['page_count'] = request.session['page_count'] + 1
 
     if not request.session[str(quiz.id)+ "_q_list"]:
@@ -172,7 +172,7 @@ def user_load_next_question(request, sitting, quiz):
     # except KeyError:
     #     request.session['page_count'] = 0
 
-    next_question = Question.objects.get_subclass(id = next_question_id)
+    next_question = Question.objects.get_subclass(id = question_ID)
     question_type = next_question.__class__.__name__
 
     return render_to_response('question.html',
@@ -250,10 +250,11 @@ def final_result_user(request, sitting, previous):
 
 def question_check_anon(request, quiz):
     guess = request.GET['guess']
-    answer = Answer.objects.get(id = guess)
-    question = answer.question  #  the id of the question
+    question_id = request.GET['question_id']
+    question = Question.objects.get_subclass(id = question_id)
+    is_correct = question.check_if_correct(guess)
 
-    if answer.correct == True:
+    if is_correct == True:
         outcome = "correct"
         current = request.session[str(quiz.id) + "_score"]
         request.session[str(quiz.id) + "_score"] = int(current) + 1
@@ -264,7 +265,7 @@ def question_check_anon(request, quiz):
         anon_session_score(request, 0, 1)
 
     if quiz.answers_at_end != True:
-        return {'previous_answer': answer,
+        return {'previous_answer': guess,
                 'previous_outcome': outcome,
                 'previous_question': question,}
 
@@ -274,10 +275,11 @@ def question_check_anon(request, quiz):
 
 def question_check_user(request, quiz, sitting):
     guess = request.GET['guess']
-    answer = Answer.objects.get(id = guess)
-    question = answer.question
+    question_id = request.GET['question_id']
+    question = Question.objects.get_subclass(id = question_id)
+    is_correct = question.check_if_correct(guess)
 
-    if answer.correct == True:
+    if is_correct == True:
         outcome = "correct"
         sitting.add_to_score(1)
         user_progress_score_update(request, question.category, 1, 1)
@@ -287,7 +289,7 @@ def question_check_user(request, quiz, sitting):
         user_progress_score_update(request, question.category, 0, 1)
 
     if quiz.answers_at_end != True:
-        return {'previous_answer': answer,
+        return {'previous_answer': guess,
                 'previous_outcome': outcome,
                 'previous_question': question,}
     else:
diff --git a/templates/quiz/answers_for_mc_question.html b/templates/quiz/answers_for_mc_question.html
new file mode 100644 (file)
index 0000000..0a06931
--- /dev/null
@@ -0,0 +1,10 @@
+{% for answer in answers %}
+<tr>
+  <td>
+       <label>
+         <input type="radio" name="guess" value="{{ answer.id }}" class="form-radio">
+         {{ answer.content }}
+       </label>
+  </td>
+</tr>
+{% endfor %}
index 527d72ff83ba48826796d5f71ac94405a57dc280..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
@@ -1,21 +0,0 @@
-<form action="/{{ quiz.url }}/take/" method="get">
-
-       
-<table class="table table-hover table-bordered" id="answerchoice">
-<tbody>
-
-
-{% for answer in answers %}
-       <tr>
-               <td>
-                       <label>
-                               <input type="radio" name="guess" value="{{ answer.id }}" class="form-radio">
-                               {{ answer.content }}
-                       </label>
-               </td>
-    </tr>
-{% endfor %}
-</form>
-</tbody>
-</table>
-<input type="submit" value="Check" class="btn btn-large btn-block btn-warning" >
diff --git a/templates/quiz/answers_for_tf_question.html b/templates/quiz/answers_for_tf_question.html
new file mode 100644 (file)
index 0000000..63c3f94
--- /dev/null
@@ -0,0 +1,16 @@
+<tr>
+  <td>
+       <label>
+         <input type="radio" name="guess" value="T" class="form-radio">
+         True
+       </label>
+  </td>
+</tr>
+<tr>
+  <td>
+       <label>
+         <input type="radio" name="guess" value="F" class="form-radio">
+         False
+       </label>
+  </td>
+</tr>
index d9ee9b8972162c92ae282b1663efe9b506f8a523..59817930adb5d226fe3d4205beb15f1bea74cab7 100644 (file)
                <p>{{ question.id }}</p>
                <p>{{ question_type }}</p>
         <p class="lead">{{ question.content }}</p>
-               {% answers_for_question question quiz %}
+
+               <form action="{% url 'quiz_question' quiz.url %}" method="get">
+                 <p>quid {{ question.id }}</p>
+                 <input type=hidden name="question_id" value="{{ question.id }}">
+
+                 <table class="table table-hover table-bordered" id="answerchoice">
+                       <tbody>
+
+                         {% ifequal question_type 'TF_Question' %}
+                             {% include 'answers_for_tf_question.html' %}
+                         {% endifequal %}
+
+                         {% ifequal question_type 'MCQuestion' %}
+                         {% answers_for_mc_question question quiz %}
+                         {% endifequal %}
+
+                       </tbody>
+                 </table>
+                 <input type="submit" value="Check" class="btn btn-large btn-block btn-warning" >
+               </form>
+
 
 {% endif %}
 
index 5ca8d159d3fe93f80dece99452dacc53a2ca1750..f9ca634078c89378c91df5b5c8799b43d0f9d7f3 100644 (file)
@@ -6,7 +6,18 @@ class TF_Question(Question):
                                   default = False,
                                   help_text = ("Tick this if the question " +
                                                "is true. Leave it blank for" +
-                                               "false."),)
+                                               " false."),)
+
+    def check_if_correct(self, guess):
+        if guess == "T":
+            guess_bool = True
+        else:
+            guess_bool = False
+
+        if guess_bool == self.correct:
+            return True
+        else:
+            return False
 
     class Meta:
         verbose_name = "True/False Question"