summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrabhu Ramachandran2018-05-09 19:19:29 +0530
committerGitHub2018-05-09 19:19:29 +0530
commit31764f78f091122a745a7da2d98e18160cb35eba (patch)
tree7ad6bca66e23ab3c18ea41bfa68e128c38936cbc
parenta7b0c232991208625564f76a56d078e9391c5cb2 (diff)
parentf5fa97802447808744cfc591cc3855a9b57e6d32 (diff)
downloadonline_test-31764f78f091122a745a7da2d98e18160cb35eba.tar.gz
online_test-31764f78f091122a745a7da2d98e18160cb35eba.tar.bz2
online_test-31764f78f091122a745a7da2d98e18160cb35eba.zip
Merge pull request #460 from ankitjavalkar/fix-attempt-error-msg
Display appropriate error when time between attempts condition is not satisfied
-rw-r--r--yaksh/models.py15
-rw-r--r--yaksh/test_models.py61
-rw-r--r--yaksh/views.py5
3 files changed, 72 insertions, 9 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index d011bb0..3010a3c 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -317,7 +317,7 @@ class Quiz(models.Model):
attempts_allowed = models.IntegerField(default=1, choices=attempts)
time_between_attempts = models.FloatField(
- "Time Between Quiz Attempts in hours"
+ "Time Between Quiz Attempts in hours", default=0.0
)
is_trial = models.BooleanField(default=False)
@@ -1356,11 +1356,18 @@ class QuestionPaper(models.Model):
)
if last_attempt:
time_lag = (timezone.now() - last_attempt.start_time).total_seconds() / 3600
- return time_lag >= self.quiz.time_between_attempts
+ can_attempt = time_lag >= self.quiz.time_between_attempts
+ msg = "You cannot start the next attempt for this quiz before {0} hour(s)".format(
+ self.quiz.time_between_attempts
+ ) if not can_attempt else None
+ return can_attempt, msg
else:
- return True
+ return True, None
else:
- return False
+ msg = "You cannot attempt {0} quiz more than {1} time(s)".format(
+ self.quiz.description, self.quiz.attempts_allowed
+ )
+ return False, msg
def create_demo_quiz_ppr(self, demo_quiz, user):
question_paper = QuestionPaper.objects.create(quiz=demo_quiz,
diff --git a/yaksh/test_models.py b/yaksh/test_models.py
index 41730c3..3bf8a00 100644
--- a/yaksh/test_models.py
+++ b/yaksh/test_models.py
@@ -630,6 +630,15 @@ class QuestionPaperTestCases(unittest.TestCase):
# All active questions
self.questions = Question.objects.filter(active=True, user=self.user)
self.quiz = Quiz.objects.get(description="demo quiz 1")
+ self.quiz_with_time_between_attempts = Quiz.objects.create(
+ description="demo quiz with time between attempts",
+ start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ end_date_time=datetime(2199, 10, 9, 10, 8, 15, 0, tzinfo=pytz.utc),
+ duration=30, active=True,
+ attempts_allowed=3, time_between_attempts=1.0,
+ pass_criteria=0,
+ instructions="Demo Instructions"
+ )
# create question paper with only fixed questions
self.question_paper_fixed_questions = QuestionPaper.objects.create(
@@ -657,6 +666,12 @@ class QuestionPaperTestCases(unittest.TestCase):
shuffle_questions=True
)
+ self.question_paper_with_time_between_attempts = QuestionPaper.objects.create(
+ quiz=self.quiz_with_time_between_attempts,
+ total_marks=0.0,
+ shuffle_questions=True
+ )
+
self.question_paper.fixed_question_order = "{0}, {1}".format(
self.questions[3].id, self.questions[5].id
)
@@ -784,8 +799,10 @@ class QuestionPaperTestCases(unittest.TestCase):
answerpaper.passed = True
answerpaper.save()
# test can_attempt_now(self):
- self.assertFalse(self.question_paper.can_attempt_now(self.user,
- self.course.id))
+ result = (False, u'You cannot attempt demo quiz 1 quiz more than 1 time(s)')
+ self.assertEquals(
+ self.question_paper.can_attempt_now(self.user, self.course.id), result
+ )
# trying to create an answerpaper with same parameters passed.
answerpaper2 = self.question_paper.make_answerpaper(self.user, self.ip,
attempt_num,
@@ -794,6 +811,46 @@ class QuestionPaperTestCases(unittest.TestCase):
self.assertEqual(answerpaper, answerpaper2)
+ def test_time_between_attempt(self):
+ """ Test make_answerpaper() method of Question Paper"""
+ already_attempted = self.attempted_papers.count()
+ attempt_num = 1
+
+ self.first_start_time = timezone.now()
+ self.first_end_time = self.first_start_time + timedelta(minutes=20)
+ self.second_start_time = self.first_start_time + timedelta(minutes=30)
+ self.second_end_time = self.second_start_time + timedelta(minutes=20)
+
+ # create answerpaper
+ self.first_answerpaper = AnswerPaper(
+ user=self.user,
+ question_paper=self.question_paper_with_time_between_attempts,
+ start_time=self.first_start_time,
+ end_time=self.first_end_time,
+ user_ip=self.ip,
+ course=self.course,
+ attempt_number=attempt_num
+ )
+ self.first_answerpaper.passed = True
+ self.first_answerpaper.save()
+
+ self.second_answerpaper = AnswerPaper(
+ user=self.user,
+ question_paper=self.question_paper_with_time_between_attempts,
+ start_time=self.second_start_time,
+ end_time=self.second_end_time,
+ user_ip=self.ip,
+ course=self.course,
+ attempt_number=attempt_num + 1
+ )
+ self.second_answerpaper.passed = True
+ self.second_answerpaper.save()
+
+ result = (False, u'You cannot start the next attempt for this quiz before 1.0 hour(s)')
+ self.assertEquals(
+ self.question_paper_with_time_between_attempts.can_attempt_now(self.user, self.course.id), result
+ )
+
def test_create_trial_paper_to_test_quiz(self):
qu_list = [str(self.questions_list[0]), str(self.questions_list[1])]
diff --git a/yaksh/views.py b/yaksh/views.py
index 1cb77fc..5ebe0b5 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -534,9 +534,8 @@ def start(request, questionpaper_id=None, attempt_num=None, course_id=None,
previous_question=last_attempt.current_question()
)
# allowed to start
- if not quest_paper.can_attempt_now(user, course_id):
- msg = "You cannot attempt {0} quiz more than {1} time(s)".format(
- quest_paper.quiz.description, quest_paper.quiz.attempts_allowed)
+ if not quest_paper.can_attempt_now(user, course_id)[0]:
+ msg = quest_paper.can_attempt_now(user, course_id)[1]
if is_moderator(user):
return prof_manage(request, msg=msg)
return view_module(request, module_id=module_id, course_id=course_id,