# -*- coding: utf-8 # Copyright 2007 by Meelis Mets from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from zipfile import ZipFile, BadZipfile, ZIP_DEFLATED from cStringIO import StringIO from AccessControl import ClassSecurityInfo, Unauthorized, getSecurityManager from elementtree.ElementTree import ElementTree class BaseFunctions: def arrayCordinatesAsNr(self, cor, array): """array to nr""" nr = 0 x = len(array[0]) y = len(array) for a in range(0,y,1): for b in range(0,x,1): nr = nr + 1 if a == int(cor[0]) and b == int(cor[1]): return nr return nr def getObjectsArray(self): """return handy arry of objcts""" objects = self.objectValues(['ChoiceObject',]) rows = int(self.getTableHeight()) cols = int(self.getTableWidth()) objectsLen = len(objects) objectsArray = [] counter = 0 for h in range(0,rows): objectsRow = [] for w in range(0,cols): if counter >= objectsLen: objectsRow.append("None") else: objectsRow.append(objects[counter]) counter = counter + 1 objectsArray.append(objectsRow) return objectsArray def createChoiceObjects(self): """script for creating choice objects""" objects = self.objectValues(['ChoiceObject',]) rows = int(self.getTableHeight()) cols = int(self.getTableWidth()) objectsLen = len(objects) counter = 0 for h in range(0,rows): for w in range(0,cols): createObject = False title = self.REQUEST.get('title_'+str(h)+'_'+str(w)) if self.REQUEST.get('pic_'+str(h)+'_'+str(w)): pic = self.REQUEST.get('pic_'+str(h)+'_'+str(w)) else: pic = False usetitle = False if self.REQUEST.get('usetitle_'+str(h)+'_'+str(w)): usetitle = True alreadychecked = False if self.REQUEST.get('alreadychecked_'+str(h)+'_'+str(w)): alreadychecked = True right = False right2 = False if self.REQUEST.get('rightwrong_'+str(h)+'_'+str(w)): rw_choice = self.REQUEST.get('rightwrong_'+str(h)+'_'+str(w)) if rw_choice == 'r1': right = True right2 = True elif rw_choice == 'r2': right = True right2 = False elif rw_choice == 'w1': right = False right2 = False elif rw_choice == 'w2': right = False right2 = True if title: if counter >= objectsLen: self.createChoiceObject(title, pic, usetitle, alreadychecked, right, right2) else: self.modifyChoiceObject(objects[counter].id, title, pic, usetitle, alreadychecked, right, right2) counter = counter + 1 return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/objects_view') def modifyChoiceObject(self, id, title, pic=False, usetitle=False, alreadychecked=False, right=False, right2=False): """ modifications """ obj = getattr(self, id) if obj.Title() != title: obj.title = title if pic: obj.getField('choiceImage').set(obj, pic) if obj.useTitle != usetitle: obj.useTitle = usetitle if obj.alreadyChecked != alreadychecked: obj.alreadyChecked = alreadychecked if obj.rightChoice != right: obj.rightChoice = right if obj.rightChoice2 != right2: obj.rightChoice2 = right2 obj.reindexObject() def createChoiceObject(self, title, pic=False, usetitle=False, alreadychecked=False, right=False, right2=False): """ creation """ import time newId = 'Choice-'+str(time.time()) id = self.invokeFactory("ChoiceObject", id=newId, title=title) obj = getattr(self,id) obj.useTitle = usetitle obj.alreadyChecked = alreadychecked if pic: obj.getField('choiceImage').set(obj, pic) obj.rightChoice = right obj.rightChoice2 = right2 def getNumbers(self): """return time array""" nrs = [] for nr in range(1, 11, 1): nrs.append(nr) return nrs def getCollectionFolder(self): """return collection list""" if hasattr(self, 'psyhvel'): psyhvel = getattr(self,'psyhvel') if hasattr(psyhvel,'tests'): collection_folder = getattr(psyhvel,'tests') return collection_folder raise "Error: Collection folder not found" def getCollectionList(self): """return collection list""" return self.getCollectionFolder().getFolderContents() def addToCollection(self): """add to collection""" collection_id = self.REQUEST.get('choose_collection') test_objects = self.REQUEST.get('test_objects') if test_objects != None: collection_folder = getattr(self.getCollectionFolder(),collection_id) if hasattr(self, str(test_objects)): self.makeAnswerObject(collection_folder,getattr(self, str(test_objects))) else: for t in test_objects: self.makeAnswerObject(collection_folder,getattr(self, str(t))) return self.REQUEST.RESPONSE.redirect(collection_folder.absolute_url()) def makeAnswerObject(self, where, what): """make object""" test_id = what.id+'db' test_title = what.Title() if not hasattr(where, test_id): from AnswerFolder import AnswerFolder answer = AnswerFolder(test_id) where._setObject(answer.id, answer) ob = getattr(where,answer.id) ob.setTitle(test_title) ob.addRefsToTests(what) if what.meta_type in ['GameFolder']: ob.isGame = True ob.reindexObject() def renderTestTable(self, width=600, owidth=200): """renders html table for dispaly tests""" objects = [] table = [] user = str(self.REQUEST.AUTHENTICATED_USER) member = self.portal_membership.getMemberById(user) trs = getToolByName(self, 'translation_service') counter = 0 testObjects = self.getFolderContents() nrOfObjs = len(testObjects) notActive = False table.append('
'+trs.utranslate('psyhvel', 'Test', self, self)+' '+str(counter+1)+' | ') else: objects.append(''+trs.utranslate('psyhvel', 'Test', self, self)+' '+str(counter+1)+' | ') elif notActive: if not isEditor: objects.append(''+trs.utranslate('psyhvel', 'Test', self, self)+' '+str(counter+1)+' | ') else: objects.append(''+trs.utranslate('psyhvel', 'Test', self, self)+' '+str(counter+1)+' | ') else: objects.append(''+trs.utranslate('psyhvel', 'Test', self, self)+' '+str(counter+1)+' | ') if obj.getObject().getCurrentPage()==0: notActive = True counter = counter + 1 if isEditor: url = self.portal_url()+'/psyhvel/library?collection='+self.id objects.append(''+trs.utranslate('psyhvel', 'Add Test', self, self)+' | ') nrOfObjs = len(objects) rows = int(nrOfObjs*owidth/width)+1 max_objs = int(width/owidth) if nrOfObjs < max_objs: max_objs = nrOfObjs for line in range(0,rows): table.append('||
') else: spacer.append(' | ') elif len(objects) > 0 and i%2 != 0 and line%2 == 0: lines.append(' | ') spacer.append(' | ') elif len(objects) > 0 and i%2 != 0 and line%2 != 0: lines.append(' | ') spacer.append(' | ') else: lines.append(' | ') spacer.append(' | ') if line%2 != 0: lines.reverse() spacer.reverse() for li in lines: table.append(li) table.append(' |