# ===========================================================================
# 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
# ===========================================================================
"""
TrueFalseElement is responsible for a block of question. Used by TrueFalseBlock.
"""
import logging
from exe.webui import common
from exe.webui.element import TextAreaElement
log = logging.getLogger(__name__)
# ===========================================================================
class TrueFalseElement(object):
"""
TrueFalseElement is responsible for a block of question.
Used by TrueFalseBlock.
"""
def __init__(self, index, idevice, question):
"""
Initialize
"""
self.index = index
self.id = unicode(index) + "b" + idevice.id
self.idevice = idevice
self.question = question
# also split out each part for a separate TextAreaElement:
# but first...
# to compensate for the strange unpickling timing when objects are
# loaded from an elp, ensure that proper idevices are set:
if question.questionTextArea.idevice is None:
question.questionTextArea.idevice = idevice
if question.feedbackTextArea.idevice is None:
question.feedbackTextArea.idevice = idevice
if question.hintTextArea.idevice is None:
question.hintTextArea.idevice = idevice
#
self.question_question = TextAreaElement(question.questionTextArea)
self.question_feedback = TextAreaElement(question.feedbackTextArea)
self.question_hint = TextAreaElement(question.hintTextArea)
# note, question.isCorrect is left as it was, and not split out.
# because there are low-level mechanisms in place somewhere
# with the radio buttons or ??? expecting that as such.
self.questionId = "question"+ unicode(index) + "b" + idevice.id
self.question_question.id = self.questionId
self.feedbackId = "feedback" + unicode(index) + "b" + idevice.id
self.question_feedback.id = self.feedbackId
self.hintId = "hint" + unicode(index) + "b" + idevice.id
self.question_hint.id = self.hintId
self.keyId = "Key" + unicode(index) + "b" + idevice.id
def process(self, request):
"""
Process arguments from the web server. Return any which apply to this
element.
"""
log.debug("process " + repr(request.args))
is_cancel = common.requestHasCancel(request)
if self.questionId in request.args \
and not is_cancel:
self.question_question.process(request)
if self.hintId in request.args \
and not is_cancel:
self.question_hint.process(request)
if self.keyId in request.args \
and not is_cancel:
if request.args[self.keyId][0] == "true":
self.question.isCorrect = True
log.debug("question " + repr(self.question.isCorrect))
else:
self.question.isCorrect = False
if self.feedbackId in request.args \
and not is_cancel:
self.question_feedback.process(request)
if "action" in request.args and request.args["action"][0] == self.id:
# before deleting the question object, remove any internal anchors:
for q_field in self.question.getRichTextFields():
q_field.ReplaceAllInternalAnchorsLinks()
q_field.RemoveAllInternalLinks()
self.idevice.questions.remove(self.question)
# disable Undo once a question has been deleted:
self.idevice.undo = False
def renderEdit(self):
"""
Returns an XHTML string for editing this option element
"""
html = self.question_question.renderEdit()
html += _("True") + " "
html += common.option(self.keyId, self.question.isCorrect, "true")
html += _("False") + " "
html += common.option(self.keyId, not self.question.isCorrect, "false")
html += "
\n"
html += common.elementInstruc(self.idevice.keyInstruc)
html += self.question_feedback.renderEdit()
html += self.question_hint.renderEdit()
html += common.submitImage(self.id, self.idevice.id,
"/images/stock-cancel.png",
_("Delete question"))
html += "
\n"
return html
def renderQuestionView(self):
"""
Returns an XHTML string for viewing this question element
"""
is_preview = 0
html = self.renderQuestion(is_preview)
if self.question.hintTextArea.content.strip() != "":
html += u''
html += u'\n '
html += u''
html += u'