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

Add participant progress bar #2234

Merged
merged 33 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4261e47
hack first version of participant progress bar
Kakadus Jun 28, 2024
721c8fc
PR review
richardebeling Jun 30, 2024
58a3ed0
Cleanup
richardebeling Jun 30, 2024
919ca05
Show correct last vote time
richardebeling Jun 30, 2024
c0c47f2
Tests, edge case handling
richardebeling Jun 30, 2024
24e5384
Fix test failure due to missing setting
richardebeling Jun 30, 2024
ca45744
More edge case handling
richardebeling Jun 30, 2024
9bfe80f
Apply suggestions from code review
richardebeling Jul 1, 2024
4595ff6
refactor GlobalRewards a bit
Kakadus Jul 1, 2024
5da3004
migrate to Fraction
Kakadus Jul 1, 2024
5893423
add test to test correct rounding of 0.07
Kakadus Jul 1, 2024
92c92cf
add test to test correct rounding of 0.07 v2
Kakadus Jul 1, 2024
a26abe9
test against float128
Kakadus Jul 1, 2024
f9fd312
fixup! add test to test correct rounding of 0.07 v2
Kakadus Jul 1, 2024
4a765c2
fixup! fixup! add test to test correct rounding of 0.07 v2
Kakadus Jul 1, 2024
b03517f
nice UI :)
janno42 Jul 1, 2024
d56dba6
removed unused code
janno42 Jul 1, 2024
1af51ee
Uniform naming
richardebeling Jul 1, 2024
7946709
extract student_global_reward.html page
Kakadus Jul 1, 2024
bb4e6a9
remove useless div
Kakadus Jul 2, 2024
e4f944c
replace inlinestyle with classes
Kakadus Jul 2, 2024
0be6bd5
introduce left-percent- and replace last inline style
Kakadus Jul 2, 2024
10b8ea3
Fix indentationl. Consistent long-line formatting (we ignore 120-char…
richardebeling Jul 3, 2024
6ce12d6
Remove unused variable
richardebeling Jul 3, 2024
404d2f3
Pluralization
richardebeling Jul 3, 2024
f81b1ef
Update tests
richardebeling Jul 3, 2024
06a65cb
Linting
richardebeling Jul 3, 2024
2562661
Update evap/static/scss/components/_progress.scss
janno42 Jul 6, 2024
e58d700
Update evap/student/tests/test_views.py
janno42 Jul 6, 2024
fa82909
Update evap/student/templates/student_global_reward.html
janno42 Jul 6, 2024
da420d2
use custom percentage filter
janno42 Jul 6, 2024
8581fac
linter
janno42 Jul 6, 2024
608ae30
Update evap/evaluation/templatetags/evaluation_filters.py
janno42 Jul 7, 2024
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
19 changes: 19 additions & 0 deletions deployment/localsettings.template.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from fractions import Fraction

from django.utils.safestring import mark_safe

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql', # postgresql', 'mysql', 'sqlite3' or 'oracle'.
Expand All @@ -15,3 +19,18 @@

# Make apache work when DEBUG == False
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]

### Evaluation progress rewards
GLOBAL_EVALUATION_PROGRESS_REWARDS: list[tuple[Fraction, str]] = [
(Fraction("0"), "0€"),
(Fraction("0.25"), "1.000€"),
(Fraction("0.6"), "3.000€"),
(Fraction("0.7"), "7.000€"),
(Fraction("0.9"), "10.000€"),
]
GLOBAL_EVALUATION_PROGRESS_EXCLUDED_COURSE_TYPE_IDS: list[int] = []
GLOBAL_EVALUATION_PROGRESS_EXCLUDED_EVALUATION_IDS: list[int] = []
GLOBAL_EVALUATION_PROGRESS_INFO_TEXT = {
richardebeling marked this conversation as resolved.
Show resolved Hide resolved
"de": mark_safe("Deine Teilnahme am Evaluationsprojekt wird helfen. Evaluiere also <b>jetzt</b>!"),
"en": mark_safe("Your participation in the evaluation helps, so evaluate <b>now</b>!"),
}
10 changes: 10 additions & 0 deletions evap/evaluation/templatetags/evaluation_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@
return None


@register.filter
def percentage_zero_on_error(fraction, population):
try:
return f"{int(float(fraction) / float(population) * 100):.0f}%"
except ValueError:
return "0%"

Check warning on line 116 in evap/evaluation/templatetags/evaluation_filters.py

View check run for this annotation

Codecov / codecov/patch

evap/evaluation/templatetags/evaluation_filters.py#L116

Added line #L116 was not covered by tests
except ZeroDivisionError:
return "0%"
janno42 marked this conversation as resolved.
Show resolved Hide resolved


@register.filter
def to_colors(question_result: RatingResult | None):
if question_result is None:
Expand Down
8 changes: 8 additions & 0 deletions evap/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import os
import sys
from fractions import Fraction
from typing import Any

from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
Expand Down Expand Up @@ -352,6 +353,13 @@ class ManifestStaticFilesStorageWithJsReplacement(ManifestStaticFilesStorage):
# Absolute filesystem path to the directory that will hold user-uploaded files.
MEDIA_ROOT = os.path.join(BASE_DIR, "upload")

### Evaluation progress rewards
GLOBAL_EVALUATION_PROGRESS_REWARDS: list[tuple[Fraction, str]] = (
[]
) # (required_voter_ratio between 0 and 1, reward_text)
GLOBAL_EVALUATION_PROGRESS_EXCLUDED_COURSE_TYPE_IDS: list[int] = []
GLOBAL_EVALUATION_PROGRESS_EXCLUDED_EVALUATION_IDS: list[int] = []
GLOBAL_EVALUATION_PROGRESS_INFO_TEXT: dict[str, str] = {"de": "", "en": ""}

### Slogans
SLOGANS_DE = [
Expand Down
2 changes: 1 addition & 1 deletion evap/static/scss/_mixins.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import "../bootstrap/scss/mixins";

@mixin bar-shadow($color) {
text-shadow: 0 0 4px $color, 0 0 4px $color, 0 0 2px $color;
text-shadow: 0 0 2px $color, 0 0 2px $color, 0 0 2px $color, 0 0 2px $color, 0 0 2px $color, 0 0 2px $color;
janno42 marked this conversation as resolved.
Show resolved Hide resolved
}

@mixin no-user-select {
Expand Down
4 changes: 4 additions & 0 deletions evap/static/scss/_utilities.scss
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ a.no-underline:hover {
.width-percent-#{$i} {
width: $i * 1% !important;
}

.left-percent-#{$i} {
left: $i * 1% !important;
}
}
8 changes: 8 additions & 0 deletions evap/static/scss/components/_card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
}
}

.card-outline-light {
border-color: $light-gray;

> .card-header {
background-color: rgba($lighter-gray, 0.5);
}
}

.card-noflex {
flex: none;
display: block;
Expand Down
28 changes: 28 additions & 0 deletions evap/static/scss/components/_progress.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,31 @@
.multi-progress-bar .progress-container {
padding-bottom: 3px;
}

.global-rewards-progress-bar {
position: relative;
display: grid;
padding-top: .5rem;
padding-inline: 1.5rem;

.progress-container {
display: flex;
height: 25px;
grid-column-start: 1;
grid-row-start: 1;
}
}

.progress-step {
transform: translateX(-50%);
width: fit-content;
grid-column-start: 1;
grid-row-start: 2;

.seperator {
width: 1px;
height: 8px;
border: $dark-gray 1px solid;
}
}

9 changes: 9 additions & 0 deletions evap/static/scss/components/_transitions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@
transform: rotate(-90deg);
}
}


.collapse-toggle-light {
font-weight: normal;

&:hover {
text-decoration: none;
}
}
53 changes: 53 additions & 0 deletions evap/student/templates/student_global_reward.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{% load evaluation_filters %}

{% if global_rewards %}
<div class="card mb-3">
<div class="card-header d-flex">
{% translate "Fundraising" %}
{% if global_rewards.last_vote_datetime %}
<div class="ms-auto fw-normal fst-italic">
{% blocktranslate trimmed with time_interval=global_rewards.last_vote_datetime|timesince %}
Last evaluation: {{ time_interval }} ago
{% endblocktranslate %}
</div>
{% endif %}
</div>
<div class="card-body">
<div class="global-rewards-progress-bar pt-2 px-4">
<span class="progress-container">
<span class="progress m-0 bg-info">
<span class="progress-bar bg-primary width-percent-{% widthratio global_rewards.bar_width_votes global_rewards.max_reward_votes 100 %}"></span>
<span class="progress-bartext">
{% blocktranslate trimmed count votes=global_rewards.vote_count with percent=global_rewards.vote_count|percentage_zero_on_error:global_rewards.participation_count %}
{{ votes }} submitted evaluation ({{ percent }})
{% plural %}
{{ votes }} submitted evaluations ({{ percent }})
{% endblocktranslate %}
</span>
</span>
</span>
{% for reward in global_rewards.rewards_with_progress %}
<div class="position-relative progress-step left-percent-{% widthratio reward.progress 1 100 %}">
<div class="mx-auto seperator"></div>
<div class="small text-center">
{{ reward.vote_ratio|percentage:1 }}
</div>
<div class="text-lg-center fw-semibold">{{ reward.text }}</div>
</div>
{% endfor %}
</div>
<div class="card card-outline-light collapsible mt-2">
<div class="card-header d-flex">
<a class="collapse-toggle collapse-toggle-light collapsed" data-bs-toggle="collapse" href="#goal-info-text" aria-controls="goal-info-text">
{% translate "Every vote counts! Read more about our fundraising goal." %}
</a>
</div>
<div class="collapse" id="goal-info-text">
<div class="card-body">
{{ global_rewards.info_text }}
</div>
</div>
</div>
</div>
</div>
{% endif %}
2 changes: 2 additions & 0 deletions evap/student/templates/student_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
</div>
{% endif %}

{% include 'student_global_reward.html' with global_rewards=global_rewards %}

{% if unfinished_evaluations %}
<div class="card card-outline-primary mb-3">
<div class="card-header">
Expand Down
93 changes: 92 additions & 1 deletion evap/student/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from fractions import Fraction
from functools import partial

from django.test.utils import override_settings
Expand Down Expand Up @@ -30,7 +31,8 @@ class TestStudentIndexView(WebTestWith200Check):
def setUpTestData(cls):
# View is only visible to users participating in at least one evaluation.
cls.user = baker.make(UserProfile, email="[email protected]")
baker.make(Evaluation, participants=[cls.user])
cls.semester = baker.make(Semester, is_active=True)
cls.evaluation = baker.make(Evaluation, course__semester=cls.semester, participants=[cls.user])

cls.test_users = [cls.user]

Expand All @@ -52,6 +54,95 @@ def test_num_queries_is_constant(self):
with self.assertNumQueries(FuzzyInt(0, 100)):
self.app.get(self.url, user=self.user)

@override_settings(
GLOBAL_EVALUATION_PROGRESS_REWARDS=[(Fraction(1, 10), "a dog"), (Fraction(5, 10), "a quokka")],
GLOBAL_EVALUATION_PROGRESS_INFO_TEXT={"de": "info_text_str", "en": "info_text_str"},
GLOBAL_EVALUATION_PROGRESS_EXCLUDED_COURSE_TYPE_IDS=[1042],
GLOBAL_EVALUATION_PROGRESS_EXCLUDED_EVALUATION_IDS=[1043],
)
def test_global_reward_progress(self):
excluded_states = [state for state in Evaluation.State if state < Evaluation.State.APPROVED]
included_states = [state for state in Evaluation.State if state >= Evaluation.State.APPROVED]

users = baker.make(UserProfile, _quantity=20, _bulk_create=True)
make_evaluation = partial(
baker.make,
Evaluation,
course__semester=self.semester,
participants=users,
voters=users[:10],
state=Evaluation.State.APPROVED,
)

# excluded
make_evaluation(is_rewarded=False)
make_evaluation(is_single_result=True)
make_evaluation(course__is_private=True)
make_evaluation(id=1043)
make_evaluation(course__type__id=1042)
make_evaluation(_quantity=len(excluded_states), state=iter(excluded_states))

# included
included_evaluations = [
*make_evaluation(_quantity=len(included_states), state=iter(included_states)),
make_evaluation(_voter_count=123, _participant_count=456),
]

baker.make(VoteTimestamp, evaluation=included_evaluations[0])

expected_participants = sum(e.num_participants for e in included_evaluations)
expected_voters = sum(e.num_voters for e in included_evaluations)
expected_voter_percent = 100 * expected_voters // expected_participants

page = self.app.get(self.url, user=self.user)
self.assertIn("Fundraising", page)
self.assertIn("info_text_str", page)
self.assertIn("Last evaluation:", page)
self.assertIn(f"{expected_voters} submitted evaluations ({expected_voter_percent}%)", page)
self.assertIn("a quokka", page)
self.assertIn("10%", page)
self.assertIn("a dog", page)
self.assertIn("50%", page)

@override_settings(GLOBAL_EVALUATION_PROGRESS_REWARDS=[(Fraction("0.07"), "a dog")])
def test_global_reward_progress_edge_cases(self):
# no active semester
Semester.objects.update(is_active=False)
page = self.app.get(self.url, user=self.user)
self.assertNotIn("7%", page)
self.assertNotIn("a dog", page)

# no voters / participants -> possibly zero division
# also: no last vote timestamp
semester = baker.make(Semester, is_active=True)
page = self.app.get(self.url, user=self.user)
self.assertNotIn("Last evaluation:", page)
self.assertIn("0 submitted evaluations (0%)", page)
self.assertIn("7%", page)
self.assertIn("a dog", page)

# more voters than required for last reward
baker.make(
Evaluation,
course__semester=semester,
_voter_count=89,
_participant_count=97,
state=Evaluation.State.EVALUATED,
)
page = self.app.get(self.url, user=self.user)
self.assertIn("89 submitted evaluations (91%)", page) # 91% is intentionally rounded down
janno42 marked this conversation as resolved.
Show resolved Hide resolved
self.assertIn("7%", page)
self.assertIn("a dog", page)

@override_settings(
GLOBAL_EVALUATION_PROGRESS_REWARDS=[],
GLOBAL_EVALUATION_PROGRESS_INFO_TEXT={"de": "info_text_str", "en": "info_text_str"},
)
def test_global_reward_progress_hidden(self):
page = self.app.get(self.url, user=self.user)
self.assertNotIn("Fundraising", page)
self.assertNotIn("info_text_str", page)


@override_settings(INSTITUTION_EMAIL_DOMAINS=["example.com"])
class TestVoteView(WebTest):
Expand Down
Loading