Allow custimizable graph sizes, to fit with newly modular results page. Also,
[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       @quickvote = QuickVote.new
33     end
34   end
35
36   def add_candidate
37     candidate_name = 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     # if the person has specified an election, we show them the voting
56     # page. otherwise, we redirect back to main the page
57     if @election
58       # look to see that the voter has been created and has voted in
59       # this election, and has confirmed their vote
60       @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?",
61                                   session.session_id, @election.id])[0]
62
63       # if the voter has not voted we destroy them
64       if @voter and not @voter.voted?
65         @voter.destroy
66         @voter = nil
67       end
68
69       # if the voter does not exist or has has been destroyed, lets
70       # create a new one
71       unless @voter
72         # create a new voter and populate it
73         @voter = QuickVoter.new
74         @voter.election = @election
75         @voter.session_id = session.session_id
76               
77         # create new vote and make it the defaulted sorted list
78         @voter.vote = Vote.new
79               @voter.save
80               @voter.vote.set_defaults!
81               @voter.reload
82       end
83     else
84       redirect_to :controller => 'site'
85     end
86   end
87
88   def confirm
89     # we need the election to verify that we have the right voter
90     election = QuickVote.ident_to_quickvote(params[:ident])
91
92     # find out who the voter is for this election
93     @voter = QuickVoter.find(:all, :conditions => ["session_id = ? and election_id = ?", 
94                                  session.session_id, election.id])[0]
95   
96     if not @voter
97       # we have not seen this  voter before. something is wrong, try
98       # again
99       redirect_to quickvote_url( :ident => params[:ident] ) 
100       
101     elsif @voter.voted? 
102       # this person has already voted, we try again
103       flash[:notice] = "You have already voted!"
104       redirect_to quickvote_url( :ident => params[:ident] )
105       
106     else
107       # record the ip address for posterity
108       @voter.ipaddress = request.env["REMOTE_ADDR"]
109       @voter.save
110       
111       # save the time the vote was made for statistical use
112       @voter.vote.time = Time.now
113       
114       # toggle the confirmation bit      
115       @voter.vote.confirm!
116      
117       @voter.reload
118       render :action => 'thanks'
119     end
120   end
121  
122   def change
123     voter = QuickVoter.find(:all, :conditions => ["session_id = ?", session.session_id])[0]
124     voter.destroy
125     redirect_to quickvote_url( :ident => params[:ident] )
126   end
127
128   def sort_candidates
129     @vote = Vote.find(params[:id])
130
131     @vote.rankings.each do |ranking|
132       ranking.rank = params['rankings-list'].index(ranking.candidate.id.to_s) + 1
133       ranking.save
134     end
135     render :nothing => true
136   end
137                 
138   def mapvoters
139     @map = GMap.new("map_div_id") 
140     @map.control_init(:large_map => true, :map_type => true) 
141     center=nil
142     QuickVote.ident_to_quickvote(params[:id]).voters.each do |voter|
143       next unless voter.ipaddress
144       location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
145       next unless location.lng and location.lat
146       unless center
147         center=[location.lat,location.lng]
148         @map.center_zoom_init(center,4)
149       end
150       marker = GMarker.new([location.lat,location.lng], :title => "Voter", :info_window => (voter.ipaddress or "unknown")+"   "+voter.vote.votestring)
151       @map.overlay_init(marker)
152     end
153   end
154   ###############################################################
155   # the following method pertains to displaying the results of a
156   # quickvote
157   ###############################################################
158
159   def results
160     unless @election = QuickVote.ident_to_quickvote(params[:ident])
161       flash[:notice] = "Cannot find quickvote #{params[:ident]}."
162       redirect_to :controller => 'site'
163       return
164     end
165     @results = @election.results
166     @candidates = {}
167     @election.candidates.each {|c| @candidates[c.id] = c}
168   end
169   
170   
171 end
172

Benjamin Mako Hill || Want to submit a patch?