Added processing and presentatin of results.
author<mako@atdot.cc> <>
Wed, 16 Aug 2006 19:30:18 +0000 (15:30 -0400)
committer<mako@atdot.cc> <>
Wed, 16 Aug 2006 19:30:18 +0000 (15:30 -0400)
app/controllers/elections_controller.rb
app/models/vote.rb
app/views/elections/_winner.rhtml [new file with mode: 0644]
app/views/elections/_winner_details.rhtml [new file with mode: 0644]
app/views/elections/detailed_results.rhtml
config/environment.rb

index 4a85c293b5ea4b424cf78c852e39ca45496feaec..4cb72bc779d29fdf7deae3f6016fabfaf005144a 100644 (file)
@@ -1,8 +1,8 @@
 class ElectionsController < ApplicationController
   model :raw_voter_list, :voter, :vote, :candidate
 
-
-  # general methods for dealing with elections
+  ## general methods for dealing with elections
+  ####################################################################
   def index
     list
     render :action => 'list'
@@ -12,8 +12,10 @@ class ElectionsController < ApplicationController
     @election_pages, @elections = paginate :elections, :per_page => 10
   end
 
-  # methods for displaying, creating, and manipulating election overview
-  # data
+  ## methods for displaying, creating,
+  ## and manipulating election overview data
+  ####################################################################
+
   def show
     @election = Election.find(params[:id])
   end
@@ -53,6 +55,7 @@ class ElectionsController < ApplicationController
 
   # methods fod display, adding, deleting, and manipulating candidate
   # information for elections
+  ####################################################################
   def new_candidates
     @election = Election.find( params[:id] )
   end
@@ -75,8 +78,9 @@ class ElectionsController < ApplicationController
     @election = Election.find( params[:id] )
   end
 
-  # methods for displaying, adding, deleting, and manipulating voters
-  # for a particular election
+  ## methods for displaying, adding, deleting, and manipulating voters
+  ## for a particular election
+  ####################################################################
   def new_voters
     @election = Election.find( params[:id] )
     if params.has_key?[:raw_voter_list]
@@ -100,15 +104,33 @@ class ElectionsController < ApplicationController
     voter.destroy
   end
   
-  def summary_results 
+  ## methods for computing and printing results
+  ####################################################################
+  def results
+    @election = Election.find( params[:id] )
+    votes = []
+
+    @election.voters.each do |voter|
+      if voter.vote and voter.vote.confirmed?
+        votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
+      end
+    end
+    
+    @voteobj = CloneproofSSDVote.new(votes)
+    @resultobj = @voteobj.result
+    @winners = @resultobj.winners
+    
+    @candidates_by_id = {}
+    @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
   end
   
   def detailed_results
-    @election = Election.find( params[:id] )
-
+   
+    self.results
+    breakpoint
     @voter_list = []
     @vote_list = []
-    @election.voters.each do |voter|
+    @election.voters. each do |voter|
       if voter.vote and voter.vote.confirmed?
         @voter_list << voter.email
        @vote_list << voter.vote
@@ -117,13 +139,11 @@ class ElectionsController < ApplicationController
 
     @vote_list.sort!
     @vote_list.sort! { |a,b| a.token <=> b.token }
-    #breakpoint
-
   end
-  
+
+  ## private methods
+  ####################################################################
   private
-    def randomize_order
-    end
 
     def process_incoming_voters(raw_voter_list)
       incoming_voters = RawVoterList.new( raw_voter_list )
index 82f34a929bb6443a7d0b5058ff1263bbb1736ab3..429e212377781d27aa4a5580d9dcb65abd5b19eb 100644 (file)
@@ -36,7 +36,7 @@ class Vote < ActiveRecord::Base
     destroy_rankings
     self.votes.each_with_index do |candidate, index| 
       ranking = Ranking.new
-      ranking.rank = index + 1
+      ranking.rank = index
       ranking.candidate =  Candidate.find(candidate)
       self.rankings << ranking
     end
diff --git a/app/views/elections/_winner.rhtml b/app/views/elections/_winner.rhtml
new file mode 100644 (file)
index 0000000..531e0d2
--- /dev/null
@@ -0,0 +1,21 @@
+<% %>
+
+<% if @winners.length > 1 %>
+  <p>There were multiple winners for the election. The winners (tied)
+  were:
+
+  <ul>
+  <% for candidate in @winners %>
+    <li><%= @candidates_by_id[candidate].name %></li>
+  <% end %>
+  </ul>
+
+<% else %>
+
+  <% winner = @winners[0] %>
+  <p>The winner of the election was:
+     <strong><%= @candidates_by_id[winner].name %></strong>
+  </p>
+
+<% end %>
+
diff --git a/app/views/elections/_winner_details.rhtml b/app/views/elections/_winner_details.rhtml
new file mode 100644 (file)
index 0000000..0903952
--- /dev/null
@@ -0,0 +1,28 @@
+<% %>
+<p>The matrix listing the number of times that any candidate was
+preferred to any other candidates is listed here:</p>
+
+<table border="1">
+<tr>
+<th></th>
+<% for candidate in @election.candidates.sort %>
+  <th><%= candidate.name %></th>
+<% end %>
+</tr>
+<% for cand1 in @election.candidates.sort %>
+  <tr>
+  <th><%= cand1.name %></th>
+  <% for cand2 in @election.candidates.sort %>
+    <td>
+    <% if cand1 == cand2 %>
+      N/A
+      <% next %>
+    <% else %>
+      <%= @voteobj.votes[cand1.id][cand2.id] %>
+    <% end %>
+    </td>
+  <% end %>
+  </tr>
+<% end %>
+</table>
+
index cfb0e5a273c6f5e1a3738f01742b44511eed6adb..46685703f77c577389fb6665487bc63aae4f7927 100644 (file)
@@ -1,10 +1,25 @@
 <% %>
 
+<h2>Result</h2>
+
+<%= render_partial 'winner' %>
+
+<h2>Result Details</h2>
+
+<%= render_partial 'winner_details' %>
+
+<h2>Election Rolls for Voter Verification</h2>
+
 <p>The voting rolls -- displayed here in alphabetical order -- for the
 last election are are follows.</p>
 
-<h2>Voters</h2>
-<table>
+<p>Information is displayed here to help voters verify that their own
+vote was recorded correctly and that the election was not tampered
+with.</p>
+
+<h3>Voters</h3>
+<table border="1">
+<th>Voters (A-Z)</th>
 <% for email in @voter_list %>
 <tr>
 <td><%= email %></td>
@@ -12,26 +27,28 @@ last election are are follows.</p>
 <% end %>
 </table>
 
-<h2>Votes (by Token)</h2>
+<h3>Votes by Token</h3>
 
 <p>The votes, listed in alphabetical order by token.</p>
 
 <table border="1">
 <tr>
-  <th rowspan="2">Token</th>
+  <th rowspan="2">Token (0-9, A-Z)</th>
   <th colspan="<%= @election.candidates.length %>">Rank of Candidates</th>
 </tr>
 <tr>
+
 <% for candidate in @election.candidates.sort.reverse %>
   <th><%= candidate %></th>
 <% end %>
+
 </tr>
 <% for vote in @vote_list %>
-<tr>
-<td><%= vote.token %></td>
-<% for ranking in vote.rankings %>
-<td><%= ranking %></td>
-<% end %>
-</tr>
+  <tr>
+  <td><%= vote.token %></td>
+  <% for ranking in vote.rankings %>
+    <td><%= ranking %></td>
+  <% end %>
+  </tr>
 <% end %>
 </table>
index de8cbafdbeb2f33f1dd364192e16b0dcd593da39..a0ed78ec7203fa80140a323524c4afc5e33ea18d 100644 (file)
@@ -52,3 +52,4 @@ end
 
 require 'uniq_token' 
 require 'randarray' 
+require 'rubyvote'

Benjamin Mako Hill || Want to submit a patch?