Commit 5c18a693f1dd6764cfd3e73a03d4ccc3eb3d1d87
Exists in
master
and in
1 other branch
Merge branch pairwise_search into master
Showing
2 changed files
with
68 additions
and
21 deletions
Show diff stats
app/controllers/choices_controller.rb
@@ -5,20 +5,22 @@ class ChoicesController < InheritedResources::Base | @@ -5,20 +5,22 @@ class ChoicesController < InheritedResources::Base | ||
5 | has_scope :active, :type => :boolean, :only => :index | 5 | has_scope :active, :type => :boolean, :only => :index |
6 | 6 | ||
7 | before_filter :authenticate | 7 | before_filter :authenticate |
8 | - | 8 | + |
9 | def index | 9 | def index |
10 | if params[:limit] | 10 | if params[:limit] |
11 | @question = current_user.questions.find(params[:question_id]) | 11 | @question = current_user.questions.find(params[:question_id]) |
12 | 12 | ||
13 | find_options = {:conditions => {:question_id => @question.id}, | 13 | find_options = {:conditions => {:question_id => @question.id}, |
14 | - :limit => params[:limit].to_i, | 14 | + :limit => params[:limit].to_i, |
15 | :order => 'score DESC' | 15 | :order => 'score DESC' |
16 | } | 16 | } |
17 | - | 17 | + |
18 | find_options[:conditions].merge!(:active => true) unless params[:include_inactive] | 18 | find_options[:conditions].merge!(:active => true) unless params[:include_inactive] |
19 | 19 | ||
20 | + find_options[:include] = [:creator] | ||
21 | + | ||
20 | if(params[:ignore_flagged]) | 22 | if(params[:ignore_flagged]) |
21 | - find_options.merge({ :include => :flags }) | 23 | + find_options[:include] << :flags |
22 | find_options[:conditions].merge!({:flags => {:id => nil}}) | 24 | find_options[:conditions].merge!({:flags => {:id => nil}}) |
23 | end | 25 | end |
24 | 26 | ||
@@ -27,22 +29,64 @@ class ChoicesController < InheritedResources::Base | @@ -27,22 +29,64 @@ class ChoicesController < InheritedResources::Base | ||
27 | @choices = Choice.find(:all, find_options) | 29 | @choices = Choice.find(:all, find_options) |
28 | 30 | ||
29 | else | 31 | else |
30 | - @question = current_user.questions.find(params[:question_id]) #eagerloads ALL choices | 32 | + @question = current_user.questions.find(params[:question_id]) |
33 | + | ||
34 | + sort_by = "score" | ||
35 | + sort_order = "DESC" | ||
36 | + | ||
37 | + if !params[:order].blank? | ||
38 | + | ||
39 | + if params[:order][:sort_order].downcase == "asc" or params[:order][:sort_order].downcase == "desc" | ||
40 | + sort_order = params[:order][:sort_order].downcase | ||
41 | + end | ||
42 | + | ||
43 | + case params[:order][:sort_by].downcase | ||
44 | + when "data" | ||
45 | + sort_by = "choices.data" | ||
46 | + when "created_date" | ||
47 | + sort_by = "choices.created_at" | ||
48 | + when "visitor_identifier" | ||
49 | + sort_by = "visitors.identifier" | ||
50 | + else | ||
51 | + sort_by = "score" | ||
52 | + end | ||
53 | + | ||
54 | + end | ||
31 | 55 | ||
32 | - if params[:inactive_ignore_flagged] | ||
33 | - @choices = @question.choices(true).inactive_ignore_flagged.find(:all) | ||
34 | - elsif params[:inactive] | ||
35 | - @choices = @question.choices(true).inactive.find(:all) | 56 | + order = "#{sort_by} #{sort_order}" |
57 | + | ||
58 | + find_options = { | ||
59 | + :include => [:creator], | ||
60 | + :conditions => {}, | ||
61 | + :order => order | ||
62 | + } | ||
63 | + | ||
64 | + if params[:filter] && !params[:filter][:data].blank? | ||
65 | + conditions = [] | ||
66 | + conditions << ['lower(data) like ?', "%#{params[:filter][:data].downcase}%"] | ||
67 | + find_options[:conditions] = [conditions.map{|c| c[0] }.join(" AND "), *conditions.map{|c| c[1..-1] }.flatten] | ||
68 | + end | ||
69 | + | ||
70 | + if (!params[:reproved].blank?) | ||
71 | + @choices = @question.choices(true).reproved.find(:all, find_options) | ||
36 | else | 72 | else |
37 | - unless params[:include_inactive] | ||
38 | - @choices = @question.choices(true).active.find(:all) | 73 | + if params[:inactive_ignore_flagged] |
74 | + @choices = @question.choices(true).inactive_ignore_flagged.find(:all, find_options) | ||
75 | + elsif params[:inactive] | ||
76 | + @choices = @question.choices(true).inactive.find(:all, find_options) | ||
39 | else | 77 | else |
40 | - @choices = @question.choices.find(:all) | 78 | + unless params[:include_inactive] |
79 | + @choices = @question.choices(true).active.find(:all, find_options) | ||
80 | + else | ||
81 | + @choices = @question.choices.find(:all, find_options) | ||
82 | + end | ||
41 | end | 83 | end |
42 | end | 84 | end |
85 | + | ||
43 | end | 86 | end |
87 | + | ||
44 | index! do |format| | 88 | index! do |format| |
45 | - format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier])} | 89 | + format.xml { render :xml => @choices.to_xml(:only => [ :data, :score, :id, :active, :created_at, :wins, :losses], :methods => [:user_created, :creator_identifier, :reproved])} |
46 | end | 90 | end |
47 | 91 | ||
48 | end | 92 | end |
@@ -62,10 +106,10 @@ class ChoicesController < InheritedResources::Base | @@ -62,10 +106,10 @@ class ChoicesController < InheritedResources::Base | ||
62 | end | 106 | end |
63 | 107 | ||
64 | def create | 108 | def create |
65 | - | 109 | + |
66 | visitor_identifier = params[:choice].delete(:visitor_identifier) | 110 | visitor_identifier = params[:choice].delete(:visitor_identifier) |
67 | 111 | ||
68 | - visitor = current_user.default_visitor | 112 | + visitor = current_user.default_visitor |
69 | if visitor_identifier | 113 | if visitor_identifier |
70 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) | 114 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) |
71 | end | 115 | end |
@@ -78,16 +122,15 @@ class ChoicesController < InheritedResources::Base | @@ -78,16 +122,15 @@ class ChoicesController < InheritedResources::Base | ||
78 | @choice = Choice.new(params[:choice]) | 122 | @choice = Choice.new(params[:choice]) |
79 | create! | 123 | create! |
80 | end | 124 | end |
81 | - | 125 | + |
82 | def flag | 126 | def flag |
83 | @question = current_user.questions.find(params[:question_id]) | 127 | @question = current_user.questions.find(params[:question_id]) |
84 | @choice = @question.choices.find(params[:id]) | 128 | @choice = @question.choices.find(params[:id]) |
85 | 129 | ||
86 | flag_params = {:choice_id => params[:id].to_i, :question_id => params[:question_id].to_i, :site_id => current_user.id} | 130 | flag_params = {:choice_id => params[:id].to_i, :question_id => params[:question_id].to_i, :site_id => current_user.id} |
87 | 131 | ||
88 | - if explanation = params[:explanation] | 132 | + if explanation = params[:explanation] |
89 | flag_params.merge!({:explanation => explanation}) | 133 | flag_params.merge!({:explanation => explanation}) |
90 | - | ||
91 | end | 134 | end |
92 | if visitor_identifier = params[:visitor_identifier] | 135 | if visitor_identifier = params[:visitor_identifier] |
93 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) | 136 | visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) |
@@ -130,6 +173,5 @@ class ChoicesController < InheritedResources::Base | @@ -130,6 +173,5 @@ class ChoicesController < InheritedResources::Base | ||
130 | end | 173 | end |
131 | end | 174 | end |
132 | 175 | ||
133 | - | ||
134 | end | 176 | end |
135 | - | 177 | + |
app/models/choice.rb
@@ -23,6 +23,7 @@ class Choice < ActiveRecord::Base | @@ -23,6 +23,7 @@ class Choice < ActiveRecord::Base | ||
23 | named_scope :active, :conditions => { :active => true } | 23 | named_scope :active, :conditions => { :active => true } |
24 | named_scope :inactive, :conditions => { :active => false} | 24 | named_scope :inactive, :conditions => { :active => false} |
25 | named_scope :inactive_ignore_flagged, :include => :flags, :conditions => {:active => false, :flags => {:id => nil}} | 25 | named_scope :inactive_ignore_flagged, :include => :flags, :conditions => {:active => false, :flags => {:id => nil}} |
26 | + named_scope :reproved, :joins => :flags | ||
26 | named_scope :not_created_by, lambda { |creator_id| | 27 | named_scope :not_created_by, lambda { |creator_id| |
27 | { :conditions => ["creator_id <> ?", creator_id] } | 28 | { :conditions => ["creator_id <> ?", creator_id] } |
28 | } | 29 | } |
@@ -40,6 +41,10 @@ class Choice < ActiveRecord::Base | @@ -40,6 +41,10 @@ class Choice < ActiveRecord::Base | ||
40 | end | 41 | end |
41 | end | 42 | end |
42 | 43 | ||
44 | + def reproved | ||
45 | + self.flags.any?{|a| a.choice_id == self.id} | ||
46 | + end | ||
47 | + | ||
43 | # if changing a choice to active, we want to regenerate prompts | 48 | # if changing a choice to active, we want to regenerate prompts |
44 | def update_prompt_queue | 49 | def update_prompt_queue |
45 | unless part_of_batch_create | 50 | unless part_of_batch_create |
@@ -83,7 +88,7 @@ class Choice < ActiveRecord::Base | @@ -83,7 +88,7 @@ class Choice < ActiveRecord::Base | ||
83 | end | 88 | end |
84 | 89 | ||
85 | def creator_identifier | 90 | def creator_identifier |
86 | - self.creator.identifier | 91 | + self.creator.identifier |
87 | end | 92 | end |
88 | 93 | ||
89 | def compute_bt_score(btprobs = nil) | 94 | def compute_bt_score(btprobs = nil) |