def setUpClass(self): self.ip = '101.0.0.1' self.user = User.objects.get(id=1) self.profile = self.user.profile self.quiz = Quiz.objects.get(pk=1) self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() # create answerpaper self.answerpaper = AnswerPaper(user=self.user, profile=self.profile, questions='1|2|3', question_paper=self.question_paper, start_time='2014-06-13 12:20:19.791297', end_time='2014-06-13 12:50:19.791297', user_ip=self.ip) self.answerpaper.questions_answered = '1' self.answerpaper.save() # answers for the Answer Paper self.answer_right = Answer(question=Question.objects.get(id=1), answer="Demo answer", correct=True, marks=1) self.answer_wrong = Answer(question=Question.objects.get(id=2), answer="My answer", correct=False, marks=0) self.answer_right.save() self.answer_wrong.save() self.answerpaper.answers.add(self.answer_right) self.answerpaper.answers.add(self.answer_wrong)
def check(request, q_id): user = request.user if not user.is_authenticated(): return my_redirect('/exam/login/') question = get_object_or_404(Question, pk=q_id) paper = QuestionPaper.objects.get(user=user, quiz__active=True) answer = request.POST.get('answer') skip = request.POST.get('skip', None) if skip is not None: next_q = paper.skip() return show_question(request, next_q) # Add the answer submitted, regardless of it being correct or not. new_answer = Answer(question=question, answer=answer, correct=False) new_answer.save() paper.answers.add(new_answer) # If we were not skipped, we were asked to check. For any non-mcq # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. if question.type == 'mcq': success = True # Only one attempt allowed for MCQ's. if answer.strip() == question.test.strip(): new_answer.correct = True new_answer.marks = question.points new_answer.error = 'Correct answer' else: new_answer.error = 'Incorrect answer' else: user_dir = get_user_dir(user) success, err_msg = code_server.run_code(answer, question.test, user_dir, question.type) new_answer.error = err_msg if success: # Note the success and save it along with the marks. new_answer.correct = success new_answer.marks = question.points new_answer.save() if not success: # Should only happen for non-mcq questions. time_left = paper.time_left() if time_left == 0: return complete(request, reason='Your time is up!') if not paper.quiz.active: return complete(request, reason='The quiz has been deactivated!') context = {'question': question, 'error_message': err_msg, 'paper': paper, 'last_attempt': answer, 'quiz_name': paper.quiz.description, 'time_left': time_left} ci = RequestContext(request) return my_render_to_response('exam/question.html', context, context_instance=ci) else: next_q = paper.completed_question(question.id) return show_question(request, next_q)
def take_exam(request, pk): if request.method == "POST": username = request.COOKIES['username'] smail = Student.objects.get(email_id__iexact=username, ) user_id = smail.id question_id_list = [] questions_list = Question.objects.filter(exam__pk=pk, ) for word in questions_list: question = Question.objects.get(question_text=word, ) a = question.id question_id_list.append(a) i = 1 while i <= len(question_id_list): #print(user_id, question_id_list[i-1], request.POST[str(i)], pk) answer = Answer(student_id=user_id, qs_id=question_id_list[i - 1], choice_id=request.POST[str(i)], exam_id=pk) answer.save() i += 1 return redirect('exam:calculate_mark', exam=pk, student=user_id) if 'username' in request.COOKIES: username = request.COOKIES['username'] smail = Student.objects.filter(email_id__iexact=username, ) sid = smail.values('id')[0] answer = Answer.objects.filter(student_id=sid['id'], exam_id=pk).count() if answer == 0: exam_details = Exam.objects.filter(pk=pk) questions_set = Question.objects.filter(exam__pk=pk) return render( request, 'exam/exam.html', { 'question_set': questions_set, 'student': smail, 'exam_details': exam_details }) else: return redirect('exam:main') else: return redirect('user:login')
def check(request, q_id, questionpaper_id=None): """Checks the answers of the user for particular question""" user = request.user if not user.is_authenticated(): return my_redirect('/exam/login/') question = get_object_or_404(Question, pk=q_id) q_paper = QuestionPaper.objects.get(id=questionpaper_id) paper = AnswerPaper.objects.get(user=request.user, question_paper=q_paper) snippet_code = request.POST.get('snippet') skip = request.POST.get('skip', None) success_msg = False success = True if skip is not None: next_q = paper.skip() return show_question(request, next_q, questionpaper_id) # Add the answer submitted, regardless of it being correct or not. if question.type == 'mcq': user_answer = request.POST.get('answer') elif question.type == 'mcc': user_answer = request.POST.getlist('answer') else: user_code = request.POST.get('answer') user_answer = snippet_code + "\n" + user_code new_answer = Answer(question=question, answer=user_answer, correct=False) new_answer.save() paper.answers.add(new_answer) # If we were not skipped, we were asked to check. For any non-mcq # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. correct, success, err_msg = validate_answer(user, user_answer, question) if correct: new_answer.correct = correct new_answer.marks = question.points new_answer.error = err_msg success_msg = True else: new_answer.error = err_msg new_answer.save() time_left = paper.time_left() if not success: # Should only happen for non-mcq questions. if time_left == 0: reason = 'Your time is up!' return complete(request, reason, questionpaper_id) if not paper.question_paper.quiz.active: reason = 'The quiz has been deactivated!' return complete(request, reason, questionpaper_id) context = {'question': question, 'error_message': err_msg, 'paper': paper, 'last_attempt': user_code, 'quiz_name': paper.question_paper.quiz.description, 'time_left': time_left} ci = RequestContext(request) return my_render_to_response('exam/question.html', context, context_instance=ci) else: if time_left <= 0: reason = 'Your time is up!' return complete(request, reason, questionpaper_id) else: next_q = paper.completed_question(question.id) return show_question(request, next_q, questionpaper_id, success_msg)