summaryrefslogtreecommitdiff
path: root/testapp
diff options
context:
space:
mode:
authorPrabhu Ramachandran2015-04-13 00:55:53 +0530
committerPrabhu Ramachandran2015-04-13 00:55:53 +0530
commit6e275bd876f3b7b25956f81fbea8ecf41b02e654 (patch)
tree375ac3b10f95caa79d19ee3ae9f09518e4b5be67 /testapp
parent7acbd64bff389a838f049234e78889a2b94a4f94 (diff)
parent8fcc1ab1de747bea09b443ba591a684e3054f65b (diff)
downloadonline_test-6e275bd876f3b7b25956f81fbea8ecf41b02e654.tar.gz
online_test-6e275bd876f3b7b25956f81fbea8ecf41b02e654.tar.bz2
online_test-6e275bd876f3b7b25956f81fbea8ecf41b02e654.zip
Merge pull request #44 from prathamesh920/save_skipped_answer
Save skipped answer
Diffstat (limited to 'testapp')
-rw-r--r--testapp/exam/models.py3
-rw-r--r--testapp/exam/templates/exam/question.html20
-rw-r--r--testapp/exam/views.py41
3 files changed, 58 insertions, 6 deletions
diff --git a/testapp/exam/models.py b/testapp/exam/models.py
index ebf1018..72fb51b 100644
--- a/testapp/exam/models.py
+++ b/testapp/exam/models.py
@@ -106,6 +106,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/templates/exam/question.html b/testapp/exam/templates/exam/question.html
index 57bfd53..eac6792 100644
--- a/testapp/exam/templates/exam/question.html
+++ b/testapp/exam/templates/exam/question.html
@@ -64,6 +64,13 @@ function validate(){
}
}
+function call_skip(url)
+{
+ form = document.forms["code"]
+ form.action = url
+ form.submit();
+}
+
</script>
{% endblock script %}
@@ -97,17 +104,18 @@ function validate(){
</div>
</div>
</div>
+
<div class = container>
-<div class="sidebar">
+ <div class="sidebar">
<p>Question Navigator </p>
<div class="pagination">
<ul>
{% for qid, num in questions.items %}
{% if qid in to_attempt %}
{% if qid == question.id|slugify %}
- <li class="active"><a href="{{ URL_ROOT }}/exam/{{ qid }}/{{ paper.attempt_number }}/{{ paper.question_paper.id }}/">{{ num }}</a></li>
+ <li class="active"><a href="#" onclick="call_skip('{{ URL_ROOT }}/exam/{{ qid }}/{{ paper.attempt_number }}/{{ paper.question_paper.id }}/')">{{ num }}</a></li>
{% else %}
- <li><a href="{{ URL_ROOT }}/exam/{{ qid }}/{{ paper.attempt_number }}/{{ paper.question_paper.id }}/">{{ num }}</a></li>
+ <li><a href="#" onclick="call_skip('{{ URL_ROOT }}/exam/{{ qid }}/{{ paper.attempt_number }}/{{ paper.question_paper.id }}/')">{{ num }}</a></li>
{% endif %}
{% endif %}
{% if qid in submitted %}
@@ -116,7 +124,7 @@ function validate(){
{% endfor %}
</ul>
</div>
-</div>
+ </div>
</div>
<h4><u> {{ question.summary }} </u><font class=pull-right>(Marks : {{ question.points }}) </font></h4><br>
@@ -132,7 +140,9 @@ function validate(){
<p id="status"></p>
<form id="code" action="{{URL_ROOT}}/exam/{{ question.id }}/check/{{ paper.attempt_number }}/{{ paper.question_paper.id }}/" method="post" enctype="multipart/form-data">
- {% csrf_token %}
+ {% csrf_token %}
+ <input type=hidden name="question_id" id="question_id" value={{ question.id }}></input>
+
{% if question.type == "mcq" %}
{% for option in question.options.strip.splitlines %}
<input name="answer" type="radio" value="{{option}}" />{{option}} <br/>
diff --git a/testapp/exam/views.py b/testapp/exam/views.py
index bfac15f..4165b7f 100644
--- a/testapp/exam/views.py
+++ b/testapp/exam/views.py
@@ -756,6 +756,10 @@ def question(request, q_id, attempt_num, questionpaper_id, success_msg=None):
'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)
@@ -763,6 +767,19 @@ def question(request, q_id, attempt_num, questionpaper_id, success_msg=None):
def show_question(request, q_id, attempt_num, 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_num,
+ 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':
+ old_skipped = paper.answers.filter(question=quest, skipped=True)
+ _save_skipped_answer(old_skipped, user_code, paper, quest)
if len(q_id) == 0:
msg = 'Congratulations! You have successfully completed the quiz.'
return complete(request, msg, attempt_num, questionpaper_id)
@@ -770,6 +787,21 @@ def show_question(request, q_id, attempt_num, questionpaper_id, success_msg=None
return question(request, q_id, attempt_num, questionpaper_id, success_msg)
+def _save_skipped_answer(old_skipped, user_answer, paper, question):
+ """
+ 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=question, answer=user_answer,
+ correct=False, skipped=True)
+ skipped_answer.save()
+ paper.answers.add(skipped_answer)
+
def check(request, q_id, attempt_num=None, questionpaper_id=None):
"""Checks the answers of the user for particular question"""
@@ -781,10 +813,14 @@ def check(request, q_id, attempt_num=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':
+ old_skipped = paper.answers.filter(question=question, skipped=True)
+ _save_skipped_answer(old_skipped, user_code, paper, question)
next_q = paper.skip()
return show_question(request, next_q, attempt_num, questionpaper_id)
@@ -804,7 +840,6 @@ def check(request, q_id, attempt_num=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,
@@ -838,6 +873,10 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None):
reason = 'The quiz has been deactivated!'
return complete(request, reason, attempt_num, questionpaper_id)
questions, to_attempt, submitted = get_questions(paper)
+ 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,