# =========================================================================== # eXe # Copyright 2004-2006, University of Auckland # Copyright 2004-2008 eXe Project, http://eXeLearning.org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # =========================================================================== """ QuizTestBlock can render and process QuizTestIdevices as XHTML """ import logging from exe.webui.block import Block from exe.webui.testquestionelement import TestquestionElement from exe.webui import common log = logging.getLogger(__name__) # =========================================================================== class QuizTestBlock(Block): """ QuizTestBlock can render and process QuizTestIdevices as XHTML """ def __init__(self, parent, idevice): """ Initialize a new Block object """ Block.__init__(self, parent, idevice) self.idevice = idevice self.questionElements = [] self.message = False if not hasattr(self.idevice,'undo'): self.idevice.undo = True i = 0 for question in idevice.questions: self.questionElements.append(TestquestionElement(i, idevice, question)) i += 1 def process(self, request): """ Process the request arguments from the web server """ Block.process(self, request) is_cancel = common.requestHasCancel(request) if ("addQuestion"+unicode(self.id)) in request.args: self.idevice.addQuestion() self.idevice.edit = True # disable Undo once a question has been added: self.idevice.undo = False if "passrate" in request.args \ and not is_cancel: self.idevice.passRate = request.args["passrate"][0] for element in self.questionElements: element.process(request) if ("action" in request.args and request.args["action"][0] == "done" or not self.idevice.edit): self.idevice.isAnswered = True # remove the undo flag in order to reenable it next time: if hasattr(self.idevice,'undo'): del self.idevice.undo for question in self.idevice.questions: if question.correctAns == -2: self.idevice.isAnswered = False self.idevice.edit = True break if "submitScore" in request.args \ and not is_cancel: self.idevice.score = self.__calcScore() if "title"+self.id in request.args \ and not is_cancel: self.idevice.title = request.args["title"+self.id][0] def renderEdit(self, style): """ Returns an XHTML string with the form element for editing this block """ html = "
\n" if not self.idevice.isAnswered: html += common.editModeHeading( _("Please select a correct answer for each question.")) html += common.textInput("title"+self.id, self.idevice.title) html += u"

\n" for element in self.questionElements: html += element.renderEdit() value = _("Add another Question") html += "
" html += common.submitButton("addQuestion"+unicode(self.id), value) html += "

" + _("Select pass rate: ") html += "\n" html += "

" + self.renderEditButtons(undo=self.idevice.undo) html += "
\n" self.idevice.isAnswered = True return html def renderView(self, style, preview=False): """ Returns an XHTML string for viewing this block """ html = u'
\n' html += u'
\n' html += u'\n' html += u'' html += self.idevice.title+'\n' html += u'
\n' html += u'
\n' % self.idevice.passRate for element in self.questionElements: if preview: html += element.renderPreview() + "
" else: html += element.renderView() + "
" html += ' --> \n""" % self.idevice.id return scriptStr def renderJavascriptForScorm(self): """ Return an XHTML string for generating the javascript for scorm export """ scriptStr = '\n""" % self.idevice.passRate return scriptStr def renderPreview(self, style): """ Returns an XHTML string for previewing this block """ html = u"
\n" html += u'\n" html += u"" html += self.idevice.title+"\n" html += u'
\n' for element in self.questionElements: html += element.renderPreview() + "
" html += '
" self.idevice.score = -1 html += u"
\n" html += self.renderViewButtons() html += u"
\n" return html def __calcScore(self): """ Return a score for preview mode. """ rawScore = 0 numQuestion = len(self.questionElements) score = 0 for question in self.idevice.questions: if (question.userAns == question.correctAns): log.info("userAns " +unicode(question.userAns) + ": " + "correctans " +unicode(question.correctAns)) rawScore += 1 if numQuestion > 0: score = rawScore * 100 / numQuestion for question in self.idevice.questions: question.userAns = -1 return score # =========================================================================== """Register this block with the BlockFactory""" from exe.engine.quiztestidevice import QuizTestIdevice from exe.webui.blockfactory import g_blockFactory g_blockFactory.registerBlockType(QuizTestBlock, QuizTestIdevice) # ===========================================================================