# -*- coding: utf-8
# -*- Mode: Python; py-indent-offset: 4 -*-
""" Match type """
__version__ = "$Revision:33$"[11:-2]
import Globals
from AccessControl import ClassSecurityInfo
from OFS.SimpleItem import SimpleItem
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.interfaces import IBaseQuestion, IQuestionAuthoring
class MatchType(BaseQuestion):
""" """
meta_type = 'match_type'
security = ClassSecurityInfo()
security.declareObjectPublic()
implements(IBaseQuestion, IQuestionAuthoring)
manage_options = SimpleItem.manage_options
def __init__(self, id):
self.id = id
self.variants = {}
self.uppervariants = {}
self.keys = {}
BaseQuestion.__init__(self)
security.declarePrivate('manage_afterAdd')
def manage_afterAdd(self, item, container):
self.index_object()
security.declareProtected(perm_view_question, 'getType')
def getType(self):
return 'match_type'
security.declareProtected(perm_edit_question, 'setAnswers')
def setAnswers(self, variants, rightanswers, uppervariants, points, counter, counter2):
if variants == {}:
variants = {'v1':'', 'v2':''}
self.variants = variants
if rightanswers == {}:
rightanswers = {'val1':'', 'val2':''}
self.keys = rightanswers
if uppervariants == {}:
uppervariants = {'v1':'', 'v2':'', 'v3':''}
self.uppervariants = uppervariants
if points == {}:
points = {'v1':'', 'v2':''}
self.points = points
self.count = counter
self.count2 = counter2
security.declareProtected(perm_view_question, 'getVariants')
def getVariants(self):
""" """
return self.variants
security.declareProtected(perm_view_question, 'getKeys')
def getKeys(self):
""" """
return self.keys
security.declareProtected(perm_view_question, 'getUpperVariants')
def getUpperVariants(self):
""" """
return self.uppervariants
security.declareProtected(perm_view_question, 'getPoints')
def getPoints(self):
""" """
return self.points
def getMaxPoints(self):
maxpoints = 0
for i in range(1, self.count+1):
maxpoints += float(self.points.get('v'+str(i)))
return maxpoints
security.declareProtected(perm_edit_question, 'changeQuestion')
def changeQuestion(self, REQUEST, add=0):
""" """
counter = 0
counter2 = 0
variants = {}
rightanswers = {}
uppervariants = {}
points = {}
notnr = 1
hasvar = 1
uppervar = 1
markedvars = 1 # 1-marked correct answers 0-haven't marked correct answer for every variant(row)
for i in range(1, 100):
if REQUEST.has_key('variant_v'+str(i)):
m = REQUEST.get('variant_v'+str(i))
if m != '':
counter = counter + 1
variants['v'+str(counter)] = m
hasvar = 0
rightanswer = REQUEST.get('val'+str(i), '')
if rightanswer == '':
markedvars = 0
rightanswers['val'+str(counter)] = rightanswer
kk = REQUEST.get('v'+str(i)+'_points')
if kk == '':
points['v'+str(counter)] = '1'
else:
notnr = self.isitFloat(kk)
points['v'+str(counter)] = REQUEST.get('v'+str(i)+'_points')
if REQUEST.has_key('uppervariant_v'+str(i)):
n = REQUEST.get('uppervariant_v'+str(i))
if n != '':
counter2 = counter2 + 1
uppervariants['v'+str(counter2)] = n
uppervar = 0
self.setAnswers(variants, rightanswers, uppervariants, points, counter, counter2)
self.setCommonData(REQUEST)
self.reindex_object()
if notnr == 1 and hasvar == 0 and uppervar == 0 and markedvars == 1:
if add == 1:
return REQUEST.RESPONSE.redirect(self.getId()+'/index_html')
return REQUEST.RESPONSE.redirect('index_html')
message = []
if hasvar == 1 or uppervar == 1:
message.append("Add choices!")
if markedvars == 0:
message.append("Mark correct answer for every row!")
if notnr == 0:
message.append("Insert correct numbers for points!")
return REQUEST.RESPONSE.redirect(self.id +'/edit?message='+'
'.join(message))
def xml2(self, fullURLs=False):
""" """
xml = u'\n'
xml += '\n'
xml=xml+ '\n'
xml=xml+ '\n'
i = 1
while i <= self.count:
xml=xml+ ''+self.keys.get('val'+str(i))+' var'+str(i)+'\n'
i = i + 1
xml=xml+ '\n'
xml=xml+ '\n'
i = 1
while i <= self.count:
xml=xml+ '\n'
i = i + 1
xml=xml+ '\n'
xml=xml+ '\n'
xml=xml+ '\n'
xml=xml+ '\n'
xml += '\n\n
\n'
xml=xml+ '\n'
xml=xml+ ''
#xml=xml+ self.getQuestionname()
#xml=xml+ self.replaceImageurl(self.getQuestionname())
xml=xml+ ''
xml=xml+ '\n'
i=1
while i<=self.count2:
xml=xml+''+self.uppervariants.get('v'+str(i)).decode('utf-8')+'\n'
i = i +1
xml=xml+ '\n'
xml=xml+ '\n'
i=1
while i<=self.count:
xml=xml+''+self.variants.get('v'+str(i)).decode('utf-8')+'\n'
i = i +1
xml=xml+ '\n'
xml=xml+ '\n'
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(perm_view_question, 'getInteractionType')
def getInteractionType(self):
""" """
return "matchInteraction"
def importQuestion(self, data, dom):
""" """
import re
additionalFiles = {}
questext = data.split('')
questext = questext[1].split(''+uppervariants.get(uvar)+''
table += ''
for v in variants:
table += ''+variants.get(v)+' | '
for uvar in uppervariants:
count = 0
for result in results:
ans_obj = result.getObject()
ans_send = ans_obj.getAnswerSend()
values = re.findall('\.*?\<\/value\>', ans_send)
matchvalues = {}
for value in values:
value = value.replace('', '')
value = value.replace('', '')
value = value.split(' ')
uppervar = value[0]
var = value[1]
var = var.replace('ar', '')
matchvalues[var] = uppervar
if uppervar == uvar and v == var:
count += 1
table += ''+str(count)+' | '
table += '
'
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.getVariants()
values = re.findall('\.*?\<\/value\>', ans_send)
matchvalues = {}
for value in values:
value = value.replace('', '')
value = value.replace('', '')
value = value.split(' ')
uppervar = value[0]
var = value[1]
var = var.replace('ar', '')
matchvalues[var] = uppervar
uppervariants = self.getUpperVariants()
for variant in variants:
if variant in matchvalues:
chosenvar = matchvalues.get(variant)
chosen = uppervariants.get(chosenvar)
text += chosen + ';'
if variant in array:
matcharray = array.get(variant)
else:
matcharray = []
matcharray.append(chosen)
array[variant] = matcharray
else: # if not answered
text += ';'
frequencies[ques_id] = array
return {'csvtext': text, 'frequencies': frequencies}
Globals.InitializeClass(MatchType)
# EOF