From fb487ab0581530bd9d5fca1f833298bbfeb721f4 Mon Sep 17 00:00:00 2001 From: Pius Uzamere Date: Thu, 3 Dec 2009 16:45:37 -0500 Subject: [PATCH] results and scoring --- app/controllers/choices_controller.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ app/models/choice.rb | 36 ++++++++++++++++++++++++++++++++++++ app/models/prompt.rb | 4 ++++ app/models/visitor.rb | 2 ++ 4 files changed, 94 insertions(+), 0 deletions(-) diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index c8220ff..94ae979 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -4,6 +4,58 @@ class ChoicesController < InheritedResources::Base belongs_to :question has_scope :active, :boolean => true, :only => :index + def index + if params[:limit] + @question = Question.find(params[:question_id])#, :include => :choices) + @choices = Choice.find(:all, :conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, :order => 'score DESC') + #@choices = @question.choices(true).sort_by {|c| 0 - c.score }.first(params[:limit].to_i)#.active + else + @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices + @choices = @question.choices(true)#.sort_by {|c| 0 - c.score } + end + index! do |format| + format.xml { render :xml => params[:data].blank? ? @choices.to_xml(:methods => [:data, :votes_count]) : @choices.to_xml(:include => [:items], :methods => [:data, :votes_count])} + format.json { render :json => params[:data].blank? ? @choices.to_json : @choices.to_json(:include => [:items]) } + end + + # index! do |format| + # if !params[:voter_id].blank? + # format.xml { render :xml => User.find(params[:voter_id]).prompts_voted_on.to_xml(:include => [:items, :votes], + # :methods => [ :active_items_count, + # :all_items_count, + # :votes_count ]) } + # format.json { render :json => User.find(params[:voter_id]).prompts_voted_on.to_json(:include => [:items, :votes], + # :methods => [ :active_items_count, + # :all_items_count, + # :votes_count ]) } + # else + # format.xml { render :xml => params[:data].blank? ? + # @prompts.to_xml : + # @prompts.to_xml(:include => [:items]) + # } + # format.json { render :json => params[:data].blank? ? @prompts.to_json : @prompts.to_json(:include => [:items]) } + # end + # end + end + + def show + @question = Question.find(params[:question_id]) + @prompt = @question.prompts.find(params[:id]) + show! do |format| + format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])} + format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])} + end + end + + def single + @question = current_user.questions.find(params[:question_id]) + @prompt = @question.prompts.pick + show! do |format| + format.xml { render :xml => @prompt.to_xml} + format.json { render :json => @prompt.to_json} + end + end + def create_from_abroad authenticate diff --git a/app/models/choice.rb b/app/models/choice.rb index 080a833..c6751fb 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -8,6 +8,25 @@ class Choice < ActiveRecord::Base attr_accessor :data + def data + item.data + end + + + has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" + has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" + def wins_plus_losses + (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) + end + + def wins + votes_count + end + + def votes_count + votes(true).size + end + after_create :generate_prompts def before_create @@ -17,7 +36,24 @@ class Choice < ActiveRecord::Base end end + def before_save + unless self.score + self.score = 0.0 + end + return true #so active record will save + end + + + def compute_score + if wins_plus_losses == 0 + return 0 + else + (wins.to_f / wins_plus_losses ) * 100 + end + end + protected + def generate_prompts #once a choice is added, we need to generate the new prompts (possible combinations of choices) diff --git a/app/models/prompt.rb b/app/models/prompt.rb index 720d96a..2f4c97a 100644 --- a/app/models/prompt.rb +++ b/app/models/prompt.rb @@ -26,6 +26,10 @@ class Prompt < ActiveRecord::Base validates_presence_of :left_choice, :on => :create, :message => "can't be blank" validates_presence_of :right_choice, :on => :create, :message => "can't be blank" + def votes_count + votes(true).size + end + def choices [left_choice, right_choice] end diff --git a/app/models/visitor.rb b/app/models/visitor.rb index bd50353..51a6975 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -16,6 +16,8 @@ class Visitor < ActiveRecord::Base choice = prompt.choices[ordinality] #we need to guarantee that the choices are in the right order (by position) prompt_vote = votes.create!(:voteable => prompt) choice_vote = votes.create!(:voteable => choice) + choice.score = choice.compute_score + choice.save! end def skip!(prompt) -- libgit2 0.21.2