questions_controller.rb
7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'fastercsv'
class QuestionsController < InheritedResources::Base
before_filter :authenticate
respond_to :xml, :json
respond_to :csv, :only => :export #leave the option for xml export here
belongs_to :site, :optional => true
def recent_votes_by_question_id
creator_id = params[:creator_id]
date = params[:date]
if creator_id
questions = Question.find(:all, :select => :id, :conditions => { :local_identifier => creator_id})
questions_list = questions.map {|record | record.quoted_id}
question_votes_hash = Vote.with_question(questions_list).recent.count(:group => :question_id)
elsif date #only for admins
question_votes_hash = Vote.recent(date).count(:group => :question_id)
else
question_votes_hash = Vote.recent.count(:group => :question_id)
end
respond_to do |format|
format.xml{ render :xml => question_votes_hash.to_xml and return}
end
end
def show
@question = Question.find(params[:id])
unless params[:barebones]
@p = @question.picked_prompt
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) }
show! do |format|
session['prompts_ids'] ||= []
format.xml {
render :xml => @question.to_xml(:methods => [:item_count], :procs => [left_choice_text, right_choice_text, picked_prompt_id])
}
end
else
show! do |format|
session['prompts_ids'] ||= []
format.xml {
render :xml => @question.to_xml(:methods => [:item_count, :votes_count])
}
end
end
end
def create
logger.info "all params are #{params.inspect}"
logger.info "vi is #{params['question']['visitor_identifier']} and local are #{params['question']['local_identifier']}."
if @question = current_user.create_question(params['question']['visitor_identifier'], :name => params['question']['name'], :local_identifier => params['question']['local_identifier'], :ideas => (params['question']['ideas'].lines.to_a.delete_if {|i| i.blank?}))
respond_to do |format|
format.xml { render :xml => @question.to_xml}
end
else
respond_to do |format|
format.xml { render :xml => @question.errors.to_xml}
end
end
end
def set_autoactivate_ideas_from_abroad
expire_page :action => :index
logger.info("INSIDE autoactivate ideas")
@question = current_user.questions.find(params[:id])
@question.it_should_autoactivate_ideas = params[:question][:it_should_autoactivate_ideas]
respond_to do |format|
if @question.save
logger.info "successfully set this question to autoactive ideas #{@question.inspect}"
format.xml { render :xml => true }
format.json { render :json => true}
else
logger.info "Some error in saving question, #{@question.inspect}"
format.xml { render(:xml => false) and return}
format.json { render :json => false }
end
end
end
def export
type = params[:type]
if type == 'votes'
export_votes
elsif type == 'items'
export_items
else
render :text => "Error! Specify a type of export"
end
# export_type = params[:export_type]
# export_format = params[:export_format] #CSV always now, could expand to xml later
end
def num_votes_by_visitor_id
@question = current_user.questions.find(params[:id])
hash = Vote.count(:conditions => "question_id = #{@question.id}", :group => "voter_id")
visitor_id_hash = {}
hash.each do |visitor_id, num_votes|
visitor = Visitor.find(visitor_id)
visitor_id_hash[visitor.identifier] = num_votes
end
respond_to do |format|
format.xml{ render :xml => visitor_id_hash.to_xml and return}
end
end
def object_info_totals_by_date
authenticate
object_type = params[:object_type]
@question = current_user.questions.find(params[:id])
if object_type == 'votes'
# eventually allow for users to specify type of export through params[:type]
hash = Vote.count(:conditions => "question_id = #{@question.id}", :group => "date(created_at)")
elsif object_type == 'user_submitted_ideas'
hash = Choice.count(:include => 'item',
:conditions => "choices.question_id = #{@question.id} AND items.creator_id <> #{@question.creator_id}",
:group => "date(choices.created_at)")
elsif object_type == 'user_sessions'
# little more work to do here:
result = Vote.find(:all, :select => 'date(created_at) as date, voter_id, count(*) as vote_count',
:conditions => "question_id = #{@question.id}", :group => 'date(created_at), voter_id')
hash = Hash.new(0)
result.each do |r|
hash[r.date]+=1
end
end
respond_to do |format|
format.xml { render :xml => hash.to_xml and return}
end
end
protected
def export_votes
@question = Question.find(params[:id])
outfile = "ideamarketplace_#{@question.id}_votes" + Time.now.strftime("%m-%d-%Y") + ".csv"
headers = ['Vote ID', 'Session ID', 'Question ID','Winner ID', 'Winner Text', 'Loser ID', 'Loser Text',
'Prompt ID', 'Left Choice ID', 'Right Choice ID', 'Created at', 'Updated at']
@votes = @question.votes
csv_data = FasterCSV.generate do |csv|
csv << headers
@votes.each do |v|
prompt = v.prompt
# these may not exist
loser_data = v.loser_choice.nil? ? "" : "'#{v.loser_choice.data.strip}'"
left_id = v.prompt.nil? ? "" : v.prompt.left_choice_id
right_id = v.prompt.nil? ? "" : v.prompt.right_choice_id
csv << [ v.id, v.voter_id, v.question_id, v.choice_id, "\'#{v.choice.data.strip}'", v.loser_choice_id, loser_data,
v.prompt_id, left_id, right_id, v.created_at, v.updated_at]
end
end
send_data(csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{outfile}")
end
def export_items
@question = Question.find(params[:id], :include => [:choices, :prompts])
outfile = "ideamarketplace_#{@question.id}_ideas_" + Time.now.strftime("%m-%d-%Y") + ".csv"
headers = ['Ideamarketplace ID','Idea ID', 'Idea', 'Wins', 'Losses', 'Score','User Submitted', 'Idea Creator ID',
'Created at', 'Last Activity', 'Active', 'Local Identifier',
'Prompts on Left', 'Prompts on Right', 'Prompts Count']
csv_data = FasterCSV.generate do |csv|
csv << headers
#ensure capital format for true and false
@question.choices.each do |c|
user_submitted = (c.item.creator != @question.creator) ? "TRUE" : "FALSE"
csv << [c.question_id, c.id, "'#{c.data.strip}'", c.wins, c.losses, c.score, user_submitted , c.item.creator_id,
c.created_at, c.updated_at, c.active, c.local_identifier,
c.prompts_on_the_left(true).size, c.prompts_on_the_right(true).size, c.prompts_count]
end
end
send_data(csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{outfile}")
end
end
class String
unless defined? "".lines
alias lines to_a
#Ruby version compatibility
end
end