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

Benjamin Mako Hill || Want to submit a patch?