""" Answer object """ __version__ = "$Revision$"[11:-2] import re import Globals from Globals import Acquisition, Persistent from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo from Products.ZCatalog.CatalogPathAwareness import CatalogAware from OFS.SimpleItem import SimpleItem from zope.interface import implements from SOAPpy import SOAPProxy from SOAPpy import Types from SOAPpy.Types import faultType from common import commons from Permissions import * from interfaces import IAnswerObject class AnswerObject(CatalogAware, SimpleItem, Persistent, Explicit, commons): """ answer """ meta_type = 'Answer Object' security = ClassSecurityInfo() security.declareObjectPublic() implements(IAnswerObject) manage_options = SimpleItem.manage_options def __init__(self, id, send, ques): self.id=id self.send = send self.ques = ques self.points = 0.0 self.feedback = '' self.default_catalog = 'cat_answers' security.declarePrivate('manage_afterAdd') def manage_afterAdd(self, item, container): self.index_object() def setPoints(self, val): self.points = val def processUserAnswerUsingR2Q2(self): url = self.getRendererServiceURL() namespace = self.getRendererServiceURL() s = SOAPProxy(url) s.config.debug = 0 s.config.dumpSOAPOut = 0 s.config.dumpSOAPIn = 0 questionId = self.getQuesId() question = questionId + '.xml' quesobj = self.getQuestionById(questionId) quesHtmlUid = quesobj.getHtmlUid() send = self.getAnswerSend() #try: #html = s._ns(namespace).respond(uid=quesHtmlUid, filename=question, values=send) # except UnicodeDecodeError: # send = send.decode('utf-8') # html = s._ns(namespace).nextStage2(uid=quesHtmlUid, values=send) # try: # html = s._ns(namespace).nextStage2(quesHtmlUid, send) # except faultType: #except: # answersended = False # html = quesobj.getPreview() # overall_score = 0 answersended = True send = unicode(send, 'utf-8') try: html = s._ns(namespace).nextStage2(quesobj.getHtmlUid(), send) except faultType: # if for some reason we get 'No processor for GUID' error, then set new uid quesobj._setHtmlUid() html = s._ns(namespace).nextStage2(quesobj.getHtmlUid(), send) # except: # if still there's some error, eg if send has different response id-s, than they should be # answersended = False # html = quesobj.getPreview() # overall_score = 0 if answersended == True: overall_score = html[3].item[1] resp_html = html[2].item[1] import base64 val = base64.decodestring(resp_html) html = val.replace('submit', 'hidden') score = float(overall_score) return {'points': score, 'html': html} def setAnswer(self): questionId = self.getQuesId() quesobj = self.getQuestionById(questionId) send = self.getAnswerSend() if (quesobj.getType() in ["Choice_type", "Choice_multiple"]): answer_data = quesobj.processUserAnswer(send) else: answer_data = self.processUserAnswerUsingR2Q2(send) points = answer_data.get('points') html = answer_data.get('html') self.setFirstPoints(points) self.setPoints(points) self.setAnswerHtml(html) self.reindex_object() url = self.absolute_url() self.cache._addElem(questionId, send, url) def setAnswerHtml(self, val): self.html = val def getQuesId(self): return self.ques def setFirstPoints(self, val): self.firstpoints = val def getFirstPoints(self): return getattr(self, 'firstpoints', self.getPoints()) def getPoints(self): return getattr(self, 'points', 0.0) def getAnswerHtml(self): """ """ html = self.html try: cm = re.compile('(.*?)', re.S) body = cm.search(html).group(1) except: body = html body = body.replace('
', '') body = body.replace('
', '') return body def getAnswerSend(self): return self.send def isCorrect(self): ques_id = self.getQuesId() ques = self.getQuestionById(ques_id) max_points = ques.getMaxPoints() isCorrect = True if self.getFirstPoints() < max_points: isCorrect = False return isCorrect def setFeedback(self, feedback): self.feedback = feedback def getFeedback(self): return getattr(self, 'feedback', '') Globals.InitializeClass(AnswerObject) #EOF