-
Notifications
You must be signed in to change notification settings - Fork 19
/
polls_controller.rb
236 lines (206 loc) · 6.58 KB
/
polls_controller.rb
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
class PollsController < ApplicationController
helper :polls
before_filter :login_required
access_control [:new, :edit, :create, :update, :destroy,
:publish, :unpublish ] => 'prodmgr'
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [:create, :publish, :unpublish, :take_survey ],
:redirect_to => { :action => :index }
verify :method => :put, :only => [ :update ],
:redirect_to => { :action => :index }
verify :method => :delete, :only => [ :destroy ],
:redirect_to => { :action => :index }
def index
new
@polls = Poll.list params[:page], prodmgr?, current_user.row_limit, current_user
end
def show
session[:polls_show_toggle_detail] ||= "HIDE"
@poll = Poll.find(params[:id])
puts "============ #{pie_poll_url(@poll)}"
@graph = open_flash_chart_object(450,450, pie_poll_url(@poll))
end
def pie
poll = Poll.find(params[:id])
# total_responses = poll.poll_options.collect(&:user_responses).size
pie_values = []
for option in poll.poll_options
if option.user_responses.size > 0
pie_values << PieValue.new(option.user_responses.size, "#{StringUtils.truncate option.description, 16}")
end
end
pie_chart = Pie.new
pie_chart.start_angle = 35
pie_chart.animate = true
pie_chart.tooltip = '#val# of #total#<br>#percent#'
pie_chart.values = pie_values
pie_chart.colours = %w(#CF2626 #5767AF #D01FC3 #356AA0 #CF5ACD #CF750C #FF7200 #8F1A1A #ADD700 #57AF9D #C3CF5A #456F4F #C79810)
chart = OpenFlashChart.new
chart.bg_colour = '#ffffcc'
chart.title = Title.new("Survey Responses")
chart.add_element(pie_chart)
chart.x_axis = nil
render :text => chart.to_s
end
def new
@poll = Poll.new
@poll.close_date = Date.jd(DateUtils.today_utc.jd + 7)
end
def create
params[:poll][:group_ids] ||= []
params[:poll][:enterprise_type_ids] ||= []
@poll = Poll.new(params[:poll])
@poll.poll_options << PollOption.new(:description => 'Choice 1...')
@poll.poll_options << PollOption.new(:description => 'Choice 2...')
if @poll.save
flash[:notice] = "Poll #{@poll.title} was successfully created."
redirect_to edit_poll_path(@poll)
else
index
render :action => :index
end
end
def edit
@poll = Poll.find(params[:id])
end
def update
params[:poll][:group_ids] ||= []
params[:poll][:enterprise_type_ids] ||= []
@poll = Poll.find(params[:id])
if @poll.update_attributes(params[:poll])
flash[:notice] = "Poll '#{@poll.title}' was successfully updated."
redirect_to poll_path(@poll)
else
render :action => :edit
end
end
def destroy
poll = Poll.find(params[:id])
title = poll.title
poll.destroy
flash[:notice] = "Poll #{title} was successfully deleted."
redirect_to polls_url
end
def publish
poll = set_active params[:id], true
flash[:notice] = "Poll #{poll.title} was successfully published."
redirect_to polls_url
end
def unpublish
poll = set_active params[:id], false
flash[:notice] = "Poll #{poll.title} was successfully unpublished."
redirect_to polls_url
end
def self.POLL_NO_THANKS_ACTION
"No Thanks"
end
def take_survey
if (params[:action] == PollsController.POLL_NO_THANKS_ACTION)
poll = Poll.find(params[:id])
poll.user_responses << poll.unselectable_poll_option
poll.save
redirect_to home_path
else
if params[:poll_option_id].nil?
take_survey_failed "You must select an option"
else
poll_option = PollOption.find(params[:poll_option_id])
if poll_option.poll.taken_survey?(current_user)
take_survey_failed "You can only answer this survey once"
else
Poll.transaction do
poll_option.user_responses << current_user
unless params[:comment_body].blank?
poll_option.poll.comments << PollComment.new(
:user_id => current_user.id,
:body => params[:comment_body])
poll_option.poll.save
end
poll_option.save
end
redirect_to poll_path(poll_option.poll)
end
end
end
end
# def self.POLL_OPTION_ANSWER_SURVEY
# "Answer Survey"
# end
#
# def self.POLL_OPTION_ASK_ME_LATER
# "Not Now"
# end
#
# def self.POLL_OPTION_NEVER
# "Never"
# end
def present_survey
@poll = Poll.find(params[:id])
if @poll.taken_survey?(current_user)
flash[:error] = "You can only answer this survey once"
redirect_to poll_path(@poll)
end
end
def toggle_details
poll = Poll.find(params[:id])
if session[:polls_show_toggle_detail] == "HIDE"
session[:polls_show_toggle_detail] = "SHOW"
else
session[:polls_show_toggle_detail] = "HIDE"
end
respond_to do |format|
format.html {
show
render poll_path(poll)
}
format.js { do_rjs_toggle_details poll }
end
end
def display_comments
poll = Poll.find(params[:id])
respond_to do |format|
format.html {
present_survey
render present_survey_poll_path(poll)
}
format.js { do_rjs_display_comments poll }
end
end
private
def take_survey_failed(msg)
@poll = Poll.find(params[:id])
flash[:error] = msg
render :action => :present_survey
end
def set_active(id, active)
@poll = Poll.find(id)
@poll.active = active
@poll.save
@poll
end
def do_rjs_toggle_details poll
render :update do |page|
if session[:polls_show_toggle_detail] == "HIDE"
page.visual_effect :blind_up, "hide_images", :duration => 0.2
page.visual_effect :blind_down, "show_images", :duration => 1
page.visual_effect :blind_up, "show_comments", :duration => 1
for option in poll.poll_options
page.visual_effect :squish, "details#{option.id}", :duration => 0.5
end
else
page.visual_effect :blind_up, "show_images", :duration => 0.2
page.visual_effect :blind_down, "hide_images", :duration => 1
page.visual_effect :blind_down, "show_comments", :duration => 1
for option in poll.poll_options
page.visual_effect :blind_down, "details#{option.id}", :duration => 1
end
end
end
end
def do_rjs_display_comments poll
render :update do |page|
page.visual_effect :blind_up, "show_comment_button", :duration => 0.2
page.visual_effect :blind_down, "addcomment", :duration => 1
end
end
end