Merge from head
[selectricity] / app / controllers / quickvote_controller.rb
1 class QuickvoteController < ApplicationController
2   layout 'main'
3   require_dependency "quick_voter"
4   require_dependency "quick_vote"
5   require_dependency "vote"
6   require_dependency "election"
7
8   #############################################################
9   # the following methods pertain to creating quickvotes
10   #############################################################
11
12   def create
13     if params[:quickvote] 
14       @quickvote = QuickVote.new(params[:quickvote])
15       # store the candidate grabbed through ajax and stored in flash
16       @quickvote.candidatelist = flash[:candlist]
17       @quickvote.description=@quickvote.description
18       # try to save, if it fails, show the page again (the flash should
19       # still be intact
20       if @quickvote.save
21         @quickvote = @quickvote.reload
22         render :action => 'success'
23       else
24         flash.keep(:candlist)
25       end 
26
27     else
28       # if we don't have a quickvote param, it means that the person
29       # here has not been hitting this page and we can clear any
30       # candlist in the flash
31       flash.delete(:candlist) if flash.has_key?(:candlist)
32     end
33   end
34
35   def add_candidate
36     candidate_name = params[:ajax][:newcandidate]
37     unless candidate_name.strip.empty?
38       if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array) 
39         flash[:candlist] << candidate_name unless flash[:candlist].index(candidate_name)
40      else
41        flash[:candlist] = [ candidate_name ]
42       end
43     end
44     flash.keep(:candlist)
45     render_partial 'candidate_list'
46   end
47  
48   #############################################################
49   # the following methods pertain to *voting* in the quickvotes
50   #############################################################
51
52   def index
53     @election = QuickVote.ident_to_quickvote(params[:ident])
54     # if the person has specified an election, we show them the voting
55     # page. otherwise, we redirect back to main the page
56     if @election
57       # look to see that the voter has been created and has voted in
58       # this election, and has confirmed their vote
59       @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
60                                   session.session_id, @election.id])[0]
61
62       # if the voter has not voted we destroy them
63       if @voter and not @voter.voted?
64         @voter.destroy
65         @voter = nil
66       end
67
68       # if the voter does not exist or has has been destroyed, lets
69       # create a new one
70       unless @voter
71         # create a new voter and populate it
72         @voter = QuickVoter.new
73         @voter.election = @election
74         @voter.session_id = session.session_id
75               
76         # create new vote and make it the defaulted sorted list
77         @voter.vote = Vote.new
78               @voter.save
79               @voter.vote.set_defaults!
80               @voter.reload
81       end
82     else
83       redirect_to :controller => 'site'
84     end
85   end
86
87   def confirm
88     # we need the election to verify that we have the right voter
89     election = QuickVote.ident_to_quickvote(params[:ident])
90
91     # find out who the voter is for this election
92     @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?", 
93                                  session.session_id, election.id])[0]
94   
95     if not @voter
96       # we have not seen this  voter before. something is wrong, try
97       # again
98       redirect_to quickvote_url( :ident => params[:ident] ) 
99       
100     elsif @voter.voted? 
101       # this person has already voted, we try again
102       flash[:notice] = "You have already voted!"
103       redirect_to quickvote_url( :ident => params[:ident] )
104       
105     else
106       # record the ip address for posterity
107       @voter.ipaddress = request.env["REMOTE_ADDR"]
108       @voter.save
109       
110       # save the time the vote was made for statistical use
111       @voter.vote.time = Time.now
112       
113       # toggle the confirmation bit      
114       @voter.vote.confirm!
115      
116       @voter.reload
117       render :action => 'thanks'
118     end
119   end
120  
121   def change
122     voter = QuickVoter.find(:all, :conditions => ["session_id = ?", session.session_id])[0]
123     voter.destroy
124     redirect_to quickvote_url( :ident => params[:ident] )
125   end
126
127   def sort_candidates
128     @vote = Vote.find(params[:id])
129
130     @vote.rankings.each do |ranking|
131       ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
132       ranking.save
133     end
134     render :nothing => true
135   end
136                 
137   def mapvoters
138     @map = GMap.new("map_div_id") 
139     @map.control_init(:large_map => true, :map_type => true) 
140     center=nil
141     QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
142       next unless voter.ipaddress
143       location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
144       next unless location.lng and location.lat
145       unless center
146         center=[location.lat,location.lng]
147         @map.center_zoom_init(center,4)
148       end
149       marker = GMarker.new([location.lat,location.lng], :title => "Voter", :info_window => (voter.ipaddress or "unknown")+"   "+voter.vote.votestring)
150       @map.overlay_init(marker)
151     end
152   end
153   ###############################################################
154   # the following method pertains to displaying the results of a
155   # quickvote
156   ###############################################################
157
158   def results
159     unless @election = QuickVote.ident_to_quickvote(params[:ident])
160       flash[:notice] = "Cannot find quickvote #{params[:ident]}."
161       redirect_to :controller => 'site'
162       return
163     end
164     @election.results
165     @candidates = {}
166     @election.candidates.each {|c| @candidates[c.id] = c}
167   end
168 end

Benjamin Mako Hill || Want to submit a patch?