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 14, 2023
1 parent cfeb1e8 commit ec6e9ff
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,64 @@
<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
$division['related_policies'] = array_filter($division['related_policies'], function($related_policy) use ($policy) {
return $related_policy['policy_id'] != $policy['policy_id'];
});

if (count($division['related_policies']) > 0) {
# 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) { ?>
<h4><?= $related_policy_batch["title"] ?></h4>
<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 ec6e9ff

Please sign in to comment.