# -*- coding: utf-8 # Copyright 2005 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField, ReferenceField from Products.Archetypes.public import LinesWidget, TextAreaWidget, IdWidget, StringWidget, SelectionWidget, RichWidget from AccessControl import ClassSecurityInfo from Products.Archetypes.public import registerType from Products.CMFCore.utils import getToolByName from Products.Krihvel.Permissions import * from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget from Products.Krihvel.BaseTask import BaseTask, BaseAnswer from Products.Krihvel.config import PROJECT_NAME schema = BaseSchema + BaseTask.schema + Schema(( StringField('ExerciseType', required=1, accessor="getExerciseType", isMetadata=1, default='No', vocabulary=['No','Yes'], widget=SelectionWidget( format="radio", label="I want to mark it", label_msgid='label_extype', description="NO if it is just for exerciseing, YES when you want mark it", description_msgid='desc_extype', i18n_domain="krihvel"), ), ReferenceField('refsToImage', accessor = 'getRefsToImage', relationship = 'References', mutator = 'addRefsToImage', allowed_types = ('ATImage','Image'), multiValued = True, widget = ReferenceBrowserWidget( visible = {'view':'invisible', 'edit':'invisible'}, ), ), )) class BasePairs(BaseTask): """ base class for pairs """ security.declareProtected('View', 'getMaxGameRows') def getMaxGameRows(self): """gets rows""" return len(self.images) security.declareProtected('View', 'getGameRows') def getGameRows(self): """gets rows""" return self.gameRows security.declareProtected('View', 'setGameRows') def setGameRows(self,REQUEST): """sets rows""" self.gameRows = REQUEST.get("set_game_rows") self._p_changed = 1 security.declareProtected('View', 'getGameData') def getGameData(self): """aaa""" return self.images security.declareProtected('View', 'getPicArrayForGame') def getPicArrayForGame(self, col): """aaa""" array = "" for item in self.images: array=array+"#"+item[col]['value'] return array[1:] security.declareProtected('View', 'getTypeArrayForGame') def getTypeArrayForGame(self, col): """aaa""" array = "" for item in self.images: array=array+"#"+item[col]['type'] return array[1:] security.declareProtected('View', 'getPicArray') def getPicArray(self): """aaa""" return self.images security.declareProtected('View', 'getUrlForJS') def getUrlForJS(self): """ jura """ return "'"+self.absolute_url()+"'" class Pairs(BaseTask,BasePairs): """ Pairs module. """ meta_type = "Pairs" archetype_name = "Pairs" allowed_content_types = ('Image',) global_allow = 0 security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/pairs_view', 'permissions': ('View',), }, ) def _renameAfterCreation(self, check_auto_id=False): pass def __init__(self, id): self.id = id self.images = [] self.gameRows = 5 #default game has 5 rows of pictures security.declareProtected('Modify portal content', 'addRow') def addRow(self, REQUEST): """ add image row to game """ image_1_text = REQUEST.get('picture_1_text') image_2_text = REQUEST.get('picture_2_text') image_1_url = REQUEST.get('picture_1_url') image_2_url = REQUEST.get('picture_2_url') user = str(REQUEST.AUTHENTICATED_USER) homefolder = self.portal_membership.getHomeFolder(user) if image_1_url != '0': col1 = {'type':'image','value':image_1_url} elif image_1_text != '': col1 = {'type':'text','value':image_1_text} else: col1 = 'None' if image_2_url != '0': col2 = {'type':'image','value':image_2_url} elif image_2_text != '': col2 = {'type':'text','value':image_2_text} else: col2 = 'None' if col1 != 'None' and col2 != 'None': self.images.append({'col1':col1,'col2':col2}) self._p_changed = 1 security.declareProtected('Modify portal content', 'delRow') def delRow(self,REQUEST): """ delete image row from game """ counter = 1 newImages = [] for image in self.images: if REQUEST.get('picture_row_'+str(counter)) != 'on': newImages.append(image) counter = counter + 1 self.images = newImages self._p_changed = 1 security.declareProtected('View', 'saveAnswer') def saveAnswer(self,ans='',ans2='',nrs='',nrs2='',corx='',cory=''): """ Saves Answer """ user = str(self.REQUEST.AUTHENTICATED_USER) homefolder = self.portal_membership.getHomeFolder(user) number = 1 typestool = getToolByName(self, 'portal_types') puf_type = getattr(typestool, 'PairsAnswer', None) if not puf_type: return 0 puf_type.global_allow = 1 while hasattr(homefolder, 'PairsAnswer-'+str(number)): number = number+1 newid = 'PairsAnswer-'+str(number) id = homefolder.invokeFactory("PairsAnswer", id=newid, title=self.title, Instruction=self.Instruction, Target=self.id) copiedObj = getattr(homefolder,id) copiedObj.images = self.images copiedObj.gameRows = self.gameRows copiedObj.ans = ans.split(",") copiedObj.ans2 = ans2.split(",") copiedObj.nrs = nrs.split(",") copiedObj.nrs2 = nrs2.split(",") copiedObj.corx = corx.split(",") copiedObj.cory = cory.split(",") puf_type.global_allow = 0 self.addRefsToWhat('refsToThisTask', copiedObj) copiedObj.addRefsToWhat('refsToThisTask', self) return self.REQUEST.RESPONSE.redirect(copiedObj.absolute_url()+'/pairs_answer') registerType(Pairs, PROJECT_NAME) class PairsAnswer(BaseAnswer,BasePairs): """ PairsAnswer module. """ meta_type = "PairsAnswer" archetype_name = "PairsAnswer" allowed_content_types = () global_allow = 0 security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/pairs_answer', 'permissions': ('View',), }, ) def __init__(self, id): self.id = id self.images = [] self.gameRows = 5 #default game has 5 rows of pictures self.ans = [] self.ans2 = [] self.nrs = [] self.nrs2 = [] self.corx = [] self.cory = [] security.declareProtected('View', 'getAns') def getAns(self): """ return """ return self.ans security.declareProtected('View', 'getAnsJS') def getAnsJS(self): """ return """ array = "" for item in self.ans: array=array+"#"+item return array[1:] security.declareProtected('View', 'getAns2') def getAns2(self): """ return """ return self.ans2 security.declareProtected('View', 'getAns2JS') def getAns2JS(self): """ return """ array = "" for item in self.ans2: array=array+"#"+item return array[1:] security.declareProtected('View', 'getNrs') def getNrs(self): """ return """ return self.nrs security.declareProtected('View', 'getNrs2') def getNrs2(self): """ return """ return self.nrs2 security.declareProtected('View', 'getCorX') def getCorX(self): """ return """ array = "" for item in self.nrs: array=array+"#"+item return array[1:] security.declareProtected('View', 'getCorY') def getCorY(self): """ return """ array = "" for item in self.nrs2: array=array+"#"+item return array[1:] def drawAnswerLines(self): """javascript darw lines""" js = '' for i in range(0,len(self.ans)): p1 = i color = "#FF0000" for j in range(0,len(self.ans)): if int(self.ans[i])==int(self.ans2[j]): p2 = j if int(self.nrs[i])==int(self.nrs2[j]): color = "#00FF00" js = js + 'colorLineDrawFunction(' js = js + 'parseInt(document.getElementById("corx_1_'+str(p1+1)+'").value),' js = js + 'parseInt(document.getElementById("cory_1_'+str(p1+1)+'").value),' js = js + 'parseInt(document.getElementById("corx_2_'+str(p2+1)+'").value),' js = js + 'parseInt(document.getElementById("cory_2_'+str(p2+1)+'").value),' js = js + '\''+color+'\');' return js security.declareProtected('View', 'getRating') def getRating(self): """ get Rating """ images=self.images c=0 a=0 a1=[] b1=[] correctAnswers=0 for image in self.nrs: b=0 for image2 in self.nrs2: if image==image2: a1.append(a) b1.append(b) b=b+1 a=a+1 for correct in a1: c1=correct c2=b1[correct] if self.ans[c1]==self.ans2[c2]: correctAnswers = correctAnswers + 1 rating = self.calculatePercent(correctAnswers,int(self.gameRows)) return rating registerType(PairsAnswer, PROJECT_NAME)