3 from karmaworld.apps.quizzes.models import Keyword
6 class BaseQuizQuestion(object):
8 def question_type(self):
9 return self.__class__.__name__
12 class MultipleChoiceQuestion(BaseQuizQuestion):
13 def __init__(self, question_text, choices):
14 self.question_text = question_text
15 self.choices = choices
17 def __unicode__(self):
18 return u"Multiple choice: {0}: {1}".format(self.question_text, ", ".join(map(str, self.choices)))
27 class MultipleChoiceOption(object):
28 def __init__(self, text, correct):
30 self.correct = correct
32 def __unicode__(self):
42 class TrueFalseQuestion(BaseQuizQuestion):
43 def __init__(self, question_text, true):
44 self.question_text = question_text
47 def __unicode__(self):
48 return u"True or false: {0}".format(self.question_text)
57 class MatchingQuestion(BaseQuizQuestion):
58 def __init__(self, question_text, left_column, right_column, left_right_mapping):
59 self.question_text = question_text
60 self.left_column = left_column
61 self.right_column = right_column
62 self.left_right_mapping = left_right_mapping
65 return zip(self.left_column, self.right_column)
67 def __unicode__(self):
68 return u"Matching question: {0} / {1}".format(self.left_column, self.right_column)
77 KEYWORD_MULTIPLE_CHOICE = 1
78 DEFINITION_MULTIPLE_CHOICE = 2
79 KEYWORD_DEFINITION_TRUE_FALSE = 3
80 KEYWORD_DEFINITION_MATCHING = 4
81 GENERATED_QUESTION_TYPE = (
82 KEYWORD_MULTIPLE_CHOICE,
83 DEFINITION_MULTIPLE_CHOICE,
84 KEYWORD_DEFINITION_TRUE_FALSE,
85 KEYWORD_DEFINITION_MATCHING,
88 MULTIPLE_CHOICE_CHOICES = 4
91 def _create_keyword_multiple_choice(keyword, keywords):
92 choices = [MultipleChoiceOption(text=keyword.word, correct=True)]
94 for other_keyword in random.sample(keywords.exclude(id=keyword.id), MULTIPLE_CHOICE_CHOICES - 1):
95 choices.append(MultipleChoiceOption(
96 text=other_keyword.word,
99 question_text = u'Pick the keyword to match "{d}"'.format(d=keyword.definition)
101 random.shuffle(choices)
103 return MultipleChoiceQuestion(question_text, choices)
106 def _create_definition_multiple_choice(keyword, keywords):
107 choices = [MultipleChoiceOption(text=keyword.definition, correct=True)]
109 for other_keyword in random.sample(keywords.exclude(id=keyword.id), MULTIPLE_CHOICE_CHOICES - 1):
110 choices.append(MultipleChoiceOption(
111 text=other_keyword.definition,
114 question_text = u'Pick the definition to match "{w}"'.format(w=keyword.word)
116 random.shuffle(choices)
118 return MultipleChoiceQuestion(question_text, choices)
121 def _create_keyword_definition_true_false(keyword, keywords):
122 true = random.choice((True, False))
125 definition = keyword.definition
127 other_keyword = random.choice(keywords.exclude(id=keyword.id))
128 definition = other_keyword.definition
130 question_text = u'The keyword "{w}" matches the definition "{d}"'. \
131 format(w=keyword.word, d=definition)
133 return TrueFalseQuestion(question_text, true)
136 def _create_keyword_definition_matching(keyword, keywords):
137 question_keywords = [keyword]
138 question_keywords.extend(random.sample(keywords.exclude(id=keyword.id), MULTIPLE_CHOICE_CHOICES - 1))
140 answer_mapping = {k.word: k.definition for k in question_keywords}
141 word_column = [k.word for k in question_keywords]
142 random.shuffle(word_column)
143 definition_column = [k.definition for k in question_keywords]
144 random.shuffle(definition_column)
146 question_text = u'Match the words with their definitions'
148 return MatchingQuestion(question_text, left_column=word_column,
149 right_column=definition_column, left_right_mapping=answer_mapping)
152 def quiz_from_keywords(note):
153 keywords = Keyword.objects.filter(note=note).exclude(word__iexact='').exclude(definition__iexact='')
156 if len(keywords) < 4:
159 for keyword in keywords:
160 if keyword.word and keyword.definition:
161 question_type = random.choice(GENERATED_QUESTION_TYPE)
163 if question_type is KEYWORD_MULTIPLE_CHOICE:
164 questions.append(_create_keyword_multiple_choice(keyword, keywords))
166 elif question_type is DEFINITION_MULTIPLE_CHOICE:
167 questions.append(_create_definition_multiple_choice(keyword, keywords))
169 elif question_type is KEYWORD_DEFINITION_TRUE_FALSE:
170 questions.append(_create_keyword_definition_true_false(keyword, keywords))
172 elif question_type is KEYWORD_DEFINITION_MATCHING:
173 questions.append(_create_keyword_definition_matching(keyword, keywords))