|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Party Cohort Class |
|
5
|
|
|
* |
|
6
|
|
|
* @package TheyWorkForYou |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace MySociety\TheyWorkForYou; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Policy DistributionCollection |
|
13
|
|
|
* This brings together a set of a person's policy distributions for a given period and party. |
|
14
|
|
|
* It covers items in the same 'group' (health, social, etc) |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
class PolicyDistributionCollection { |
|
18
|
|
|
public string $group_slug; |
|
19
|
|
|
public string $group_name; |
|
20
|
|
|
public string $comparison_period_slug; |
|
21
|
|
|
public int $person_id; |
|
22
|
|
|
public string $party_slug; |
|
23
|
|
|
/** @var PolicyDistributionPair[] */ |
|
24
|
|
|
public array $policy_pairs; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
string $group_slug, |
|
28
|
|
|
string $group_name, |
|
29
|
|
|
string $comparison_period_slug, |
|
30
|
|
|
int $person_id, |
|
31
|
|
|
string $party_slug, |
|
32
|
|
|
array $policy_pairs |
|
33
|
|
|
) { |
|
34
|
|
|
$this->group_slug = $group_slug; |
|
35
|
|
|
$this->group_name = $group_name; |
|
36
|
|
|
$this->comparison_period_slug = $comparison_period_slug; |
|
37
|
|
|
$this->person_id = $person_id; |
|
38
|
|
|
$this->party_slug = $party_slug; |
|
39
|
|
|
$this->policy_pairs = $policy_pairs; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function latestUpdate(array $latest_dates) { |
|
43
|
|
|
$latest_date = null; |
|
44
|
|
|
foreach ($this->policy_pairs as $pair) { |
|
45
|
|
|
$date = $latest_dates[$pair->policy_id] ?? null; |
|
46
|
|
|
if ($date && (!$latest_date || $date > $latest_date)) { |
|
47
|
|
|
$latest_date = $date; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
return $latest_date; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
public static function getPersonDistributions(array $allowed_sets, int $person_id, string $party_slug, string $period_slug, int $house = HOUSE_TYPE_COMMONS): array { |
|
55
|
|
|
|
|
56
|
|
|
$pairs = PolicyDistributionPair::getPersonDistributions($person_id, $party_slug, $period_slug, $house); |
|
57
|
|
|
|
|
58
|
|
|
$policies = new Policies(); |
|
59
|
|
|
|
|
60
|
|
|
$collections = []; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($policies->getSets() as $set_slug => $policy_ids) { |
|
63
|
|
|
if (!in_array($set_slug, $allowed_sets)) { |
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
$group_name = $policies->getSetDescriptions()[$set_slug]; |
|
67
|
|
|
$group_slug = $set_slug; |
|
68
|
|
|
$comparison_period_slug = $period_slug; |
|
69
|
|
|
$policy_pairs = array_filter($pairs, function ($pair) use ($policy_ids) { |
|
70
|
|
|
return in_array($pair->getPolicyID(), $policy_ids) && !$pair->member_distribution->noDataAvailable(); |
|
71
|
|
|
}); |
|
72
|
|
|
$collection = new PolicyDistributionCollection($group_slug, $group_name, $comparison_period_slug, $person_id, $party_slug, $policy_pairs); |
|
73
|
|
|
|
|
74
|
|
|
$collections[] = $collection; |
|
75
|
|
|
} |
|
76
|
|
|
return $collections; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Retrieves the significant policy distributions from an array of PolicyDistributionCollection objects. |
|
81
|
|
|
* |
|
82
|
|
|
* @param PolicyDistributionCollection[] $distributions An array of PolicyDistributionCollection objects. |
|
83
|
|
|
* @return ?PolicyDistributionCollection An array of significant PolicyDistributionCollection objects. |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function getSignificantDistributions(array $distributions): ?PolicyDistributionCollection { |
|
86
|
|
|
|
|
87
|
|
|
if (empty($distributions)) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$significant_pairs = []; |
|
92
|
|
|
|
|
93
|
|
|
foreach ($distributions as $distribution) { |
|
94
|
|
|
foreach ($distribution->policy_pairs as $pair) { |
|
95
|
|
|
if ($pair->sigScoreDifference()) { |
|
96
|
|
|
$significant_pairs[] = $pair; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// possibly we've picked up a duplicate distribution if it was in multiple sets |
|
102
|
|
|
// so dedupe based on the policy_id |
|
103
|
|
|
|
|
104
|
|
|
$significant_pairs = array_values(array_unique($significant_pairs, SORT_REGULAR)); |
|
105
|
|
|
|
|
106
|
|
|
return new PolicyDistributionCollection( |
|
107
|
|
|
$distributions[0]->group_slug, |
|
108
|
|
|
$distributions[0]->group_name, |
|
109
|
|
|
$distributions[0]->comparison_period_slug, |
|
110
|
|
|
$distributions[0]->person_id, |
|
111
|
|
|
$distributions[0]->party_slug, |
|
112
|
|
|
$significant_pairs |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|