X-Git-Url: https://projects.mako.cc/source/selectricity/blobdiff_plain/8ae0d71896fa022dd80e5a5de993e1382564fcb1..597d4add3416d06c2befb6368a06f6cea4215a39:/app/models/selectricity_service.rb diff --git a/app/models/selectricity_service.rb b/app/models/selectricity_service.rb index 3f3e42e..aba9c02 100644 --- a/app/models/selectricity_service.rb +++ b/app/models/selectricity_service.rb @@ -1,10 +1,38 @@ +# Selectricity: Voting Machinery for the Masses +# Copyright (C) 2007, 2008 Benjamin Mako Hill +# Copyright (C) 2007 Massachusetts Institute of Technology +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . + require 'action_controller/integration' class SelectricityService < ActionWebService::Base web_service_api SelectricityAPI + + ## Casts a quickvote. + ## Expects a quickvote name, a voter ID, and a list of candidate ID's respectively. + ## Returns a string containing any potential errors that occurred in the process. def cast_quickvote(election_name, voter_id, vote_list) election = QuickVote.ident_to_quickvote election_name if election + candidates=election.candidates.collect { |c| c.id } + vote_list[0].each do |vote| + raise ArgumentError.new("Invalid Candidate ID #{vote}") unless candidates.index(vote) + end + raise ArgumentError.new("You must rank all candidates") unless candidates.length <= vote_list[0].length + raise ArgumentError.new("Please rank each candidate only once") if vote_list[0].uniq! voter = QuickVoter.new voter.election = election voter.ipaddress = "XMLRPC Request" @@ -15,12 +43,19 @@ class SelectricityService < ActionWebService::Base voter.save! voter.vote.confirm! voter.save! + else + raise ArgumentError.new("Cannot find election #{election_name}") end end + + ## Converts QuickVote candidate ID's to names + ## Takes in a QuickVote name and a list of candidate ID's, and returns the names of + ## each candidate. Useful for doing just a few lookups; it's more efficient to use + ## get_quickvote_candidate_map for presenting info about an entire election. def quickvote_candidate_ids_to_names(shortname, id_list) qv=QuickVote.ident_to_quickvote(shortname) candidates={} - return [] unless qv + raise ArgumentError.new("Quickvote by name #{shortname} doesn't exist") unless qv qv.results qv.candidates.each {|c| candidates[c.id] = c} results=[] @@ -34,14 +69,16 @@ class SelectricityService < ActionWebService::Base } results end + + ## Return the results of a QuickVote. + ## Takes in the name of a quickvote, and returns a structure as described by + ## QuickVoteResultStruct def get_quickvote_results(shortname) #TODO: Validate shortname qv=QuickVote.ident_to_quickvote(shortname) - result=VoteResultStruct.new - result.errors=[] + result=QuickVoteResultStruct.new unless qv - result.errors << "No quickvote with name #{shortname} found!" - return result + raise ArgumentError.new("No quickvote with name #{shortname} found!") end qv.results result.plurality_winners=qv.plurality_result.winners @@ -51,13 +88,17 @@ class SelectricityService < ActionWebService::Base result.borda_winners=qv.borda_result.winners result end + + ## Returns information regarding all the candidates in a QuickVote + ## Takes in a QuickVote name, and returns the list of names and ID's of candidates + ## This can be useful for presenting the user with a list of readable names, while + ## the software sends results to us in the numeric ID's we require. The two lists are in + ## respective order. def get_quickvote_candidate_map(shortname) qv=QuickVote.ident_to_quickvote(shortname) - result=CandidateMap.new - result.errors=[] + result=QuickVoteCandidateMap.new unless qv - result.errors << "No quickvote with name #{shortname} found!" - return result + raise ArgumentError.new("No quickvote with name #{shortname} found!") end candidates={} qv.candidates.each {|c| candidates[c.id] = c.name} @@ -65,16 +106,60 @@ class SelectricityService < ActionWebService::Base result.candidate_names=candidates.values result end + + ## Get information on all the votes cast in a QuickVote + ## Takes in the name of a QuickVote, returns an array of QuickVoterInfo structures. def get_quickvote_votes(shortname) - qv=QuickVote.ident_to_quickvote(shortname) - votes=Array.new + qv = QuickVote.ident_to_quickvote(shortname) + unless qv - return result + raise ArgumentError.new("Cannot find QuickVote #{shortname}") + end + + qv.votes.collect do |vote| + QuickVoterInfo.new(:voter_id => vote.voter.id, + :voter_ipaddress => vote.voter.ipaddress, + :vote_time => vote.time.to_i, + :vote => vote.votes, + :voter_session_id => vote.voter.session_id) end - qv.votes.each do |vote| - votes << VoteInfo.new(:voter_id => vote.voter.id, :voter_ipaddress => vote.voter.ipaddress, :vote_time => vote.time.to_i) + end + + ## Gets a list of all QuickVotes in the system. + def list_quickvotes() + QuickVote.find(:all).collect do |election| + get_quickvote(election.name) + end + end + + ## Gets information on a particular QuickVote + ## Takes in a QuickVote name + def get_quickvote(shortname) + unless election = QuickVote.ident_to_quickvote(shortname) + raise ArgumentError.new("Cannot find QuickVote named #{shortname}") end - return votes + + QuickVoteStruct.new( + :id => election.id, + :name => election.name, + :description => election.description, + :candidate_ids => election.candidates.collect {|c| c.id }, + :candidate_names => election.candidates.collect {|c| c.name } ) end - + + ## Create a QuickVote + ## Pass in a QuickVoteStruct populated with all the fields but the candidate ID's + ## Any candidate ID's you fill in will be ignored anyway. + def create_quickvote(election) + qv = QuickVote.new(:name => election.name, + :description => election.description) + qv.candidate_names = election.candidate_names + + if qv.save + return "" + else + raise ArgumentError.new("Saving quickvote FAILED:"+qv.errors.inspect) + end + end + end