diff --git a/app/controllers/prompts_controller.rb b/app/controllers/prompts_controller.rb index a51a6fa..6f4e96b 100644 --- a/app/controllers/prompts_controller.rb +++ b/app/controllers/prompts_controller.rb @@ -64,8 +64,14 @@ class PromptsController < InheritedResources::Base #@prompt.choices.each(&:compute_score!) respond_to do |format| if successful - format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok } - format.json { render :json => @question.picked_prompt.to_json(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok } + if @question.id == 120 #test0330 + next_prompt = @question.catchup_choose_prompt + else + next_prompt = @question.picked_prompt + end + + format.xml { render :xml => next_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok } + format.json { render :json => next_prompt.to_json(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok } else format.xml { render :xml => c, :status => :unprocessable_entity } format.json { render :json => c, :status => :unprocessable_entity } @@ -173,4 +179,4 @@ class PromptsController < InheritedResources::Base end_of_association_chain.with_choice_id(params[:choice_id]) end end -end \ No newline at end of file +end diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 8e90378..22bbd49 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -56,7 +56,12 @@ class QuestionsController < InheritedResources::Base def show @question = Question.find(params[:id]) unless params[:barebones] - @p = @question.picked_prompt + if params[:algorithm] && params[:algorithm] == "catchup" + logger.info("Question #{@question.id} requested catchup algorithm!") + @p = @question.catchup_choose_prompt + else + @p = @question.picked_prompt + end left_choice_text = Proc.new { |options| options[:builder].tag!('left_choice_text', @p.left_choice.item.data) } right_choice_text = Proc.new { |options| options[:builder].tag!('right_choice_text', @p.right_choice.item.data) } picked_prompt_id = Proc.new { |options| options[:builder].tag!('picked_prompt_id', @p.id) } diff --git a/app/models/question.rb b/app/models/question.rb index 5f58e20..bd33c33 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -37,17 +37,28 @@ class Question < ActiveRecord::Base end until @p.active? return @p end - + # adapted from ruby cookbook(2006): section 5-11 - def catchup_choose_prompt_id + def catchup_choose_prompt weighted = catchup_prompts_weights # Rand returns a number from 0 - 1, so weighted needs to be normalized - target = rand - weighted.each do |item, weight| - return item if target <= weight - target -= weight + prompt = nil + + until prompt && prompt.active? + target = rand + prompt_id = nil + + weighted.each do |item, weight| + if target <= weight + prompt_id = item + break + end + target -= weight + end + prompt = Prompt.find(prompt_id, :include => ['left_choice', 'right_choice']) end # check if prompt has two active choices here, maybe we can set this on the prompt level too? + prompt end @@ -56,8 +67,13 @@ class Question < ActiveRecord::Base weights = Hash.new(0) throttle_min = 0.05 #assuming all prompts exist - prompts.each do |p| - weights[p.id] = [(1.0/ (p.votes.size + 1).to_f).to_f, throttle_min].min + + #the_prompts = prompts.find(:all, :select => 'id, votes_count') + #We don't really need to instantiate all the objects + the_prompts = ActiveRecord::Base.connection.select_all("SELECT id, votes_count from prompts where question_id =#{self.id}") + + the_prompts.each do |p| + weights[p["id"].to_i] = [(1.0/ (p["votes_count"].to_i + 1).to_f).to_f, throttle_min].min end normalize!(weights) weights -- libgit2 0.21.2