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 getSetDescriptions() { |
100
|
|
|
return $this->set_descs; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Array |
105
|
|
|
* |
106
|
|
|
* Return an array of policies. |
107
|
|
|
* |
108
|
|
|
* @return array Array of policies in the form `[ {id} , {text} ]` |
109
|
|
|
*/ |
110
|
|
|
public function getPoliciesData() { |
111
|
|
|
$out = []; |
112
|
|
|
foreach ($this->policies as $policy_id => $policy_text) { |
113
|
|
|
$out[] = [ |
114
|
|
|
'id' => $policy_id, |
115
|
|
|
'text' => $policy_text, |
116
|
|
|
'commons_only' => in_array($policy_id, $this->commons_only), |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
return $out; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Shuffle |
124
|
|
|
* |
125
|
|
|
* Shuffles the list of policy positions. |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
|
|
|
130
|
|
|
public function shuffle() { |
131
|
|
|
$random = Utility\Shuffle::keyValue($this->policies); |
132
|
|
|
|
133
|
|
|
$new_policies = new self(); |
134
|
|
|
$new_policies->policies = $random; |
135
|
|
|
|
136
|
|
|
return $new_policies; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Limit To Set |
141
|
|
|
* |
142
|
|
|
* Limit the policies to those in a set and order accordingly |
143
|
|
|
* |
144
|
|
|
* @param string $set The name of the set to use. |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
|
|
public function limitToSet($set) { |
149
|
|
|
|
150
|
|
|
// Sanity check the set exists |
151
|
|
|
if (isset($this->sets[$set])) { |
152
|
|
|
$out = []; |
153
|
|
|
// Reassemble the new policies list based on the set. |
154
|
|
|
foreach ($this->sets[$set] as $set_policy) { |
155
|
|
|
if (isset($this->policies[$set_policy])) { |
156
|
|
|
$out[$set_policy] = $this->policies[$set_policy]; |
157
|
|
|
} else { |
158
|
|
|
// if we've limited the policies to a single one then we only |
159
|
|
|
// want to complain here if we're looking for that policy and |
160
|
|
|
// it does not exist. Otherwise, if the single policy isn't in |
161
|
|
|
// the set we want to return an empty set |
162
|
|
|
if (!isset($this->policy_id) || $set_policy == $this->policy_id) { |
163
|
|
|
throw new \Exception('Policy ' . $set_policy . ' in set "' . $set . '" does not exist.'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$new_policies = new self($this->policy_id); |
169
|
|
|
$new_policies->policies = $out; |
170
|
|
|
|
171
|
|
|
return $new_policies->shuffle(); |
172
|
|
|
|
173
|
|
|
} else { |
174
|
|
|
throw new \Exception('Policy set "' . $set . '" does not exist.'); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function limitToArray($policies) { |
179
|
|
|
$out = []; |
180
|
|
|
// Reassemble the new policies list based on the set. |
181
|
|
|
foreach ($policies as $policy) { |
182
|
|
|
if (isset($this->policies[$policy])) { |
183
|
|
|
$out[$policy] = $this->policies[$policy]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$new_policies = new self(); |
188
|
|
|
$new_policies->policies = $out; |
189
|
|
|
|
190
|
|
|
return $new_policies; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getPolicyDetails($policyID) { |
194
|
|
|
$q = $this->db->query( |
195
|
|
|
"SELECT policy_id, title, description, image, image_attrib, image_license, image_license_url, image_source |
196
|
|
|
FROM policies WHERE policy_id = :policy_id", |
197
|
|
|
[':policy_id' => $policyID] |
198
|
|
|
)->first(); |
199
|
|
|
|
200
|
|
|
$props = [ |
201
|
|
|
'policy_id' => $q['policy_id'], |
202
|
|
|
'title' => $q['title'], |
203
|
|
|
// remove full stops from the end of descriptions. Some of them have them and |
204
|
|
|
// some of them don't so we enforce consistency here |
205
|
|
|
'description' => preg_replace('/\. *$/', '', $q['description']), |
206
|
|
|
'image' => '/images/header-debates-uk.jpg', // TODO: get a better default image |
207
|
|
|
'image_license' => '', |
208
|
|
|
'image_attribution' => '', |
209
|
|
|
'image_source' => '', |
210
|
|
|
'image_license_url' => '', |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
$image = $q['image']; |
214
|
|
|
|
215
|
|
|
if ($image && file_exists(BASEDIR . '/' . $image)) { |
216
|
|
|
$props['image'] = $image; |
217
|
|
|
$props['image_license'] = $q['image_license']; |
218
|
|
|
$props['image_attribution'] = $q['image_attrib']; |
219
|
|
|
$props['image_source'] = $q['image_source']; |
220
|
|
|
$props['image_license_url'] = $q['image_license_url']; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $props; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.