def diagnostic(request): if request.method == 'POST': #submit form user_id = request.POST['user'] current_user = User.objects.get(id=user_id) num = request.POST['num'] for i in range(int(num)): k = "question" + str(i) question_id = request.POST[k] k = "answer" + str(i) answer = request.POST[k] question = SATQuestion.objects.get(id=question_id) aq = Answered_Sat_Q() aq.unanswered_q = question aq.user = UserData.objects.get(user__id=user_id) if answer == question.correct_answer: aq.correct = 1 else: aq.correct = 0 aq.save() return redirect(diagnostic_complete) else: current_user = request.user #get questions and populate accordingly rand_qs = SATQuestion.objects.order_by('?')[:20] num = len(rand_qs) return render_to_response('diagnostic.html', {'rand_qs': rand_qs, 'user': current_user, 'num': num}, context_instance=RequestContext(request))
def get_question(request): #question was answered if request.method == "POST": #grab answer and question id from POST data answer = request.POST['answer'] question_id = request.POST['question'] user_id = request.POST['user'] current_user = User.objects.get(id=user_id) question = SATQuestion.objects.get(id=question_id) #create Answered SAT Question object aq = Answered_Sat_Q() aq.unanswered_q = question aq.user = UserData.objects.get(user__id=user_id) if answer == question.correct_answer: aq.correct = 1 else: aq.correct = 0 aq.save() return render_to_response('questionanswered.html', {'question': question, 'user': current_user, 'answer': answer}) #we ask a question else: if request.user.is_authenticated(): current_user = request.user else: #if anonymous user, redirect to registration return redirect(register) answered_qs = Answered_Sat_Q.objects.filter(user__user=current_user) #if answered_qs: # return render_to_response('question.html', # {'question': answered_qs[0].unanswered_q, 'user': current_user}, # context_instance=RequestContext(request)) #FOR TESTING ONLY if not answered_qs: #diagnostic test return redirect(diagnostic) pass question = get_good_question.choose_q(current_user) #question = SATQuestion.objects.get(id=9) return render_to_response('question.html', {'question': question, 'user': current_user}, context_instance=RequestContext(request))