From ff5fc201bf7f44a6dc9d6d93fee7bc0ce47d1be2 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 24 Feb 2015 11:21:48 +0530 Subject: On skip the answer will be saved. The user answer will be retrieved when the same question is loaded again later. --- testapp/exam/models.py | 3 +++ testapp/exam/views.py | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'testapp') diff --git a/testapp/exam/models.py b/testapp/exam/models.py index 88ba9ef..0ad6021 100644 --- a/testapp/exam/models.py +++ b/testapp/exam/models.py @@ -105,6 +105,9 @@ class Answer(models.Model): # Is the answer correct. correct = models.BooleanField(default=False) + # Whether skipped or not. + skipped = models.BooleanField(default=False) + def __unicode__(self): return self.answer diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 8d7dd18..576405f 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -743,6 +743,10 @@ def question(request, q_id, attempt_no, questionpaper_id, success_msg=None): context = {'question': q, 'questions' : questions, 'paper': paper, 'user': user, 'quiz_name': quiz_name, 'time_left': time_left, 'success_msg': success_msg, 'to_attempt' : to_attempt, 'submitted' : submitted} + if q.type == 'code': + skipped_answer = paper.answers.filter(question=q, skipped=True) + if skipped_answer: + context['last_attempt'] = skipped_answer[0].answer ci = RequestContext(request) return my_render_to_response('exam/question.html', context, context_instance=ci) @@ -768,10 +772,23 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None): return my_redirect('/exam/login/') question = get_object_or_404(Question, pk=q_id) snippet_code = request.POST.get('snippet') + user_code = request.POST.get('answer') skip = request.POST.get('skip', None) success_msg = False success = True if skip is not None: + if question.type == 'code': + user_answer = user_code # not taking snippet here. + old_skipped = paper.answers.filter(question=question, skipped=True) + if old_skipped: + skipped_answer = old_skipped[0] + skipped_answer.answer=user_answer + skipped_answer.save() + else: + skipped_answer = Answer(question=question, answer=user_answer, + correct=False, skipped=True) + skipped_answer.save() + paper.answers.add(skipped_answer) next_q = paper.skip() return show_question(request, next_q, attempt_no, questionpaper_id) @@ -788,7 +805,6 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None): assign.save() user_answer = 'ASSIGNMENT UPLOADED' else: - user_code = request.POST.get('answer') user_answer = snippet_code + "\n" + user_code new_answer = Answer(question=question, answer=user_answer, @@ -839,6 +855,10 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None): for num, value in enumerate(all_questions, 1): questions[value] = num questions = collections.OrderedDict(sorted(questions.items())) + old_answer = paper.answers.filter(question=question, skipped=True) + if old_answer: + old_answer[0].answer = user_code + old_answer[0].save() context = {'question': question, 'questions': questions, 'error_message': err_msg, 'paper': paper, 'last_attempt': user_code, -- cgit From da7014b32635f47d0fd66931ce5961e54f82ae94 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 24 Feb 2015 14:13:38 +0530 Subject: Answer is now saved from the question navigator as well --- testapp/exam/templates/exam/question.html | 20 ++++++++++++++------ testapp/exam/views.py | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) (limited to 'testapp') diff --git a/testapp/exam/templates/exam/question.html b/testapp/exam/templates/exam/question.html index 96e7bd6..5d03b37 100644 --- a/testapp/exam/templates/exam/question.html +++ b/testapp/exam/templates/exam/question.html @@ -48,7 +48,12 @@ function setSnippetHeight() ta.style.height = height; autoresize(); } - +function call_skip(url) +{ + form = document.forms["code"] + form.action = url + form.submit(); +} {% endblock script %} @@ -82,17 +87,18 @@ function setSnippetHeight() +
-

{{ question.summary }} (Marks : {{ question.points }})


@@ -117,7 +123,9 @@ function setSnippetHeight()

- {% csrf_token %} + {% csrf_token %} + + {% if question.type == "mcq" %} {% for option in question.options.strip.splitlines %} {{option}}
diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 576405f..ba48b60 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -754,6 +754,29 @@ def question(request, q_id, attempt_no, questionpaper_id, success_msg=None): def show_question(request, q_id, attempt_no, questionpaper_id, success_msg=None): """Show a question if possible.""" + user = request.user + q_paper = QuestionPaper.objects.get(id=questionpaper_id) + paper = AnswerPaper.objects.get(user=request.user, attempt_number=attempt_no, + question_paper=q_paper) + if not user.is_authenticated() or paper.end_time < datetime.datetime.now(): + return my_redirect('/exam/login/') + old_qid = request.POST.get('question_id') + if old_qid is not None: + + quest = Question.objects.get(pk=old_qid) + user_code = request.POST.get('answer') + if quest.type == 'code': + user_answer = user_code # not taking snippet here. + old_skipped = paper.answers.filter(question=quest, skipped=True) + if old_skipped: + skipped_answer = old_skipped[0] + skipped_answer.answer=user_answer + skipped_answer.save() + else: + skipped_answer = Answer(question=quest, answer=user_answer, + correct=False, skipped=True) + skipped_answer.save() + paper.answers.add(skipped_answer) if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' return complete(request, msg, attempt_no, questionpaper_id) -- cgit From 9b7df35eca1557982155fdcc7993b4a7ba77235b Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 31 Mar 2015 17:41:05 +0530 Subject: Created a function to save skipped questions. If the question is skipped for the first time then a new object of the answer is created, else the old skipped answer object is modified. --- testapp/exam/views.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'testapp') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index ba48b60..7103006 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -762,27 +762,31 @@ def show_question(request, q_id, attempt_no, questionpaper_id, success_msg=None) return my_redirect('/exam/login/') old_qid = request.POST.get('question_id') if old_qid is not None: - quest = Question.objects.get(pk=old_qid) user_code = request.POST.get('answer') if quest.type == 'code': - user_answer = user_code # not taking snippet here. old_skipped = paper.answers.filter(question=quest, skipped=True) - if old_skipped: - skipped_answer = old_skipped[0] - skipped_answer.answer=user_answer - skipped_answer.save() - else: - skipped_answer = Answer(question=quest, answer=user_answer, - correct=False, skipped=True) - skipped_answer.save() - paper.answers.add(skipped_answer) + _save_skipped_answer(old_skipped, user_code, paper) if len(q_id) == 0: msg = 'Congratulations! You have successfully completed the quiz.' return complete(request, msg, attempt_no, questionpaper_id) else: return question(request, q_id, attempt_no, questionpaper_id, success_msg) +def _save_skipped_answer(old_skipped, user_answer, paper): + """ + Saves the answer on skip. Only the code questions are saved. + Snippet is not saved with the answer. + """ + if old_skipped: + skipped_answer = old_skipped[0] + skipped_answer.answer=user_answer + skipped_answer.save() + else: + skipped_answer = Answer(question=quest, answer=user_answer, + correct=False, skipped=True) + skipped_answer.save() + paper.answers.add(skipped_answer) def check(request, q_id, attempt_no=None, questionpaper_id=None): """Checks the answers of the user for particular question""" @@ -801,17 +805,8 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None): success = True if skip is not None: if question.type == 'code': - user_answer = user_code # not taking snippet here. old_skipped = paper.answers.filter(question=question, skipped=True) - if old_skipped: - skipped_answer = old_skipped[0] - skipped_answer.answer=user_answer - skipped_answer.save() - else: - skipped_answer = Answer(question=question, answer=user_answer, - correct=False, skipped=True) - skipped_answer.save() - paper.answers.add(skipped_answer) + _save_skipped_answer(old_skipped, user_code, paper) next_q = paper.skip() return show_question(request, next_q, attempt_no, questionpaper_id) -- cgit