From 9ed1252db53695b9d1d67182f2fc4fe98037d04b Mon Sep 17 00:00:00 2001 From: Tom Walker Date: Sun, 20 Jul 2014 17:39:05 +0100 Subject: [PATCH] allowed ability to set pass mark --- quiz/models.py | 20 ++++++++++++++++++++ quiz/templates/result.html | 11 +++++++++++ quiz/tests.py | 19 +++++++++++++++++-- quiz/views.py | 2 ++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/quiz/models.py b/quiz/models.py index e7a286d..5524bb5 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -1,5 +1,8 @@ import re from django.db import models +from django.core.exceptions import ValidationError +from django.core.validators import MaxValueValidator + from model_utils.managers import InheritanceManager @@ -73,6 +76,13 @@ class Quiz(models.Model): " permitted. Non users" " cannot sit this exam.") + pass_mark = models.SmallIntegerField(blank=True, + default=0, + help_text="Percentage required to" + " pass exam.", + validators=[ + MaxValueValidator(100)]) + def save(self, force_insert=False, force_update=False, *args, **kwargs): self.url = re.sub('\s+', '-', self.url).lower() @@ -82,6 +92,9 @@ class Quiz(models.Model): if self.single_attempt is True: self.exam_paper = True + if self.pass_mark > 100: + raise ValidationError(u'%s is above 100' % self.pass_mark) + super(Quiz, self).save(force_insert, force_update, *args, **kwargs) class Meta: @@ -373,6 +386,13 @@ class Sitting(models.Model): """ return filter(None, self.incorrect_questions.split(',')) + @property + def check_if_passed(self): + if self.get_percent_correct >= self.quiz.pass_mark: + return True + else: + return False + class Question(models.Model): """ diff --git a/quiz/templates/result.html b/quiz/templates/result.html index 20e2021..6d3170f 100644 --- a/quiz/templates/result.html +++ b/quiz/templates/result.html @@ -37,6 +37,17 @@ You answered {{ score }} questions correctly out of {{ max_score }}, giving you {{ percent }} percent correct

+ {% if quiz.pass_mark %} + + {% if sitting.check_if_passed %} +

You have passed this exam.

+ {% else %} +

You have not passed this exam.

+ {% endif %} + + + {% endif %} +

Review the questions below and try the exam again in the future.

{% if user.is_authenticated %} diff --git a/quiz/tests.py b/quiz/tests.py index 2ceddaa..815b3d6 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -2,6 +2,7 @@ from django.conf import settings from django.contrib.auth.models import User +from django.core.exceptions import ValidationError from django.core.urlresolvers import resolve from django.http import HttpRequest from django.test import TestCase @@ -91,6 +92,14 @@ class TestQuiz(TestCase): def test_anon_q_list(self): self.assertEqual(self.quiz1.anon_q_list(), '1_q_list') + def test_pass_mark(self): + self.assertEqual(self.quiz1.pass_mark, False) + self.quiz1.pass_mark = 50 + self.assertEqual(self.quiz1.pass_mark, 50) + self.quiz1.pass_mark = 101 + with self.assertRaises(ValidationError): + self.quiz1.save() + class TestProgress(TestCase): def setUp(self): @@ -163,7 +172,8 @@ class TestSitting(TestCase): self.quiz1 = Quiz.objects.create(id=1, title='test quiz 1', description='d1', - url='tq1') + url='tq1', + pass_mark=50) self.question1 = MCQuestion.objects.create(id=1, content='squawk') @@ -195,6 +205,7 @@ class TestSitting(TestCase): def test_scoring(self): self.assertEqual(self.sitting.get_current_score, 0) + self.assertEqual(self.sitting.check_if_passed, False) self.sitting.add_to_score(1) self.assertEqual(self.sitting.get_current_score, 1) @@ -208,6 +219,8 @@ class TestSitting(TestCase): self.assertEqual(self.sitting.get_current_score, 3) self.assertEqual(self.sitting.get_percent_correct, 100) + self.assertEqual(self.sitting.check_if_passed, True) + def test_incorrect_and_complete(self): self.assertEqual(self.sitting.get_incorrect_questions(), []) @@ -451,7 +464,8 @@ class TestQuestionViewsUser(TestCase): title='test quiz 1', description='d1', url='tq1', - category=self.c1) + category=self.c1, + pass_mark=50) self.quiz2 = Quiz.objects.create(id=2, title='test quiz 2', @@ -561,6 +575,7 @@ class TestQuestionViewsUser(TestCase): self.assertEqual(Sitting.objects.count(), 0) self.assertTemplateUsed('result.html') self.assertEqual(response.context['score'], 1) + self.assertContains(response, 'You have passed') def test_quiz_take_user_answer_end(self): self.client.login(username='jacob', password='top_secret') diff --git a/quiz/views.py b/quiz/views.py index f5e22e1..ad04ed3 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -179,6 +179,7 @@ def final_result_user(request, sitting, quiz, previous): 'score': score, 'max_score': max_score, 'percent': percent, + 'sitting': sitting, 'previous': previous}, context_instance=RequestContext(request)) else: @@ -188,6 +189,7 @@ def final_result_user(request, sitting, quiz, previous): 'score': score, 'max_score': max_score, 'percent': percent, + 'sitting': sitting, 'questions': questions, 'incorrect_questions': incorrect}, context_instance=RequestContext(request)) -- 2.39.5