# -*- coding: utf-8 # Copyright 2005 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField 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.BaseTask import BaseTask, BaseAnswer from Products.Krihvel.config import PROJECT_NAME schema = BaseSchema + BaseTask.schema + Schema(( LinesField('ExerciseType', accessor="getExerciseType", isMetadata=1, 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"), ), )) class BasePairs(BaseTask): """ base class for pairs """ def getMaxGameRows(self): """gets rows""" return len(self.images) def getGameRows(self): """gets rows""" return self.gameRows def setGameRows(self,REQUEST): """sets rows""" self.gameRows = REQUEST.get("set_game_rows") self._p_changed = 1 def getGameData(self): """aaa""" return self.images def getPicArrayForGame(self, col): """aaa""" array = "" for item in self.images: array=array+"#"+item[col]['value'] return array[1:] def getTypeArrayForGame(self, col): """aaa""" array = "" for item in self.images: array=array+"#"+item[col]['type'] return array[1:] def getPicArray(self): """aaa""" return self.images 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 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' self.images.append({'col1':col1,'col2':col2}) self._p_changed = 1 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 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 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 = [] def getAns(self): """ return """ return self.ans def getAnsJS(self): """ return """ array = "" for item in self.ans: array=array+"#"+item return array[1:] def getAns2(self): """ return """ return self.ans2 def getAns2JS(self): """ return """ array = "" for item in self.ans2: array=array+"#"+item return array[1:] def getNrs(self): """ return """ return self.nrs def getNrs2(self): """ return """ return self.nrs2 def getCorX(self): """ return """ array = "" for item in self.corx: array=array+"#"+item return array[1:] def getCorY(self): """ return """ array = "" for item in self.cory: array=array+"#"+item return array[1:] 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)) print rating return rating registerType(PairsAnswer, PROJECT_NAME)