|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Party Cohort Class |
| 4 | + * Policy Distribution Collection Class |
5 | 5 | * |
6 | 6 | * @package TheyWorkForYou |
7 | 7 | */ |
8 | 8 |
|
9 | 9 | namespace MySociety\TheyWorkForYou; |
10 | 10 |
|
11 | 11 | /** |
12 | | - * Policy DistributionCollection |
| 12 | + * PolicyDistributionCollection |
13 | 13 | * This brings together a set of a person's policy distributions for a given period and party. |
14 | 14 | * It covers items in the same 'group' (health, social, etc) |
15 | 15 | */ |
@@ -49,6 +49,35 @@ public function __construct( |
49 | 49 | $this->policy_pairs = $policy_pairs; |
50 | 50 | } |
51 | 51 |
|
| 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 | + |
52 | 81 | public function latestUpdate(array $latest_dates) { |
53 | 82 | $latest_date = null; |
54 | 83 | foreach ($this->policy_pairs as $pair) { |
|
0 commit comments