diff options
author | ankitjavalkar | 2021-01-11 10:09:03 +0530 |
---|---|---|
committer | GitHub | 2021-01-11 10:09:03 +0530 |
commit | eb01109de13d6de6e87edc72526f7aec8539802f (patch) | |
tree | e872816bf1321e2617630694a88f2a647a6299cd | |
parent | 98124aaa624ea09e131b6e3563f78bc5d48061ad (diff) | |
parent | d7b7b2967b68045bda590af57a123b4d17d16e9c (diff) | |
download | online_test-eb01109de13d6de6e87edc72526f7aec8539802f.tar.gz online_test-eb01109de13d6de6e87edc72526f7aec8539802f.tar.bz2 online_test-eb01109de13d6de6e87edc72526f7aec8539802f.zip |
Merge pull request #767 from ankitjavalkar/add_stats_q
Add feature to count number of times MCQ option has been selected
-rw-r--r-- | yaksh/models.py | 43 | ||||
-rw-r--r-- | yaksh/templates/yaksh/statistics_question.html | 23 | ||||
-rw-r--r-- | yaksh/templatetags/custom_filters.py | 7 | ||||
-rw-r--r-- | yaksh/test_views.py | 4 |
4 files changed, 64 insertions, 13 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 686d0e6..73fbb66 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1992,6 +1992,32 @@ class AnswerPaperManager(models.Manager): questions.append(question.id) return Counter(questions) + def get_per_answer_stats(self, questionpaper_id, attempt_number, + course_id, status='completed'): + papers = self.filter(question_paper_id=questionpaper_id, + course_id=course_id, + attempt_number=attempt_number, status=status) + questions = Question.objects.filter( + questions__id__in=papers, + ).distinct() + + stats = {} + for question in questions: + answers = Answer.objects.filter( + answerpaper__id__in=papers, question=question.id + ).values('answer', 'question__id', 'answerpaper__id') + question_ans_count = {} + answerpaper_count = [] + for ans in answers: + if ans.get('answerpaper__id'): + if ans.get('answer') not in question_ans_count: + question_ans_count[ans.get('answer')] = 1 + else: + question_ans_count[ans.get('answer')] += 1 + answerpaper_count.append(ans.get('answerpaper__id')) + stats[question] = question_ans_count + return stats + def get_all_questions_answered(self, questionpaper_id, attempt_number, course_id, status='completed'): ''' Return a dict of answered question id as key and count as value''' @@ -2045,16 +2071,27 @@ class AnswerPaperManager(models.Manager): course_id) questions = self.get_all_questions(questionpaper_id, attempt_number, course_id) + per_answer_stats = self.get_per_answer_stats( + questionpaper_id, attempt_number, course_id + ) all_questions = Question.objects.filter( id__in=set(questions), active=True ).order_by('type') for question in all_questions: if question.id in questions_answered: - question_stats[question] = [questions_answered[question.id], - questions[question.id]] + question_stats[question] = { + 'answered': [questions_answered[question.id], + questions[question.id]], + 'per_answer': per_answer_stats[question], + } + else: - question_stats[question] = [0, questions[question.id]] + question_stats[question] = { + 'answered': [0, questions[question.id]], + 'per_answer': per_answer_stats[question], + } + return question_stats def _get_answerpapers_for_quiz(self, questionpaper_id, course_id, diff --git a/yaksh/templates/yaksh/statistics_question.html b/yaksh/templates/yaksh/statistics_question.html index 5983835..d70256b 100644 --- a/yaksh/templates/yaksh/statistics_question.html +++ b/yaksh/templates/yaksh/statistics_question.html @@ -1,4 +1,5 @@ {% extends "manage.html" %} +{% load custom_filters %} {% block title %} Question Statistics {% endblock %} {% block pagetitle %} Statistics for {{ quiz.description }}{% endblock pagetitle %} @@ -20,8 +21,8 @@ {% if question_stats %} <p><b>Total number of participants: {{ total }}</b></p> <table class="table table-responsive-sm"> - <tr class="bg-light yakshred"><th>Question</th><th>Type</th><th>Total</th><th>Answered Correctly</th></tr> - {% for question, value in question_stats.items %} + <tr class="bg-light yakshred"><th>Question</th><th></th><th>Type</th><th>Total</th><th>Answered Correctly</th></tr> + {% for question, data in question_stats.items %} <tr> <td style="width: 45%"> <a href="#collapse_question_{{question.id}}" data-toggle="collapse"> @@ -61,12 +62,14 @@ <p> <ol> {% for tc in question.testcase_set.all %} - <li> - {{ tc.mcqtestcase.options|safe }} - {% if tc.mcqtestcase.correct %} - <span class="badge badge-success">Correct</span> - {% endif %} - </li> + <li> + {{ tc.mcqtestcase.options }} + {% if tc.mcqtestcase.correct %} + <span class="badge badge-primary">Correct</span> + {% endif %} + {% get_dict_value data.per_answer tc.id|stringformat:"i" as num %} + <span class="badge badge-info">Answered: {{ num }}</span> + </li> {% endfor %} </ol> </p> @@ -76,7 +79,9 @@ </div> </td> <td>{{ question.type }}</td> - <td>{{value.1}}</td><td>{{ value.0 }} ({% widthratio value.0 value.1 100 %}%)</td> + <td>{{data.answered.1}}</td><td>{{ data.answered.0 }} ({% widthratio data.answered.0 data.answered.1 100 %}%)</td> + + </tr> {% endfor %} </table> diff --git a/yaksh/templatetags/custom_filters.py b/yaksh/templatetags/custom_filters.py index 7eba939..81572a7 100644 --- a/yaksh/templatetags/custom_filters.py +++ b/yaksh/templatetags/custom_filters.py @@ -211,3 +211,10 @@ def get_lesson_views(course_id, lesson_id): return TrackLesson.objects.filter( course_id=course_id, lesson_id=lesson_id, watched=True ).count(), course.students.count() + + +@register.simple_tag +def get_dict_value(dictionary, key): + return dictionary.get(key, None) + + diff --git a/yaksh/test_views.py b/yaksh/test_views.py index b46a6d9..31066d1 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -5656,10 +5656,12 @@ class TestShowStatistics(TestCase): "course_id": self.course.id}), follow=True ) + question_stats = response.context['question_stats'] self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'yaksh/statistics_question.html') + self.assertIn(self.question, list(question_stats.keys())) self.assertSequenceEqual( - response.context['question_stats'][self.question], [1, 1] + list(question_stats.values())[0]['answered'], [1, 1] ) self.assertEqual(response.context['attempts'][0], 1) self.assertEqual(response.context['total'], 1) |