1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
19 class QuickvoteController < ApplicationController
22 require_dependency "quick_voter"
23 require_dependency "quick_vote"
24 require_dependency "vote"
25 require_dependency "election"
27 #############################################################
28 # the following methods pertain to creating quickvotes
29 #############################################################
33 @quickvote = QuickVote.new(params[:quickvote])
35 # check to see if any of the advanced options have been changed
36 new_qv = QuickVote.new
37 if @quickvote.election_method != new_qv.election_method \
38 or @quickvote.enddate.day != new_qv.enddate.day \
39 or @quickvote.viewable != new_qv.viewable \
40 or @quickvote.notices != new_qv.notices
45 show_advanced ||= false
49 # store the candidate grabbed through ajax and stored in flash
50 @quickvote.candidate_names = flash[:candidate_names]
51 @quickvote.description=@quickvote.description
53 #record who created the quickvote so that person can monitor it easily
54 @quickvote.quickuser = session.session_id
56 #Give registered users additional QuickVote functionality
57 @quickvote.user_id = session[:user][:id] if session[:user]
59 # try to save, if it fails, show the page again (the flash should
62 @quickvote = @quickvote.reload
63 # blank sidebar and show the success page
65 render :action => 'success'
68 @sidebar_content = render_to_string(:partial => 'create_sidebar',
69 :locals => {:show_advanced => show_advanced})
70 flash.keep(:candidate_names)
74 # if we don't have a quickvote param, it means that the person
75 # here has not been hitting this page and we can clear any
76 # candidate_names list in the flash
77 flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
78 @quickvote = QuickVote.new
79 @sidebar_content = render_to_string(:partial => 'create_sidebar',
80 :locals => {:show_advanced => show_advanced})
86 candidate_name = params[:ajax][:newcandidate]
87 unless candidate_name.strip.empty?
88 if flash.has_key?(:candidate_names) \
89 and flash[:candidate_names].instance_of?(Array)
90 unless flash[:candidate_names].index(candidate_name)
91 flash[:candidate_names] << candidate_name
94 flash[:candidate_names] = [ candidate_name ]
97 flash.keep(:candidate_names)
98 render :partial => 'candidate_list'
101 #############################################################
102 # the following methods pertain to *voting* in the quickvotes
103 #############################################################
106 @election = QuickVote.ident_to_quickvote(params[:ident])
107 # if the person has specified an election, we show them the voting
108 # page. otherwise, we redirect back to main the page
110 # look to see that the voter has been created and has voted in
111 # this election, and has confirmed their vote
112 @voter = QuickVoter.find(:all,
113 :conditions => ["session_id = ? and election_id = ?",
114 session.session_id, @election.id])[0]
116 # if the voter has not voted we destroy them
117 if @voter and not @voter.voted?
122 # if the voter does not exist or has has been destroyed, lets
125 # create a new voter and populate it
126 @voter = QuickVoter.new
127 @voter.election = @election
128 @voter.session_id = session.session_id
130 # create new vote and make it the defaulted sorted list
131 @voter.vote = Vote.new
133 @voter.vote.set_defaults!
137 redirect_to :controller => 'front'
143 # we need the election to verify that we have the right voter
144 election = QuickVote.ident_to_quickvote(params[:ident])
146 # find out who the voter is for this election
147 @voter = QuickVoter.find(:all,
148 :conditions => ["session_id = ? and election_id = ?",
149 session.session_id, election.id])[0]
152 # we have not seen this voter before. something is wrong, try
154 redirect_to quickvote_url( :ident => params[:ident] )
157 # this person has already voted, we try again
158 flash[:notice] = "You have already voted!"
159 redirect_to quickvote_url( :ident => params[:ident] )
163 # record the ip address for posterity
164 @voter.ipaddress = request.env["HTTP_X_FORWARDED_FOR"]
167 # toggle the confirmation bit
171 render :action => 'thanks'
176 voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
177 session.session_id])[0]
179 redirect_to quickvote_url( :ident => params[:ident] )
183 @map = GMap.new("map_div_id")
184 @map.control_init(:large_map => true, :map_type => true)
186 @election=QuickVote.ident_to_quickvote(params[:id])
187 @election.voters.each do |voter|
188 next unless voter.ipaddress
191 if Cache and location=Cache.get("GEO:#{voter.ipaddress}")
193 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
194 Cache.set "GEO:#{voter.ipaddress}", location
196 location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
198 next unless location.lng and location.lat
201 center = [location.lat, location.lng]
202 @map.center_zoom_init(center, 4)
205 marker = GMarker.new([location.lat,location.lng],
207 :info_window => (voter.ipaddress or "unknown"))
208 @map.overlay_init(marker)
212 ###############################################################
213 # the following method pertains to displaying the results of a
215 ###############################################################
218 unless @election = QuickVote.ident_to_quickvote(params[:ident])
219 flash[:notice] = "Cannot find quickvote #{params[:ident]}."
220 redirect_to :controller => 'front'
223 if @election.viewable == 0 && @election.active == 1
224 render :action => 'not_viewable' and return
226 @results = @election.results
228 @election.candidates.each {|c| @candidates[c.id] = c}
229 @names = @election.names_by_id
230 @sidebar_content = render_to_string :partial => 'results_sidebar'
234 @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",