Skip to content

Commit

Permalink
[Work-in-Progress] Correct answers and points (djaodjin#431)
Browse files Browse the repository at this point in the history
Implements a Quiz Score Calculator and Correct Answer Model.
  • Loading branch information
Deep-Chill committed Feb 20, 2024
1 parent f3b2217 commit 8916894
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
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_answer = 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)
42 changes: 41 additions & 1 deletion djaopsp/scores/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from survey.queries import datetime_or_now

from ..compat import import_string, six
from ..models import ScorecardCache
from ..models import ScorecardCache, CorrectAnswer
from ..utils import get_score_weight


Expand Down Expand Up @@ -150,6 +150,46 @@ def get_scorecards(self, campaign, prefix, title=None, includes=None,
return scorecard_caches


def QuizScoreCalculator(ScoreCalculator):

def __init__(self):
# self.points_unit_id=
pass

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=self.yes
).distinct()

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

if correct_answer and answer.measured == correct_answer.correct_answer.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

0 comments on commit 8916894

Please sign in to comment.