2018-02-09 19:07:10 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-06-28 18:16:06 +03:00
|
|
|
class SurveysController < ApplicationController
|
|
|
|
|
load_resource :conference, find_by: :short_title
|
2018-02-09 19:07:10 +02:00
|
|
|
load_and_authorize_resource except: :reply
|
|
|
|
|
load_resource only: :reply
|
|
|
|
|
skip_authorization_check only: :reply
|
2016-06-28 18:16:06 +03:00
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
@surveys = @conference.surveys.select(&:active?)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
@survey_submission = @survey.survey_submissions.new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def reply
|
2018-02-09 19:07:10 +02:00
|
|
|
unless can? :reply, @survey
|
|
|
|
|
redirect_to conference_survey_path(@conference, @survey), alert: 'This survey is currently closed'
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2016-06-28 18:16:06 +03:00
|
|
|
survey_submission = params[:survey_submission]
|
|
|
|
|
|
|
|
|
|
@survey.survey_questions.each do |survey_question|
|
|
|
|
|
reply = survey_question.survey_replies.find_by(user: current_user)
|
|
|
|
|
reply_text = survey_submission[survey_question.id.to_s].reject(&:blank?).join(',')
|
|
|
|
|
|
|
|
|
|
if reply
|
2022-02-14 17:53:58 +01:00
|
|
|
reply.update(text: reply_text) unless reply.text == reply_text
|
2016-06-28 18:16:06 +03:00
|
|
|
else
|
|
|
|
|
survey_question.survey_replies.create!(text: reply_text, user: current_user)
|
|
|
|
|
end
|
2018-02-09 19:07:10 +02:00
|
|
|
|
|
|
|
|
user_survey_submission = @survey.survey_submissions.find_by(user: current_user)
|
|
|
|
|
if user_survey_submission
|
2022-02-14 17:53:58 +01:00
|
|
|
user_survey_submission.update_attribute(:updated_at, Time.current)
|
2018-02-09 19:07:10 +02:00
|
|
|
else
|
|
|
|
|
@survey.survey_submissions.create!(user: current_user)
|
|
|
|
|
end
|
2016-06-28 18:16:06 +03:00
|
|
|
end
|
|
|
|
|
|
2019-05-04 20:58:18 +02:00
|
|
|
redirect_back(fallback_location: root_path)
|
2016-06-28 18:16:06 +03:00
|
|
|
end
|
|
|
|
|
end
|