# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ Associate 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 AssociateType(BaseQuestion): """ """ meta_type = 'associate_type' security = ClassSecurityInfo() security.declareObjectPublic() implements(IBaseQuestion, IQuestionAuthoring) manage_options = SimpleItem.manage_options def __init__(self, id): self.id=id self.answers = {} self.keys = {} self.points = {} 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, variants1, variants2, counter): self.answers = variants1 self.keys = variants2 self.loend = counter security.declareProtected(perm_edit_question, 'setPoints') def setPoints(self, values): self.points = values security.declareProtected(perm_view_question, 'getPoints') def getPoints(self): """ """ return self.points security.declareProtected(perm_view_question, 'getAnswers') def getAnswers(self): """ """ return self.answers security.declareProtected(perm_view_question, 'getKeys') def getKeys(self): """ """ return self.keys security.declareProtected(perm_view_question, 'getType') def getType(self): return 'associate_type' def getMaxPoints(self): maxpoints = 0 for i in range(1, self.loend+1): maxpoints += float(self.getPoints().get('v'+str(i))) return maxpoints def getAllVariants(self): variants1 = self.getAnswers() variants2 = self.getKeys() allvariants = {} j = 1 k = 1 i = 1 while i<=len(variants1)*2: if i % 2 == 1: allvariants['v'+str(i)] = variants1.get('v'+str(j)) j += 1 if i % 2 == 0: allvariants['v'+str(i)] = variants2.get('v'+str(k)) k += 1 i += 1 return allvariants security.declareProtected(perm_edit_question, 'changeQuestion') def changeQuestion(self, REQUEST, add=0): """ """ variants1 = {'v1':'',} variants2 ={'v1':''} points = {'v1': ''} counter = 0 notnr = 1 hasvar = 1 haspair = 0 for i in range(1, 100): if REQUEST.has_key('v'+str(i)): m = REQUEST.get('v'+str(i), '') if m != '': hasvar = 0 counter = counter + 1 variants1['v'+str(counter)] = REQUEST.get('v'+str(i), '') if REQUEST.has_key('v'+str(i)+'_pair'): variants2['v'+str(counter)] = REQUEST.get('v'+str(i)+'_pair', '') if REQUEST.get('v'+str(i)+'_pair') == '': haspair = 1 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') self.setAnswers(variants1, variants2, counter) self.setPoints(points) self.setCommonData(REQUEST) self.reindex_object() if notnr == 1 and hasvar == 0 and haspair == 0: if add == 1: return REQUEST.RESPONSE.redirect(self.getId()+'/index_html') return REQUEST.RESPONSE.redirect('index_html') message = [] if hasvar == 1: message.append("Add pairs!") if hasvar == 0 and haspair == 1: message.append("Add a pair for all!") if hasvar == 0 and 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 j = 1 while i <= self.loend: xml=xml + 'v'+str(j)+' v'+str(j+1)+'\n' j = j + 2 i = i + 1 xml=xml + '\n' xml=xml + '\n' i = 1 j = 1 while i<=self.loend: xml=xml + '\n' j = j + 2 i = i +1 xml=xml + '\n' xml=xml + '\n' xml=xml + '\n' xml=xml + '\n' # xml=xml + '

' # xml=xml+ self.replaceImageurl(self.getQuestion()) # xml=xml + '

\n' xml += '\n' xml += '\n\n\n' i=1 j=1 k=1 while i<=self.loend*2: xml=xml + '' if i % 2 == 1: xml = xml + self.getAnswers().get('v'+str(j)).decode('utf-8') j = j + 1 if i % 2 == 0: xml = xml + self.getKeys().get('v'+str(k)).decode('utf-8') k = k + 1 i = i +1 xml= xml +'\n' xml=xml + '\n' xml=xml + '
\n' xml=xml + self.map_response() xml=xml + '
\n' return xml security.declareProtected(perm_view_question, 'getInteractionType') def getInteractionType(self): """ """ return "associateInteraction" def importQuestion(self, data, dom): """ """ import re additionalFiles = {} questext = data.split('') questext = questext[1].split('' for var in variants1: table += ''+variants1.get(var)+'' table += '' array = {} for result in results: ans_obj = result.getObject() ans_send = ans_obj.getAnswerSend() values = re.findall('\.*?\<\/value\>', ans_send) i = 1 for v in variants1: v_pair = 0 var_pairs = [] v = 'v'+str(i) for val in values: val = val.replace('', '') val = val.replace('', '') var = val.split(' ') var1 = var[0] var2 = var[1] if var1==v or var2==v: if var1 == v: variant = allvariants.get(var2) if var2 == v: variant = allvariants.get(var1) var_pairs.append(variant) v_pair = 1 if v in array: vararray = array.get(v) else: vararray = [] vararray.append(variant) array[v] = vararray i += 2 for var in array: table += ''+allvariants.get(var)+'' varanswers = array.get(var) for v in variants2: variant = variants2.get(v) count = varanswers.count(variant) table += ''+str(count)+'' for v in variants1: variant = variants1.get(v) count = varanswers.count(variant) 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 = {} variants1 = self.getAnswers() variants2 = self.getKeys() values = re.findall('\.*?\<\/value\>', ans_send) allvariants = self.getAllVariants() i = 1 for v in variants1: v_pair = 0 var_pairs = [] v = 'v'+str(i) for val in values: val = val.replace('', '') val = val.replace('', '') var = val.split(' ') var1 = var[0] var2 = var[1] print "asso var1: ", var1 print "asso var2: ", var2 if var1==v or var2==v: if var1 == v: variant = allvariants.get(var2) if var2 == v: variant = allvariants.get(var1) if variant != None: var_pairs.append(variant) v_pair = 1 if v in array: vararray = array.get(v) else: vararray = [] vararray.append(variant) array[v] = vararray if v_pair == 1: text += '' print "associate var_pairs: ", var_pairs for p in var_pairs: text += p if var_pairs.index(p)+1 != len(var_pairs): text += ', ' text += ';' if v_pair == 0: text += '-;' i += 2 frequencies[ques_id] = array return {'csvtext': text, 'frequencies': frequencies} Globals.InitializeClass(AssociateType) # EOF