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:
parent
59930211ce
commit
31fd299c8c
5 changed files with 122 additions and 107 deletions
|
@ -13,6 +13,7 @@ $anon_tokens_table = "election_anon_tokens";
|
|||
$tmp_tokens_table = "election_tmp_tokens";
|
||||
$votes_table = "election_votes";
|
||||
$members_table = "foundationmembers";
|
||||
$results_table = "election_results";
|
||||
|
||||
if (is_readable ("include/localconfig.php")) {
|
||||
/* 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 .= " WHERE election_id = '".$escaped_election_id."'";
|
||||
$query .= " ORDER BY id";
|
||||
|
||||
$result = mysql_query ($query, $handle);
|
||||
|
||||
|
@ -172,14 +174,8 @@ function elec_choices_get ($handle, $election_id) {
|
|||
return $retval;
|
||||
}
|
||||
|
||||
function elec_verify_elections ($choices_nb, $choices) {
|
||||
if ($choices_nb === FALSE || $choices === FALSE)
|
||||
return FALSE;
|
||||
|
||||
if ($choices_nb < 1)
|
||||
return FALSE;
|
||||
|
||||
if (count ($choices) < $choices_nb || count ($choices) <= 1)
|
||||
function elec_verify_elections ($choices) {
|
||||
if ($choices === FALSE || count ($choices) <= 1)
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
|
@ -204,6 +200,25 @@ function elec_get_election ($handle, $election_id) {
|
|||
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) {
|
||||
$gmdate = gmdate ("Y-m-d H:i:s");
|
||||
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) {
|
||||
$votes_array = array ();
|
||||
|
||||
$index=0;
|
||||
foreach ($choices as $choice) {
|
||||
$name = "vote".$choice["id"];
|
||||
$index++;
|
||||
$name = "pref".$index;
|
||||
if (isset ($_POST[$name]) && $_POST[$name] != "") {
|
||||
array_push ($votes_array, $choice["id"]);
|
||||
array_push ($votes_array, substr($_POST[$name],4));
|
||||
}
|
||||
}
|
||||
|
||||
return $votes_array;
|
||||
}
|
||||
|
||||
function elec_verify_vote_is_valid ($choices_nb, $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.";
|
||||
}
|
||||
function elec_verify_vote_is_valid ($choices, $vote, $votes_array) {
|
||||
|
||||
return "";
|
||||
}
|
||||
|
@ -274,18 +284,20 @@ function elec_insert_new_anon_token ($handle, $election_id, $anon_token) {
|
|||
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;
|
||||
|
||||
if ($handle === FALSE)
|
||||
return FALSE;
|
||||
|
||||
error_log($vote, 0);
|
||||
$escaped_vote = mysql_real_escape_string ($vote, $handle);
|
||||
$escaped_anon_token_id = mysql_real_escape_string ($anon_token_id, $handle);
|
||||
|
||||
$query = "INSERT INTO " . $votes_table . " (choice_id, anon_id)";
|
||||
$query .= " VALUES ('".$escaped_vote."', '".$escaped_anon_token_id."')";
|
||||
$query = "INSERT INTO " . $votes_table . " (choice_id, anon_id, preference)";
|
||||
$query .= " VALUES ('".$escaped_vote."', '".$escaped_anon_token_id."', '".$preference."')";
|
||||
|
||||
error_log($query, 0);
|
||||
$result = mysql_query ($query, $handle);
|
||||
if (!$result)
|
||||
return FALSE;
|
||||
|
@ -346,6 +358,7 @@ function elec_get_anon_tokens_for_election ($handle, $election_id) {
|
|||
return $retval;
|
||||
}
|
||||
|
||||
/* Leaving this here as legacy code. Unused for preferential voting. */
|
||||
function elec_get_results_election ($handle, $election_id) {
|
||||
global $anon_tokens_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);
|
||||
|
||||
$query = "SELECT choice_id FROM " . $votes_table;
|
||||
$query = "SELECT choice_id,preference FROM " . $votes_table;
|
||||
$query .= " WHERE anon_id = '".$escaped_anon_token_id."'";
|
||||
/* -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
|
||||
* votes, but we don't need it there since we already have the anonymous
|
||||
* token as a proof of the blank vote. */
|
||||
$query .= " AND choice_id != '-1'";
|
||||
$query .= " ORDER BY choice_id";
|
||||
$query .= " ORDER BY preference";
|
||||
|
||||
$result = mysql_query ($query, $handle);
|
||||
|
||||
|
@ -429,7 +442,7 @@ function elec_get_votes_for_anon_token ($handle, $anon_token_id) {
|
|||
} else {
|
||||
$result_array = array ();
|
||||
while ($buffer = mysql_fetch_assoc ($result)) {
|
||||
$result_array[] = $buffer["choice_id"];
|
||||
$result_array[] = $buffer;
|
||||
}
|
||||
$retval = $result_array;
|
||||
}
|
||||
|
|
43
foundation.gnome.org/vote/include/schema.sql
Normal file
43
foundation.gnome.org/vote/include/schema.sql
Normal 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`)
|
||||
|
|
@ -2,46 +2,39 @@
|
|||
|
||||
function step2_do () {
|
||||
global $election;
|
||||
global $choices_nb;
|
||||
global $choices;
|
||||
global $vote;
|
||||
global $votes_array;
|
||||
|
||||
$result = "<h2>Step 2/4 - Choose your vote</h2>\n";
|
||||
|
||||
$result .= "<p><strong>".$election["question"]."</strong></p>\n";
|
||||
$result .= "<p>Possible answers:</p>\n";
|
||||
$result .= "<p>Choose your candidates in the order of your preference by ";
|
||||
$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";
|
||||
if ($choices_nb == 1) {
|
||||
$result .= "<p>\n";
|
||||
foreach ($choices as $choice) {
|
||||
$checked = "";
|
||||
if ($choice["id"] == $vote) {
|
||||
$checked = " checked=\"checked\"";
|
||||
}
|
||||
$result .= "<p>You can vote for as few or as many candidates as you choose. ";
|
||||
$result .= "Your vote will be counted for your first choice candidate as ";
|
||||
$result .= "long as he is still in the race, and when he is eliminated, your ";
|
||||
$result .= "vote will transfer to the next preference candidate still in the ";
|
||||
$result .= "race.</p>";
|
||||
|
||||
$result .= "<input type=\"radio\" name=\"vote\" value=\"".$choice["id"]."\"".$checked."> ".$choice["choice"]."<br />\n";
|
||||
}
|
||||
$result .= "</p>\n";
|
||||
$result .= "<p><noscript>Note: This page requires Javascript</noscript></p>";
|
||||
|
||||
} else {
|
||||
$result .= "<p>Possible choices:</p>\n";
|
||||
|
||||
$result .= "<p>\n";
|
||||
foreach ($choices as $choice) {
|
||||
$checked = "";
|
||||
if (in_array ($choice["id"], $votes_array)) {
|
||||
$checked = " checked=\"checked\"";
|
||||
}
|
||||
$result .= "<div class=\"canddata\">\n";
|
||||
$result .= "<h3>Candidates</h3>\n";
|
||||
$result .= "<ul id=\"candidates\">\n";
|
||||
$result .= "</ul>\n";
|
||||
$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";
|
||||
if ($choices_nb > 1)
|
||||
$result .= "<p><em>You can choose up to ".$choices_nb." answers.</em></p>\n";
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
function step3_do () {
|
||||
global $election;
|
||||
global $choices_nb;
|
||||
global $choices;
|
||||
global $vote;
|
||||
global $votes_array;
|
||||
|
@ -10,30 +9,11 @@ function step3_do () {
|
|||
$result = "<h2>Step 3/4 - Confirm your vote</h2>\n";
|
||||
|
||||
$result .= "<p><strong>".$election["question"]."</strong></p>\n";
|
||||
if (($choices_nb == 1 && $vote >= 0) ||
|
||||
($choices_nb > 1 && count ($votes_array) >= 1)) {
|
||||
if (count ($votes_array) >= 1) {
|
||||
$result .= "<p>You choose to vote for:</p>\n";
|
||||
|
||||
$result .= "<div class=\"votedata\">\n";
|
||||
if ($choices_nb == 1) {
|
||||
|
||||
$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";
|
||||
$result .= "<ol>\n";
|
||||
foreach ($votes_array as $vote) {
|
||||
$found = FALSE;
|
||||
foreach ($choices as $choice) {
|
||||
|
@ -49,9 +29,8 @@ function step3_do () {
|
|||
$error .= "There was an unkown vote: ".$vote."<br />\n";
|
||||
}
|
||||
}
|
||||
$result .= "</ul>\n";
|
||||
$result .= "</ol>\n";
|
||||
|
||||
}
|
||||
$result .= "</div>\n";
|
||||
|
||||
} else {
|
||||
|
@ -60,7 +39,8 @@ function step3_do () {
|
|||
$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;
|
||||
}
|
||||
|
|
|
@ -37,7 +37,6 @@ function step4_do () {
|
|||
global $error;
|
||||
global $handle;
|
||||
global $election_id;
|
||||
global $choices_nb;
|
||||
global $vote;
|
||||
global $votes_array;
|
||||
global $email;
|
||||
|
@ -64,31 +63,18 @@ function step4_do () {
|
|||
return $result;
|
||||
}
|
||||
|
||||
if ($choices_nb == 1) {
|
||||
|
||||
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 {
|
||||
|
||||
$index=0;
|
||||
foreach ($votes_array as $vote) {
|
||||
//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) {
|
||||
$error .= "Can not insert a vote: ".mysql_error ($handle)."<br />\n";
|
||||
elec_sql_rollback ($handle);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue