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

[Scorecards ]Added Year Comparison to Homepage #730

Draft
wants to merge 17 commits into
base: 2025-scorecards
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions caps/templates/caps/icons/link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svg width="{{ width | default:'1em' }}" height="{{ height | default:'1em' }}" viewBox="0 0 24 24" class="bi {{ classes }}" {% if role %}role="{{ role }}"{% endif %} fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/></svg>
2 changes: 1 addition & 1 deletion caps/templates/caps/icons/scorecards-star.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<svg width="{{ width | default:'1em' }}" height="{{ height | default:'1em' }}" viewBox="0 0 24 24" class="bi {{ classes }}" {% if role %}role="{{ role }}"{% endif %} fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M10.937.962c.374-.95 1.789-.95 2.164 0l2.418 6.45c.17.428.6.713 1.082.713h5.943c1.098 0 1.577 1.316.713 1.96l-4.229 3.665a1.09 1.09 0 0 0-.395.555c-.068.221-.06.457.02.674l1.544 6.303c.376 1.012-.841 1.882-1.762 1.259l-5.744-3.51a1.2 1.2 0 0 0-1.344 0l-5.744 3.51c-.92.623-2.138-.248-1.762-1.26l1.544-6.302a1.05 1.05 0 0 0 .02-.674 1.089 1.089 0 0 0-.396-.555L.78 10.086c-.864-.645-.383-1.961.713-1.961h5.943c.234 0 .462-.067.656-.194a1.12 1.12 0 0 0 .425-.518l2.42-6.45Z" fill="currentColor" stroke="currentColor" stroke-width=".5" stroke-linecap="round" stroke-linejoin="round"/></svg>
<svg width="{{ width | default:'1em' }}" height="{{ height | default:'1em' }}" viewBox="0 0 24 24" class="bi {{ classes }}" {% if role %}role="{{ role }}"{% endif %} fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M10.937.962c.374-.95 1.789-.95 2.164 0l2.418 6.45c.17.428.6.713 1.082.713h5.943c1.098 0 1.577 1.316.713 1.96l-4.229 3.665a1.09 1.09 0 0 0-.395.555c-.068.221-.06.457.02.674l1.544 6.303c.376 1.012-.841 1.882-1.762 1.259l-5.744-3.51a1.2 1.2 0 0 0-1.344 0l-5.744 3.51c-.92.623-2.138-.248-1.762-1.26l1.544-6.302a1.05 1.05 0 0 0 .02-.674 1.089 1.089 0 0 0-.396-.555L.78 10.086c-.864-.645-.383-1.961.713-1.961h5.943c.234 0 .462-.067.656-.194a1.12 1.12 0 0 0 .425-.518l2.42-6.45Z" fill="currentColor" stroke="#000" stroke-width=".5" stroke-linecap="round" stroke-linejoin="round"/></svg>
102 changes: 77 additions & 25 deletions scoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,48 @@ def questions_answered_for_councils(cls, plan_ids=None, plan_year=None):

return questions

@classmethod
def get_average(cls, scoring_group=None, filter=None, year=None):
if year is None:
plan_year = PlanYear.objects.get(is_current=True)
year = plan_year.year
else:
try:
plan_year = PlanYear.objects.get(year=year)
except PlanYear.DoesNotExist:
plan_year = None

"""
This excludes plans with zero score as it's assumed that if they have 0 then they
were not marked, or the council has no plan, and hence including them would artificially
reduce the average.
"""
has_score = PlanScore.objects.filter(total__gt=0, year=year)
if scoring_group is not None:
has_score = has_score.filter(
council__authority_type__in=scoring_group["types"],
council__country__in=scoring_group["countries"],
)

if filter is not None:
kwargs = {}
for field in PlanSection.FILTER_FIELD_MAP.keys():
if filter.get(field):
kwargs[PlanSection.FILTER_FIELD_MAP[field]] = filter[field]

has_score = has_score.filter(Q(**kwargs))

aggregates = {
"maximum": Max("weighted_total"),
"average": Avg("weighted_total"),
}
if plan_year and plan_year.previous_year:
aggregates["previous_average"] = Avg("previous_year__weighted_total")

has_score_avg = has_score.aggregate(**aggregates)

return has_score, has_score_avg

@classmethod
def ruc_cluster_description(cls, ruc_cluster):
codes_to_descriptions = dict(
Expand Down Expand Up @@ -263,28 +305,8 @@ def section_codes(cls, year=settings.PLAN_YEAR):
def get_average_scores(
cls, scoring_group=None, filter=None, year=settings.PLAN_YEAR
):
"""
This excludes plans with zero score as it's assumed that if they have 0 then they
were not marked, or the council has no plan, and hence including them would artificially
reduce the average.
"""
has_score = PlanScore.objects.filter(total__gt=0, year=year)
if scoring_group is not None:
has_score = has_score.filter(
council__authority_type__in=scoring_group["types"],
council__country__in=scoring_group["countries"],
)

if filter is not None:
kwargs = {}
for field in PlanSection.FILTER_FIELD_MAP.keys():
if filter.get(field):
kwargs[PlanSection.FILTER_FIELD_MAP[field]] = filter[field]

has_score = has_score.filter(Q(**kwargs))

has_score_avg = has_score.aggregate(
maximum=Max("weighted_total"), average=Avg("weighted_total")
has_score, has_score_avg = PlanScore.get_average(
scoring_group=scoring_group, filter=filter, year=year
)
has_score_list = has_score.values_list("pk", flat=True)

Expand All @@ -299,6 +321,8 @@ def get_average_scores(
averages = {}
for score in scores:
averages[score.code] = {
"code": score.code,
"title": score.description,
"weighted": round(score.average_weighted),
"score": round(score.average_score),
"max": score.max_score,
Expand All @@ -316,6 +340,11 @@ def get_average_scores(
"percentage": round(avg_score),
}

if has_score_avg.get("previous_average"):
averages["total"]["change"] = avg_score - round(
has_score_avg["previous_average"]
)

return averages


Expand Down Expand Up @@ -455,7 +484,7 @@ def sections_for_plans(
return sections

@classmethod
def get_all_council_scores(cls, plan_year=settings.PLAN_YEAR):
def get_all_council_scores(cls, plan_year=settings.PLAN_YEAR, as_list=False):
"""
This excludes plans with zero score as it's assumed that if they have 0 then they
were not marked, or the council has no plan
Expand All @@ -464,22 +493,45 @@ def get_all_council_scores(cls, plan_year=settings.PLAN_YEAR):
cls.objects.all()
.select_related("plan_section", "plan_score")
.filter(plan_score__year=plan_year, plan_score__total__gt=0)
.annotate(
previous_year_score=Subquery(
PlanSectionScore.objects.filter(
plan_score=OuterRef("plan_score__previous_year"),
plan_score__council_id=OuterRef("plan_score__council_id"),
plan_section__code=OuterRef("plan_section__code"),
).values("weighted_score")
)
)
.annotate(change=(F("weighted_score") - F("previous_year_score")))
.order_by("plan_score__council_id", "plan_section__code")
.values(
"plan_score__total",
"plan_score__council_id",
"score",
"weighted_score",
"plan_section__code",
"max_score",
"change",
)
)
councils = defaultdict(dict)
if as_list:
councils = defaultdict(list)
else:
councils = defaultdict(dict)
for score in scores:
councils[score["plan_score__council_id"]][score["plan_section__code"]] = {
obj = {
"code": score["plan_section__code"],
"weighted": score["weighted_score"],
"score": score["score"],
"max": score["max_score"],
"change": score["change"],
}
if as_list:
councils[score["plan_score__council_id"]].append(obj)
else:
councils[score["plan_score__council_id"]][
score["plan_section__code"]
] = obj

return councils

Expand Down
46 changes: 45 additions & 1 deletion scoring/static/scoring/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,54 @@ forEachElement('#js-toggle-previous-year-score', function(el) {
});
});

var toggleCheckbox = document.getElementById('js-toggle-previous-year-difference');
if (toggleCheckbox) {
toggleCheckbox.addEventListener('change', function() {
var isChecked = this.checked;

forEachElement('.js-previous-year-difference-header', function(header) {
header.setAttribute('colspan', isChecked ? '2' : '1');
});

forEachElement('.js-previous-year-score-difference', function(element) {
element.style.display = isChecked ? 'revert' : 'none';
});

forEachElement('.js-previous-year-score-difference-table', function(table) {
table.setAttribute('has-difference', isChecked ? 'true' : 'false');
});

this.setAttribute('aria-checked', isChecked);
});
}

// Mobile category selector for Homepage table
forEachElement('.js-category-select', function(categorySelect) {
const announcementElement = document.querySelector('.js-category-select-announcement');

categorySelect.addEventListener('change', function() {
const selectedValue = this.value;
const selectedText = this.options[this.selectedIndex].text;

if (announcementElement) {
announcementElement.textContent = 'Now showing: ' + selectedText;
}

forEachElement('.js-score-row', function(element) {
element.style.display = 'none';
});

forEachElement('.' + selectedValue, function(element) {
element.style.display = 'revert';
});
});
});

function ajaxLoadCouncilTypeScorecard(url) {
const selectors = [
'#home-page-main-filter',
'.scorecard-table'
'.scorecard-table',
'.scorecard-table-mobile'
];

selectors.forEach(selector => {
Expand Down
12 changes: 0 additions & 12 deletions scoring/static/scoring/scss/_utils.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ $utilities: map-merge(
class: column-gap,
values: $spacers
),
"width": map-merge(
map-get($utilities, "width"),
(
values: map-merge(
map-get(map-get($utilities, "width"), "values"),
(
10: 10%,
15: 15%
),
),
),
),
),
);

Expand Down
1 change: 1 addition & 0 deletions scoring/static/scoring/scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
@import "footer";
@import "accordion";
@import "hero";
@import "tables";
@import "scoring-table";
@import "table-section-council";
@import "table-question-council";
Expand Down
3 changes: 1 addition & 2 deletions scoring/static/scoring/scss/mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ $max-viewport: 1792;

@mixin progress-bar-body($color) {
span {
opacity: 0.8;
@include responsive(font-size, 13, 14);
font-size: 0.8rem;
}
.score-bar {
border-color: $color;
Expand Down
Loading
Loading