# -*- coding: utf-8
# -*- Mode: Python; py-indent-offset: 4 -*-
""" Gap Match type """
__version__ = "$Revision:33$"[11:-2]
import Globals
from AccessControl import ClassSecurityInfo
from OFS.SimpleItem import SimpleItem
import re
from random import shuffle
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 GapMatchType(BaseQuestion):
""" """
meta_type = 'gap_match_type'
security = ClassSecurityInfo()
security.declareObjectPublic()
implements(IBaseQuestion, IQuestionAuthoring)
manage_options = SimpleItem.manage_options
def __init__(self, id):
self.id=id
self.variants = {}
self.gapvariants = {}
self.gappoints = {}
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 'gap_match_type'
security.declareProtected(perm_edit_question, 'setOtherVariants')
def setOtherVariants(self, variants):
self.variants = variants
txt = self.variants
othervars = txt.split(',')
othervariants = []
for v in othervars:
v.strip()
if v != "":
othervariants.append(v)
self.othervariants = othervariants
self.otherlength = len(self.othervariants)
security.declareProtected(perm_view_question, 'getOtherVariantsTuple')
def getOtherVariantsTuple(self):
""" """
return self.othervariants
security.declareProtected(perm_edit_question, 'setAllVariants')
def setAllVariants(self): #siia siis kõik variandid kokku ühte listi ja siis shuffleda
""" """
all = []
first = self.getGapvariants()
second = self.getOtherVariantsTuple()
for i in range (0, len(second)):
z = second[i]
all.append(z)
for i in range (1, len(first)+1):
x = first.get('gap'+str(i))
all.append(x)
shuffle(all)
self.allvariants = all
return all
security.declareProtected(perm_view_question, 'getAllVariants')
def getAllVariants(self):
return self.allvariants
def getAnswers(self):
all = {}
first = self.getGapvariants()
second = self.getOtherVariantsTuple()
i = 1
for j in range (1, len(first)+1):
var = first.get('gap'+str(j))
all['v'+str(i)] = var
i += 1
for var in second:
all['v'+str(i)] = var
i += 1
return all
def VariantsTable(self): #variandid tabelisse
""" """
if not hasattr(self, 'allvariants'):
return ""
allvariants = self.getAllVariants()
AllLength = len(allvariants)
table = '
'
return table
security.declareProtected(perm_view_question, 'getVariants')
def getVariants(self):
""" """
return self.variants
security.declareProtected(perm_view_question, 'setGapVariants')
def setGapVariants(self, gapvariants):
self.gapvariants = gapvariants
security.declareProtected(perm_view_question, 'getGapvariants')
def getGapvariants(self):
""" """
return self.gapvariants
security.declareProtected(perm_edit_question, 'setGapPoints')
def setGapPoints(self, gap_points):
self.gappoints = gap_points
security.declareProtected(perm_view_question, 'getGappoints')
def getGappoints(self):
""" """
return getattr(self, 'gappoints', {})
security.declareProtected(perm_view_question, 'getMaxPoints')
def getMaxPoints(self):
maxpoints = 0
gappoints = self.getGappoints()
for i in range(1, len(gappoints)+1):
maxpoints += float(gappoints.get('gappoints'+str(i)))
return maxpoints
security.declareProtected(perm_edit_question, 'changeQuestion')
def changeQuestion(self, REQUEST):
""" """
variants = REQUEST.get('variants')
self.setOtherVariants(variants)
self.setCommonData(REQUEST)
allgaps = re.findall('\[.*?\]', self.getQuestion(REQUEST))
gapscount = len(allgaps)
self.reindex_object()
message = []
# display message if at least one gap [] is empty
for gap in allgaps:
# check is that gap empty
if gap == "[]":
message.append("You have to write correct answer into each gap!")
break;
if gapscount == 0:
message.append("Mark which words you want gapped!")
# if something is wrong
if len(message) > 0:
return REQUEST.RESPONSE.redirect(self.getId() +'/edit?message='+'
'.join(message))
return REQUEST.RESPONSE.redirect(self.getId()+'/gap_match2.html')
def gaps(self):
""" """
txt = self.getQuestion().decode('utf-8')
allgaps = re.finditer('\[.*?\]', txt)
allgaps1 = re.finditer('\[.*?\]', txt)
correctanswer = {}
p = None
table = u''
return table
def add_variants(self, REQUEST):
""" """
# save variants and points
gappoints = {}
gapvariants = self.getGapvariants()
for i in range(1, self.gapscount+1):
if REQUEST.has_key('gap'+str(i)):
gapvariants['gap'+str(i)] = REQUEST.get('gap'+str(i))
points = REQUEST.get('gappoints'+str(i))
if (points == ''):
points = '1'
gappoints['gappoints'+str(i)] = points
self.setAllVariants()
self.setGapVariants(gapvariants)
self.setGapPoints(gappoints)
# send to waramu if needed
self._sendToWaramu()
return REQUEST.RESPONSE.redirect(self.absolute_url())
def replace(self, txt):
x = re.findall('\[.*?\]', txt)
i = 1
for k in x:
txt = txt.replace(k, '', 1)
i = i + 1
return txt
def xml2(self, fullURLs=False):
""" """
xml = u'\n'
xml += '\n'
xml=xml+'\n'
xml=xml+'\n'
gapvariants = self.getGapvariants()
gapscount = len(gapvariants)
for i in range(1, gapscount + 1):
xml=xml+'v'+str(i)+' g'+str(i)+'\n'
xml=xml+'\n'
xml=xml+'\n'
gappoints = self.getGappoints()
for i in range(1, gapscount + 1):
xml=xml+'\n'
xml=xml+'\n'
xml=xml+'\n'
xml=xml+'\n'
xml=xml+'\n'
xml=xml+'\n'
question = self.getQuestion()
if fullURLs:
from xml.sax.saxutils import escape
question = escape(question)
question = self.replace(question)
xml += ''
question = self.fixQuestionText(question)
if not fullURLs:
question = self.replaceImageurl(question)
xml += question
xml += '\n'
j = 1
for i in range(1, gapscount + 1):
xml=xml+''+gapvariants.get('gap'+str(i)).decode('utf-8')+'\n'
j = j + 1
for i in range(1, self.otherlength + 1):
xml=xml+''+self.othervariants[i-1].decode('utf-8')+'\n'
j = j + 1
# question = self.replace(self.getQuestion())
# xml += ''
# xml += self.replaceImageurl(question)
# xml += '
\n'
xml=xml+'\n'
xml=xml+'\n'
xml=xml+ self.map_response()
xml=xml+'\n'
return xml
security.declareProtected('QTAuthor: View question', 'getInteractionType')
def getInteractionType(self):
""" """
return "gapMatchInteraction"
def importQuestion(self, data, dom):
""" """
additionalFiles = {}
questext = data.split('')
questext = questext[-1].split('', '['+variant+']')
gapvariants['gap'+str(i)] = variant
self.setQuestion(questext)
self.setGapVariants(gapvariants)
othervariants = ''
keys = allvariants.keys()
for i in range(0, len(allvariants)):
var = allvariants.get(keys[i])
othervariants += var
if i != len(allvariants)-1:
othervariants += ', '
self.setOtherVariants(othervariants)
mapEntries = dom.getElementsByTagName("mapEntry")
for i in range(1, len(mapEntries)+1):
mappedValue = mapEntries[i-1].getAttribute("mappedValue")
points['gappoints'+str(i)] = mappedValue.encode('utf-8')
self.setGapPoints(points)
self.setAllVariants()
self.setWhichImages(questext)
return additionalFiles
def getQuestionStats(self):
""" """
table = ' | '
allvariants = self.getAnswers()
query = {'getQuesId': self.getId(),}
results = self.cat_answers(query)
for v in allvariants:
table += ''+allvariants.get(v)+' | '
table += '
'
gaps = self.getGapvariants()
for g in gaps:
table += ''+g+' | '
for v in allvariants:
count = 0
for result in results:
ans_obj = result.getObject()
ans_send = ans_obj.getAnswerSend()
values = re.findall('\.*?\<\/value\>', ans_send)
for value in values:
value = value.replace('', '')
value = value.replace('', '')
value = value.split(' ')
var = value[0]
gap = value[1].replace('g', 'gap')
if g == gap 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 = {}
values = re.findall('\.*?\<\/value\>', ans_send)
allvariants = self.getAnswers()
for value in values:
value = value.replace('', '')
value = value.replace('', '')
value = value.split(' ')
var = value[0]
chosen = allvariants.get(var)
text += chosen + ';'
gap = value[1].replace('g', 'gap')
if gap in array:
gaparray = array.get(gap)
else:
gaparray = []
gaparray.append(chosen)
array[gap] = gaparray
frequencies[ques_id] = array
return {'csvtext': text, 'frequencies': frequencies}
Globals.InitializeClass(GapMatchType)
# EOF