# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ Hottext 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 HotTextType(BaseQuestion): """ """ meta_type = 'hottext_type' security = ClassSecurityInfo() security.declareObjectPublic() implements(IBaseQuestion, IQuestionAuthoring) manage_options = SimpleItem.manage_options def __init__(self, id): self.id=id 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 'hottext_type' security.declareProtected(perm_edit_question, 'setVariants') def setVariants(self, variants): self.gapvariants = variants security.declareProtected(perm_view_question, 'getVariants') def getVariants(self): """ """ return self.gapvariants security.declareProtected(perm_edit_question, 'setCorrectAnswer') def setCorrectAnswer(self, correct): self.gappoints = correct security.declareProtected(perm_view_question, 'getCorrectAnswer') def getCorrectAnswer(self): """ """ return self.gappoints security.declareProtected(perm_edit_question, 'setGapsCount') def setGapsCount(self, count): self.gapscount = count security.declareProtected(perm_view_question, 'getGapsCount') def getGapsCount(self): """ """ return self.gapscount security.declareProtected(perm_view_question, 'getMaxPoints') def getMaxPoints(self): return 1.0 security.declareProtected(perm_edit_question, 'changeQuestion') def changeQuestion(self, REQUEST): """ """ self.setCommonData(REQUEST) self.reindex_object() allgaps = re.findall('\[.*?\]', self.getQuestion(REQUEST)) gapscount = len(allgaps) 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 choice into each square brackets!") break; if gapscount == 0: message.append("Mark which words you want to make choices!") # if something is wrong if len(message) > 0: return REQUEST.RESPONSE.redirect(self.absolute_url() +'/edit?message='+'
'.join(message)) return REQUEST.RESPONSE.redirect(self.absolute_url()+'/hottext2.html') def gaps(self): """ """ txt = self.getQuestion().decode('utf-8') allgaps = re.finditer('\[.*?\]', txt) allgaps1 = re.finditer('\[.*?\]', txt) p = None table = u'' table += '' gapscount = 0 for some in allgaps1: gapscount = gapscount + 1 self.setGapsCount(gapscount) textgaps = {} variants = self.getVariants() correctanswer = self.getCorrectAnswer() i = 1 for y in allgaps: x = y.group() var = 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): if (variants.get('gap'+str(k))): variant = variants.get('gap'+str(k)).decode('utf-8') if var == variant and x == 0: table += '' if correctanswer.get('gap') == 'v'+str(k): table += '' else: table += '' x = 1 if x == 0: table += '' table += '' p = y i += 1 table += '' table += '
'+self.utranslate('Text')+''+self.utranslate('Gaps')+'
'+txt[e:z[0]]+'
'+y.group()+'
'+txt[z[1]:]+'
' self.textgaps = textgaps return table def add_variants(self, REQUEST): """ """ # save variants and right answer correct = {} variants = {} for i in range(1, self.gapscount + 1): if REQUEST.has_key('gap'+str(i)): variants['gap'+str(i)] = REQUEST.get('gap'+str(i)) if REQUEST.has_key('gap_true'): correct['gap'] = REQUEST.get('gap_true') else: return self.restrictedTraverse('hottext2.html')(message='Mark correct answer!') self.setVariants(variants) self.setCorrectAnswer(correct) # send to waramu self._sendToWaramu() return REQUEST.RESPONSE.redirect(self.absolute_url()) def replace(self, txt): x = re.findall('\[.*?\]', txt) i = 1 for k in x: var = k[1:-1] txt = txt.replace(k, ''+var+'', 1) i = i + 1 return txt def ques_text(self): #hottexti indexisse küsimus ilusti.. nagu ta hiljem enam-vähem on txt = self.getQuestion() rightone = self.getCorrectAnswer().get('gap') x = re.findall('\[.*?\]', txt) i = 1 for k in x: var = k[1:-1] if(str(rightone) == 'v'+str(i)): txt = txt.replace(k, ''+var+'', 1) else: txt = txt.replace(k, ''+var+'', 1) i = i + 1 return txt def xml2(self, fullURLs=False): """ """ xml = u'\n' xml += '\n' xml += '\n' xml += '\n' xml += '' if self.getCorrectAnswer().has_key('gap'): xml += self.getCorrectAnswer().get('gap') xml += '\n' xml += '\n' xml += '\n' xml += '\n' xml += '\n' xml += '0\n' xml += '\n' xml += '\n' xml += '\n' xml += '\n' xml += '

\n' question = self.getQuestion() if fullURLs: from xml.sax.saxutils import escape question = escape(question) question = self.replace(question) question = self.fixQuestionText(question) if not fullURLs: question = self.replaceImageurl(question) xml += question xml += '

\n' xml += '
\n' xml += '
\n' xml += self.match_correct() xml += '
\n' return xml security.declareProtected(perm_view_question, 'getInteractionType') def getInteractionType(self): """ """ return "hottextInteraction" def importQuestion(self, data, dom): """ """ additionalFiles = {} cm = re.compile('(.*?)', re.S) questext = cm.search(data).group(1) #prompt = dom.getElementsByTagName("prompt") 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) variants = {} choices = dom.getElementsByTagName("hottext") for i in range(1, len(choices)+1): var_id = choices[i-1].getAttribute("identifier") vartext = choices[i-1].childNodes[0].data variants['gap'+str(i)] = vartext.encode('utf-8') self.setVariants(variants) x = re.findall('\', questext) for k in x: index = x.index(k) vartext = variants.get('gap'+str(index+1)) text = '['+vartext+']' questext = questext.replace(k, text, 1) self.setQuestion(questext) correctanswer = {} correctresponse = dom.getElementsByTagName("correctResponse") correct = correctresponse[0].getElementsByTagName("value") correct = correct[0].childNodes[0].data correctanswer['gap'] = correct.encode('utf-8') self.setCorrectAnswer(correctanswer) # ques_feedback_true = '' # ques_feedback_false = '' # modalfeedbacks = dom.getElementsByTagName("modalFeedback") # for feedback in modalfeedbacks: # showhide = feedback.getAttribute("showHide") # if showhide == "show": # ques_feedback_true = feedback.childNodes[0].data # if showhide == "hide": # ques_feedback_false = feedback.childNodes[0].data self.setWhichImages(questext) return additionalFiles def getQuestionStats(self): """ """ import re table = '' variants = self.getVariants() query = {'getQuesId': self.getId(),} results = self.cat_answers(query) for var in variants: table += '' count = 0 for result in results: ans_obj = result.getObject() ans_send = ans_obj.getAnswerSend() cm = re.compile('(.*?)', re.S) val = cm.search(ans_send).group(1) val = val.replace('v', 'gap') if val == var: count += 1 table += '' table += '
VariantsFrequencies
'+variants.get(var)+''+str(count)+'
' 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() cm = re.compile('(.*?)', re.S) cm_results = cm.search(ans_send) chosen = "" if cm_results: val = cm_results.group(1) val = val.replace('v', 'gap') chosen = variants.get(val) array.append(chosen) text += chosen + ';' frequencies[ques_id] = array return {'csvtext': text, 'frequencies': frequencies} Globals.InitializeClass(HotTextType) # EOF