Skip to content

Commit a3ab07e

Browse files
committed
Group voting summaries by periods
- policies are grouped into five year periods - start and end date displayed are adjusted depending on entry dates
1 parent 5ecae59 commit a3ab07e

3 files changed

Lines changed: 86 additions & 11 deletions

File tree

classes/PolicyDistributionCollection.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22

33
/**
4-
* Party Cohort Class
4+
* Policy Distribution Collection Class
55
*
66
* @package TheyWorkForYou
77
*/
88

99
namespace MySociety\TheyWorkForYou;
1010

1111
/**
12-
* Policy DistributionCollection
12+
* PolicyDistributionCollection
1313
* This brings together a set of a person's policy distributions for a given period and party.
1414
* It covers items in the same 'group' (health, social, etc)
1515
*/
@@ -49,6 +49,35 @@ public function __construct(
4949
$this->policy_pairs = $policy_pairs;
5050
}
5151

52+
/**
53+
54+
* @return PolicyDistributionYearGroup[]
55+
*/
56+
public function getPairsByRecencyBucket(int $bucket_size = 5, ?int $person_start_year = null, ?int $person_end_year = null): array {
57+
// Votes can't be in the future, and shouldn't extend past when the
58+
// person left office.
59+
$current_year = (int) date('Y');
60+
$cap_high = $person_end_year ? min($person_end_year, $current_year) : $current_year;
61+
62+
$buckets = [];
63+
foreach ($this->policy_pairs as $pair) {
64+
$year = $pair->member_distribution->end_year
65+
?: $pair->member_distribution->start_year;
66+
$bucket_start_year = intdiv($year, $bucket_size) * $bucket_size;
67+
$buckets[$bucket_start_year][] = $pair;
68+
}
69+
krsort($buckets); // most recent start year first
70+
71+
$result = [];
72+
foreach ($buckets as $bucket_start_year => $pairs) {
73+
shuffle($pairs); // keep random order within the bucket
74+
$low = $person_start_year ? max($bucket_start_year, $person_start_year) : $bucket_start_year;
75+
$high = min($bucket_start_year + $bucket_size - 1, $cap_high);
76+
$result[] = new PolicyDistributionYearGroup($low, $high, $pairs);
77+
}
78+
return $result;
79+
}
80+
5281
public function latestUpdate(array $latest_dates) {
5382
$latest_date = null;
5483
foreach ($this->policy_pairs as $pair) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Policy Distribution YearG roup
5+
*
6+
* @package TheyWorkForYou
7+
*/
8+
9+
namespace MySociety\TheyWorkForYou;
10+
11+
/**
12+
* PolicyDistributionYearGroup
13+
* A set of policy pairs that share a recency window (based on the year the
14+
* member most recently voted on the policy). Used to add a time structure
15+
* beneath each policy group in the all-time voting summary.
16+
*/
17+
18+
class PolicyDistributionYearGroup {
19+
public int $low_year;
20+
public int $high_year;
21+
/** @var PolicyDistributionPair[] */
22+
public array $policy_pairs;
23+
24+
public function __construct(int $low_year, int $high_year, array $policy_pairs) {
25+
$this->low_year = $low_year;
26+
$this->high_year = $high_year;
27+
$this->policy_pairs = $policy_pairs;
28+
}
29+
30+
/**
31+
* Time phrase describing the year group, designed to read after "Last voted on",
32+
* e.g. "between 2015 and 2019" or "in 2024".
33+
*/
34+
public function label(): string {
35+
if ($this->low_year === $this->high_year) {
36+
return "in {$this->high_year}";
37+
} else {
38+
return "between {$this->low_year} and {$this->high_year}";
39+
}
40+
}
41+
}

www/includes/easyparliament/templates/html/mp/votes.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,20 @@
151151
How <?= $full_name ?> voted on <?= $segment->group_name ?>&nbsp;<small><a class="nav-anchor" href="<?= $member_url ?>/votes#<?= $segment->group_slug ?>">#</a></small>
152152
</h2>
153153

154-
<p>For votes held while they were in office:</p>
155-
156-
<ul class="vote-descriptions">
157-
<?php foreach ($segment->policy_pairs as $policy_pair) {
158-
159-
include '_vote_description.php';
160-
161-
} ?>
162-
</ul>
154+
<?php
155+
$person_start_year = !empty($entry_date) ? (int) $entry_date : null;
156+
$person_end_year = (!empty($leave_date) && (int) $leave_date !== 9999) ? (int) $leave_date : null;
157+
?>
158+
<?php foreach ($segment->getPairsByRecencyBucket(5, $person_start_year, $person_end_year) as $bucket) { ?>
159+
<h3 class="vote-recency-heading">Last voted on <?= $bucket->label() ?></h3>
160+
<ul class="vote-descriptions">
161+
<?php foreach ($bucket->policy_pairs as $policy_pair) {
162+
163+
include '_vote_description.php';
164+
165+
} ?>
166+
</ul>
167+
<?php } ?>
163168

164169
<p class="voting-information-provenance">
165170

0 commit comments

Comments
 (0)