Example #1
0
 def test_create_and_save(self):
   cat_test = CatTest()
   cat_test.name = "short"
   cat_test.save()
   user = User.objects.create_user('john', '*****@*****.**', 'johnpassword')
   user.save()
   domain = Domain()
   domain.name = "Number"
   domain.create_date = datetime.datetime(2012,03,06)
   domain.save()
   item_bank = ItemBank()
   item_bank.name = "Fractions"
   item_bank.topic = "Addition"
   item_bank.domain = domain
   item_bank.question_type = QuestionType.objects.get(pk=1)
   item_bank.template = ItemBankTemplate.objects.get(pk=1)
   item_bank.save()
   user_item_bank = UserItemBank()
   user_item_bank.user = user
   user_item_bank.item_bank = item_bank
   user_item_bank.save()
   ibq = ItemBankQuestion()
   ibq.item_bank = item_bank
   ibq.save()
   user_cat_test = UserCatTest()
   user_cat_test.user = user
   user_cat_test.item_bank = item_bank
   user_cat_test.cat_test = cat_test
   user_cat_test.save()
   cat_test_item = CatTestItem()
   cat_test_item.user_cat_test = user_cat_test
   cat_test_item.item_bank_question = ibq
   cat_test_item.time_taken = 12
   cat_test_item.save()
   answer = FractionWithConstant()
   answer.const = 1
   answer.denom = 2
   answer.num = 3
   answer.save()
   ctifa = CatTestItemFractionAnswer()
   ctifa.cat_test_item = cat_test_item
   ctifa.fraction = answer
   ctifa.save()
   ctifa = CatTestItemFractionAnswer.objects.filter(cat_test_item = cat_test_item)[0]
   self.assertEquals(ctifa.fraction,answer)
Example #2
0
def question(request):
  #check for log in
  if not request.user.is_authenticated():
    return HttpResponseRedirect('/accounts/login/?next=%s' % request.path)
  user = request.user
  user_cat_test = UserCatTest.objects.filter(user=user)
  user_cat_test = user_cat_test.order_by('-id')[0]
  if request.method == 'GET':
  #get next question
    cat_test_item = user_cat_test.nextQuestion()
    ibq = cat_test_item.item_bank_question    
  #check item type
    if user_cat_test.item_bank.question_type.name == "fraction":
      ifq = ItemBankFractionQuestion.objects.get(item_bank_question=ibq)
      q = ifq.fraction_bank_question.question
      form = FractionWithConstantForm()    
    return render_to_response('question.html',{"user_cat_test":user_cat_test,"question":q,'form': form},context_instance=RequestContext(request))
  else:
    #process then redirect to feedback
    #get last item
    cat_test_item = CatTestItem.objects.filter(user_cat_test=user_cat_test)
    cat_test_item = cat_test_item.order_by('-id')[0]
    ibq = cat_test_item.item_bank_question     
    if user_cat_test.item_bank.question_type.name == "fraction":
      f = FractionWithConstantForm(request.POST)
      ifq = ItemBankFractionQuestion.objects.get(item_bank_question=ibq)
      q = ifq.fraction_bank_question.question
      answer = FractionWithConstant()
      if 'const' in request.POST and request.POST['const']:         
        try:
          answer.const = int(request.POST['const'])
        except:
          answer.const = 0        
      else:
        answer.const = 0
      if 'num' in request.POST and request.POST['num']:    
        try:
          answer.num = int(request.POST['num'])
        except:
          answer.num = 0        
      else:
        answer.num = 0
      if 'denom' in request.POST and request.POST['denom']:     
        try:
          answer.denom = int(request.POST['denom'])
        except:
          answer.denom = 1               
      else:
        answer.denom = 1      
      answer.save()
      ctifa = CatTestItemFractionAnswer()
      ctifa.cat_test_item = cat_test_item
      ctifa.fraction = answer
      ctifa.save()
    if answer == q.answer:
      correct = 1
    else:
      correct = 0
    if 'time' in request.POST and request.POST['time']:       
      time_taken = request.POST['time']
    else:
      time_taken = 0       
    cat_test_item.time_taken = time_taken
    cat_test_item.correct = correct
    cat_test_item.save()
    user_cat_test.simAbility()	
    return HttpResponseRedirect('/feedback/')