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

Source for file pear-election.php

Documentation is available at pear-election.php

  1. <?php
  2. class PEAR_Election
  3. {
  4.     var $dbh;
  5.     var $karma;
  6.     var $user;
  7.     function PEAR_Election()
  8.     {
  9.         $this->dbh &$GLOBALS['dbh'];
  10.         $this->user = isset($GLOBALS['auth_user']$GLOBALS['auth_user']->handle : false;
  11.         $this->karma =new Damblan_Karma($this->dbh);
  12.     }
  13.  
  14.     function listElections()
  15.     {
  16.         if ($this->karma->has($this->user'pear.admin')) {
  17.             return $this->dbh->getAll('
  18.                 SELECT
  19.                     IF(votestart > NOW(),"no","yes") as active, elections.*
  20.                 FROM elections
  21.                 ORDER BY votestart DESC
  22.             'array()DB_FETCHMODE_ASSOC);
  23.         else {
  24.             // if we aren't admin, we can't touch other people's elections
  25.             return $this->dbh->getAll('
  26.                 SELECT
  27.                     IF(votestart > NOW(),"no","yes") as active, elections.*
  28.                 FROM elections WHERE
  29.                     votestart > NOW() AND
  30.                     creator=?
  31.             'array($this->user)DB_FETCHMODE_ASSOC);
  32.         }
  33.     }
  34.  
  35.     function electionExists($id)
  36.     {
  37.         return $this->dbh->getOne('SELECT id FROM elections WHERE id=?'array($id));
  38.     }
  39.  
  40.     function setupChoices($id$info)
  41.     {
  42.         $all $this->dbh->getAssoc('
  43.             SELECT choice,summary,summary_link
  44.             FROM election_choices
  45.             WHERE election_id=?
  46.             ORDER BY choice
  47.         'falsearray($id)DB_FETCHMODE_ASSOC);
  48.         for ($i = 1; $i <= $_POST['choices']$i++{
  49.             if (empty($_POST['summary' $i]&& isset($all[$i])) {
  50.                 $info['summary' $i$all[$i]['summary'];
  51.                 $info['summary_link' $i$all[$i]['summary_link'];
  52.             else {
  53.                 $info['summary' $i=
  54.                     empty($_POST['summary' $i]'' $_POST['summary' $i];
  55.                 $info['summary_link' $i=
  56.                     empty($_POST['summary_link' $i]'' $_POST['summary_link' $i];
  57.             }
  58.         }
  59.         $info['choices'$_POST['choices'];
  60.         return $info;
  61.     }
  62.  
  63.     function getInfo($id)
  64.     {
  65.         $all $this->dbh->getAll('
  66.             SELECT
  67.                 purpose, detail,
  68.                 YEAR(votestart) as year, MONTH(votestart) as month,
  69.                 DAYOFMONTH(votestart) as day, voteend - votestart as length,
  70.                 minimum_choices as minimum, maximum_choices maximum,
  71.                 eligiblevoters, COUNT(c.choice) as choices
  72.             FROM elections e, election_choices c
  73.             WHERE id=? AND c.election_id = e.id
  74.             GROUP BY c.election_id
  75.         'array($id)DB_FETCHMODE_ASSOC);
  76.         if (!count($all)) {
  77.             return false;
  78.         }
  79.         $info $all[0];
  80.         return $info;
  81.     }
  82.  
  83.     function validateStep1($new = true)
  84.     {
  85.         $error = array();
  86.         if (empty($_POST['purpose'])) {
  87.             $error['Election Purpose (summary) is required';
  88.         }
  89.         if (empty($_POST['choices'])) {
  90.             $error['Number of Choices is required';
  91.         else {
  92.             if (!is_numeric($_POST['choices']||
  93.                   ((int) $_POST['choices'!= $_POST['choices'])) {
  94.                 $error['Number of Choices must be an integer';
  95.             }
  96.             if ($_POST['choices'< 2 || $_POST['choices'> 20{
  97.                 $error['Number of Choices must be between 2 and 20';
  98.             }
  99.         }
  100.         if (empty($_POST['eligiblevoters'])) {
  101.             $error['Eligible Voters is required';
  102.         else {
  103.             if (!is_numeric($_POST['eligiblevoters'])) {
  104.                 $error['Eligible Voters must be "PEAR Developers" or "General PHP Public"';
  105.             }
  106.             if ($_POST['eligiblevoters'!= 1 && $_POST['eligiblevoters'!= 2{
  107.                 $error['Eligible Voters must be "PEAR Developers" or "General PHP Public"';
  108.             }
  109.         }
  110.         if (empty($_POST['detail'])) {
  111.             $error['Election detail is required';
  112.         }
  113.         $nextyear date('Y'+ 1;
  114.         $thisyear $nextyear - 1;
  115.         if (empty($_POST['year'])) {
  116.             $error['Year is required';
  117.         else {
  118.             if (!is_numeric($_POST['year']|| $_POST['year'!= (int) $_POST['year']{
  119.                 $error['Month is invalid';
  120.             elseif ($_POST['year'!= $nextyear && $_POST['year'!= $thisyear{
  121.                 $error['Year is invalid, must be next year or this year';
  122.             }
  123.         }
  124.         if (empty($_POST['month'])) {
  125.             $error['Month is required';
  126.         else {
  127.             if (!is_numeric($_POST['month']|| $_POST['month'!= (int) $_POST['month']{
  128.                 $error['Month is invalid';
  129.             elseif ($_POST['month'< 1 || $_POST['month'> 12{
  130.                 $error['Month is invalid, must be 1-12';
  131.             else {
  132.                 if ($_POST['month'< 10 && $_POST['month'][0!= '0'{
  133.                     $_POST['month''0' $_POST['month'];
  134.                 }
  135.             }
  136.         }
  137.         if (empty($_POST['day'])) {
  138.             $error['Day is required';
  139.         else {
  140.             if (!is_numeric($_POST['day']|| $_POST['day'!= (int) $_POST['day']{
  141.                 $error['Day is invalid';
  142.             elseif ($_POST['day'< 1 || $_POST['day'date('d'strtotime(
  143.                   $_POST['year''-' $_POST['month''-01 +1 month -1 day'))) {
  144.                 $error['Day is invalid, must be 1-' date('d'strtotime(
  145.                     $_POST['year''-' $_POST['month''-01 +1 month -1 day'));
  146.             else {
  147.                 if ($_POST['day'< 10 && $_POST['day'][0!= '0'{
  148.                     $_POST['day''0' $_POST['day'];
  149.                 }
  150.             }
  151.         }
  152.         $date $_POST['year''-' $_POST['month''-' $_POST['day'];
  153.         if (strtotime($date!= strtotime(date('Y-m-d'strtotime($date)))) {
  154.             $error['Full date is invalid';
  155.         }
  156.         if ($new && (strtotime($datetime()) / 86400 < 29.0{
  157.             $error['Voting must start at least 30 days from today';
  158.         }
  159.         if (empty($_POST['length'])) {
  160.             $error['Election length is required';
  161.         else {
  162.             if (!is_numeric($_POST['length']|| $_POST['length'!= (int) $_POST['length']{
  163.                 $error['Voting length is invalid';
  164.             elseif ($_POST['length'< 1 || $_POST['length'> 14{
  165.                 $error['Voting length must be between 1 and 14 days';
  166.             }
  167.         }
  168.         if (empty($_POST['minimum'])) {
  169.             $error['Election minimum votes needed is required';
  170.         else {
  171.             if (!is_numeric($_POST['minimum']||
  172.                   $_POST['minimum'!= (int) $_POST['minimum']{
  173.                 $error['Minimum votes needed is invalid';
  174.             elseif ($_POST['minimum'< 1 || $_POST['minimum'> 19{
  175.                 $error['Minimum votes needed must be between 1 and 19';
  176.             }
  177.         }
  178.         if (empty($_POST['maximum'])) {
  179.             $error['Election maximum votes needed is required';
  180.         else {
  181.             if (!is_numeric($_POST['maximum']||
  182.                   $_POST['maximum'!= (int) $_POST['maximum']{
  183.                 $error['Maximum votes needed is invalid';
  184.             elseif ($_POST['maximum'< 1 || $_POST['maximum'> 19{
  185.                 $error['Maximum votes needed must be between 1 and 19';
  186.             }
  187.         }
  188.         if ($_POST['maximum'$_POST['minimum']{
  189.             $error['Maximum votes needed must be greater or the same as minimum votes needed';
  190.         }
  191.         if ($_POST['minimum'$_POST['choices']{
  192.             $error['Minimum votes needed must be less than or equal to the number of choices';
  193.         }
  194.         if ($_POST['maximum'$_POST['choices']{
  195.             $error['Maximum votes allowed must be less than or equal to the number of choices';
  196.         }
  197.         return $error;
  198.     }
  199.  
  200.     function validateStep2()
  201.     {
  202.         $error = array();
  203.         if (isset($_POST['add1choice'])) {
  204.             $error['Added one choice';
  205.             $_POST['choices']++;
  206.         elseif (isset($_POST['delete1choice'])) {
  207.             if ($_POST['choices'<= $_POST['maximum']{
  208.                 $error['Cannot delete, must have at least the as many choices as ' .
  209.                     'the Maximum votes allowed';
  210.             else {
  211.                 $error['Deleted last choice';
  212.                 $_POST['choices']--;
  213.             }
  214.         else {
  215.             for ($i = 1; $i <= $_POST['choices']$i++{
  216.                 if (empty($_POST['summary' $i])) {
  217.                     $error['Summary for Choice #' $i ' must not be empty';
  218.                 }
  219.                 if (empty($_POST['summary_link' $i])) {
  220.                     $error['Link to more info for Choice #' $i ' must not be empty';
  221.                 }
  222.             }
  223.         }
  224.         return $error;
  225.     }
  226.  
  227.     function saveNewElection()
  228.     {
  229.         $startdate date('Y-m-d',
  230.             strtotime($_POST['year''-' $_POST['month''-' $_POST['day']));
  231.         $enddate date('Y-m-d',
  232.             strtotime($startdate ' +' $_POST['length'' days'));
  233.         $this->dbh->query('
  234.             INSERT INTO elections
  235.              (purpose, detail, votestart, voteend, creator, createdate, minimum_choices,
  236.               maximum_choices, eligiblevoters)
  237.             VALUES(?,?,?,?,?,NOW(),?,?,?)
  238.         'array(
  239.             $_POST['purpose'],
  240.             $_POST['detail'],
  241.             $startdate,
  242.             $enddate,
  243.             $this->user,
  244.             $_POST['minimum'],
  245.             $_POST['maximum'],
  246.             $_POST['eligiblevoters']
  247.             ));
  248.         $id mysqli_insert_id($this->dbh->connection);
  249.         for ($i = 1; $i <= $_POST['choices']$i++{
  250.             $this->dbh->query('
  251.                 INSERT INTO election_choices
  252.                     (election_id, choice, summary, summary_link)
  253.                 VALUES(?,?,?,?)
  254.             'array($id$i$_POST['summary' $i]$_POST['summary_link' $i]));
  255.         }
  256.     }
  257.  
  258.     function saveEditedElection()
  259.     {
  260.         $id $_POST['election_id'];
  261.         $info $this->getInfo($id);
  262.         $startdate date('Y-m-d',
  263.             strtotime($_POST['year''-' $_POST['month''-' $_POST['day']));
  264.         $enddate date('Y-m-d',
  265.             strtotime($startdate ' +' $_POST['length'' days'));
  266.         $this->dbh->query('
  267.             UPDATE elections
  268.             SET
  269.                 purpose=?,
  270.                 detail=?,
  271.                 votestart=?,
  272.                 voteend=?,
  273.                 minimum_choices=?,
  274.                 maximum_choices=?,
  275.                 eligiblevoters=?
  276.             WHERE
  277.                 id=?
  278.         'array(
  279.             $_POST['purpose'],
  280.             $_POST['detail'],
  281.             $startdate,
  282.             $enddate,
  283.             $_POST['minimum'],
  284.             $_POST['maximum'],
  285.             $_POST['eligiblevoters'],
  286.             $id
  287.             ));
  288.         $this->dbh->query('DELETE FROM election_choices WHERE election_id=?'array($id));
  289.         for ($i = 1; $i <= $_POST['choices']$i++{
  290.             $this->dbh->query('
  291.                 INSERT INTO election_choices
  292.                     (election_id, choice, summary, summary_link)
  293.                 VALUES(?,?,?,?)
  294.             'array($id$i$_POST['summary' $i]$_POST['summary_link' $i]));
  295.         }
  296.     }
  297.  
  298.     function canEdit($id)
  299.     {
  300.         if ('yes' == $this->dbh->getOne('
  301.             SELECT
  302.                 IF(votestart > NOW(),"no","yes") FROM elections
  303.                 WHERE id=?'array($id))) {
  304.             // cannot edit active or old elections
  305.             return false;
  306.         }
  307.         if ($this->karma->has($this->user'pear.admin')) {
  308.             return true;
  309.         }
  310.         if (!$this->electionExists($id)) {
  311.             return false;
  312.         }
  313.         if ($this->user == $this->dbh->getOne('SELECT creator FROM elections WHERE id=?',
  314.               array($id))) {
  315.             return true;
  316.         }
  317.         return false;
  318.     }
  319. }

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