Import changes from the maemo preferential voting scripts

The changes come from the SVN repository at
https://garage.maemo.org/svn/maemo2midgard/maemo-elections/vote,
revision 1433.

This commit only ports changes in the include/ subdirectory.
This commit is contained in:
Tobias Mueller 2009-06-04 20:15:43 +02:00
parent 59930211ce
commit 31fd299c8c
5 changed files with 122 additions and 107 deletions

View file

@ -13,6 +13,7 @@ $anon_tokens_table = "election_anon_tokens";
$tmp_tokens_table = "election_tmp_tokens"; $tmp_tokens_table = "election_tmp_tokens";
$votes_table = "election_votes"; $votes_table = "election_votes";
$members_table = "foundationmembers"; $members_table = "foundationmembers";
$results_table = "election_results";
if (is_readable ("include/localconfig.php")) { if (is_readable ("include/localconfig.php")) {
/* You can use such a file to have a local config for testing purpose. */ /* You can use such a file to have a local config for testing purpose. */
@ -156,6 +157,7 @@ function elec_choices_get ($handle, $election_id) {
$query = "SELECT * FROM " . $choices_table; $query = "SELECT * FROM " . $choices_table;
$query .= " WHERE election_id = '".$escaped_election_id."'"; $query .= " WHERE election_id = '".$escaped_election_id."'";
$query .= " ORDER BY id";
$result = mysql_query ($query, $handle); $result = mysql_query ($query, $handle);
@ -172,14 +174,8 @@ function elec_choices_get ($handle, $election_id) {
return $retval; return $retval;
} }
function elec_verify_elections ($choices_nb, $choices) { function elec_verify_elections ($choices) {
if ($choices_nb === FALSE || $choices === FALSE) if ($choices === FALSE || count ($choices) <= 1)
return FALSE;
if ($choices_nb < 1)
return FALSE;
if (count ($choices) < $choices_nb || count ($choices) <= 1)
return FALSE; return FALSE;
return TRUE; return TRUE;
@ -204,6 +200,25 @@ function elec_get_election ($handle, $election_id) {
return mysql_fetch_assoc ($result); return mysql_fetch_assoc ($result);
} }
function elec_get_results ($handle, $election_id) {
global $results_table;
if ($handle === FALSE)
return FALSE;
$escaped_election_id = mysql_real_escape_string ($election_id, $handle);
$query = "SELECT * FROM " . $results_table; //FIXME: Don't use wildcards
$query .= " WHERE election_id = '".$escaped_election_id."'";
$result = mysql_query ($query, $handle);
if (!$result)
return FALSE;
return mysql_fetch_assoc ($result);
}
function elec_election_is_current ($election) { function elec_election_is_current ($election) {
$gmdate = gmdate ("Y-m-d H:i:s"); $gmdate = gmdate ("Y-m-d H:i:s");
return ($gmdate >= $election["voting_start"] && $gmdate <= $election["voting_end"]); return ($gmdate >= $election["voting_start"] && $gmdate <= $election["voting_end"]);
@ -223,24 +238,19 @@ function elec_election_get_type ($election) {
function elec_vote_get_votes_from_post ($choices) { function elec_vote_get_votes_from_post ($choices) {
$votes_array = array (); $votes_array = array ();
$index=0;
foreach ($choices as $choice) { foreach ($choices as $choice) {
$name = "vote".$choice["id"]; $index++;
$name = "pref".$index;
if (isset ($_POST[$name]) && $_POST[$name] != "") { if (isset ($_POST[$name]) && $_POST[$name] != "") {
array_push ($votes_array, $choice["id"]); array_push ($votes_array, substr($_POST[$name],4));
} }
} }
return $votes_array; return $votes_array;
} }
function elec_verify_vote_is_valid ($choices_nb, $choices, $vote, $votes_array) { function elec_verify_vote_is_valid ($choices, $vote, $votes_array) {
if ($choices_nb == 1)
return "";
if (count ($votes_array) > $choices_nb) {
return "you chose ".count ($votes_array)." answers, while you can't choose more than ".$choices_nb." answers.";
}
return ""; return "";
} }
@ -274,18 +284,20 @@ function elec_insert_new_anon_token ($handle, $election_id, $anon_token) {
return mysql_insert_id ($handle); return mysql_insert_id ($handle);
} }
function elec_insert_new_vote ($handle, $anon_token_id, $vote) { function elec_insert_new_vote ($handle, $anon_token_id, $vote, $preference) {
global $votes_table; global $votes_table;
if ($handle === FALSE) if ($handle === FALSE)
return FALSE; return FALSE;
error_log($vote, 0);
$escaped_vote = mysql_real_escape_string ($vote, $handle); $escaped_vote = mysql_real_escape_string ($vote, $handle);
$escaped_anon_token_id = mysql_real_escape_string ($anon_token_id, $handle); $escaped_anon_token_id = mysql_real_escape_string ($anon_token_id, $handle);
$query = "INSERT INTO " . $votes_table . " (choice_id, anon_id)"; $query = "INSERT INTO " . $votes_table . " (choice_id, anon_id, preference)";
$query .= " VALUES ('".$escaped_vote."', '".$escaped_anon_token_id."')"; $query .= " VALUES ('".$escaped_vote."', '".$escaped_anon_token_id."', '".$preference."')";
error_log($query, 0);
$result = mysql_query ($query, $handle); $result = mysql_query ($query, $handle);
if (!$result) if (!$result)
return FALSE; return FALSE;
@ -346,6 +358,7 @@ function elec_get_anon_tokens_for_election ($handle, $election_id) {
return $retval; return $retval;
} }
/* Leaving this here as legacy code. Unused for preferential voting. */
function elec_get_results_election ($handle, $election_id) { function elec_get_results_election ($handle, $election_id) {
global $anon_tokens_table; global $anon_tokens_table;
global $votes_table; global $votes_table;
@ -413,14 +426,14 @@ function elec_get_votes_for_anon_token ($handle, $anon_token_id) {
$escaped_anon_token_id = mysql_real_escape_string ($anon_token_id, $handle); $escaped_anon_token_id = mysql_real_escape_string ($anon_token_id, $handle);
$query = "SELECT choice_id FROM " . $votes_table; $query = "SELECT choice_id,preference FROM " . $votes_table;
$query .= " WHERE anon_id = '".$escaped_anon_token_id."'"; $query .= " WHERE anon_id = '".$escaped_anon_token_id."'";
/* -1 is not a valid value: it's the default value for referenda. /* -1 is not a valid value: it's the default value for referenda.
* It's a blank vote. There was a bug that let this choice be saved in the * It's a blank vote. There was a bug that let this choice be saved in the
* votes, but we don't need it there since we already have the anonymous * votes, but we don't need it there since we already have the anonymous
* token as a proof of the blank vote. */ * token as a proof of the blank vote. */
$query .= " AND choice_id != '-1'"; $query .= " AND choice_id != '-1'";
$query .= " ORDER BY choice_id"; $query .= " ORDER BY preference";
$result = mysql_query ($query, $handle); $result = mysql_query ($query, $handle);
@ -429,7 +442,7 @@ function elec_get_votes_for_anon_token ($handle, $anon_token_id) {
} else { } else {
$result_array = array (); $result_array = array ();
while ($buffer = mysql_fetch_assoc ($result)) { while ($buffer = mysql_fetch_assoc ($result)) {
$result_array[] = $buffer["choice_id"]; $result_array[] = $buffer;
} }
$retval = $result_array; $retval = $result_array;
} }

View file

@ -0,0 +1,43 @@
CREATE TABLE `elections` (
`id` int(11) NOT NULL auto_increment,
`type` enum('elections','referendum') NOT NULL default 'elections',
`name` varchar(150) NOT NULL default '',
`voting_start` datetime default NULL,
`voting_end` datetime default '0000-00-00 00:00:00',
`choices_nb` int(11) NOT NULL default '0',
`question` text NOT NULL,
PRIMARY KEY (`id`)
CREATE TABLE `election_anon_tokens` (
`id` int(11) NOT NULL auto_increment,
`anon_token` varchar(200) NOT NULL default '',
`election_id` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=903 DEFAULT CHARSET=utf8
CREATE TABLE `election_choices` (
`id` int(11) NOT NULL auto_increment,
`election_id` int(11) NOT NULL default '0',
`choice` varchar(150) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8
CREATE TABLE `election_tmp_tokens` (
`election_id` int(11) NOT NULL default '0',
`member_id` int(11) NOT NULL default '0',
`tmp_token` varchar(200) NOT NULL default ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*
from members database we prepare anon tokens
then insert those anon tokens to database
of course before a new election record should be created since its id is needed for anon_tokens
and election_choices are to be inserted
rest is handled by itself iirc
<bolsh> There's "election_votes" and "foundationmembers" too
I'm not sure if there's a join done between foundationmembers and the other tables
*/
CREATE TABLE `election_votes` (
`id` int(11) NOT NULL auto_increment,
`choice_id` int(11) NOT NULL default '0',
`anon_id` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)

View file

@ -2,46 +2,39 @@
function step2_do () { function step2_do () {
global $election; global $election;
global $choices_nb;
global $choices; global $choices;
global $vote; global $vote;
global $votes_array; global $votes_array;
$result = "<h2>Step 2/4 - Choose your vote</h2>\n"; $result = "<h2>Step 2/4 - Choose your vote</h2>\n";
$result .= "<p><strong>".$election["question"]."</strong></p>\n"; $result .= "<p>Choose your candidates in the order of your preference by ";
$result .= "<p>Possible answers:</p>\n"; $result .= "clicking on them. Don't worry, if you click on someone by mistake ";
$result .= "you can correct it later. Once you are happy with the order, ";
$result .= "submit your vote. You will have the chance to review and confirm";
$result .= " your ballot on the next page.</p>";
$result .= "<div class=\"votedata\">\n"; $result .= "<p>You can vote for as few or as many candidates as you choose. ";
if ($choices_nb == 1) { $result .= "Your vote will be counted for your first choice candidate as ";
$result .= "<p>\n"; $result .= "long as he is still in the race, and when he is eliminated, your ";
foreach ($choices as $choice) { $result .= "vote will transfer to the next preference candidate still in the ";
$checked = ""; $result .= "race.</p>";
if ($choice["id"] == $vote) {
$checked = " checked=\"checked\"";
}
$result .= "<input type=\"radio\" name=\"vote\" value=\"".$choice["id"]."\"".$checked."> ".$choice["choice"]."<br />\n"; $result .= "<p><noscript>Note: This page requires Javascript</noscript></p>";
}
$result .= "</p>\n";
} else { $result .= "<p>Possible choices:</p>\n";
$result .= "<p>\n"; $result .= "<div class=\"canddata\">\n";
foreach ($choices as $choice) { $result .= "<h3>Candidates</h3>\n";
$checked = ""; $result .= "<ul id=\"candidates\">\n";
if (in_array ($choice["id"], $votes_array)) { $result .= "</ul>\n";
$checked = " checked=\"checked\""; $result .= "</div>";
} $result .= "<div class=\"prefdata\">";
$result .= "<h3>Preferences</h3>\n";
$result .= "<ol id=\"preferences\">\n";
$result .= "</ol>\n";
$result .= "<input type=\"checkbox\" name=\"vote".$choice["id"]."\"".$checked."> ".$choice["choice"]."<br />\n";
}
$result .= "</p>\n";
}
$result .= "</div>\n"; $result .= "</div>\n";
if ($choices_nb > 1)
$result .= "<p><em>You can choose up to ".$choices_nb." answers.</em></p>\n";
return $result; return $result;
} }

View file

@ -2,7 +2,6 @@
function step3_do () { function step3_do () {
global $election; global $election;
global $choices_nb;
global $choices; global $choices;
global $vote; global $vote;
global $votes_array; global $votes_array;
@ -10,30 +9,11 @@ function step3_do () {
$result = "<h2>Step 3/4 - Confirm your vote</h2>\n"; $result = "<h2>Step 3/4 - Confirm your vote</h2>\n";
$result .= "<p><strong>".$election["question"]."</strong></p>\n"; $result .= "<p><strong>".$election["question"]."</strong></p>\n";
if (($choices_nb == 1 && $vote >= 0) || if (count ($votes_array) >= 1) {
($choices_nb > 1 && count ($votes_array) >= 1)) {
$result .= "<p>You choose to vote for:</p>\n"; $result .= "<p>You choose to vote for:</p>\n";
$result .= "<div class=\"votedata\">\n"; $result .= "<div class=\"votedata\">\n";
if ($choices_nb == 1) { $result .= "<ol>\n";
$choice = null;
foreach ($choices as $opt) {
if ($opt["id"] == $vote) {
$choice = $opt;
break;
}
}
if ($choice != null)
$result .= "<p>".$choice["choice"]."</p>\n";
else {
$result .= "<p>Unknown vote: ".$vote."</p>\n";
$error .= "There was an unkown vote: ".$vote."<br />\n";
}
} else {
$result .= "<ul>\n";
foreach ($votes_array as $vote) { foreach ($votes_array as $vote) {
$found = FALSE; $found = FALSE;
foreach ($choices as $choice) { foreach ($choices as $choice) {
@ -49,9 +29,8 @@ function step3_do () {
$error .= "There was an unkown vote: ".$vote."<br />\n"; $error .= "There was an unkown vote: ".$vote."<br />\n";
} }
} }
$result .= "</ul>\n"; $result .= "</ol>\n";
}
$result .= "</div>\n"; $result .= "</div>\n";
} else { } else {
@ -60,7 +39,8 @@ function step3_do () {
$result .= "</div>\n"; $result .= "</div>\n";
} }
$result .= "<p>To confirm this vote, please continue to the next step.</p>\n"; $result .= "<p>To confirm this vote, please continue to the next step. ";
$result .= "To modify your choice, hit the \"Back\" button in your browser.</p>\n";
return $result; return $result;
} }

View file

@ -37,7 +37,6 @@ function step4_do () {
global $error; global $error;
global $handle; global $handle;
global $election_id; global $election_id;
global $choices_nb;
global $vote; global $vote;
global $votes_array; global $votes_array;
global $email; global $email;
@ -64,31 +63,18 @@ function step4_do () {
return $result; return $result;
} }
if ($choices_nb == 1) { $index=0;
if ($vote != -1) {
//FIXME verify that $vote is valid for this election/referendum
$res = elec_insert_new_vote ($handle, $anon_token_id, $vote);
if (!$res) {
$error .= "Can not insert a vote: ".mysql_error ($handle)."<br />\n";
elec_sql_rollback ($handle);
return $result;
}
}
} else {
foreach ($votes_array as $vote) { foreach ($votes_array as $vote) {
//FIXME verify that $vote is valid for this election/referendum //FIXME verify that $vote is valid for this election/referendum
$res = elec_insert_new_vote ($handle, $anon_token_id, $vote); $index++;
error_log($vote.", ".$index.", ".$anon_token_id, 0);
$res = elec_insert_new_vote ($handle, $anon_token_id, $vote, $index);
if (!$res) { if (!$res) {
$error .= "Can not insert a vote: ".mysql_error ($handle)."<br />\n"; $error .= "Can not insert a vote: ".mysql_error ($handle)."<br />\n";
elec_sql_rollback ($handle); elec_sql_rollback ($handle);
return $result; return $result;
} }
}
} }