Skip to content

Commit

Permalink
Look up associated policies for divisions.
Browse files Browse the repository at this point in the history
Fixes #1745.
  • Loading branch information
dracos committed Dec 15, 2023
1 parent e72da9b commit de6038a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 34 deletions.
38 changes: 31 additions & 7 deletions classes/Divisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class Divisions {
* @param Member $member The member to get positions for.
*/

public function __construct(Member $member = null, PolicyPositions $positions = null, Policies $policies = null)
public function __construct(Member $member = null, PolicyPositions $positions = null)
{
$this->member = $member;
$this->positions = $positions;
$this->policies = $policies;
$this->policies = new Policies;
$this->db = new \ParlDB;
}

Expand Down Expand Up @@ -231,7 +231,7 @@ public function getMemberDivisionsForPolicy($policyID = null) {
ORDER by policy_id, division_date DESC",
$args
);

# possibly add another query here to get related policies that use the same votes
return $this->divisionsByPolicy($q);
}

Expand Down Expand Up @@ -584,6 +584,29 @@ private function getBasicDivisionDetails($row, $vote) {

# Policy-related information

# So one option is just to query for it here
# we want to add an array of policies aside the current policy
# and if they have the same or different direction as thie current division
# in the row

# fetch related policies from database
$q = $this->db->query(
"SELECT policy_id, direction
FROM policydivisions
WHERE division_id = :division_id",
array(':division_id' => $row['division_id'])
);
$division['related_policies'] = array();

$policy_lookup = $this->policies->getPolicies();
foreach ($q as $policy) {
$division['related_policies'][] = array(
'policy_id' => $policy['policy_id'],
'policy_title' => $policy_lookup[$policy['policy_id']],
'direction' => $policy['direction'],
);
}

if (array_key_exists('direction', $row)) {
$division['direction'] = $row['direction'];
if ( strpos( $row['direction'], 'strong') !== false ) {
Expand Down Expand Up @@ -615,18 +638,19 @@ private function getParliamentDivisionDetails($row) {
private function divisionsByPolicy($q) {
$policies = array();

# iterate through each division, and adds it to an array of policies
# if there is only one policy being queried, it will be an array of 1
foreach ($q as $row) {
$policy_id = $row['policy_id'];

# if this policy hasn't come up yet, create the key for it
if ( !array_key_exists($policy_id, $policies) ) {
$policies[$policy_id] = array(
'policy_id' => $policy_id,
'divisions' => array()
);
if ( $this->policies ) {
$policies[$policy_id]['desc'] = $this->policies->getPolicies()[$policy_id];
$policies[$policy_id]['header'] = $this->policies->getPolicyDetails($policy_id);
}
$policies[$policy_id]['desc'] = $this->policies->getPolicies()[$policy_id];
$policies[$policy_id]['header'] = $this->policies->getPolicyDetails($policy_id);
if ( $this->positions ) {
$policies[$policy_id]['position'] = $this->positions->positionsById[$policy_id];
}
Expand Down
50 changes: 25 additions & 25 deletions tests/_fixtures/divisions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@
<field name="image_source">http://example.org/source</field>
</row>
<row>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="title">Second Policy</field>
<field name="description">Another policy to test policies</field>
<field name="image">/image/test-policy-image.png</field>
Expand Down Expand Up @@ -812,122 +812,122 @@
</row>
<row>
<field name="division_id">pw-3012-01-01-1-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-2-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-3-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-4-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-5-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-6-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-7-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-8-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-9-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-10-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-11-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-12-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-13-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-14-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-15-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-16-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-17-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-18-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-19-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-20-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-21-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-22-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-23-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
<row>
<field name="division_id">pw-3012-01-01-24-commons</field>
<field name="policy_id">364</field>
<field name="policy_id">810</field>
<field name="direction">Majority</field>
</row>
</table_data>
Expand Down
2 changes: 1 addition & 1 deletion www/docs/mp/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@
$policiesList = new MySociety\TheyWorkForYou\Policies;
}
$positions = new MySociety\TheyWorkForYou\PolicyPositions( $policiesList, $MEMBER );
$divisions = new MySociety\TheyWorkForYou\Divisions($MEMBER, $positions, $policiesList);
$divisions = new MySociety\TheyWorkForYou\Divisions($MEMBER, $positions);

if ( $policyID ) {
$data['policydivisions'] = $divisions->getMemberDivisionsForPolicy($policyID);
Expand Down
4 changes: 4 additions & 0 deletions www/docs/style/sass/pages/_mp.scss
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@
line-height: 1.2em;
}

.policy-vote__related-policies-title {
margin-top: 1em;
}

.policy-votes-hero {
position: relative;
margin-bottom: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,66 @@
<p class="vote-description__covid">This absence may have been affected by <a href="<?= $member_url ?>/votes#covid-19">COVID-19 restrictions</a>.</p>
<?php } ?>
<a class="vote-description__source" href="/divisions/<?= $division['division_id'] ?>/mp/<?= $person_id ?>">Show vote</a>
</li>

<?php
# remove the current policy from the list of related policies
if (isset($policy)) {
$division['related_policies'] = array_filter($division['related_policies'], function($related_policy) use ($policy) {
return $related_policy['policy_id'] != $policy['policy_id'];
});
}

if (isset($policy) && count($division['related_policies'])) {
# We want to split the related policies into two groups:
# 1. those that are the same direction as the current policy (e.g. the vote is in favour of both policies)
# 2. those that are the opposite direction to the current policy (e.g. the vote is read positive in one police, and negative in another)
$same_direction = array();
$other_direction = array();
foreach ($division['related_policies'] as $related_policy) {
$current_policy_direction_for_vote = $division["direction"];
$related_policy_direction_for_vote = $related_policy["direction"];
# remove " (strong)" from the end of the direction if it's present
$current_policy_direction_for_vote = preg_replace("/ \(strong\)$/", "", $current_policy_direction_for_vote);
$related_policy_direction_for_vote = preg_replace("/ \(strong\)$/", "", $related_policy_direction_for_vote);
$is_same_direction = $current_policy_direction_for_vote == $related_policy_direction_for_vote;
# if related direction is 'abstention' then it's the same direction
if ($related_policy_direction_for_vote == "abstention") {
$is_same_direction = true;
}
if ($is_same_direction) {
$same_direction[] = $related_policy;
} else {
$other_direction[] = $related_policy;
}
}

# get an array of the two sets so we can loop through them both in the same code
$related_policies = [
[
"set" => $same_direction,
"title" => "This vote is also related to:",
"class" => "policy-vote__related-policies",
],
[
"set" => $other_direction,
"title" => "This policy conflicts with:",
"class" => "policy-vote__opposing-policies",
],
];

foreach ($related_policies as $related_policy_batch) { ?>
<?php if (count($related_policy_batch["set"]) > 0) { ?>
<p class="policy-vote__related-policies-title"><?= $related_policy_batch["title"] ?></p>
<ul class="<?= $related_policy_batch["class"] ?>">
<?php foreach ($related_policy_batch["set"] as $related_policy) { ?>
<li>
<a href="<?= $member_url ?>/divisions?policy=<?= $related_policy['policy_id'] ?>">
<?= $related_policy['policy_title'] ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
<?php } ?>
</li>

0 comments on commit de6038a

Please sign in to comment.