# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ Choice question 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 ChoiceType(BaseQuestion): """ """ meta_type = 'Choice_type' security = ClassSecurityInfo() security.declareObjectPublic() implements(IBaseQuestion, IQuestionAuthoring) def __init__(self, id): self.id=id self.answers={} self.keys={} self.feedback={} 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, rightanswers, counter, feedbacks, ques_feedback_true, ques_feedback_false): if variants == {}: variants = {'v1':'', 'v2':''} self.answers = variants self.keys = rightanswers self.count = counter if feedbacks == {}: feedbacks = {'v1':'', 'v2':''} self.feedback = feedbacks self.setQuesfeedTrue(ques_feedback_true) self.quesfeed_false = ques_feedback_false 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, 'getFeedback') def getFeedback(self): """ """ return self.feedback def isFeed(self): """ """ self.feed = '' for i in range(1, self.count + 1): x = self.feedback.get('v'+str(i)) if x != '': self.feed = x return self.feed security.declareProtected(perm_view_question, 'getFeed') def getFeed(self): """ """ return self.feed security.declareProtected(perm_view_question, 'getQuesfeedTrue') def getQuesfeedTrue(self): """ """ return self.quesfeed_true security.declareProtected(perm_edit_question, 'setQuesfeedTrue') def setQuesfeedTrue(self, val): self.quesfeed_true = val security.declareProtected(perm_view_question, 'getQuesfeedFalse') def getQuesfeedFalse(self): """ """ return self.quesfeed_false security.declareProtected(perm_view_question, 'getType') def getType(self): return 'Choice_type' def getMaxPoints(self): return 1.0 security.declareProtected(perm_edit_question, 'changeQuestion') def changeQuestion(self, REQUEST, add=0): """ """ var_count = int(REQUEST.get('js_helper')) variants = {} rightanswers ={} feedbacks = {} counter = 0 markedRight = 1 hasvariants = 1 for i in range(1, var_count+1): if REQUEST.has_key('v'+str(i)): m = REQUEST.get('v'+str(i), '') if m != '': hasvariants = 0 counter += 1 variants['v'+str(counter)] = REQUEST.get('v'+str(i), '') feedbacks['v'+str(counter)] = REQUEST.get('feedback_v'+str(i), '') if REQUEST.has_key('val'): rightanswers['val'] = REQUEST.get('val') v = REQUEST.get('val') numb = v.replace('v', '') if int(numb) <= counter: markedRight = 0 ques_feedback_true = REQUEST.get('ques_feedback_true', '') ques_feedback_false = REQUEST.get('ques_feedback_false', '') self.setAnswers(variants, rightanswers, counter, feedbacks, ques_feedback_true, ques_feedback_false) self.setCommonData(REQUEST) self.reindex_object() if hasvariants == 0 and markedRight == 0: if add == 1: return REQUEST.RESPONSE.redirect(self.getId()+'/index_html') return REQUEST.RESPONSE.redirect('index_html') message = [] if hasvariants == 1: message.append('Add choices!') if hasvariants == 0 and markedRight == 1: message.append('Mark correct choice!') return REQUEST.RESPONSE.redirect(self.id +'/edit?message='+''.join(message)) def isitFeedback(self): """ """ for x in self.feedback.values(): if x: return 1 if self.getQuesfeedTrue() != '' or self.quesfeed_false != '': return 1 return 0 def xml2(self, fullURLs=False): """ """ xml = u'\n' xml += '\n' xml=xml+'\n' xml=xml+'\n' xml += '' if self.getKeys().has_key('val'): xml += self.getKeys().get('val') xml += '\n' xml=xml+'\n' xml=xml+'\n' xml=xml+'\n' xml=xml+'\n' xml=xml+'0\n' xml=xml+'\n' xml=xml+'\n' if self.isitFeedback() == 1: xml=xml+ '\n' xml=xml+'\n' xml += '\n\n\n' xml += '\n' #xml += ''+self.replaceImageurl(self.getQuestion())+'' for i in range(1, self.count+1): xml += '\n' xml += '\n' if self.feedback.get('v'+str(i)) != '': xml=xml+'' xml=xml+self.feedback.get('v'+str(i)).decode('utf-8') xml=xml+'' xml=xml+'\n' xml=xml+'\n' xml=xml+'\n' if self.isitFeedback() == 1 and self.keys.has_key('val'): xml=xml+ self.match_correct_feedback() if self.getQuesfeedTrue() != '': xml=xml + '' xml=xml + self.getQuesfeedTrue().decode('utf-8')+'\n' if self.quesfeed_false != '': xml=xml+ '' xml=xml +self.getQuesfeedFalse().decode('utf-8')+'\n' else: xml += self.match_correct() xml += '' return xml security.declareProtected('QTAuthor: View question', 'getInteractionType') def getInteractionType(self): """ """ return "choiceInteraction" def importQuestion(self, data, dom): """ """ import re additionalFiles = {} questext = data.split('') questext = questext[1].split(''+variants.get(var)+'' 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) if val == var: count += 1 table += ''+str(count)+'' 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 = [] variants = self.getAnswers() cm = re.compile('(.*?)', re.S) cm_results = cm.search(ans_send) var = "" if cm_results: val = cm_results.group(1) var = variants.get(val) array.append(var) text += var + ";" frequencies[ques_id] = array return {'csvtext': text, 'frequencies': frequencies} def getCSVstatsWithNumbers(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() cm = re.compile('(.*?)', re.S) cm_results = cm.search(ans_send) var = "" if cm_results: val = cm_results.group(1) #var = variants.get(val) if self.getKeys().get("val") == val: var = 1 else: var = 0 array.append(var) text += str(var) + ";" frequencies[ques_id] = array return {'csvtext': text, 'frequencies': frequencies} def calculateUserAnswerPoints(self, user_answer): points = 0.0 if self.getKeys().get("val") == user_answer: points = 1.0 return points def generateUserAnswerHtml(self, user_answer): questiontext = self.getQuestion() responsename = 'RESPONSE_' + self.getId() html = "" html += "%s" % self.getTitle() html += questiontext html += "" variants = self.getAnswers() for var in variants: html += "" html += "" html += "" html += "" html += "%s" % variants.get(var) html += "" html += "" html += "" return html def processUserAnswer(self, answer_send): import re cm = re.compile('(.*?)', re.S) cm_results = cm.search(answer_send) points = 0.0 html = self.getPreview() if cm_results: user_answer = cm_results.group(1) points = self.calculateUserAnswerPoints(user_answer) html = self.generateUserAnswerHtml(user_answer) return {'points': points, 'html': html} Globals.InitializeClass(ChoiceType) # EOF
\n\n