Failed Conditions
Branch master (215e8c)
by Johannes
04:26
created

Policies::limitToArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 10
cp 0
crap 12
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
18
    /**
19
     * Policy Positions
20
     *
21
     * Array of all policy positions available to use.
22
     *
23
     * Arrays are in the form `{id} => {text}`
24
     */
25
26
    protected $policies = array(
27
        363 => 'introducing <b>foundation hospitals</b>',
28
        810 => 'greater <b>regulation of gambling</b>',
29
        811 => '<b>smoking bans</b>',
30
        826 => 'equal <b>gay rights</b>',
31
        837 => 'a <strong>wholly elected</strong> House of Lords',
32
        975 => '<strong>investigations</strong> into the Iraq war',
33
        984 => 'replacing <b>Trident</b> with a new nuclear weapons system',
34
        996 => 'a <b>transparent Parliament</b>',
35
        1027 => 'a referendum on the UK\'s membership of the <b>EU</b>',
36
        1030 => 'measures to <b>prevent climate change</b>',
37
        1049 => 'the <b>Iraq war</b>',
38
        1050 => 'the <b>hunting ban</b>',
39
        1051 => 'introducing <b>ID cards</b>',
40
        1052 => 'university <b>tuition fees</b>',
41
        1053 => 'Labour\'s <b title="Including voting to maintain them">anti-terrorism laws</b>',
42
        1065 => 'more <b>EU integration</b>',
43
        1071 => 'allowing ministers to <b>intervene in inquests</b>',
44
        1074 => 'greater <b>autonomy for schools</b>',
45
        1079 => 'removing <b>hereditary peers</b> from the House of Lords',
46
        1084 => 'a more <a href="https://en.wikipedia.org/wiki/Proportional_representation">proportional system</a> for electing MPs',
47
        1087 => 'a <b>stricter asylum system</b>',
48
        1105 => 'the privatisation of <b>Royal Mail</b>',
49
        1109 => 'encouraging <b>occupational pensions</b>',
50
        1110 => 'increasing the <b>rate of VAT</b>',
51
        1113 => 'an <b>equal number of electors</b> per parliamentary constituency',
52
        1120 => 'capping <b>civil service redundancy payments</b>',
53
        1124 => 'automatic enrolment in <b>occupational pensions</b>',
54
        1132 => 'raising England&rsquo;s <b>undergraduate tuition fee</b> cap to &pound;9,000 per year',
55
        1136 => '<b>fewer MPs</b> in the House of Commons',
56
        6667 => 'the policies included in the 2010 <b><a href="http://webarchive.nationalarchives.gov.uk/20100527091800/http://programmeforgovernment.hmg.gov.uk/">Conservative - Liberal Democrat Coalition Agreement</a></b>',
57
        6670 => 'a reduction in spending on <b>welfare benefits</b>',
58
        6671 => 'reducing central government <b>funding of local government</b>',
59
        6672 => 'reducing <b>housing benefit</b> for social tenants deemed to have excess bedrooms (which Labour describe as the "bedroom tax")',
60
        6673 => 'paying higher benefits over longer periods for those unable to work due to <b>illness or disability</b>',
61
        6674 => 'raising <b>welfare benefits</b> at least in line with prices',
62
        6676 => 'reforming the <b>NHS</b> so GPs buy services on behalf of their patients',
63
        6677 => 'restricting the provision of services to <b>private patients</b> by the NHS',
64
        6678 => 'greater restrictions on <b>campaigning by third parties</b>, such as charities, during elections',
65
        6679 => 'reducing the rate of <b>corporation tax</b>',
66
        6680 => 'raising the threshold at which people start to pay <b>income tax</b>',
67
        6681 => 'increasing the tax rate applied to <b>income over &pound;150,000</b>',
68
        6682 => 'ending <b>financial support</b> for some 16-19 year olds in training and further education',
69
        6683 => 'local councils keeping money raised from <b>taxes on business premises</b> in their areas',
70
        6684 => 'making local councils responsible for helping those in <b>financial need</b> afford their <b>council tax</b> and reducing the amount spent on such support',
71
        6685 => 'a <b>banker&rsquo;s bonus tax</b>',
72
        6686 => 'allowing <b>marriage</b> between two people of same sex',
73
        6687 => '<a href="https://en.wikipedia.org/wiki/Academy_(English_school)">academy schools</a>',
74
        6688 => 'use of <b>UK military forces</b> in combat operations overseas',
75
        6690 => 'measures to reduce <b>tax avoidance</b>',
76
        6691 => 'stronger tax <b>incentives for companies to invest</b> in assets',
77
        6692 => 'slowing the rise in <b>rail fares</b>',
78
        6693 => 'lower taxes on <b>fuel for motor vehicles</b>',
79
        6694 => 'higher taxes on <b>alcoholic drinks</b>',
80
        6695 => 'more <b>powers for local councils</b>',
81
        6696 => 'the introduction of elected <b>Police and Crime Commissioners</b>',
82
        6698 => '<b>fixed periods between parliamentary elections</b>',
83
        6699 => 'higher <b>taxes on plane tickets</b>',
84
        6697 => 'selling England&rsquo;s state owned <b>forests</b>',
85
        6702 => 'spending public money to create <b>guaranteed jobs for young people</b> who have spent a long time unemployed',
86
        6703 => 'laws to promote <b>equality and human rights</b>',
87
        6704 => 'financial incentives for <b>low carbon</b> emission <b>electricity generation</b> methods',
88
        6705 => 'requiring pub companies to offer <b>pub landlords rent-only leases</b>',
89
        6706 => 'strengthening the <b>Military Covenant</b>',
90
        6707 => 'restricting the scope of <b>legal aid</b>',
91
        6708 => 'transferring <b>more powers to the Welsh Assembly</b>',
92
        6709 => 'transferring <b>more powers to the Scottish Parliament</b>',
93
        6710 => '<b>culling badgers</b> to tackle bovine tuberculosis',
94
        6711 => 'an annual tax on the value of expensive homes (popularly known as a <b>mansion tax</b>)',
95
        6715 => 'allowing national security sensitive evidence to be put before <b>courts in secret sessions</b>',
96
        6716 => 'allowing employees to exchange some employment <b>rights for shares</b> in the company they work for',
97
        6718 => 'restrictions on <b>fees</b> charged to tenants by <b>letting agents</b>',
98
        6719 => 'limits on success <b>fees</b> paid to lawyers in <b>no-win no fee cases</b>',
99
        6720 => 'a statutory <b>register of lobbyists</b>',
100
        6721 => 'requiring the mass <b>retention of information about communications</b>',
101
        6731 => 'more restrictive <b>regulation of trade union activity</b>',
102
        6732 => 'allowing <b>terminally ill people</b> to be given <b>assistance to end their life</b>',
103
        6733 => 'higher <b>taxes on banks</b>',
104
        6734 => 'stronger <b>enforcement of immigration rules</b>',
105
        6736 => '<b>a veto for MPs</b> from England, Wales and Northern Ireland <b>over laws specifically impacting their part of the UK</b>',
106
        6741 => 'greater regulation of <b>hydraulic fracturing (fracking)</b> to extract shale gas',
107
        6753 => 'new <b>high speed rail</b> infrastructure',
108
        6751 => '<b>mass surveillance</b> of people&rsquo;s communications and activities',
109
        6764 => 'a <b>right to remain for EU nationals</b> already in living in the UK',
110
        6761 => '<b>UK membership of the EU</b>',
111
        6758 => '<b>merging police and fire services</b> under Police and Crime Commissioners',
112
        6756 => 'reducing <b>capital gains tax</b>',
113
        6747 => 'greater public control of <b>bus services</b>',
114
        6746 => 'a <b>publicly owned railway system</b>',
115
        6744 => 'phasing out <b>secure tenancies for life</b>',
116
        6743 => 'charging a <b>market rent to high earners renting a council home</b>',
117
        6757 => '<b>military action against <a href="https://en.wikipedia.org/wiki/Islamic_State_of_Iraq_and_the_Levant">ISIL (Daesh)</a></b>',
118
        842  => 'a lower <b>voting age</b>',
119
    );
120
121
    /**
122
     * Policy Sets
123
     *
124
     * Collections of policies (by ID number) to be used for sorted and
125
     * restricted displays.
126
     */
127
128
    private $sets = array(
129
        'summary' => array(
130
            1113,
131
            1136,
132
            1132,
133
            1052,
134
            1109,
135
            1110,
136
            1027,
137
            1084,
138
            1065,
139
            6670,
140
            6673,
141
            6674,
142
            6678,
143
            984,
144
            837,
145
            1079,
146
            6671,
147
            6672,
148
        ),
149
        'social' => array(
150
            826,
151
            811,
152
            1050,
153
            6686,
154
            6703,
155
            6732,
156
        ),
157
        'foreignpolicy' => array(
158
            6688,
159
            1049,
160
            975,
161
            984,
162
            1065,
163
            1027,
164
            6706,
165
            6764,
166
            6761,
167
            6757,
168
        ),
169
        'welfare' => array(
170
            6672,
171
            6674,
172
            6673,
173
            6684,
174
            6670,
175
            6702,
176
        ),
177
        'taxation' => array(
178
            6680,
179
            1110,
180
            6694,
181
            6699,
182
            6693,
183
            6681,
184
            1109,
185
            1124,
186
            6685,
187
            6733,
188
            6711,
189
            6716,
190
            6731,
191
            6756,
192
        ),
193
        'business' => array(
194
            6679,
195
            6690,
196
            6691,
197
            6753,
198
        ),
199
        'health' => array(
200
            6677,
201
            6676,
202
            363,
203
            811,
204
            6732,
205
        ),
206
        'education' => array(
207
            1074,
208
            1132,
209
            6687,
210
            6682,
211
            1052
212
        ),
213
        'reform' => array(
214
            6671,
215
            1113,
216
            1136,
217
            996,
218
            1084,
219
            837,
220
            6683,
221
            6678,
222
            6698,
223
            1079,
224
            6708,
225
            6709,
226
            6695,
227
            6736,
228
            842,
229
        ),
230
        'home' => array(
231
            1087,
232
            1071,
233
            1051,
234
            6696,
235
            6721,
236
            6734,
237
            6751,
238
            6758,
239
        ),
240
        'environment' => array(
241
            1030,
242
            6693,
243
            6697,
244
            6699,
245
            6704,
246
            6710,
247
            6741,
248
            6753,
249
        ),
250
        'transport' => array(
251
            6747,
252
            6692,
253
            6693,
254
            6699,
255
            6746,
256
        ),
257
        'housing' => array(
258
            6744,
259
            6743,
260
        ),
261
        'misc' => array(
262
            810,
263
            1120,
264
            1053,
265
            1105,
266
            6705,
267
            6707,
268
            6715,
269
            6720,
270
            6719,
271
            6718,
272
            6667,
273
        )
274
    );
275
276
    protected $set_descs = array(
277
        'social' => 'Social Issues',
278
        'foreignpolicy' => 'Foreign Policy and Defence',
279
        'welfare' => 'Welfare and Benefits',
280
        'taxation' => 'Taxation and Employment',
281
        'business' => 'Business and the Economy',
282
        'health' => 'Health',
283
        'education' => 'Education',
284
        'reform' => 'Constitutional Reform',
285
        'home' => 'Home Affairs',
286
        'environment' => 'Environmental Issues',
287
        'transport' => 'Transport',
288
        'housing' => 'Housing',
289
        'misc' => 'Miscellaneous Topics',
290
    );
291
292
    private $db;
293
294
    private $policy_id;
295
296 6
    public function __construct($policy_id = NULL) {
297 6
        $this->db = new \ParlDB;
298
299 6
        if ( $policy_id ) {
300 2
            $this->policy_id = $policy_id;
301 2
            $this->policies = array(
302 2
                $policy_id => $this->policies[$policy_id]
303 2
            );
304 2
        }
305 6
    }
306
307 4
    public function getPolicies() {
308 4
        return $this->policies;
309
    }
310
311
    public function getSetDescriptions() {
312
      return $this->set_descs;
313
    }
314
315
    /**
316
     * Get Array
317
     *
318
     * Return an array of policies.
319
     *
320
     * @return array Array of policies in the form `[ {id} , {text} ]`
321
     */
322 1
    public function getArray() {
323 1
        $out = array();
324 1
        foreach ($this->policies as $policy_id => $policy_text)
325
        {
326 1
            $out[] = array(
327 1
                $policy_id,
328
                $policy_text
329 1
            );
330 1
        }
331 1
        return $out;
332
    }
333
334
    /**
335
     * Shuffle
336
     *
337
     * Shuffles the list of policy positions.
338
     *
339
     * @return self
340
     */
341
342
    public function shuffle() {
343
344
        $keys = array_keys($this->policies);
345
        shuffle($keys);
346
        $random = array();
347
        foreach ($keys as $key) {
348
            $random[$key] = $this->policies[$key];
349
        }
350
351
        $new_policies = new self();
352
        $new_policies->policies = $random;
353
354
        return $new_policies;
355
    }
356
357
    /**
358
     * Limit To Set
359
     *
360
     * Limit the policies to those in a set and order accordingly
361
     *
362
     * @param string $set The name of the set to use.
363
     *
364
     * @return self
365
     */
366 1
    public function limitToSet($set) {
367
368
        // Sanity check the set exists
369 1
        if (isset($this->sets[$set]))
370 1
        {
371 1
            $out = array();
372
            // Reassemble the new policies list based on the set.
373 1
            foreach ($this->sets[$set] as $set_policy)
374
            {
375 1
                if (isset($this->policies[$set_policy]))
376 1
                {
377 1
                    $out[$set_policy] = $this->policies[$set_policy];
378 1
                } else {
379
                    // if we've limited the policies to a single one then we only
380
                    // want to complain here if we're looking for that policy and
381
                    // it does not exist. Otherwise, if the single policy isn't in
382
                    // the set we want to return an empty set
383 1
                    if ( !isset($this->policy_id) || $set_policy == $this->policy_id ) {
384
                        throw new \Exception ('Policy ' . $set_policy . ' in set "' . $set . '" does not exist.');
385
                    }
386
                }
387 1
            }
388
389 1
            $new_policies = new self($this->policy_id);
390 1
            $new_policies->policies = $out;
391
392 1
            return $new_policies;
393
394
        } else {
395
            throw new \Exception ('Policy set "' . $set . '" does not exist.');
396
        }
397
    }
398
399
    public function limitToArray($policies) {
400
          $out = array();
401
          // Reassemble the new policies list based on the set.
402
          foreach ($policies as $policy) {
403
              if (isset($this->policies[$policy])) {
404
                  $out[$policy] = $this->policies[$policy];
405
              }
406
          }
407
408
          $new_policies = new self();
409
          $new_policies->policies = $out;
410
411
          return $new_policies;
412
    }
413
414
    public function getPolicyDetails($policyID) {
415
        $q = $this->db->query(
416
            "SELECT policy_id, title, description, image, image_attrib, image_license, image_license_url, image_source
417
            FROM policies WHERE policy_id = :policy_id",
418
            array(':policy_id' => $policyID)
419
        );
420
421
        $props = array(
422
            'policy_id' => $q->field(0, 'policy_id'),
423
            'title' => $q->field(0, 'title'),
424
            // remove full stops from the end of descriptions. Some of them have them and
425
            // some of them don't so we enforce consistency here
426
            'description' => preg_replace('/\. *$/', '', $q->field(0, 'description')),
427
            'image' => '/images/header-debates-uk.jpg', // TODO: get a better default image
428
            'image_license' => '',
429
            'image_attribution' => '',
430
            'image_source' => '',
431
            'image_license_url' => ''
432
        );
433
434
        $image = $q->field(0, 'image');
435
436
        if ( $image && file_exists(BASEDIR . '/' . $image)) {
437
            $props['image'] = $image;
438
            $props['image_license'] = $q->field(0, 'image_license');
439
            $props['image_attribution'] = $q->field(0, 'image_attrib');
440
            $props['image_source'] = $q->field(0, 'image_source');
441
            $props['image_license_url'] = $q->field(0, 'image_license_url');
442
        }
443
444
        return $props;
445
    }
446
447
}
448