Skip to content

Commit

Permalink
display MP/Party policy differences on profile page
Browse files Browse the repository at this point in the history
If an MP has policies where they are in opposition to their party then
display that in the votes box on their profile page. Otherwise default
to the existing random selection of votes.
  • Loading branch information
struan committed Jul 2, 2015
1 parent 0ccafb6 commit 50cd881
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 23 deletions.
43 changes: 43 additions & 0 deletions classes/Member.php
Expand Up @@ -314,6 +314,49 @@ private function left_house_line($house, $house_name) {
}
}

public function getPartyPolicyDiffs($party, $policiesList, $positions, $only_diffs = false) {
$policy_diffs = array();
$party_positions = $party->getAllPolicyPositions($policiesList);

foreach ( $positions->positionsById as $policy_id => $details ) {
if ( $details['score'] != -1 ) {
$mp_score = $details['score'];
$party_score = $party_positions[$policy_id]['score'];

$score_diff = $this->calculatePolicyDiffScore($mp_score, $party_score);

// skip anything that isn't a yes vs no diff
if ( $only_diffs && $score_diff < 2 ) {
continue;
}
$policy_diffs[$policy_id] = $score_diff;
}
}

$score_diffs = arsort($policy_diffs);

return $policy_diffs;
}

private function calculatePolicyDiffScore( $mp_score, $party_score ) {
$score_diff = abs($mp_score - $party_score);
// if they are on opposite sides of mixture of for and against
if (
( $mp_score < 0.4 && $party_score > 0.6 ) ||
( $mp_score > 0.6 && $party_score < 0.4 )
) {
$score_diff += 2;
// if on is mixture of for and against and one is for/against
} else if (
( $mp_score > 0.4 && $mp_score < 0.6 && ( $party_score > 0.6 || $party_score < 0.4 ) ) ||
( $party_score > 0.4 && $party_score < 0.6 && ( $mp_score > 0.6 || $mp_score < 0.4 ) )
) {
$score_diff += 1;
}

return $score_diff;
}

public static function getRegionalList($postcode, $house, $type) {
$db = new \ParlDB;

Expand Down
67 changes: 54 additions & 13 deletions classes/Party.php
Expand Up @@ -22,22 +22,43 @@ public function __construct($name) {
$this->db = new \ParlDB;
}

public function calculateAllPolicyPositions($policies) {
$positions = array();

foreach ( $policies->getPolicies() as $policy_id => $policy_text ) {
list( $position, $score ) = $this->calculate_policy_position($policy_id, true);
if ( $position === null ) {
continue;
}
public function getAllPolicyPositions($policies) {
$positions = $this->fetchPolicyPositionsByMethod($policies, "policy_position");

$positions[$policy_id] = array(
'policy_id' => $policy_id,
'position' => $position,
'score' => $score,
'desc' => $policy_text
);
return $positions;
}

public function policy_position($policy_id, $want_score = false) {
$position = $this->db->query(
"SELECT score
FROM partypolicy
WHERE
party = :party
AND house = 1
AND policy_id = :policy_id",
array(
':party' => $this->name,
':policy_id' => $policy_id
)
);

if ( $position->rows ) {
$score = $position->field(0, 'score');
$score_desc = score_to_strongly($score);

if ( $want_score ) {
return array( $score_desc, $score);
} else {
return $score_desc;
}
} else {
return null;
}
}

public function calculateAllPolicyPositions($policies) {
$positions = $this->fetchPolicyPositionsByMethod($policies, "calculate_policy_position");

return $positions;
}
Expand Down Expand Up @@ -126,6 +147,26 @@ public function cache_position( $position ) {
);
}

private function fetchPolicyPositionsByMethod($policies, $method) {
$positions = array();

foreach ( $policies->getPolicies() as $policy_id => $policy_text ) {
list( $position, $score ) = $this->$method($policy_id, true);
if ( $position === null ) {
continue;
}

$positions[$policy_id] = array(
'policy_id' => $policy_id,
'position' => $position,
'score' => $score,
'desc' => $policy_text
);
}

return $positions;
}

private function get_vote_scores($vote) {
$absent = 1;
$both = 1;
Expand Down
4 changes: 3 additions & 1 deletion classes/PolicyPositions.php
Expand Up @@ -112,6 +112,7 @@ private function getMemberPolicyPositions ($limit = NULL) {
$this->positionsById[$policy[0]] = array(
'policy_id' => $policy[0],
'desc' => $dream_info['full_sentence'],
'score' => $dream_info['score'],
'voted' => $dream_info['voted']
);
$i++;
Expand All @@ -138,6 +139,7 @@ private function displayDreamComparison($dreamid, $desc, $inverse=false) {

if (isset($extra_info["public_whip_dreammp${dreamid}_distance"])) {
if ($extra_info["public_whip_dreammp${dreamid}_both_voted"] == 0) {
$dmpscore = -1;
$english = 'never voted';
$dmpdesc = 'Has <strong>never voted</strong> on';
} else {
Expand All @@ -152,7 +154,7 @@ private function displayDreamComparison($dreamid, $desc, $inverse=false) {
// $extra_info["public_whip_dreammp${dreamid}_both_voted"];
}
$dmpdesc .= ' ' . $desc;
$out = array( 'full_sentence' => $dmpdesc, 'voted' => $english );
$out = array( 'full_sentence' => $dmpdesc, 'voted' => $english, 'score' => $dmpscore );
}
return $out;
}
Expand Down
39 changes: 36 additions & 3 deletions tests/PartyTest.php
Expand Up @@ -21,11 +21,36 @@ public function testLoad() {
$this->assertEquals( 'A Party', $party->name );
}

public function testCalculatePositions() {
public function testGetPolicyPositions() {
$positions = $this->getAllPositions('getAllPolicyPositions');

$expectedPositions = array(
'363' => array(
'position' => 'strongly against',
'score' => '0.9',
'desc' => 'introducing <b>foundation hospitals</b>',
'policy_id' => 363
)
);

$this->assertEquals($expectedPositions, $positions);

}

public function testGetRestrictedPositions() {
$party = new MySociety\TheyWorkForYou\Party('A Party');
$policies = new MySociety\TheyWorkForYou\Policies();
$policies = new MySociety\TheyWorkForYou\Policies(6667);

$positions = $party->getAllPolicyPositions($policies);

$positions = $party->calculateAllPolicyPositions($policies);
$expectedPositions = array();

$this->assertEquals($expectedPositions, $positions);

}

public function testCalculatePositions() {
$positions = $this->getAllPositions('calculateAllPolicyPositions');

$expectedResults = array(
'810' => array(
Expand All @@ -39,4 +64,12 @@ public function testCalculatePositions() {
$this->assertEquals($expectedResults, $positions);
}

private function getAllPositions($method) {
$party = new MySociety\TheyWorkForYou\Party('A Party');
$policies = new MySociety\TheyWorkForYou\Policies();

$positions = $party->$method($policies);
return $positions;
}

}
11 changes: 11 additions & 0 deletions www/docs/mp/index.php
Expand Up @@ -464,6 +464,17 @@
// Generate limited voting record list
$data['policyPositions'] = new MySociety\TheyWorkForYou\PolicyPositions($policies, $MEMBER, 6);

// generate party policy diffs
$party = new MySociety\TheyWorkForYou\Party($MEMBER->party());
$positions = new MySociety\TheyWorkForYou\PolicyPositions( $policiesList, $MEMBER );
$party_positions = $party->getAllPolicyPositions($policiesList);
$policy_diffs = $MEMBER->getPartyPolicyDiffs($party, $policiesList, $positions, true);

$data['sorted_diffs'] = $policy_diffs;
$data['party_positions'] = $party_positions;
$data['positions'] = $positions->positionsById;
$data['policies'] = $policiesList->getPolicies();

// Send the output for rendering
MySociety\TheyWorkForYou\Renderer::output('mp/profile', $data);

Expand Down
39 changes: 33 additions & 6 deletions www/includes/easyparliament/templates/html/mp/profile.php
Expand Up @@ -55,11 +55,39 @@
<?php if (count($policyPositions->positions) > 0): ?>
<div class="panel">
<a name="votes"></a>
<h2 data-magellan-destination="votes">A selection of <?= $full_name ?>'s votes</h2>
<h2 data-magellan-destination="votes"><?= $full_name ?>&rsquo;s voting in Parliament</h2>

<p><a href="<?= $member_url ?>/votes">See full list of topics voted on</a></p>

<?php if (count($policyPositions->positions) > 0): ?>
<?php if (count($sorted_diffs) > 0): ?>

<p>
<?= $full_name ?> is a <?= $party ?> MP, and so on the <b>vast majority</b> of issues votes the <b>same way</b> as other <?= $party ?> MPs.
</p>

<p>
However, <?= $full_name ?> sometimes <b>differs</b> from their party collegues, such as:
</p>

<ul class="vote-descriptions">
<?php foreach ($sorted_diffs as $policy_id => $score): ?>
<li>
<?= ucfirst(strip_tags($policies[$policy_id])) ?>. <?= $full_name ?> voted <b><?= $positions[$policy_id]['voted'] ?></b>. Most <?= $party ?> MPs voted <b><?= $party_positions[$policy_id]['position'] ?></b>.
<a class="vote-description__source" href="<?= $member_url?>/divisions?policy=<?= $policy_id ?>">Details</a>
</li>
<?php endforeach; ?>
</ul>

<p>We have <b>lots more</b> plain English analysis of <?= $full_name ?>&rsquo;s voting record on issues like health, welfare, taxation and more. Visit <a href="<?= $member_url ?>/votes"><?= $full_name ?>&rsquo;s full vote analysis page</a> for more.</p>

<?php elseif (count($policyPositions->positions) > 0 ): ?>
<p>
<?= $full_name ?> is a <?= $party ?> MP, and so on the <b>vast majority</b> of issues votes the <b>same way</b> as other <?= $party ?> MPs.
</p>


<p>
This is a selection of <?= $full_name ?>&rsquo;s votes.
</p>

<ul class="vote-descriptions">
<?php foreach ($policyPositions->positions as $key_vote): ?>
Expand All @@ -70,9 +98,8 @@
<?php endforeach; ?>
</ul>

<p>New: more <a href="<?= $member_url ?>/votes">analysis of votes</a> on <a href="<?= $member_url ?>/votes#health">health</a>, <a href="<?= $member_url ?>/votes#welfare">welfare</a>, <a href="<?= $member_url ?>/votes#foreign">foreign policy</a>, <a href="<?= $member_url ?>/votes#social">social issues</a>, <a href="<?= $member_url ?>/votes#taxation">taxation</a> and more.</p>

<?php else: ?>
<p>We have <b>lots more</b> plain English analysis of <?= $full_name ?>&rsquo;s voting record on issues like health, welfare, taxation and more. Visit <a href="<?= $member_url ?>/votes"><?= $full_name ?>&rsquo;s full vote analysis page</a> for more.</p>
<?php elseif (count($policyPositions->positions) == 0 ): ?>

<p>No votes to display.</p>

Expand Down

0 comments on commit 50cd881

Please sign in to comment.