2 from karmaworld.apps.quizzes.models import Keyword
5 class BaseQuizQuestion(object):
7 def question_type(self):
8 return self.__class__.__name__
11 class MultipleChoiceQuestion(BaseQuizQuestion):
12 def __init__(self, question_text, choices):
13 self.question_text = question_text
14 self.choices = choices
16 def __unicode__(self):
17 return u"Multiple choice: {0}: {1}".format(self.question_text, ", ".join(map(str, self.choices)))
26 class MultipleChoiceOption(object):
27 def __init__(self, text, correct):
29 self.correct = correct
31 def __unicode__(self):
41 class TrueFalseQuestion(BaseQuizQuestion):
42 def __init__(self, question_text, true):
43 self.question_text = question_text
46 def __unicode__(self):
47 return u"True or false: {0}".format(self.question_text)
56 KEYWORD_MULTIPLE_CHOICE = 1
57 DEFINITION_MULTIPLE_CHOICE = 2
58 KEYWORD_DEFINITION_TRUE_FALSE = 3
59 GENERATED_QUESTION_TYPE = (
60 KEYWORD_MULTIPLE_CHOICE,
61 DEFINITION_MULTIPLE_CHOICE,
62 KEYWORD_DEFINITION_TRUE_FALSE,
65 MULTIPLE_CHOICE_CHOICES = 4
68 def _create_keyword_multiple_choice(keyword, keywords):
69 choices = [MultipleChoiceOption(text=keyword.word, correct=True)]
71 for other_keyword in random.sample(keywords.exclude(id=keyword.id), MULTIPLE_CHOICE_CHOICES - 1):
72 choices.append(MultipleChoiceOption(
73 text=other_keyword.word,
76 question_text = u'Pick the keyword to match "{d}"'.format(d=keyword.definition)
78 return MultipleChoiceQuestion(question_text, choices)
81 def _create_definition_multiple_choice(keyword, keywords):
82 choices = [MultipleChoiceOption(text=keyword.definition, correct=True)]
84 for other_keyword in random.sample(keywords.exclude(id=keyword.id), MULTIPLE_CHOICE_CHOICES - 1):
85 choices.append(MultipleChoiceOption(
86 text=other_keyword.definition,
89 question_text = u'Pick the definition to match "{w}"'.format(w=keyword.word)
91 return MultipleChoiceQuestion(question_text, choices)
94 def _create_keyword_definition_true_false(keyword, keywords):
95 true = random.choice((True, False))
98 definition = keyword.definition
100 other_keyword = random.choice(keywords.exclude(id=keyword.id))
101 definition = other_keyword.definition
103 question_text = u'The keyword "{w}" matches the definition "{d}"'. \
104 format(w=keyword.word, d=definition)
106 return TrueFalseQuestion(question_text, true)
109 def quiz_from_keywords(note):
110 keywords = Keyword.objects.filter(note=note)
113 if len(keywords) < 4:
116 for keyword in keywords:
117 if keyword.word and keyword.definition:
118 question_type = random.choice(GENERATED_QUESTION_TYPE)
120 if question_type is KEYWORD_MULTIPLE_CHOICE:
121 questions.append(_create_keyword_multiple_choice(keyword, keywords))
123 elif question_type is DEFINITION_MULTIPLE_CHOICE:
124 questions.append(_create_definition_multiple_choice(keyword, keywords))
126 elif question_type is KEYWORD_DEFINITION_TRUE_FALSE:
127 questions.append(_create_keyword_definition_true_false(keyword, keywords))