# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ Order 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 OrderType(BaseQuestion): """ """ meta_type = 'order_type' security = ClassSecurityInfo() security.declareObjectPublic() implements(IBaseQuestion, IQuestionAuthoring) manage_options = SimpleItem.manage_options def __init__(self, id): self.id=id self.answers={} self.count = {} 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, counter): self.answers = variants self.count = counter security.declareProtected(perm_view_question, 'getAnswers') def getAnswers(self): """ """ return self.answers security.declareProtected(perm_view_question, 'getType') def getType(self): return 'order_type' def getMaxPoints(self): return 1.0 security.declareProtected(perm_edit_question, 'changeQuestion') def changeQuestion(self, REQUEST, add=0): """ """ variants ={} counter = 0 for i in range(1, 100): if REQUEST.has_key('v'+str(i)): m = REQUEST.get('v'+str(i), '') if m != '': counter = counter +1 variants['v'+str(counter)] = REQUEST.get('v'+str(i), '') self.setAnswers(variants, counter) self.setCommonData(REQUEST) self.reindex_object() if add == 1: return REQUEST.RESPONSE.redirect(self.getId()+'/index_html') return REQUEST.RESPONSE.redirect('index_html') def xml2(self, fullURLs=False): """ """ xml = u'\n' xml += '\n' xml=xml+'\n' xml=xml+'\n' i = 1 while i <= self.count: xml=xml+'v'+str(i)+'\n' i = i + 1 xml=xml+'\n' xml=xml+'\n' xml=xml+'\n' xml=xml+'\n' #xml=xml+'

\n' #xml=xml+self.getQuestionname() xml=xml+'\n' xml += '\n\n\n' i=1 while i<=self.count: xml=xml+''+self.answers.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.match_correct() xml=xml+'
\n' #REQUEST.RESPONSE.setHeader("Content-type", "text/xml") return xml security.declareProtected('QTAuthor: View question', 'getInteractionType') def getInteractionType(self): """ """ return "orderInteraction" def importQuestion(self, data, dom): """ """ additionalFiles = {} # questext = data.split('') # questext = questext[1].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) self.setQuestion(questext) variants = {} # correctresponse = dom.getElementsByTagName("correctResponse") # correct = correctresponse[0].getElementsByTagName("value") # correct = correct[0].childNodes[0].data choices = dom.getElementsByTagName("simpleChoice") for i in range(1, len(choices)+1): choiceIdentifier = choices[i-1].getAttribute("identifier") choice = choices[i-1].childNodes choicetext = choice[0].data variants['v'+str(i)] = choicetext.encode('utf-8') counter = len(choices) # 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.setAnswers(variants, counter) self.setWhichImages(questext) return additionalFiles def getQuestionStats(self): """ """ import re table = '' variants = self.getAnswers() query = {'getQuesId': self.getId(),} results = self.cat_answers(query) for var in variants: table += '' table += '' for i in range(1, len(variants)+1): table += '' for j in range(1, len(variants)+1): count = 0 for result in results: ans_obj = result.getObject() ans_send = ans_obj.getAnswerSend() values = re.findall('\.*?\<\/value\>', ans_send) if values != []: value = values[i-1].replace('', '') value = value.replace('', '') val = value.replace('v', '') if int(val) == j: count += 1 table += '' table += '' table += '
Variants/
Positions
'+variants.get(var)+'
'+str(i)+''+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.getAnswers() values = re.findall('\.*?\<\/value\>', ans_send) ordervalues = [] for value in values: value = value.replace('', '') value = value.replace('', '') ordervalues.append(value) for variant in variants: try: position = ordervalues.index(variant) + 1 except: position = '' text += str(position)+';' var = variants.get(variant) if var in array: orderarray = array.get(var) else: orderarray = [] orderarray.append(position) array[var] = orderarray frequencies[ques_id] = array return {'csvtext': text, 'frequencies': frequencies} Globals.InitializeClass(OrderType) # EOF