pearweb_election
[ class tree: pearweb_election ] [ index: pearweb_election ] [ all elements ]

Source for file election_results.php

Documentation is available at election_results.php

  1. <?php
  2. /**
  3.  * Get common settings.
  4.  */
  5. require_once dirname(__FILE__) . '/../include/pear-config.php';
  6. // Get the database class.
  7. require_once 'DB.php';
  8. $options = array(
  9.     'persistent' => false,
  10.     'portability' => DB_PORTABILITY_ALL,
  11. );
  12. $dbh =& DB::connect(PEAR_DATABASE_DSN, $options);
  13. if (PEAR::isError($dbh)) {
  14.     die ("Failed to connect: $dsn\n");
  15. }
  16.  
  17. $dbh->setFetchMode(DB_FETCHMODE_ASSOC);
  18. $toProcess = $dbh->getAll('
  19.             SELECT *
  20.             FROM elections e LEFT JOIN election_results r on e.id = r.election_id
  21.             WHERE r.election_id IS NULL AND e.voteend < NOW()
  22.         ');
  23. if (count($toProcess)) {
  24.     foreach ($toProcess as $election) {
  25.         $totalabstain = $dbh->getOne('
  26.             SELECT COUNT(*) FROM election_votes_abstain WHERE election_id=?
  27.         ', array($election['id']));
  28.         if ($election['maximum_choices'] == 1) {
  29.             $totalvotes = $dbh->getOne('
  30.                 SELECT COUNT(*) FROM election_votes_single WHERE election_id=?
  31.             ', array($election['id'])) + $totalabstain;
  32.             $results = $dbh->getAll('
  33.                 SELECT COUNT(*) as total, vote
  34.                 FROM election_votes_single
  35.                 WHERE
  36.                     election_id=?
  37.                 GROUP BY vote
  38.             ', array($election['id']), DB_FETCHMODE_ASSOC);
  39.         } else {
  40.             $totalvotes = $dbh->getOne('
  41.                 SELECT COUNT(*) FROM election_votes_multiple WHERE election_id=?
  42.             ', array($election['id'])) + $totalabstain;
  43.             $results = $dbh->getAll('
  44.                 SELECT COUNT(*) as total, vote
  45.                 FROM election_votes_multiple
  46.                 WHERE
  47.                     election_id=?
  48.                 GROUP BY vote
  49.             ', array($election['id']), DB_FETCHMODE_ASSOC);
  50.         }
  51.         foreach ($results as $vote) {
  52.             $dbh->query('
  53.                 INSERT INTO election_results
  54.                 (election_id, choice, votepercent, votetotal)
  55.                 VALUES(?,?,?,?)
  56.             ', array($election['id'], $vote['vote'], $vote['total'] / $totalvotes,
  57.                 $vote['total']));
  58.         }
  59.     }
  60. }

Documentation generated on Mon, 11 Mar 2019 15:34:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.