From c9665a459e21e47b43845025bf1e8df3cdfe5040 Mon Sep 17 00:00:00 2001 From: Tom Walker Date: Tue, 17 Jun 2014 22:15:55 +0100 Subject: [PATCH] worked through views, template tags and templates for true/false question type. Works so far --- multichoice/models.py | 8 +++++++ quiz/models.py | 6 +++--- quiz/templatetags/quiz_tags.py | 9 +++----- quiz/views.py | 24 +++++++++++---------- templates/quiz/answers_for_mc_question.html | 10 +++++++++ templates/quiz/answers_for_question.html | 21 ------------------ templates/quiz/answers_for_tf_question.html | 16 ++++++++++++++ templates/quiz/question.html | 22 ++++++++++++++++++- true_false/models.py | 13 ++++++++++- 9 files changed, 86 insertions(+), 43 deletions(-) create mode 100644 templates/quiz/answers_for_mc_question.html create mode 100644 templates/quiz/answers_for_tf_question.html diff --git a/multichoice/models.py b/multichoice/models.py index 7b82a71..e80d42c 100644 --- a/multichoice/models.py +++ b/multichoice/models.py @@ -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" diff --git a/quiz/models.py b/quiz/models.py index 13c5dca..d505fc8 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -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() diff --git a/quiz/templatetags/quiz_tags.py b/quiz/templatetags/quiz_tags.py index bf8a941..0492f95 100644 --- a/quiz/templatetags/quiz_tags.py +++ b/quiz/templatetags/quiz_tags.py @@ -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): diff --git a/quiz/views.py b/quiz/views.py index 97064b0..cf7dff7 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -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 index 0000000..0a06931 --- /dev/null +++ b/templates/quiz/answers_for_mc_question.html @@ -0,0 +1,10 @@ +{% for answer in answers %} + + + + + +{% endfor %} diff --git a/templates/quiz/answers_for_question.html b/templates/quiz/answers_for_question.html index 527d72f..e69de29 100644 --- a/templates/quiz/answers_for_question.html +++ b/templates/quiz/answers_for_question.html @@ -1,21 +0,0 @@ -
- - - - - - -{% for answer in answers %} - - - -{% endfor %} - - -
- -
- diff --git a/templates/quiz/answers_for_tf_question.html b/templates/quiz/answers_for_tf_question.html new file mode 100644 index 0000000..63c3f94 --- /dev/null +++ b/templates/quiz/answers_for_tf_question.html @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/templates/quiz/question.html b/templates/quiz/question.html index d9ee9b8..5981793 100644 --- a/templates/quiz/question.html +++ b/templates/quiz/question.html @@ -44,7 +44,27 @@

{{ question.id }}

{{ question_type }}

{{ question.content }}

- {% answers_for_question question quiz %} + +
+

quid {{ question.id }}

+ + + + + + {% ifequal question_type 'TF_Question' %} + {% include 'answers_for_tf_question.html' %} + {% endifequal %} + + {% ifequal question_type 'MCQuestion' %} + {% answers_for_mc_question question quiz %} + {% endifequal %} + + +
+ +
+ {% endif %} diff --git a/true_false/models.py b/true_false/models.py index 5ca8d15..f9ca634 100644 --- a/true_false/models.py +++ b/true_false/models.py @@ -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" -- 2.39.5