# -*- 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(( # LinesField('ExType', # accessor="getExType", # isMetadata=1, # vocabulary=['Test','Game'], # widget=SelectionWidget( # format="select", # label="Type", # label_msgid='label_type', # description="How exercise will be generated?", # description_msgid='desc_captype', # i18n_domain="krihvel"), # ), )) class BaseMath(BaseTask): """ MathAnswer 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 = [] 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 def getExercises(self): """ gets and prints exercise array """ return self.exercises def resetExercises(self): """ gets and prints exercise array """ self.exercises = [] self._p_changed = 1 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') 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') 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') 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 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 def getOperations(self): """ gets and prints exercise array """ operations = [] for exercise in self.exercises: operations.append(self.makeOperation(exercise)) return operations 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 #return REQUEST.RESPONSE.redirect(copiedObj.absolute_url()+'/math_answer') 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 = [] def getAnswers(self): """ gets and prints answers array """ return self.answers 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)