Skip to content

Commit

Permalink
Rearchitect how this works
Browse files Browse the repository at this point in the history
In support of openva/rs-api#10.
  • Loading branch information
waldoj committed Feb 17, 2018
1 parent 84b1d0c commit 855b55f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
57 changes: 44 additions & 13 deletions htdocs/includes/class.Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,62 @@ class Vote
{

# Take an LIS ID, return a vote tally.
function get_aggregate($lis_id, $session_id)
function get_aggregate()
{

# Make sure we've got the information that we need.
if (!isset($lis_id) || empty($lis_id))
if (empty($this->lis_id))
{
return FALSE;
}

if (!isset($session_id) || empty($session_id))
if ( empty($this->session_id) && empty($this->session_year) )
{
return FALSE;
}

# Check that the data is clean.
if (strlen($lis_id) > 12)
if (strlen($this->lis_id) > 12)
{
return FALSE;
}
if (strlen($session_id) > 3)
if (strlen($this->session_id) > 4)
{
return FALSE;
}
if (strlen($this->session_year) <> 4)
{
return FALSE;
}

$database = new Database;
$database->connect_old();

# If we have a session year, but not a session ID, look up the session ID.
if ( empty($this->session_id) && !empty($this->session_year) )
{

$sql = 'SELECT id
FROM sessions
WHERE year="' . $this->session_year . '"
AND suffix IS NULL';
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0)
{
die('No such vote found.');
}
$session_info = mysql_fetch_assoc($result);
$this->session_id = $session_info['id'];

}

/*
* Query the DB.
*/
$sql = 'SELECT chamber, outcome, tally
FROM votes
WHERE lis_id="' . $lis_id . '"
AND session_id = ' . $session_id;
WHERE lis_id="' . $this->lis_id . '"
AND session_id = ' . $this->session_id;
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0)
{
Expand All @@ -47,30 +72,36 @@ function get_aggregate($lis_id, $session_id)

}

function get_detailed($lis_id, $session_id)
/*
* Get detailed information about how individual legislators voted.
*/
function get_detailed()
{

# Make sure we've got the information that we need.
if (!isset($lis_id) || empty($lis_id))
if (!isset($this->lis_id) || empty($this->lis_id))
{
return FALSE;
}

if (!isset($session_id) || empty($session_id))
if (!isset($this->session_id) || empty($this->session_id))
{
return FALSE;
}

# Check that the data is clean.
if (strlen($lis_id) > 12)
if (strlen($this->lis_id) > 12)
{
return FALSE;
}
if (strlen($session_id) > 3)
if (strlen($this->session_id) > 3)
{
return FALSE;
}

$database = new Database;
$database->connect_old();

// The following bit was commented out of the WHERE portion of this query:
//
// AND votes.session_id='.$bill['session_id'].'
Expand All @@ -90,7 +121,7 @@ function get_detailed($lis_id, $session_id)
ON representatives_votes.representative_id = representatives.id
LEFT JOIN districts
ON representatives.district_id=districts.id
WHERE votes.lis_id="'.$lis_id.'" AND votes.session_id="' . $session_id . '"
WHERE votes.lis_id="'.$this->lis_id.'" AND votes.session_id="' . $this->session_id . '"
ORDER BY vote ASC, name ASC';
$result = mysql_query($sql);
if (mysql_num_rows($result) < 1)
Expand Down
6 changes: 4 additions & 2 deletions htdocs/vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
* Create a new instance of the Vote class, and get aggregate data about the outcome.
*/
$vote_info = new Vote;
$vote = $vote_info->get_aggregate($lis_id, $bill['session_id']);
$vote_info->lis_id = $lis_id;
$vote_info->session_id = $bill['session_id'];
$vote = $vote_info->get_aggregate();

$page_title = strtoupper($bill['number']) . ': ' . $bill['catch_line'];
$page_body = '<p>';
Expand All @@ -105,7 +107,7 @@
/*
* Get detailed information about the vote -- who voted how.
*/
$legislators = $vote_info->get_detailed($lis_id, $bill['session_id']);
$legislators = $vote_info->get_detailed();

# Step through the legislators data to establish which party voted which way, building up
# an array of data.
Expand Down

0 comments on commit 855b55f

Please sign in to comment.