# -*- coding: utf-8
# -*- Mode: Python; py-indent-offset: 4 -*-
""" Choice Multiple type"""
__version__ = "$Revision:33$"[11:-2]
import Globals
from AccessControl import ClassSecurityInfo
from OFS.SimpleItem import SimpleItem
from cStringIO import StringIO
from zope.interface import implements
from Products.QTAuthor.common import commons
from Products.QTAuthor.BaseQuestion import BaseQuestion
from Products.QTAuthor.Permissions import *
from Products.QTAuthor.utils import get_video_dimensions
from Products.QTAuthor.interfaces import IBaseQuestion, IQuestionAuthoring
class ChoiceMultiple(BaseQuestion):
""" """
meta_type = 'Choice_multiple'
security = ClassSecurityInfo()
security.declareObjectPublic()
implements(IBaseQuestion, IQuestionAuthoring)
manage_options = SimpleItem.manage_options
def __init__(self, id):
self.id = id
self.answers = {}
self.keys = {}
self.points = {}
self.video_id = ''
self.video_dimensions = None
BaseQuestion.__init__(self)
security.declarePrivate('manage_afterAdd')
def manage_afterAdd(self, item, container):
self.index_object()
security.declareProtected(perm_edit_question, 'setAnswers')
def setAnswers(self, variants, rightanswers, counter):
if variants == {}:
variants = {'v1':'', 'v2':''}
self.answers = variants
self.keys = rightanswers
self.count = counter
security.declareProtected(perm_edit_question, 'setPoints')
def setPoints(self, points):
if points == {}:
points = {'v1':'', 'v2':''}
self.points = points
security.declareProtected(perm_view_question, 'getPoints')
def getPoints(self):
""" """
return self.points
security.declareProtected(perm_view_question, 'getAnswers')
def getAnswers(self):
""" """
return self.answers#['val']
security.declareProtected(perm_view_question, 'getKeys')
def getKeys(self):
return self.keys
security.declareProtected(perm_view_question, 'getType')
def getType(self):
return 'Choice_multiple'
security.declareProtected(perm_view_question, 'getVideoId')
def getVideoId(self):
if not hasattr(self, 'video_id'):
self.video_id = ''
return self.video_id
def setMaxPoints(self):
j = 1
y = 0
while j<=self.count:
if self.getPoints().get('v'+str(j)) != None:
x = self.getPoints().get('v'+str(j))
nr = float(x.replace('v', ''))
if nr>0:
y = y + nr
j = j + 1
self.maxpoints = y
def getMaxPoints(self):
return getattr(self,'maxpoints',self.setMaxPoints())
security.declareProtected(perm_edit_question, 'changeQuestion')
def changeQuestion(self, REQUEST, add=0):
""" """
hasVideo = REQUEST.has_key('video')
if hasVideo:
self.video_id = REQUEST.get('video', '')
variants = {}
rightanswers ={}
points = {}
counter = 0
notnr = 1
markedRight = 1
hasvariants = 1
for i in range(1, 100):
if REQUEST.has_key('v'+str(i)):
m = REQUEST.get('v'+str(i), '')
if m != '':
hasvariants = 0
counter = counter + 1
variants['v'+str(counter)] = REQUEST.get('v'+str(i), '')
varpoints = REQUEST.get('v'+str(i)+'_points', '')
if REQUEST.has_key('v'+str(i)+'_true'):
rightanswers['v'+str(counter)] = ('v'+str(counter))
markedRight = 0
if varpoints == '':
varpoints = '1'
if varpoints == '':
points['v'+str(counter)] = '0'
else:
if notnr == 1:
notnr = self.isitFloat(varpoints)
points['v'+str(counter)] = varpoints
self.setAnswers(variants, rightanswers, counter)
self.setPoints(points)
self.setCommonData(REQUEST)
self.reindex_object()
if notnr == 1 and markedRight == 0:
if add == 1:
return REQUEST.RESPONSE.redirect(self.getId()+'/index_html')
return REQUEST.RESPONSE.redirect('index_html')
message = []
if hasvariants == 1:
message.append('Add choices!')
if hasvariants == 0 and markedRight == 1:
message.append('Mark correct choices!')
if hasvariants == 0 and notnr == 0:
message.append('Insert correct numbers for points!')
if not hasVideo:
return REQUEST.RESPONSE.redirect(self.id +'/edit?message='+' '.join(message))
if hasVideo:
return REQUEST.RESPONSE.redirect(self.getId()+'/edit?video='+self.video_id+'&message='+' '.join(message))
def getVideoDimensions(self):
if not hasattr(self, 'video_dimensions') or self.video_dimensions == None:
vid = getattr(self.videos, self.getVideoId())
contents = StringIO()
data = vid.data
if type(data) is type(''):
contents.write(data)
else:
while data is not None:
contents.write(data.data)
data = data.next
contents.seek(0)
width, height = get_video_dimensions('mov', contents.read())
self.video_dimensions = {'width':width, 'height':height}
return self.video_dimensions
def xml2(self, fullURLs=False):
""" """
video_id = self.getVideoId()
if video_id != '':
dims = self.getVideoDimensions()
xml = u'\n'
xml += '\n'
xml=xml+ '\n'
xml=xml+ '\n'
i=1
while i<=self.count:
if self.getKeys().get('v'+str(i)) != None:
xml=xml+''+self.getKeys().get('v'+str(i))+'\n'
i = i +1
xml=xml+'\n'
#calculating upperbound
j = 1
y = 0
while j<=self.count:
if self.getPoints().get('v'+str(j)) != None:
x = self.getPoints().get('v'+str(j))
nr = float(x.replace('v', ''))
if nr>0:
y = y + nr
j = j + 1
xml=xml+'\n'
i=1
while i<=self.count:
if self.getPoints().get('v'+str(i)) != None:
xml=xml+'\n'
i = i + 1
xml=xml+'\n'
xml=xml+'\n'
xml=xml+'\n'
xml=xml+'\n'
xml += '
\n\n
\n'
if video_id != '':
xml += '\n'
xml=xml+'\n'
#xml=xml+''
i=1
while i<=self.count:
xml=xml+''+self.getAnswers().get('v'+str(i)).decode('utf-8')+'\n'
i = i +1
xml=xml+'\n'
xml=xml+'\n'
xml=xml+ self.map_response()
xml=xml+'\n'
#REQUEST.RESPONSE.setHeader("Content-type", "text/xml")
return xml
security.declareProtected('QTAuthor: View question', 'getInteractionType')
def getInteractionType(self):
""" """
return "choiceInteraction"
def importQuestion(self, data, dom):
""" """
import re
additionalFiles = {}
questext = data.split('')
questext = questext[1].split(''+variants.get(var)+''
count = 0
for result in results:
ans_obj = result.getObject()
ans_send = ans_obj.getAnswerSend()
values = re.findall('\.*?\<\/value\>', ans_send)
for val in values:
val = val.replace('', '')
val = val.replace('', '')
if val == var:
count += 1
table += '
'+str(count)+'
'
table += ''
return table
def getCSVstats(self, ans_send, frequencies):
import re
text = ""
ques_id = self.getId()
if ques_id in frequencies:
array = frequencies.get(ques_id)
else:
array = {}
variants = self.getAnswers()
values = re.findall('\.*?\<\/value\>', ans_send)
multiple_values = []
for val in values:
i = values.index(val)
val = val.replace('', '')
val = val.replace('', '')
multiple_values.append(val)
values[i] = val
values.sort()
for var in variants:
if var in values:
text += "1;"
mval = 1
else:
text += "0;"
mval = 0
if var in array:
multiarray = array.get(var)
else:
multiarray = []
multiarray.append(mval)
array[var] = multiarray
frequencies[ques_id] = array
return {'csvtext': text, 'frequencies': frequencies}
def calculateUserAnswerPoints(self, user_answers):
ques_points = self.getPoints()
points = 0.0
for user_answer in user_answers:
points += float(ques_points.get(user_answer))
if points < 0:
return 0.0
return points
def generateUserAnswerHtml(self, user_answers):
questiontext = self.getQuestion()
responsename = 'RESPONSE_' + self.getId()
html = "
"
html += "
%s
" % self.getTitle()
html += questiontext
html += "
"
variants = self.getAnswers()
for var in variants:
html += "
"
html += ""
html += ""
html += ""
html += "%s" % variants.get(var)
html += "
"
html += "
"
html += "
"
return html
def processUserAnswer(self, answer_send):
import re
values = re.findall('\.*?\<\/value\>', answer_send)
user_answers = []
for val in values:
val = val.replace('', '')
val = val.replace('', '')
val = val.strip()
if val != '':
user_answers.append(val)
points = self.calculateUserAnswerPoints(user_answers)
html = self.generateUserAnswerHtml(user_answers)
return {'points': points, 'html': html}
Globals.InitializeClass(ChoiceMultiple)
# EOF