summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
authoradityacp2017-01-13 09:47:05 +0530
committerGitHub2017-01-13 09:47:05 +0530
commit0e3e0cdbf322df9b5c8b7da0301cebe71eadcbe9 (patch)
tree8969f5db3aeb70350e089e97fb5e37bd0e4db1ab /yaksh
parent7fb288ffd992c912a8e2288aa97d6c6ceeedf1a1 (diff)
parent57a5d99c7ed5aed498943686b2c951bdaba3852a (diff)
downloadonline_test-0e3e0cdbf322df9b5c8b7da0301cebe71eadcbe9.tar.gz
online_test-0e3e0cdbf322df9b5c8b7da0301cebe71eadcbe9.tar.bz2
online_test-0e3e0cdbf322df9b5c8b7da0301cebe71eadcbe9.zip
Merge pull request #179 from maheshgudi/mcq_bugfix
Fixed a bug which would mark all MCQ and MCC questions' answers as correct
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/models.py17
-rw-r--r--yaksh/templates/yaksh/question.html22
-rw-r--r--yaksh/test_models.py56
-rw-r--r--yaksh/views.py6
4 files changed, 56 insertions, 45 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index d65970b..271ed6d 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -1059,28 +1059,25 @@ class AnswerPaper(models.Model):
For code questions success is True only if the answer is correct.
"""
- result = {'success': True, 'error': ['Incorrect answer'], 'weight': 0.0}
- correct = False
+ result = {'success': False, 'error': ['Incorrect answer'], 'weight': 0.0}
if user_answer is not None:
if question.type == 'mcq':
expected_answer = question.get_test_case(correct=True).options
if user_answer.strip() == expected_answer.strip():
- correct = True
+ result['success'] = True
result['error'] = ['Correct answer']
elif question.type == 'mcc':
expected_answers = []
for opt in question.get_test_cases(correct=True):
expected_answers.append(opt.options)
if set(user_answer) == set(expected_answers):
+ result['success'] = True
result['error'] = ['Correct answer']
- correct = True
elif question.type == 'code':
user_dir = self.user.profile.get_user_dir()
json_result = code_server.run_code(question.language, json_data, user_dir)
result = json.loads(json_result)
- if result.get('success'):
- correct = True
- return correct, result
+ return result
def regrade(self, question_id):
try:
@@ -1105,10 +1102,10 @@ class AnswerPaper(models.Model):
answer = user_answer.answer
json_data = question.consolidate_answer_data(answer) \
if question.type == 'code' else None
- correct, result = self.validate_answer(answer, question, json_data)
- user_answer.correct = correct
+ result = self.validate_answer(answer, question, json_data)
+ user_answer.correct = result.get('success')
user_answer.error = result.get('error')
- if correct:
+ if result.get('success'):
user_answer.marks = (question.points * result['weight'] /
question.get_maximum_test_case_weight()) \
if question.partial_grading and question.type == 'code' else question.points
diff --git a/yaksh/templates/yaksh/question.html b/yaksh/templates/yaksh/question.html
index 7cbca57..6473c2f 100644
--- a/yaksh/templates/yaksh/question.html
+++ b/yaksh/templates/yaksh/question.html
@@ -161,33 +161,11 @@ function call_skip(url)
</div>
<div class="panel-body">
{% if question.type == "mcq" %}
- {% if error_message %}
- <p>
- <div class="panel panel-danger">
- <div class="panel-heading">
- {% for err in error_message %}
- {{ err }}
- {% endfor %}
- </div>
- </div>
- </p>
- {% endif %}
{% for test_case in test_cases %}
<input name="answer" type="radio" value="{{ test_case.options }}" />{{ test_case.options|safe }} <br/>
{% endfor %}
{% endif %}
{% if question.type == "mcc" %}
- {% if error_message %}
- <p>
- <div class="panel panel-danger">
- <div class="panel-heading">
- {% for err in error_message %}
- {{ err }}
- {% endfor %}
- </div>
- </div>
- </p>
- {% endif %}
{% for test_case in test_cases %}
<input name="answer" type="checkbox" value="{{ test_case.options }}"> {{ test_case.options|safe }}
<br>
diff --git a/yaksh/test_models.py b/yaksh/test_models.py
index 48dfb8e..1a6bcba 100644
--- a/yaksh/test_models.py
+++ b/yaksh/test_models.py
@@ -533,22 +533,22 @@ class AnswerPaperTestCases(unittest.TestCase):
)
self.mcc_based_testcase.save()
- def test_validate_and_regrade_mcc_question(self):
+ def test_validate_and_regrade_mcc_correct_answer(self):
# Given
mcc_answer = ['a']
self.answer = Answer(question=self.question3,
- answer=mcc_answer,
- )
+ answer=mcc_answer,
+ )
self.answer.save()
self.answerpaper.answers.add(self.answer)
# When
json_data = None
- correct, result = self.answerpaper.validate_answer(mcc_answer,
- self.question3, json_data)
+ result = self.answerpaper.validate_answer(mcc_answer,
+ self.question3, json_data
+ )
# Then
- self.assertTrue(correct)
self.assertTrue(result['success'])
self.assertEqual(result['error'], ['Correct answer'])
self.answer.correct = True
@@ -570,7 +570,7 @@ class AnswerPaperTestCases(unittest.TestCase):
self.assertEqual(self.answer.marks, 0)
self.assertFalse(self.answer.correct)
- def test_validate_and_regrade_mcq_question(self):
+ def test_validate_and_regrade_mcq_correct_answer(self):
# Given
mcq_answer = 'a'
self.answer = Answer(question=self.question2,
@@ -581,11 +581,11 @@ class AnswerPaperTestCases(unittest.TestCase):
# When
json_data = None
- correct, result = self.answerpaper.validate_answer(mcq_answer,
- self.question2, json_data)
+ result = self.answerpaper.validate_answer(mcq_answer,
+ self.question2, json_data
+ )
# Then
- self.assertTrue(correct)
self.assertTrue(result['success'])
self.answer.correct = True
self.answer.marks = 1
@@ -607,6 +607,42 @@ class AnswerPaperTestCases(unittest.TestCase):
self.assertFalse(self.answer.correct)
+ def test_mcq_incorrect_answer(self):
+ # Given
+ mcq_answer = 'a'
+ self.answer = Answer(question=self.question2,
+ answer=mcq_answer,
+ )
+ self.answer.save()
+ self.answerpaper.answers.add(self.answer)
+
+ # When
+ json_data = None
+ result = self.answerpaper.validate_answer(mcq_answer,
+ self.question2, json_data
+ )
+
+ # Then
+ self.assertTrue(result['success'])
+
+ def test_mcc_incorrect_answer(self):
+ # Given
+ mcc_answer = ['b']
+ self.answer = Answer(question=self.question3,
+ answer=mcc_answer,
+ )
+ self.answer.save()
+ self.answerpaper.answers.add(self.answer)
+
+ # When
+ json_data = None
+ result = self.answerpaper.validate_answer(mcc_answer,
+ self.question3, json_data
+ )
+
+ # Then
+ self.assertFalse(result['success'])
+
def test_answerpaper(self):
""" Test Answer Paper"""
self.assertEqual(self.answerpaper.user.username, 'demo_user')
diff --git a/yaksh/views.py b/yaksh/views.py
index 7ecf6aa..0d54f8a 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -488,12 +488,12 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None):
# safely in a separate process (the code_server.py) running as nobody.
json_data = current_question.consolidate_answer_data(user_answer) \
if current_question.type == 'code' else None
- correct, result = paper.validate_answer(user_answer, current_question, json_data)
- if correct or result.get('success'):
+ result = paper.validate_answer(user_answer, current_question, json_data)
+ if result.get('success'):
new_answer.marks = (current_question.points * result['weight'] /
current_question.get_maximum_test_case_weight()) \
if current_question.partial_grading and current_question.type == 'code' else current_question.points
- new_answer.correct = correct
+ new_answer.correct = result.get('success')
error_message = None
new_answer.error = json.dumps(result.get('error'))
next_question = paper.completed_question(current_question.id)