# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ TextEntry type """ __version__ = "$Revision:33$"[11:-2] import Globals from AccessControl import ClassSecurityInfo from OFS.SimpleItem import SimpleItem import re 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 TextEntryType(BaseQuestion): """ """ meta_type = 'text_entry_type' security = ClassSecurityInfo() security.declareObjectPublic() implements(IBaseQuestion, IQuestionAuthoring) manage_options = SimpleItem.manage_options def __init__(self, id): self.id=id self.gapvariants = {} self.gappoints = {} self.number = 0 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 'text_entry_type' security.declareProtected(perm_view_question, 'getMaxPoints') def getMaxPoints(self): maxpoints = 0 i = 1 while i < self.gapscount + 1: for j in range(1, 4): if self.gapvariants.get('gap'+str(i)+'_'+str(j)): points=self.gappoints.get('gappoints'+str(i)+'_'+str(j)) if maxpoints < points: maxpoints = points j = j + 1 i += 1 return float(maxpoints) security.declareProtected(perm_edit_question, 'changeQuestion') def changeQuestion(self, REQUEST): """ """ 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 len(message) > 0: return REQUEST.RESPONSE.redirect(self.getId() +'/edit?message='+'
'.join(message)) return REQUEST.RESPONSE.redirect(self.absolute_url()+'/text_entry2.html') 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 setTextGaps(self): txt = self.getQuestion() allgaps = re.findall('\[.*?\]', txt) textgaps = {} i = 1 for gap in allgaps: textgaps['gap'+str(i)] = gap i += 1 self.textgaps = textgaps def getTextGaps(self): return self.textgaps def gaps(self): """ """ txt = self.getQuestion().decode('utf-8') allgaps = re.finditer('\[.*?\]', txt) allgaps1 = re.finditer('\[.*?\]', txt) correctanswer = {} p = None table = '' table += '' gapscount = 0 for some in allgaps1: gapscount = gapscount + 1 self.gapscount = gapscount textgaps = {} gapvariants = self.getVariants() gappoints = self.getPoints() i = 1 for y in allgaps: x = y.group() correctanswer['res'+str(i)] = x[1:-1] textgaps['gap'+str(i)] = x z = y.span() e = 0 if p: e=p.span()[1] table += '' table += '' x = 0 for k in range(1, gapscount + 1): m = correctanswer.get('res'+str(i)) print "gap: ", k print "correctanswer: ", m.encode("utf-8") gapvariant1 = gapvariants.get('gap'+str(k)+'_1') if (gapvariant1 != None): gapvariant1 = gapvariant1.decode('utf-8') #print "gapvariant1: ", gapvariant1 if m == gapvariant1 and x == 0: table += '' table += '' for j in range(2, 4): table += '' table += '' x = 1 if x == 0: table += '' table += '' table += '' table += '' p = y i = i + 1 table += '' table += '
'+self.utranslate('Text')+''+self.utranslate('Choices')+''+self.utranslate('Points')+'
'+txt[e:z[0]]+'
'+y.group()+'1.
'+str(j)+'.
1.
2.
3.
'+txt[z[1]:]+'
' self.textgaps = textgaps return table def add_variants(self, REQUEST): """ save variants and points """ gapvariants = {} gappoints = {} number = 0 for i in range(1, self.gapscount + 1): for j in range(1, 4): if REQUEST.has_key('gap'+str(i)+'_'+str(j)): gapvariant = REQUEST.get('gap'+str(i)+'_'+str(j)) gapvariants['gap'+str(i)+'_'+str(j)] = gapvariant points = REQUEST.get('gappoints'+str(i)+'_'+str(j)) if gapvariant != '' and points == '': points = '1' gappoints['gappoints'+str(i)+'_'+str(j)] = points number += 1 self.number = number self.setVariants(gapvariants, gappoints) # send to waramu if needed self._sendToWaramu() return REQUEST.RESPONSE.redirect(self.absolute_url()) security.declareProtected(perm_edit_question, 'setVariants') def setVariants(self, gapvariants, gappoints): self.gapvariants = gapvariants self.gappoints = gappoints security.declareProtected(perm_view_question, 'getVariants') def getVariants(self): """ """ return self.gapvariants # security.declareProtected(perm_edit_question, 'setPoints') # def setPoints(self, points): # self.gappoints = points security.declareProtected(perm_view_question, 'getPoints') def getPoints(self): """ """ return self.gappoints def setGapsCount(self, count): self.gapscount = count def getGapsCount(self): return self.gapscount security.declareProtected(perm_view_question, 'getGapstext') def getGapstext(self): #text_entry_indexisse: kõik lüngad, variandid ja punktid tabelina """ """ table = u'' table += '' table += '' gapvariants = self.getVariants() gappoints = self.getPoints() gapscount = self.getGapsCount() textgaps = self.getTextGaps() for i in range(1, gapscount+1): try: table += '' except UnicodeEncodeError: table += '' table += '' table += '' for j in range(2, 4): if gapvariants.get('gap'+str(i)+'_'+str(j)) != '' and gapvariants.get('gap'+str(i)+'_'+str(j)) != None: gapvariant = gapvariants.get('gap'+str(i)+'_'+str(j)).decode('utf-8') table += '' table += '' table += '
'+self.utranslate('Gaps')+''+self.utranslate('Choices')+''+self.utranslate('Points')+'
'+textgaps.get('gap'+str(i)).decode('utf-8')+'
'+textgaps.get('gap'+str(i))+'1. '+gapvariants.get('gap'+str(i)+'_1').decode('utf-8')+''+gappoints.get('gappoints'+str(i)+'_1')+'
'+str(j)+'. '+gapvariant+''+gappoints.get('gappoints'+str(i)+'_'+str(j))+'
' return table def xml2(self, fullURLs=False): """ """ xml = u'\n' xml += '\n' i = 1 while i < self.gapscount + 1: j = 1 xml=xml+'\n' xml=xml+'\n' xml=xml+''+self.gapvariants.get('gap'+str(i)+'_1').decode('utf-8')+'\n' xml=xml+'\n' xml=xml+'\n' for j in range(1, 4): if self.gapvariants.get('gap'+str(i)+'_'+str(j)): xml=xml+'\n' j = j + 1 xml=xml+'\n' xml=xml+'\n' xml += '\n' % i i = i + 1 xml=xml+'\n' xml=xml+'

' question = self.getQuestion() if fullURLs: from xml.sax.saxutils import escape question = escape(question) question = self.replace(question) if not fullURLs: question = self.replaceImageurl(question) if not isinstance(question, unicode): question = question.decode('utf-8') xml += question xml=xml+'

' xml=xml+'
\n' xml += '' for i in range(1, self.gapscount + 1): xml += '' xml += '' xml += '' xml += '' % (i, self.getId()) xml += '' xml += '' % i xml += '0.0' xml += '' xml += '' xml += '' xml += '' % i xml += '' % (i, self.getId()) xml += '' xml += '' xml += '' xml += "" xml=xml+'
\n' return xml security.declareProtected(perm_view_question, 'getInteractionType') def getInteractionType(self): """ """ return "textEntryInteraction" def importQuestion(self, data, dom): """ """ additionalFiles = {} questext = data.split('') questext = questext[1].split('') questext = questext[0] images = re.findall('src\=".*?\"', questext) url = self.get_root().absolute_url() for im in images: z = im im = im.split('"') im = im[1] new_img_id = self.random_generator('image_') additionalFiles[im] = new_img_id questext = questext.replace(z, 'src="'+url+'/images/'+new_img_id+'"', 1) # def recukrsiivne(self, element, quetionText=""): # # for node in idemBody.childNodes: # if node is text node: # questionText += node.nodeValue # if node is 'interaction' bla bla: # quesitontext += "[vairant]" # if node is tavaline node: # questionText += self.recurksriine(node) # # return qustionText # itemBody = getElement('itembody') # questonText = self.recukrsiivne(itemBody, "") allvariants = {} gapvariants = {} gappoints = {} rightanswers = [] responseDeclarations = dom.getElementsByTagName("responseDeclaration") for res in responseDeclarations: i = responseDeclarations.index(res) + 1 correctresponse = res.getElementsByTagName("correctResponse") correctanswer = correctresponse[0].getElementsByTagName("value") correct = correctanswer[0].childNodes[0].data rightanswers.append(correct) mapEntries = res.getElementsByTagName("mapEntry") for j in range(1, 4): if len(mapEntries)>=j: mappedValue = mapEntries[j-1].getAttribute("mappedValue") mapKey = mapEntries[j-1].getAttribute("mapKey") else: mappedValue = '' mapKey = '' gappoints['gappoints'+str(i)+'_'+str(j)] = mappedValue.encode('utf-8') gapvariants['gap'+str(i)+'_'+str(j)] = mapKey.encode('utf-8') x = re.findall('\', questext) for k in x: index = x.index(k) correct = rightanswers[index] text = '['+correct.encode('utf-8')+']' questext = questext.replace(k, text, 1) self.setQuestion(questext) self.setTextGaps() self.setGapsCount(len(responseDeclarations)) self.setVariants(gapvariants, gappoints) self.setWhichImages(questext) return additionalFiles def getQuestionStats(self): """ """ table = '' variants = self.getVariants() query = {'getQuesId': self.getId(),} results = self.cat_answers(query) array = {} for result in results: ans_obj = result.getObject() ans_send = ans_obj.getAnswerSend() pairs = re.findall('\.*?\<\/pair\>', ans_send) for pair in pairs: pair_index = pairs.index(pair) cm = re.compile('(.*?)', re.S) val = cm.search(ans_send).group(1) gap = 'gap'+str(pair_index+1) if gap in array: gaparray = array.get(gap) else: gaparray = {'different': [], 'allvariants': []} different = gaparray.get('different') if val not in different: different.append(val) gaparray['different'] = different allvariants = gaparray.get('allvariants') allvariants.append(val) gaparray['allvariants'] = allvariants array[gap] = gaparray for i in range(1, len(array)+1): 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 = {} pairs = re.findall('\.*?\<\/pair\>', ans_send) for pair in pairs: pair_index = pairs.index(pair) cm = re.compile('(.*?)', re.S) val = cm.search(ans_send).group(1) text += val + ';' gap = 'gap'+str(pair_index+1) if gap in array: gaparray = array.get(gap) else: gaparray = {'different': [], 'allvariants': []} different = gaparray.get('different') if val not in different: different.append(val) gaparray['different'] = different allvariants = gaparray.get('allvariants') allvariants.append(val) gaparray['allvariants'] = allvariants array[gap] = gaparray frequencies[ques_id] = array return {'csvtext': text, 'frequencies': frequencies} Globals.InitializeClass(TextEntryType) # EOF
GapsFrequencies
gap'+str(i)+'' gap = array.get('gap'+str(i)) different = gap.get('different') allvariants = gap.get('allvariants') for j in range(1, 4): if variants.get('gap'+str(i)+'_'+str(j)) != None and variants.get('gap'+str(i)+'_'+str(j)) != '': var = variants.get('gap'+str(i)+'_'+str(j)) varcount = allvariants.count(var) table += var + '=' +str(varcount) + '
' if var in different: different.remove(var) for var in different: varcount = allvariants.count(var) table += var + '=' + str(varcount) + '
' table += '