osem-fcy/app/controllers/surveys_controller.rb
Stella Rouzi 683f62a27c Add more options for surveys
* Survey period constraints
* Add abilities
* Enforce required questions
* Update survey_submssion updated_at
* Do a full match of possible answer and user reply
2018-07-03 08:46:15 +03:00

45 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class SurveysController < ApplicationController
load_resource :conference, find_by: :short_title
load_and_authorize_resource except: :reply
load_resource only: :reply
skip_authorization_check only: :reply
def index
@surveys = @conference.surveys.select(&:active?)
end
def show
@survey_submission = @survey.survey_submissions.new
end
def reply
unless can? :reply, @survey
redirect_to conference_survey_path(@conference, @survey), alert: 'This survey is currently closed'
return
end
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
reply.update_attributes(text: reply_text) unless reply.text == reply_text
else
survey_question.survey_replies.create!(text: reply_text, user: current_user)
end
user_survey_submission = @survey.survey_submissions.find_by(user: current_user)
if user_survey_submission
user_survey_submission.update_attributes(updated_at: Time.current)
else
@survey.survey_submissions.create!(user: current_user)
end
end
redirect_to :back
end
end