function showVoteInput($q) { $info = explode(".", $q); $district = $info[0]; $machine = $info[1]; echo "<center><h1 class='title'>Enter Election Results for District: <span class='big'>{$district}</span>, Machine: <span class='big'>{$machine}</span></h1></center>"; echo "<form action = './saveInputs.php' method ='post'>"; //get categories $categoriesResult = getCategories(); $count = 0; $categoryCount = 0; echo "<table><tr><td class='category'>"; while ($category = mysql_fetch_array($categoriesResult)) { //get candidates $id = $category['category_id']; //echo " ID = {$id} "; $candidates = getCandidates($id); echo "<h1> {$category['category_name']}</h1><table>"; while ($candidate = mysql_fetch_array($candidates)) { $info = "{$district},{$machine},{$candidate['candidate_id']},"; //echo $info; $hidden = "candidateID" . $count++; echo "<tr><td width = 25px></td><td>{$candidate['candidate_name']}</td><td>"; echo "<input type ='number' id='{$candidate['candidate_id']}' name='{$candidate['candidate_id']}' value = "; echo "'"; echo getElectionResults($district, $machine, $candidate['candidate_id']); echo "'"; //$functionStr = "ajaxGetInfo({$info}, this.value)"; // . $info . ")"; echo " oninput='ajaxGetInfo(this.value, this.name)' min=0></td></tr>"; echo "<input type=hidden name = {$hidden} METHOD='POST' value='{$candidate['candidate_id']}'>"; // echo "<input type=hidden name = 'candidateID' METHOD='POST' value='{$candidate['candidate_id']}'>"; } echo "</table>"; echo "<input type=hidden id = 'district' value='{$district}'>"; echo "<input type=hidden id = 'machine' value='{$machine}'>"; if ($categoryCount % 3 == 2) { echo "</td><td style='padding-left:45px'>"; } $categoryCount++; } echo "</td></tr></table>"; }
function findBorda($ballots, $seats) { $cand_list = getCandidates($ballots); $cand_num = count($cand_list); $tallies = array(); foreach ($ballots as $ballot) { foreach (range(0, $cand_num - 1) as $num) { if (isset($ballot[$num])) { $cname = $ballot[$num]; if (!isset($tallies[$cname])) { $tallies[$cname] = 0; } $tallies[$cname] += $cand_num - $num; } } } $slate = array(); unset($tallies['-']); return getSlate($tallies, $slate, $seats); }
function runElections() { $years = array(); $condorcets = array(); foreach (new DirectoryIterator('elections') as $fileInfo) { if ($fileInfo->isDot()) { continue; } else { $filepath = 'elections/' . $fileInfo->getFilename(); $cands = getCandidates($filepath); $data = json_decode(file_get_contents($filepath), 1); $year = $data['ELECTION']['id']; print $year . "\n"; $ballots = $data['BALLOTS']; $cprobs = runBallots($year, $cands, $ballots); $years[$year] = $cprobs; } } return $years; }
if ($classParts[count($classParts) - $i] != $parts[count($parts) - $i]) { $matches = false; break; } } if ($matches) { if (count($classParts) - count($parts) + 1 > 1) { $candidates[] = implode('\\', array_slice($classParts, 0, count($classParts) - count($parts) + 1)); } } } $output[$name] = $candidates; } return $output; } $names = getCandidates($names, $classmap); $namesWithCandidates = $names; $names = array_map(function ($name) { return $name[0]; }, $names); // And what is already used? $uses = array_reduce(getNodesByType($tree, 'PHPParser_Node_Stmt_Use'), function ($output, $use) { foreach ($use->uses as $u) { $output[] = implode('\\', $u->name->parts); } return $output; }, array()); // print_r($uses); class InvalidSourceException extends Exception { }
echo "<h3>Election: {$election['name']} " . date_format($date, "m/d/Y") . " (" . $election['location'] . ")</h3>"; //list ballot items //get categories $categoriesResult = getCategories($election_id); $count = 0; $categoryCount = 0; $newCount = -1; while ($category = $categoriesResult->fetch_assoc()) { //mysql_fetch_array($categoriesResult)){ echo "<table><tr><td class='category'>"; $hiddenCategory = "category" . $categoryCount++; //get candidates $ballotItemID = $category['id']; //echo " ID = {$ballotItemID} "; $unique = $ballotItemID . "-question"; $candidates = getCandidates($ballotItemID); $ballotItem = $category['question']; //echo "Ballot item is " . $ballotItem; // echo "<h4>Ballot Item: <input type='text' name='{$unique}' value='{$ballotItem}'"; //echo " oninput='jsBallotItem(this.value, this.name)'"; echo "></td></tr>"; echo "<input type=hidden name = {$hiddenCategory} METHOD='POST' value='{$ballotItemID}_{$categoryCount}'>"; $choiceCount = 1; while ($candidate = $candidates->fetch_assoc()) { //mysql_fetch_array($candidates)){ $candidate_id = $candidate['id']; $name = $candidate['response']; $hidden = "candidateID" . $count++; echo "<tr><td width = 25px></td><td>"; echo "Choice {$choiceCount}: <input type ='text' name='{$candidate_id}' value='{$name}'";
initial_ballot_value */ $result = array(); foreach (new DirectoryIterator('elections') as $fileInfo) { if ($fileInfo->isDot()) { continue; } else { $fn = 'elections/' . $fileInfo->getFilename(); $elec_data = json_decode(file_get_contents($fn), 1); $seats = $elec_data['ELECTION']['seats']; $year = $elec_data['ELECTION']['id']; if ('2011' != $year) { continue; } $ballots = evenBallotLength($elec_data['BALLOTS']); $candidates = getCandidates($ballots); $num_ballots = count($ballots); $droop = calculateDroop($num_ballots, $seats); $committee = array(); foreach (range(1, 100) as $num) { if (0 == $num % 100) { print $year . " : " . $num . " memory " . memory_get_usage() . "\n"; } $logs = tallyBallots($ballots, $candidates, $seats, $droop); $last_log = array_pop($logs); foreach ($last_log['committee'] as $elected) { if (!isset($committee[$elected->eid])) { $committee[$elected->eid] = 0; } $committee[$elected->eid] += 1; }
function getTotalCandidateCount() { global $variables; //getCandidates $categoryResult = getCategories(); $count = 0; while ($category = mysql_fetch_array($categoryResult)) { $id = $category[$variables['category_id_categories']]; $result = getCandidates($id); $count += mysql_numrows($result); } return $count; }
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $PRINT = 0; $start = microtime(1); $iters = 100000; foreach (range(0, $iters) as $j) { $ballot_set_name = 'ballot_' . sprintf("%07d", $j); $dir = substr($ballot_set_name, -3); $filepath = 'sim_ballots_uneven/' . $dir . '/' . $ballot_set_name . '.json'; //findCondorcet($filepath); //checkIsFile($filepath); print "working on {$filepath}\n"; $cands = getCandidates($filepath); $ballots = json_decode(file_get_contents($filepath), 1); $cprobs = runBallots($cands, $ballots); arsort($cprobs); print json_encode($cprobs); exit; //$years[$year] = $cprobs; $now = microtime(1); $num_done = $j + 1; $per_iter = ($now - $start) / $num_done; $left_to_do = $iters - $num_done; $time_remaining = round($per_iter * $left_to_do / 60); if (0 == $j % 2) { print $j . " iterations completed "; print $time_remaining . " minutes to go\n"; }
function getTotalCandidateCount() { //getCandidates $election_id = getCurrentElectionID(); $categoryResult = getCategories($election_id); $count = 0; while ($category = $categoryResult->fetch_assoc()) { //mysql_fetch_array($categoryResult)){ $id = $category['id']; $result = getCandidates($id); $count += $result->num_rows; //mysql_numrows($result); } return $count; }
<?php $candidates = 30; $seats = 12; //foreach (new DirectoryIterator('elections') as $fileInfo) { // if($fileInfo->isDot()) { // continue; // } else { $fn = 'sim_ballots_uneven/000/ballot_0000000.json'; $ballots = json_decode(file_get_contents($fn), 1); $seats = 12; $ballots = evenBallotLength($ballots); $candidates = count(getCandidates($ballots)); $num_ballots = count($ballots); //APPROVAL $approv = findApproval($ballots, $seats); $out = ''; foreach ($approv as $eid => $tally) { $out .= $eid . "," . $tally . "\n"; } //print json_encode($approv); exit; //$outfile = 'historical_approval/approval_'.$year.'.csv'; //file_put_contents($outfile,$out); //BORDA $borda = findBorda($ballots, $seats, $candidates); $out = ''; foreach ($borda as $eid => $tally) { $out .= $eid . "," . $tally . "\n"; } print json_encode($borda); exit;
function showVoteInput($q) { //Not editable if prior election $canEdit = $_SESSION['is_active']; //Can't edit if readonly user if (!(stripos($_SESSION['privilege'], 'read') === false)) { $canEdit = false; } $info = explode(".", $q); $district = $info[0]; $machine = $info[1]; //get name of district $query = "Select * from districts where id = {$district}"; //$result = mysql_query($query) or die("Query Failed!" . $query); $result = runQuery($query); $districtInfo = $result->fetch_assoc(); //mysql_fetch_array($result); $name = $districtInfo['name']; echo "<center><h1 class='title'>Enter Election Results for District: <span class='big'>{$name}</span>, Machine: <span class='big'>{$machine}</span></h1></center>"; echo "<form action = './saveInputs.php' method ='post'>"; //get categories $election_id = getCurrentElectionID(); $categoriesResult = getCategories($election_id); $count = 0; $categoryCount = 0; echo "<table><tr><td class='category'>"; while ($category = $categoriesResult->fetch_assoc()) { //get candidates $id = $category['id']; //echo " ID = {$id} "; $candidates = getCandidates($id); echo "<h1> {$category['question']}</h1><table>"; while ($candidate = $candidates->fetch_assoc()) { $hidden = "candidateID" . $count++; echo "<tr><td width = 25px></td><td>{$candidate['response']}</td><td>"; echo "<input type ='number' id='{$candidate['id']}' name='{$candidate['id']}' value = "; echo "'"; echo getElectionResults($district, $machine, $candidate['id']); echo "'"; if ($canEdit) { echo " oninput='ajaxGetInfo(this.value, this.name)' min=0>"; } else { //READONLY echo " READONLY>"; } echo "</td></tr>"; echo "<input type=hidden name = {$hidden} METHOD='POST' value='{$candidate['id']}'>"; // echo "<input type=hidden name = 'candidateID' METHOD='POST' value='{$candidate['candidate_id']}'>"; } echo "</table>"; echo "<input type=hidden id = 'district' value='{$district}'>"; echo "<input type=hidden id = 'machine' value='{$machine}'>"; if ($categoryCount % 3 == 2) { echo "</td><td style='padding-left:45px'>"; } $categoryCount++; } echo "</td></tr></table>"; echo "<input type=hidden name = 'maxCount' value='{$count}'>"; // echo "<input type=hidden name = 'maxCategoryCount' value='{$categoryCount}'>"; }
function getResults($result, $filepath) { global $PRINT; $cands = getCandidates($filepath); $pairs = getAllPairs($cands); $data = json_decode(file_get_contents($filepath), 1); $year = $data['ELECTION']['id']; if ($PRINT) { print "\n\n ----- {$year} -----\n\n"; } foreach ($data['BALLOTS'] as $ballot) { $pairs = runBallot($cands, $pairs, $ballot); } $pairs = rerunForNeitherOnBallot($cands, $pairs, $data['BALLOTS']); //printFormattedPairs($pairs); $new_cands[$year] = array(); foreach ($cands as $cc) { $new_cands[$year][$cc]['beat'] = array(); $new_cands[$year][$cc]['lost_to'] = array(); $new_cands[$year][$cc]['tied'] = array(); } foreach ($cands as $c1) { foreach ($cands as $c2) { if ($c1 != $c2) { if ($pairs[$c1][$c2] > $pairs[$c2][$c1]) { // print "$c1 beats $c2\n"; $new_cands[$year][$c1]['beat'][] = $c2; $new_cands[$year][$c2]['lost_to'][] = $c1; } if ($pairs[$c1][$c2] < $pairs[$c2][$c1]) { // print "$c2 beats $c1\n"; } if ($pairs[$c1][$c2] == $pairs[$c2][$c1]) { $new_cands[$year][$c2]['tied'][] = $c1; } } } } //print "\n\n ----- $year Summary -----\n\n"; //printFormattedResult($new_cands); return $new_cands; }
<body style="background-color: #F3E2A9"> <?php include "SaveElection.php"; connect(); echo "<br><br><center><h1>Enter Results</h1><h2> District: {$_POST['district']}, Machine: {$_POST['machine']}</h2></center>"; echo "<form action = './saveInputs.php' method ='post'>"; //get categories $categoriesResult = getCategories(); $count = 0; $categoryCount = 0; echo "<table><tr><td>"; while ($category = mysql_fetch_array($categoriesResult)) { //get candidates $id = $category['category_id']; //echo " ID = {$id} "; $candidates = getCandidates($id); echo "<h1> {$category['category_name']}</h1><table>"; while ($candidate = mysql_fetch_array($candidates)) { $hidden = "candidateID" . $count++; echo "<tr><td width = 25px></td><td>{$candidate['candidate_name']}</td><td> <input type ='text' name = '{$candidate['candidate_id']}'></td></tr>"; echo "<input type = hidden name = {$hidden} METHOD = 'POST' value = '{$candidate['candidate_id']}'>"; } echo "</table>"; if ($categoryCount % 3 == 2) { echo "</td><td style='padding-left:45px'>"; } $categoryCount++; } echo "</td></tr></table>"; echo "<INPUT type = hidden name = 'district' METHOD = 'POST' value = '{$_POST['district']}'>"; echo "<INPUT type = hidden name = 'machine' METHOD = 'POST' value = '{$_POST['machine']}'>";
<?php $fn2010 = 'elections/2010_election.json'; $fn2011 = 'elections/2011_election.json'; $data2010 = json_decode(file_get_contents($fn2010), 1); $data2011 = json_decode(file_get_contents($fn2011), 1); $ballots2010 = $data2010['BALLOTS']; $ballots2011 = $data2011['BALLOTS']; $cands2010 = getCandidates($ballots2010); $cands2011 = getCandidates($ballots2011); foreach ($cands2010 as $c) { if (!in_array($c, $cands2011)) { //print "$c NOT in 2011\n"; } } $missing_eids = array(); foreach ($cands2011 as $c) { if (!in_array($c, $cands2010)) { $missing_eids[] = $c; } } $fac2011 = array(); $fac2010 = $data2010['CANDIDATES']; foreach ($fac2010 as $faccand) { if (in_array($faccand['EID'], $cands2011)) { $fac2011[] = $faccand; } } foreach ($missing_eids as $miss) { $sub = array(); $sub['Full'] = 'Yes';