# -*- 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, 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(( )) class BaseMath(BaseTask): """ Base Math module. """ class Math(BaseTask, BaseMath): """ Math module. """ meta_type = "Math" archetype_name = "Math" allowed_content_types = () global_allow = 0 security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/math_view', 'permissions': ('View',), }, ) def __init__(self, id): self.id = id self.exercises = [] security.declareProtected('Modify portal content', 'addExercise') def addExercise(self,REQUEST): """ add exercise """ title=REQUEST.get('title') operator=REQUEST.get('operator') if len(operator)<=1: operator = [operator,] min=REQUEST.get('min') max=REQUEST.get('max') opnr=REQUEST.get('op_nr') exercise = {'title':title,'operator':operator,'min':min,'max':max,'opnr':opnr,} self.exercises.append(exercise) self._p_changed = 1 security.declareProtected('View', 'getExercises') def getExercises(self): """ gets and prints exercise array """ return self.exercises security.declareProtected('Manage Portal', 'resetExercises') def resetExercises(self): """ gets and prints exercise array """ self.exercises = [] self._p_changed = 1 security.declareProtected('Modify portal content', 'delExercise') def delExercise(self,REQUEST): """ gets and prints exercise array """ id=int(REQUEST.get('id')) self.exercises.pop(id) self._p_changed = 1 return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/math_view') security.declareProtected('Modify portal content', 'upExercise') def upExercise(self,REQUEST): """ moves exercise up in array """ id=int(REQUEST.get('id')) if id > 0: temp=self.exercises[id-1] self.exercises[id-1]=self.exercises[id] self.exercises[id]=temp self._p_changed = 1 return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/math_view') security.declareProtected('Modify portal content', 'downExercise') def downExercise(self,REQUEST): """ moves exercise up in array """ id=int(REQUEST.get('id')) if id < len(self.exercises)-1: temp=self.exercises[id+1] self.exercises[id+1]=self.exercises[id] self.exercises[id]=temp self._p_changed = 1 return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/math_view') security.declareProtected('View', 'getExs') def getExs(self): """ gets and prints exercise array """ exs = [] for e in self.exercises: op = self.makeOperation(e) exs.append({'title':e['title'],'operations':op,}) return exs security.declareProtected('View', 'makeOperation') def makeOperation(self, info): """ operation creating """ import random opnr = int(info['opnr']) counter=0 operations = [] ops = [] op = info['operator'] min=int(info['min']) max=int(info['max']) while counter < opnr: a = random.randrange(min,max) b = random.randrange(min,max) o = random.randrange(len(op)) operator = op[o] if operator=='+': answer = a + b while answer <= min or answer >= max: a = random.randrange(min,max) b = random.randrange(min,max) answer = a + b elif operator=='-': answer = a - b while answer <= min or answer >= max: b = random.randrange(min,max) a = random.randrange(b,max) answer = a - b elif operator=='*': operator='x' answer = a * b while answer <= min or answer >= max: a = random.randrange(min,max) b = random.randrange(min,max) answer = a * b elif operator=='/': operator=':' answer = a * b while answer <= min or answer >= max: a = random.randrange(1,max) b = random.randrange(1,max) answer = a * b a = answer answer = a / b operation = str(a)+operator+str(b) if operation not in ops: ops.append(operation) operations.append({'a':a,'b':b,'operator':operator,'answer':answer}) counter = counter + 1 return operations security.declareProtected('View', 'getOperations') def getOperations(self): """ gets and prints exercise array """ operations = [] for exercise in self.exercises: operations.append(self.makeOperation(exercise)) return operations security.declareProtected('View', 'saveAnswer') def saveAnswer(self,REQUEST): """ Saves Answer """ answers = [] operations = [] c = 0 for exercise in self.exercises: title = exercise['title'] for i in range(0,int(exercise['opnr'])): a = REQUEST.get('a_'+str(c)+'_'+str(i)) b = REQUEST.get('b_'+str(c)+'_'+str(i)) operator = REQUEST.get('operator_'+str(c)+'_'+str(i)) correct = REQUEST.get('correct_'+str(c)+'_'+str(i)) answer = REQUEST.get('answer_'+str(c)+'_'+str(i)) operations.append({'a':a,'b':b,'operator':operator,'correct':correct,'answer':answer}) answers.append({'title':title,'operations':operations}) operations = [] c = c + 1 user = str(REQUEST.AUTHENTICATED_USER) homefolder = self.portal_membership.getHomeFolder(user) number = 1 typestool = getToolByName(self, 'portal_types') puf_type = getattr(typestool, 'MathAnswer', None) if not puf_type: return 0 puf_type.global_allow = 1 while hasattr(homefolder, 'MathAnswer-'+str(number)): number = number+1 newid = 'MathAnswer-'+str(number) id = homefolder.invokeFactory("MathAnswer", id=newid, title=self.title, Instruction=self.Instruction, Target=self.id) copiedObj = getattr(homefolder,id) copiedObj.answers = answers puf_type.global_allow = 0 self.addRefsToWhat('refsToThisTask', copiedObj) copiedObj.addRefsToWhat('refsToThisTask', self) return copiedObj registerType(Math, PROJECT_NAME) class MathAnswer(BaseAnswer, BaseMath): """ MathAnswer module. """ meta_type = "MathAnswer" archetype_name = "MathAnswer" allowed_content_types = () global_allow = 0 security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/math_answer', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/math_answer', 'permissions': ('Modify portal content',), }, ) def __init__(self, id): self.id = id self.answers = [] security.declareProtected('View', 'getAnswers') def getAnswers(self): """ gets and prints answers array """ return self.answers security.declareProtected('View', 'getRating') def getRating(self): """ get Rating """ correctAnswers=0 answersTotal=0 for answer in self.answers: for operation in answer['operations']: if operation['answer']==operation['correct']: correctAnswers = correctAnswers + 1 answersTotal = answersTotal + 1 rating = self.calculatePercent(correctAnswers,answersTotal) return rating registerType(MathAnswer, PROJECT_NAME)