Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
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')
Exemplo n.º 3
0
def check(request, q_id):
    user = request.user
    if not user.is_authenticated():
        return my_redirect('/exam/register/')
    question = get_object_or_404(Question, pk=q_id)
    paper = QuestionPaper.objects.get(user=user, test__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 not new_answer.attempted and answer  :
     	if answer.strip() == question.right_response.strip():
        	new_answer.correct = True
        	new_answer.marks = question.points
        	new_answer.error = 'Correct answer'
    	else:
       		new_answer.correct = False
        	new_answer.marks = -(question.neg_points)
        	new_answer.error = 'Incorrect answer'
        
        
    	new_answer.attempted = True # Only one attempt allowed for MCQ's.
        new_answer.save()


    next_q = paper.completed_question(question.id)
    return show_question(request, next_q)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
class AnswerPaperTestCases(unittest.TestCase):
    @classmethod
    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 test_answerpaper(self):
        """ Test Answer Paper"""
        self.assertEqual(self.answerpaper.user.username, 'demo_user')
        self.assertEqual(self.answerpaper.profile_id, 1)
        self.assertEqual(self.answerpaper.user_ip, self.ip)
        questions = self.answerpaper.questions
        num_questions = len(questions.split('|'))
        self.assertEqual(questions, '1|2|3')
        self.assertEqual(num_questions, 3)
        self.assertEqual(self.answerpaper.question_paper, self.question_paper)
        self.assertEqual(self.answerpaper.start_time,
                         '2014-06-13 12:20:19.791297')
        self.assertEqual(self.answerpaper.end_time,
                         '2014-06-13 12:50:19.791297')

    def test_current_question(self):
        """ Test current_question() method of Answer Paper"""
        current_question = self.answerpaper.current_question()
        self.assertEqual(current_question, '2')

    def test_completed_question(self):
        """ Test completed_question() method of Answer Paper"""
        question = self.answerpaper.completed_question(1)
        self.assertEqual(self.answerpaper.questions_left(), 2)

    def test_questions_left(self):
        """ Test questions_left() method of Answer Paper"""
        self.assertEqual(self.answerpaper.questions_left(), 2)

    def test_skip(self):
        """ Test skip() method of Answer Paper"""
        next_question_id = self.answerpaper.skip()
        self.assertTrue(next_question_id is not None)
        self.assertEqual(next_question_id, '3')

    def test_answered_str(self):
        """ Test answered_str() method of Answer Paper"""
        answered_question = self.answerpaper.get_answered_str()
        self.assertEqual(answered_question, '1')

    def test_update_marks_obtained(self):
        """ Test get_marks_obtained() method of Answer Paper"""
        self.answerpaper.update_marks_obtained()
        self.assertEqual(self.answerpaper.marks_obtained, 1.0)

    def test_update_percent(self):
        """ Test update_percent() method of Answerpaper"""
        self.answerpaper.update_percent()
        self.assertEqual(self.answerpaper.percent, 33.33)

    def test_update_passed(self):
        """ Test update_passed method of AnswerPaper"""
        self.answerpaper.update_passed()
        self.assertFalse(self.answerpaper.passed)

    def test_get_question_answer(self):
        """ Test get_question_answer() method of Answer Paper"""
        answered = self.answerpaper.get_question_answers()
        first_answer = answered.values()[0][0]
        self.assertEqual(first_answer.answer, 'Demo answer')
        self.assertTrue(first_answer.correct)
        self.assertEqual(len(answered), 2)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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)