# -*- coding: utf-8 # Copyright 2006 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import BooleanField, ImageField, StringField from Products.Archetypes.public import BooleanWidget, ImageWidget, StringWidget, SelectionWidget from Products.Archetypes.public import BaseContent, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME from BaseTest import BaseTask schema = BaseTask.schema.copy() + Schema(( ImageField('backgroundImage', accessor = 'getBackgroundImage', #max_size = (300,300), required = 1, widget = ImageWidget( label = 'Background image', description = '', label_msgid="label_background_image", description_msgid="desc_background_image", i18n_domain="psyhvel", visible = {'view': 'invisible', 'edit':'visible'}, ), ), )) schema.delField('multipleChoice') schema.delField('answerRequired') #schema.delField('time') schema.moveField('time', pos='bottom') class DragFunctions: """ Functions that are needed to run flower task in test view""" def getIdenticalElements(self, shape, width, height, place): elements = self.getCurrentSheet().getElements() identical = [] for elem in elements: if shape==elem.getElementShape() and width==int(elem.getElementWidth()) and height==int(elem.getElementHeight()) and place==int(elem.getElementPlace()): identical.append(elem) return identical def getDragElements(self): """ return drag elements for drawing """ return self.getCurrentSheet().getDragElements() def generateDragFunction(self): """ return javascript for dragging """ return self.getCurrentSheet().generateDragFunction() def detectQuarter(self,x,y,cx,cy): if x < cx and y < cy: return 1 elif x < cx and y >= cy: return 2 elif x >= cx and y < cy: return 3 else: return 4 def saveDragTaskAnswer(self, REQUEST): """ saving elements data """ current_id = self.getCurrentSheet().id answers = {} elements = self.getCurrentSheet().getElements() cx = self.getCurrentSheet().getBackgroundImageWidth()/2 cy = self.getCurrentSheet().getBackgroundImageHeight()/2 c = 0 for obj in elements: a_quater = 1 a_place = 1 iid = "answer_element_"+str(c) x = int(REQUEST.get(iid+'_x')) y = int(REQUEST.get(iid+'_y')) place = int(obj.getElementPlace()) shape = obj.getElementShape() width = int(obj.getElementWidth()) height = int(obj.getElementHeight()) for ident_elem in self.getIdenticalElements(shape, width, height, place): correct_x = int(ident_elem.getElementX()) correct_y = int(ident_elem.getElementY()) if answers.has_key(obj.id+'_place'): if answers[obj.id+'_place'] == 2: break if self.detectQuarter(correct_x,correct_y,cx,cy) == self.detectQuarter(x,y,cx,cy): a_quater = 2 if (correct_x + place) >= x and x > (correct_x - place) and (correct_y + place) >= y and y > (correct_y - place): a_place = 2 break answers[obj.id+'_quarter'] = a_quater answers[obj.id+'_place'] = a_place c = c + 1 self.setDataToDatabase(current_id, answers) self.setCurrentPage() self.submitToTheNextPage(REQUEST) class DragTask(BaseTask): """ Drag task""" meta_type = "DragTask" archetype_name = "Drag Task" global_allow = 0 exclude_from_nav = True filter_content_types = True allowed_content_types = ('ElementObject') security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/drag_task', 'permissions': ('View',), }, { 'id':'edit', 'name':'Configuration', 'action':'string:${object_url}/base_edit', 'permissions': ('Modify portal content',), }, { 'id':'create', 'name':'Create', 'action':'string:${object_url}/drag_task_edit', 'permissions': ('Modify portal content',), }, { 'id':'metadata', 'name':'Info', 'action':'string:${object_url}/base_metadata', 'permissions': ('Modify portal content',), } ) def getBackgroundImageWidth(self): """image width""" return self.backgroundImage.width def getBackgroundImageHeight(self): """image height""" return self.backgroundImage.height def getElements(self): """return all elements""" return self.objectValues("ElementObject") def saveDragElements(self, data): """ saving elements data """ data_raw = data.split(",") elementNumber = 0 for obj in self.getElements(): dex = int(data_raw[elementNumber]) dey = int(data_raw[elementNumber+1]) obj.setElementPosition(dex,dey) elementNumber = elementNumber + 2 def getDragElements(self): """ return drag elements for drawing """ elements = [] for element in self.getElements(): elements.append(element.getElementData()) return elements def generateDragFunction(self): """javascript drag function""" elements = self.getElements() js = "function my_DragFunc(){" c = 0 for element in elements: ct = str(c) js = js + "if (dd.obj.name.indexOf('answer_element_"+ct+"') >= 0){" js = js + "document.getElementById('answer_element_"+ct+"_x').value=" js = js + "dd.elements.answer_element_"+ct+".x-dd.elements.eCanvas.x;" js = js + "document.getElementById('answer_element_"+ct+"_y').value=" js = js + "dd.elements.answer_element_"+ct+".y-dd.elements.eCanvas.y;" js = js + "}" c = c + 1 js = js + "}" return js def getObjectsXML(self, files): """special xml""" xml = self.getBaseXML() img = self.getField('backgroundImage') ctype = img.getContentType(self) fname = img.getFilename(self) imagefile = img.getBaseUnit(self) xml += '\n' xml += '\n' xml += '\n' xml += '' files.append({'filename':fname, 'file':str(imagefile)}) return [xml, files] registerType(DragTask, PROJECT_NAME)