class AnswerSchema(ContentSchema): title = colander.SchemaNode( colander.String(), title=_(u'Answering Choice:'), ) correct = colander.SchemaNode( colander.Bool(), title=_(u'Answer Type'), widget=RadioChoiceWidget(values=[ [True, _("Correct Answer")], [False, _("Incorrect Answer")]]) )
class Quiz(Content): """Quiz Content type.""" implements(IDefaultWorkflow) id = Column(Integer, ForeignKey('contents.id'), primary_key=True) def check_answers(self, questions, answers): sumtotal = 0 sumcorrect = 0 questioncorrect = { question.name: [False, [0, 0]] for question in questions } for question in questions: if question.question_type == "text": sumtotal += 1 questioncorrect[question.name][1][1] += 1 if question.name in answers and len( answers[question.name]) > 0: if question.correct_answer == answers[question.name][0]: questioncorrect[question.name][0] = True questioncorrect[question.name][1][0] += 1 sumcorrect += 1 else: answerchoices = question.children # import pdb; pdb.set_trace() for answerchoice in answerchoices: if answerchoice.correct is True: sumtotal += 1 questioncorrect[question.name][1][1] += 1 if question.question_type == "radio": if question.name in answers and len( answers[question.name]) > 0: if answerchoice.title == answers[ question.name][0]: sumcorrect += 1 questioncorrect[question.name][0] = True questioncorrect[question.name][1][0] = 1 else: if question.name in answers: for choice in answers[question.name]: # import pdb; pdb.set_trace() if answerchoice.title == choice: sumcorrect += 1 questioncorrect[ question.name][0] = True questioncorrect[ question.name][1][0] += 1 return { "questioncorrect": questioncorrect, "sumtotal": sumtotal, "sumcorrect": sumcorrect } type_info = Content.type_info.copy( name=u'Quiz', title=_(u'Quiz'), add_view=u'add_quiz', addable_to=[u'Document'], )
class AnswerAddForm(AddFormView): schema_factory = AnswerSchema add = Answer item_type = _(u"Answer") @property def success_url(self): # pragma: no cover return self.request.resource_url(self.context) def save_success(self, appstruct): prevanswers = self.context.children question_type = self.context.question_type if question_type == "text": self.request.session.flash( u'Cannot add answer to freetext question'.format( self.context.title), 'error') raise HTTPFound(location=self.request.resource_url(self.context)) elif question_type == "radio" and appstruct['correct'] is True: for prevanswer in prevanswers: if prevanswer.correct is True: self.request.session.flash( u'Question already has a correct answer'.format( self.context.title), 'error') raise HTTPFound(location=self.request.resource_url( self.context)) super(AnswerAddForm, self).save_success(appstruct)
class QuestionAddForm(AddFormView): schema_factory = QuestionSchema add = Question item_type = _(u"Question") def __init__(self, context, request, **kwargs): super(QuestionAddForm, self).__init__(context, request, **kwargs) kotti_quiz_group.need()
class Answer(Content): """Answer Content type.""" id = Column(Integer, ForeignKey('contents.id'), primary_key=True) correct = Column(Boolean()) type_info = Content.type_info.copy( name=u'Answer', title=_(u'Answer'), add_view=u'add_answer', addable_to=[u'Question'], )
class Question(Content): """Question Content type.""" id = Column(Integer, ForeignKey('contents.id'), primary_key=True) correct_answer = Column(Unicode(256)) question_type = Column(String()) # change the type info to your needs type_info = Content.type_info.copy( name=u'Question', title=_(u'Question'), add_view=u'add_question', addable_to=[u'Quiz'], )
class QuestionSchema(ContentSchema): title = colander.SchemaNode( colander.String(), title=_(u'Question'), ) question_type = colander.SchemaNode( colander.String(), title=_(u'Question Type'), validator=colander.OneOf(["radio", "checkbox", "text"]), widget=RadioChoiceWidget(values=[ ["radio", _("singlechoice")], ["checkbox", _("multiplechoice")], ["text", _("freetext")]]) ) correct_answer = colander.SchemaNode( colander.String(), title=_(u'Correct Answer'), missing=None, default=colander.null )
class QuizAddForm(AddFormView): schema_factory = QuizSchema add = Quiz item_type = _(u"Quiz")
class QuizSchema(ContentSchema): title = colander.SchemaNode( colander.String(), title=_(u'Quiz Title'), )