2017-11-05 22:16:32 +01:00
|
|
|
class PollsController < ApplicationController
|
2020-08-12 13:38:18 +02:00
|
|
|
before_action -> { require_plugin_enabled FoodsoftPolls }
|
2017-11-05 22:16:32 +01:00
|
|
|
|
|
|
|
def index
|
|
|
|
@polls = Poll.page(params[:page]).per(@per_page).order(created_at: :desc)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@poll = Poll.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@poll = Poll.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@poll = Poll.new(poll_params)
|
|
|
|
@poll.created_by = current_user
|
|
|
|
|
|
|
|
if @poll.save
|
|
|
|
redirect_to @poll, notice: t('.notice')
|
|
|
|
else
|
|
|
|
render action: 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@poll = Poll.find(params[:id])
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless user_has_no_right
|
|
|
|
|
|
|
|
redirect_to polls_path, alert: t('.no_right')
|
2017-11-05 22:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@poll = Poll.find(params[:id])
|
|
|
|
|
|
|
|
if user_has_no_right
|
|
|
|
redirect_to polls_path, alert: t('.no_right')
|
2022-10-13 18:25:52 +02:00
|
|
|
elsif @poll.update(poll_params)
|
2017-11-05 22:16:32 +01:00
|
|
|
redirect_to @poll, notice: t('.notice')
|
|
|
|
else
|
|
|
|
render action: 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@poll = Poll.find(params[:id])
|
|
|
|
|
|
|
|
if user_has_no_right
|
|
|
|
redirect_to polls_path, alert: t('.no_right')
|
|
|
|
else
|
|
|
|
@poll.destroy
|
|
|
|
redirect_to polls_path, notice: t('.notice')
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
redirect_to polls_path, alert: t('.error', error: e.message)
|
2017-11-05 22:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def vote
|
|
|
|
@poll = Poll.find(params[:id])
|
|
|
|
|
|
|
|
if @poll.one_vote_per_ordergroup
|
|
|
|
ordergroup = current_user.ordergroup
|
|
|
|
return redirect_to polls_path, alert: t('.no_ordergroup') unless ordergroup
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2017-11-05 22:16:32 +01:00
|
|
|
attributes = { ordergroup: ordergroup }
|
|
|
|
else
|
|
|
|
attributes = { user: current_user }
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to @poll, alert: t('.no_right') unless @poll.user_can_vote?(current_user)
|
|
|
|
|
|
|
|
@poll_vote = @poll.poll_votes.where(attributes).first_or_initialize
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless request.post?
|
2017-11-05 22:16:32 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
@poll_vote.update!(note: params[:note], user: current_user)
|
2017-11-05 22:16:32 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
if @poll.single_select?
|
|
|
|
choices = {}
|
|
|
|
choice = params[:choice]
|
|
|
|
choices[choice] = '1' if choice
|
|
|
|
else
|
|
|
|
choices = params[:choices].try(:to_h) || {}
|
|
|
|
end
|
2017-11-05 22:16:32 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
@poll_vote.poll_choices = choices.map do |choice, value|
|
|
|
|
poll_choice = @poll_vote.poll_choices.where(choice: choice).first_or_initialize
|
|
|
|
poll_choice.update!(value: value)
|
|
|
|
poll_choice
|
2017-11-05 22:16:32 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
|
|
|
|
redirect_to @poll
|
2017-11-05 22:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def user_has_no_right
|
|
|
|
@poll.created_by != current_user && !current_user.role_admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
def poll_params
|
|
|
|
params
|
|
|
|
.require(:poll)
|
|
|
|
.permit(:name, :starts_date_value, :starts_time_value, :ends_date_value,
|
2021-03-01 15:27:26 +01:00
|
|
|
:ends_time_value, :description, :one_vote_per_ordergroup, :voting_method,
|
|
|
|
:multi_select_count, :min_points, :max_points, choices: [],
|
|
|
|
required_ordergroup_custom_fields: [], required_user_custom_fields: [])
|
2017-11-05 22:16:32 +01:00
|
|
|
end
|
|
|
|
end
|