function regenerate_vote_summary($poll_id) { // Find vote history file $history_fp = @fopen(vote_history_file_path($poll_id), "r"); if ($history_fp === FALSE) { return FALSE; } // Read votes into summary array $count = 0; $votes = array(); while (!@feof($history_fp)) { $history_line = @fgets($history_fp); if (empty($history_line)) { continue; } $history = explode_history($history_line); // Add to summary array $count++; $vote_value_id = $history[1]; if (isset($votes[$vote_value_id])) { $votes[$vote_value_id]++; } else { $votes[$vote_value_id] = 1; } } @fclose($history_fp); // Open summary file $summary_fp = @fopen(vote_summary_file_path($poll_id), "a"); if ($summary_fp === FALSE) { vote_die("Unable to open summary file for writing"); } if (@ftruncate($summary_fp, 0) === FALSE) { @fclose($summary_fp); vote_die("Unable to truncate summary file"); } // Regenerate summary based on counts from history in summary array fputs($summary_fp, $count . "\n"); // Total count foreach ($votes as $vote_id => $value) { fputs($summary_fp, $vote_id . "|" . $value . "\n"); } @fclose($summary_fp); return TRUE; }
function vote_summary_add($poll_id, $vote_value_id) { // Get existing poll summary $summarylist = vote_summary_list($poll_id); // Open/create summary file $summary_fp = @fopen(vote_summary_file_path($poll_id), "w"); if ($summary_fp === FALSE) { die("Unable to open summary file for writing"); } @flock($summary_fp, LOCK_EX); // Update total vote count if ($summarylist === FALSE || count($summarylist) < 1) { $count = 1; } else { $count = $summarylist[0] + 1; } fputs($summary_fp, $count . "\n"); // Add vote to value total if ($summarylist === FALSE) { // First vote $out = $vote_value_id . "|1"; fputs($summary_fp, $out . "\n"); } else { // Iterate through existing vote values $vote_counted = FALSE; for ($i = 1; $i < sizeof($summarylist); $i++) { $summary_row = explode_history($summarylist[$i]); if ($summary_row[0] === $vote_value_id) { // Increase vote count for this value $summary_row[1] += 1; $vote_counted = TRUE; } // Write out new vote count for this id $out = implode("|", $summary_row); fputs($summary_fp, $out . "\n"); } if ($vote_counted === FALSE) { // This is the first vote for this value $out = $vote_value_id . "|1"; fputs($summary_fp, $out . "\n"); } } fclose($summary_fp); }