Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct answers and points (#431) #445

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion djaopsp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.conf import settings as django_settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from survey.models import Sample, get_extra_field_class, Campaign
from survey.models import Sample, get_extra_field_class, Campaign, Question, Choice
from pages.models import PageElement

from .compat import python_2_unicode_compatible
Expand Down Expand Up @@ -113,3 +113,14 @@ class SurveyEvent(models.Model):

def __str__(self):
return "%s-campaign" % str(self.element)


class CorrectAnswer(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE,
related_name="correct_answers")
correct_choice = models.ForeignKey(Choice, on_delete=models.CASCADE,
related_name="correct_answers_on")
points_per_answer = models.IntegerField(default=1)

def __str__(self):
return "%s-CorrectAnswer" % str(self.question.path)
36 changes: 36 additions & 0 deletions djaopsp/scores/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ def get_scorecards(self, campaign, prefix, title=None, includes=None,
return scorecard_caches


class QuizScoreCalculator(ScoreCalculator):

def get_scored_answers(self, campaign, includes=None,
excludes=None, prefix=None):
"""
Retuns answers with score computed based on the CorrectAnswer model.
"""
results = []
queryset = Answer.objects.filter(
unit_id=F('question__default_unit_id'),
sample__in=includes,
question__default_unit_id=self.yes_no_unit_id,
question__path__startswith=prefix,
measured__isnull=False
).distinct()

for answer in queryset:
correct_answer = answer.question.correct_answers.first()

if correct_answer and answer.measured == correct_answer.correct_choice.pk:
answer.measured = correct_answer.points_per_answer
# Not sure if this if/else statement is the best way for this logic. Might make more
# sense to implement it within get_score_weight
else:
answer.measured = get_score_weight(campaign, answer.question.path, default_value=0)

answer.numerator = answer.measured # XXX for freeze_scores
answer.unit_id = self.points_unit_id
answer.answer_id = answer.pk # XXX for freeze_scores
answer.is_planned = includes[0].extra # XXX for freeze_scores
answer.id = answer.question_id # XXX for freeze_scores
results += [answer]

return results


def freeze_scores(sample, excludes=None, collected_by=None, created_at=None,
segment_path=None, score_sample=None):
"""
Expand Down