|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Policies Class |
|
4
|
|
|
* |
|
5
|
|
|
* @package TheyWorkForYou |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace MySociety\TheyWorkForYou; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Policies |
|
12
|
|
|
* |
|
13
|
|
|
* Class to provide management (selection, sorting etc) of PublicWhip policies. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
class Policies { |
|
17
|
|
|
private $commons_only = [ |
|
18
|
|
|
811, |
|
19
|
|
|
826, |
|
20
|
|
|
1053, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
# policies where votes affected by covid voting restrictions |
|
24
|
|
|
protected $covid_affected = [1136, 6860]; |
|
25
|
|
|
|
|
26
|
|
|
private $db; |
|
27
|
|
|
|
|
28
|
|
|
private $policy_id; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array $policies key-value of policy id to the context description. |
|
31
|
|
|
* e.g. "363": "introducing <b>foundation hospitals</b>" |
|
32
|
|
|
*/ |
|
33
|
|
|
private array $policies; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array $sets - key-value pair of a set slug to an array of policy IDs |
|
37
|
|
|
* in that set. |
|
38
|
|
|
* e.g. "foreignpolicy": ["975", "984"] |
|
39
|
|
|
*/ |
|
40
|
|
|
private array $sets; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array $set_descs - key-value pairs of a set slug to a description |
|
44
|
|
|
* of that set. |
|
45
|
|
|
* e.g. "foreignpolicy": "Foreign Policy" |
|
46
|
|
|
*/ |
|
47
|
|
|
private array $set_descs; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array $all_policy_agreements - key-value pair of a policy ID to |
|
51
|
|
|
* an array of |
|
52
|
|
|
* agreements links. |
|
53
|
|
|
* e.g. "1030": [{ |
|
54
|
|
|
* "division_name": "Approval of SI setting 2050 Net Zero target date", |
|
55
|
|
|
* "alignment": "agree", |
|
56
|
|
|
* "gid": "2019-06-24b.530.1", |
|
57
|
|
|
* "url": "/debates/?id=2019-06-24b.530.1", |
|
58
|
|
|
* "house": "commons", |
|
59
|
|
|
* "strength": "strong", |
|
60
|
|
|
* "date": "2019-06-24" |
|
61
|
|
|
* }] |
|
62
|
|
|
*/ |
|
63
|
|
|
public array $all_policy_agreements; |
|
64
|
|
|
|
|
65
|
|
|
public function __construct($policy_id = null) { |
|
66
|
|
|
$this->db = new \ParlDB(); |
|
67
|
|
|
|
|
68
|
|
|
if (defined('TESTING') && TESTING == true) { |
|
|
|
|
|
|
69
|
|
|
$policy_data = json_decode(file_get_contents(dirname(__FILE__) . '/../tests/policies.json'), true); |
|
70
|
|
|
} else { |
|
71
|
|
|
$policy_data = json_decode(file_get_contents(RAWDATA . '/scrapedjson/policies.json'), true); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
$this->policies = $policy_data['policies']; |
|
74
|
|
|
$this->set_descs = $policy_data['set_descs']; |
|
75
|
|
|
$this->sets = $policy_data['sets']; |
|
76
|
|
|
|
|
77
|
|
|
$this->all_policy_agreements = $policy_data['agreements'] ?? []; |
|
78
|
|
|
|
|
79
|
|
|
if ($policy_id) { |
|
80
|
|
|
$this->policy_id = $policy_id; |
|
81
|
|
|
$this->policies = [ |
|
82
|
|
|
$policy_id => $this->policies[$policy_id], |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getCovidAffected() { |
|
88
|
|
|
return $this->covid_affected; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getPolicies() { |
|
92
|
|
|
return $this->policies; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getPolicyIDs() { |
|
96
|
|
|
return array_keys($this->policies); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getSets() { |
|
100
|
|
|
return $this->sets; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getSetDescriptions() { |
|
104
|
|
|
return $this->set_descs; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get Array |
|
109
|
|
|
* |
|
110
|
|
|
* Return an array of policies. |
|
111
|
|
|
* |
|
112
|
|
|
* @return array Array of policies in the form `[ {id} , {text} ]` |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getPoliciesData() { |
|
115
|
|
|
$out = []; |
|
116
|
|
|
foreach ($this->policies as $policy_id => $policy_text) { |
|
117
|
|
|
$out[] = [ |
|
118
|
|
|
'id' => $policy_id, |
|
119
|
|
|
'text' => $policy_text, |
|
120
|
|
|
'commons_only' => in_array($policy_id, $this->commons_only), |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
return $out; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Shuffle |
|
128
|
|
|
* |
|
129
|
|
|
* Shuffles the list of policy positions. |
|
130
|
|
|
* |
|
131
|
|
|
* @return self |
|
132
|
|
|
*/ |
|
133
|
|
|
|
|
134
|
|
|
public function shuffle() { |
|
135
|
|
|
$random = Utility\Shuffle::keyValue($this->policies); |
|
136
|
|
|
|
|
137
|
|
|
$new_policies = new self(); |
|
138
|
|
|
$new_policies->policies = $random; |
|
139
|
|
|
|
|
140
|
|
|
return $new_policies; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Limit To Set |
|
145
|
|
|
* |
|
146
|
|
|
* Limit the policies to those in a set and order accordingly |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $set The name of the set to use. |
|
149
|
|
|
* |
|
150
|
|
|
* @return self |
|
151
|
|
|
*/ |
|
152
|
|
|
public function limitToSet($set) { |
|
153
|
|
|
|
|
154
|
|
|
// Sanity check the set exists |
|
155
|
|
|
if (isset($this->sets[$set])) { |
|
156
|
|
|
$out = []; |
|
157
|
|
|
// Reassemble the new policies list based on the set. |
|
158
|
|
|
foreach ($this->sets[$set] as $set_policy) { |
|
159
|
|
|
if (isset($this->policies[$set_policy])) { |
|
160
|
|
|
$out[$set_policy] = $this->policies[$set_policy]; |
|
161
|
|
|
} else { |
|
162
|
|
|
// if we've limited the policies to a single one then we only |
|
163
|
|
|
// want to complain here if we're looking for that policy and |
|
164
|
|
|
// it does not exist. Otherwise, if the single policy isn't in |
|
165
|
|
|
// the set we want to return an empty set |
|
166
|
|
|
if (!isset($this->policy_id) || $set_policy == $this->policy_id) { |
|
167
|
|
|
throw new \Exception('Policy ' . $set_policy . ' in set "' . $set . '" does not exist.'); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$new_policies = new self($this->policy_id); |
|
173
|
|
|
$new_policies->policies = $out; |
|
174
|
|
|
|
|
175
|
|
|
return $new_policies->shuffle(); |
|
176
|
|
|
|
|
177
|
|
|
} else { |
|
178
|
|
|
throw new \Exception('Policy set "' . $set . '" does not exist.'); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function limitToArray($policies) { |
|
183
|
|
|
$out = []; |
|
184
|
|
|
// Reassemble the new policies list based on the set. |
|
185
|
|
|
foreach ($policies as $policy) { |
|
186
|
|
|
if (isset($this->policies[$policy])) { |
|
187
|
|
|
$out[$policy] = $this->policies[$policy]; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$new_policies = new self(); |
|
192
|
|
|
$new_policies->policies = $out; |
|
193
|
|
|
|
|
194
|
|
|
return $new_policies; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function getPolicyDetails($policyID) { |
|
198
|
|
|
$q = $this->db->query( |
|
199
|
|
|
"SELECT policy_id, title, description, image, image_attrib, image_license, image_license_url, image_source |
|
200
|
|
|
FROM policies WHERE policy_id = :policy_id", |
|
201
|
|
|
[':policy_id' => $policyID] |
|
202
|
|
|
)->first(); |
|
203
|
|
|
|
|
204
|
|
|
$props = [ |
|
205
|
|
|
'policy_id' => $q['policy_id'], |
|
206
|
|
|
'title' => $q['title'], |
|
207
|
|
|
// remove full stops from the end of descriptions. Some of them have them and |
|
208
|
|
|
// some of them don't so we enforce consistency here |
|
209
|
|
|
'description' => preg_replace('/\. *$/', '', $q['description']), |
|
210
|
|
|
'image' => '/images/header-debates-uk.jpg', // TODO: get a better default image |
|
211
|
|
|
'image_license' => '', |
|
212
|
|
|
'image_attribution' => '', |
|
213
|
|
|
'image_source' => '', |
|
214
|
|
|
'image_license_url' => '', |
|
215
|
|
|
]; |
|
216
|
|
|
|
|
217
|
|
|
$image = $q['image']; |
|
218
|
|
|
|
|
219
|
|
|
if ($image && file_exists(BASEDIR . '/' . $image)) { |
|
220
|
|
|
$props['image'] = $image; |
|
221
|
|
|
$props['image_license'] = $q['image_license']; |
|
222
|
|
|
$props['image_attribution'] = $q['image_attrib']; |
|
223
|
|
|
$props['image_source'] = $q['image_source']; |
|
224
|
|
|
$props['image_license_url'] = $q['image_license_url']; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $props; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.