Skip to content

Commit

Permalink
zip QuestionResults colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kakadus committed Nov 13, 2023
1 parent d5c853f commit 2d7868f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
11 changes: 6 additions & 5 deletions evap/evaluation/templatetags/evaluation_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.translation import gettext_lazy as _

from evap.evaluation.models import BASE_UNIPOLAR_CHOICES, Contribution, Evaluation
from evap.results.tools import RatingResult
from evap.rewards.tools import can_reward_points_be_used_by
from evap.student.forms import HeadingField

Expand Down Expand Up @@ -76,7 +77,7 @@

@register.filter(name="zip")
def _zip(a, b):
return zip(a, b)
return zip(a, b, strict=True)


@register.filter
Expand Down Expand Up @@ -110,12 +111,12 @@ def percentage_one_decimal(fraction, population):


@register.filter
def to_colors(choices):
if not choices:
def to_colors(question_result: RatingResult | None):
if not question_result:
# When displaying the course distribution, there are no associated voting choices.
# In that case, we just use the colors of a unipolar scale.
return BASE_UNIPOLAR_CHOICES["colors"]
return choices.colors
return BASE_UNIPOLAR_CHOICES["colors"][:-1]
return question_result.colors


@register.filter
Expand Down
2 changes: 1 addition & 1 deletion evap/results/templates/distribution_with_grade.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="distribution-bar {{ question_result.choices.css_class }}"
{% if question_result.question.is_bipolar_likert_question %} style="left: {{ question_result.minus_balance_count|percentage_one_decimal:question_result.count_sum }}"{% endif %}
>
{% with colors=question_result.choices|to_colors %}
{% with colors=question_result|to_colors %}
{% for value, color in distribution|zip:colors %}
{% if value != 0 %}
<div class="vote-bg-{{ color }}" style="width: {{ value|percentage_one_decimal:1 }};">&nbsp;</div>
Expand Down
9 changes: 5 additions & 4 deletions evap/results/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ def __init__(self, question, answer_counters, additional_text_result=None) -> No
self.additional_text_result = additional_text_result
self.counts = None
self.zipped_choices = None
counts = OrderedDict(
(value, [0, name, color, value]) for (name, color, value) in self.choices.as_name_color_value_tuples()
)
counts.pop(NO_ANSWER)
self.colors = tuple((color for _, _, color, _ in counts.values()))
if answer_counters is not None:
counts = OrderedDict(
(value, [0, name, color, value]) for (name, color, value) in self.choices.as_name_color_value_tuples()
)
counts.pop(NO_ANSWER)
for answer_counter in answer_counters:
counts[answer_counter.answer][0] = answer_counter.count
self.zipped_choices = list(counts.values())
Expand Down

0 comments on commit 2d7868f

Please sign in to comment.