"a random order or as they "
"are set?")
+ max_questions = models.PositiveIntegerField(blank=True, null=True,
+ help_text="Number of questions to be "
+ "answered on each attempt")
+
answers_at_end = models.BooleanField(blank=False,
default=False,
help_text="Correct answer is NOT"
question_set = quiz.question_set.all() \
.select_subclasses()
+ if quiz.max_questions and quiz.max_questions < len(question_set):
+ question_set = question_set[:quiz.max_questions]
+
questions = ""
for question in question_set:
questions += str(question.id) + ","
new_sitting = self.create(user=user,
quiz=quiz,
+ question_order=questions,
question_list=questions,
incorrect_questions="",
current_score=0,
Used to store the progress of logged in users sitting a quiz.
Replaces the session system used by anon users.
+ Question_order is a list of integer pks of all the questions in the
+ quiz, in order.
+
Question_list is a list of integers which represent id's of
the unanswered questions in csv format.
quiz = models.ForeignKey(Quiz)
+ question_order = models.CommaSeparatedIntegerField(max_length=1024)
+
question_list = models.CommaSeparatedIntegerField(max_length=1024)
incorrect_questions = models.CommaSeparatedIntegerField(max_length=1024,
def get_current_score(self):
return self.current_score
+ def _question_ids(self):
+ return [int(n) for n in self.question_order.split(',') if n]
+
@property
def get_percent_correct(self):
dividend = float(self.current_score)
- divisor = self.quiz.question_set.all().select_subclasses().count()
+ divisor = len(self._question_ids())
if divisor < 1:
return 0 # prevent divide by zero error
self.user_answers = json.dumps(current)
self.save()
+ def get_questions(self):
+ question_ids = self._question_ids()
+ return sorted(
+ self.quiz.question_set.filter(id__in=question_ids).select_subclasses(),
+ key=lambda q: question_ids.index(q.id))
+
@property
def questions_with_user_answers(self):
output = {}
user_answers = json.loads(self.user_answers)
- for question in self.quiz.question_set.all().select_subclasses():
+ for question in self.get_questions():
output[question] = user_answers[unicode(question.id)]
return output
+ @property
+ def get_max_score(self):
+ return len(self._question_ids())
class Question(models.Model):
"""
results = {
'quiz': self.quiz,
'score': self.sitting.get_current_score,
- 'max_score': self.quiz.get_max_score,
+ 'max_score': self.sitting.get_max_score,
'percent': self.sitting.get_percent_correct,
'sitting': self.sitting,
'previous': self.previous,
self.sitting.mark_quiz_complete()
- if self.quiz.exam_paper is False:
- self.sitting.delete()
-
if self.quiz.answers_at_end:
- results['questions'] = self.quiz.get_questions()
+ results['questions'] = self.sitting.get_questions()
results['incorrect_questions'] =\
self.sitting.get_incorrect_questions
+ if self.quiz.exam_paper is False:
+ self.sitting.delete()
+
return render(self.request, 'result.html', results)
def anon_load_sitting(self):