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
|
|
|
|
27
|
|
|
public function getPairfromPolicyID(int $policy_id): ?PolicyDistributionPair { |
28
|
|
|
foreach ($this->policy_pairs as $pair) { |
29
|
|
|
if ($pair->policy_id == $policy_id) { |
30
|
|
|
return $pair; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
return null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
string $group_slug, |
38
|
|
|
string $group_name, |
39
|
|
|
string $comparison_period_slug, |
40
|
|
|
int $person_id, |
41
|
|
|
string $party_slug, |
42
|
|
|
array $policy_pairs |
43
|
|
|
) { |
44
|
|
|
$this->group_slug = $group_slug; |
45
|
|
|
$this->group_name = $group_name; |
46
|
|
|
$this->comparison_period_slug = $comparison_period_slug; |
47
|
|
|
$this->person_id = $person_id; |
48
|
|
|
$this->party_slug = $party_slug; |
49
|
|
|
$this->policy_pairs = $policy_pairs; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function latestUpdate(array $latest_dates) { |
53
|
|
|
$latest_date = null; |
54
|
|
|
foreach ($this->policy_pairs as $pair) { |
55
|
|
|
$date = $latest_dates[$pair->policy_id] ?? null; |
56
|
|
|
if ($date && (!$latest_date || $date > $latest_date)) { |
57
|
|
|
$latest_date = $date; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return $latest_date; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Retrieves an array of PolicyDistributionCollection objects for a specific person. |
66
|
|
|
* |
67
|
|
|
* @param array $allowed_sets An array of allowed policy sets. |
68
|
|
|
* @param int $person_id The ID of the person. |
69
|
|
|
* @param string $party_slug The slug of the person's political party. |
70
|
|
|
* @param string $period_slug The slug representing the time period. |
71
|
|
|
* @param int $house The house type (default is HOUSE_TYPE_COMMONS). |
72
|
|
|
* |
73
|
|
|
* @return PolicyDistributionCollection[] An array of PolicyDistributionCollection objects. |
74
|
|
|
*/ |
75
|
|
|
public static function getPersonDistributions(array $allowed_sets, int $person_id, string $party_slug, string $period_slug, int $house = HOUSE_TYPE_COMMONS): array { |
76
|
|
|
|
77
|
|
|
$pairs = PolicyDistributionPair::getPersonDistributions($person_id, $party_slug, $period_slug, $house); |
78
|
|
|
|
79
|
|
|
$policies = new Policies(); |
80
|
|
|
|
81
|
|
|
$collections = []; |
82
|
|
|
|
83
|
|
|
foreach ($policies->getSets() as $set_slug => $policy_ids) { |
84
|
|
|
if (!in_array($set_slug, $allowed_sets)) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
$group_name = $policies->getSetDescriptions()[$set_slug]; |
88
|
|
|
$group_slug = $set_slug; |
89
|
|
|
$comparison_period_slug = $period_slug; |
90
|
|
|
$policy_pairs = array_filter($pairs, function ($pair) use ($policy_ids) { |
91
|
|
|
return in_array($pair->getPolicyID(), $policy_ids) && !$pair->member_distribution->noDataAvailable(); |
92
|
|
|
}); |
93
|
|
|
$collection = new PolicyDistributionCollection($group_slug, $group_name, $comparison_period_slug, $person_id, $party_slug, $policy_pairs); |
94
|
|
|
|
95
|
|
|
$collections[] = $collection; |
96
|
|
|
} |
97
|
|
|
return $collections; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Retrieves the significant policy distributions from an array of PolicyDistributionCollection objects. |
102
|
|
|
* |
103
|
|
|
* @param PolicyDistributionCollection[] $distributions An array of PolicyDistributionCollection objects. |
104
|
|
|
* @return ?PolicyDistributionCollection An array of significant PolicyDistributionCollection objects. |
105
|
|
|
*/ |
106
|
|
|
public static function getSignificantDistributions(array $distributions): ?PolicyDistributionCollection { |
107
|
|
|
|
108
|
|
|
if (empty($distributions)) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$significant_pairs = []; |
113
|
|
|
|
114
|
|
|
foreach ($distributions as $distribution) { |
115
|
|
|
foreach ($distribution->policy_pairs as $pair) { |
116
|
|
|
if ($pair->sigScoreDifference()) { |
117
|
|
|
$significant_pairs[] = $pair; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// possibly we've picked up a duplicate distribution if it was in multiple sets |
123
|
|
|
// so dedupe based on the policy_id |
124
|
|
|
|
125
|
|
|
$significant_pairs = array_values(array_unique($significant_pairs, SORT_REGULAR)); |
126
|
|
|
|
127
|
|
|
return new PolicyDistributionCollection( |
128
|
|
|
$distributions[0]->group_slug, |
129
|
|
|
$distributions[0]->group_name, |
130
|
|
|
$distributions[0]->comparison_period_slug, |
131
|
|
|
$distributions[0]->person_id, |
132
|
|
|
$distributions[0]->party_slug, |
133
|
|
|
$significant_pairs |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|