# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ Hotspot type """ __version__ = "$Revision:33$"[11:-2] import Globals from AccessControl import ClassSecurityInfo from OFS.SimpleItem import SimpleItem import cStringIO try: from PIL import Image except ImportError: pass from zope.interface import implements import re from Products.QTAuthor.common import commons from Products.QTAuthor.BaseQuestion import BaseQuestion from Products.QTAuthor.Images import Images from Products.QTAuthor.Permissions import * from Products.QTAuthor.interfaces import IBaseQuestion, IQuestionAuthoring class HotSpotType(BaseQuestion): """ hotspot type """ meta_type = 'hotspot_type' security = ClassSecurityInfo() security.declareObjectPublic() implements(IBaseQuestion, IQuestionAuthoring) manage_options = SimpleItem.manage_options def __init__(self, id): self.id=id self.coordinates = {} self.coordsCount = 0 self.count = 0 self.rightanswer = '' self.hasvariants = 0 self.markedright = 0 BaseQuestion.__init__(self) security.declarePrivate('manage_afterAdd') def manage_afterAdd(self, item, container): self.index_object() security.declareProtected(perm_view_question, 'getCoords') def getCoords(self): """ """ return self.coordinates security.declareProtected(perm_edit_question, 'setPicture') def setPicture(self, chosenPicture): #saan pildi id """ """ self.chosenPicture = chosenPicture picture = getattr(self.images, str(chosenPicture)) self.pictureHeight = picture.height self.pictureWidth = picture.width pictureContentType = picture.content_type.split('/') self.pictureType = pictureContentType[-1] self.pictureTitle = picture.title self.pictureId = picture.getId() security.declareProtected(perm_view_question, 'getPictureId') def getPictureId(self): """ """ return self.chosenPicture security.declareProtected(perm_view_question, 'getType') def getType(self): return 'hotspot_type' def getMaxPoints(self): return 1.0 security.declareProtected(perm_view_question, 'getPicture') def getPicture(self, page): """ """ z = self.chosenPicture picture = getattr(self.images, str(z)) x = str(picture) suva = x.split('/>') if page == 'hotspot2': pic = suva[0] + ' onload="hotspot2_load()" onmousedown="showCoords(event);"/>' else: pic = suva[0] + ' onload="hotspot_load()" />' return pic def drawPic(self): """ draw image """ x = self.chosenPicture pic = getattr(self.images, x) data = pic.data photo = cStringIO.StringIO() if type(data) is type(''): photo.write(data) else: while data is not None: photo.write(data.data) data = data.next #picContentType = pic.content_type.split('/') #type = picContentType[-1] photo.seek(0) im = Image.open(photo) from PIL import ImageDraw for i in range (1, int(self.count) + 1): l = self.coordinates.get('coords'+str(i)) l2 = l.split(',') xx = int(l2[0]) yy = int(l2[1]) z = ImageDraw.ImageDraw(im) try: z.ellipse((xx-7, yy-7, xx+7, yy+7), "green") except ValueError: # if .gif file then can't specify color. second parameter has to be integer z.ellipse((xx-7, yy-7, xx+7, yy+7)) c = cStringIO.StringIO() try: im.save(c, "JPEG", quality=100) except (KeyError, IOError): im.save(c, "GIF", quality=100) c.seek(0) return c security.declareProtected(perm_view_question, 'getpictureHeight') def getpictureHeight(self): """ """ return self.pictureHeight security.declareProtected(perm_edit_question, 'changeQuestion') def changeQuestion(self, REQUEST, add=0): """ """ coordinates = {} self.coordsCount = REQUEST.get('coords_count') counter = 0 hasvariants = 1 #1-kui ei ole ühtegi varianti antud, 0-kui on markedright = 1 #1-kui ei ole märgitud, 0-kui on for i in range (1, int(self.coordsCount) + 1): if REQUEST.has_key('coords'+str(i)): counter = counter + 1 coordinates['coords'+str(counter)] = REQUEST.get('coords'+str(i)) hasvariants = 0 self.setHasVariants(hasvariants) self.count = counter self.coordinates = coordinates if REQUEST.has_key('val'): self.rightanswer = REQUEST.get('val') markedright = 0 else: self.rightanswer = '' self.setMarkedRight(markedright) self.drawPic() self.setCommonData(REQUEST) self.reindex_object() msg = '' if hasvariants: msg += "Please mark your choices!" if not hasvariants and markedright: if msg: msg+= "
" msg += "Please mark correct choice!" if hasvariants == 0 and markedright == 0: if add == 1: return REQUEST.RESPONSE.redirect(self.getId()+'/index_html') return REQUEST.RESPONSE.redirect('index_html') return REQUEST.RESPONSE.redirect(self.getId()+'/edit?message='+msg) security.declareProtected(perm_view_question, 'getMarkedRight') def getMarkedRight(self): """ """ return self.markedright security.declareProtected(perm_edit_question, 'setMarkedRight') def setMarkedRight(self, val): self.markedright = val security.declareProtected(perm_view_question, 'getHasVariants') def getHasVariants(self): """ """ return self.hasvariants security.declareProtected(perm_edit_question, 'setHasVariants') def setHasVariants(self, val): self.hasvariants = val def oldCoords2(self): #et kuvada vanad koordinaadid muutmiselehele - seda ei ole vaja- """ """ #inputs = '' inputs = '' z = self.rightanswer right = z.replace('v', '') for i in range (1, int(self.count) + 1): inputs = inputs + '' if(int(right)!=i): inputs = inputs + '' else: inputs = inputs + '' inputs = inputs + '' return inputs def oldCoords(self): #vanad koordid algselt hidden fieldidena ja siis sealt loeb tabelisse """ """ inputs = '' z = self.rightanswer right = z.replace('v', '') for i in range (1, int(self.count) + 1): inputs = inputs + '' inputs = inputs + '' return inputs def xml2(self, fullURLs=False): """ """ xml= u'\n' xml=xml+ '\n' xml=xml+ '\n' xml=xml+ '\n' xml=xml+ ''+self.rightanswer+'\n' xml=xml+ '\n' xml=xml+ '\n' xml=xml+ '\n' xml=xml+ '\n' xml=xml+ '

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

\n' xml=xml+ '\n' xml=xml+ ''+str(self.getId())+'\n' else: xml=xml+ self.get_root().absolute_url() + '/images/'+str(self.getPictureId())+'">'+str(self.getPictureId())+'\n' for i in range(1, int(self.coordsCount)+1): z = self.coordinates.get('coords'+str(i)).split(',') y_coords = z[-1] x_coords = z[0] y = int(y_coords) + 1 x = int(x_coords) + 1 xml=xml+ '\n' xml=xml+ '\n' xml=xml+ '
\n' xml=xml+ self.match_correct() xml=xml+ '
\n' return xml security.declareProtected(perm_view_question, 'getInteractionType') def getInteractionType(self): """ """ return "hotspotInteraction" security.declareProtected(perm_view_question, 'getExtraFilesXML') def getExtraFilesXML(self): return '' def importQuestion(self, data, dom): """ """ import re additionalFiles = {} questext = data.split('') questext = questext[1].split('' 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) value = val.replace('v', '') if int(value) == i: count += 1 table += '' table += '
'+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 = [] cm = re.compile('(.*?)', re.S) val = cm.search(ans_send).group(1) var = val.replace('v', 'variant') text += ''+var+'' array.append(val) frequencies[ques_id] = array return {'csvtext': text, 'frequencies': frequencies} Globals.InitializeClass(HotSpotType) # EOF