2014-10-06 05:55:25 +00:00
|
|
|
class Admin::SurveysController < OrbitAdminController
|
|
|
|
include Admin::SurveysHelper
|
|
|
|
|
|
|
|
before_action ->(module_app = @app_title) { set_variables module_app }
|
2015-11-27 11:55:47 +00:00
|
|
|
before_action :set_survey, only: [:edit, :destroy, :export, :set_answers, :jump, :duplicate_it, :answer_sets]
|
2014-10-06 05:55:25 +00:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
@app_title = 'survey'
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
@filter_fields = {}
|
|
|
|
@table_fields = [:title, 'survey.postdate', 'survey.deadline', 'survey.results_count', 'survey.update_user']
|
|
|
|
|
|
|
|
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:postdate=>"desc"}
|
|
|
|
|
|
|
|
@surveys = QuestionnaireSurvey.all.order_by(sort)
|
|
|
|
|
|
|
|
@surveys = search_data(@surveys,[:title]).page(params[:page]).per(10)
|
|
|
|
|
|
|
|
if request.xhr?
|
|
|
|
render :partial => "index"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-27 11:55:47 +00:00
|
|
|
def answer_sets
|
|
|
|
@table_fields = ['survey.taken_by', 'survey.taken_date']
|
|
|
|
@survey_answers = @survey.survey_answers.desc(:created_at).page(params[:page]).per(15)
|
|
|
|
end
|
|
|
|
|
|
|
|
def answer_set
|
|
|
|
@survey_answer = SurveyAnswer.find(params["id"]) rescue nil
|
|
|
|
@survey = @survey_answer.questionnaire_survey if !@survey_answer.nil?
|
|
|
|
@user = @survey_answer.user.nil? ? nil : (User.find(@survey_answer.user) rescue nil)
|
|
|
|
end
|
|
|
|
|
2014-10-06 05:55:25 +00:00
|
|
|
def new
|
|
|
|
@survey = QuestionnaireSurvey.new
|
|
|
|
@primary_locale = I18n.locale.to_s
|
|
|
|
@secondary_locale = (I18n.locale == :zh_tw) ? "en" : "zh_tw"
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@survey = QuestionnaireSurvey.new(survey_params)
|
|
|
|
|
|
|
|
@survey.create_user_id = current_user.id
|
|
|
|
@survey.update_user_id = current_user.id
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @survey.save
|
|
|
|
format.html { redirect_to(admin_surveys_path) }
|
|
|
|
format.xml { render :xml => @survey, :status => :created, :location => @survey }
|
|
|
|
else
|
|
|
|
format.html { render :action => "new" }
|
|
|
|
format.xml { render :xml => @survey.errors, :status => :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@primary_locale = I18n.locale.to_s
|
|
|
|
@secondary_locale = (I18n.locale == :zh_tw) ? "en" : "zh_tw"
|
|
|
|
|
|
|
|
@sqs = []
|
|
|
|
@survey.survey_questions.each do |sq|
|
|
|
|
sq['title_translations'] = sq.title_translations
|
|
|
|
sq['description_translations'] = sq.description_translations
|
|
|
|
sq['qid'] = sq.id.to_s
|
|
|
|
temp = []
|
|
|
|
sq.survey_question_options.each do |sqo|
|
|
|
|
sqo['name_translations'] = sqo.name_translations
|
|
|
|
sqo['qid'] = sqo.id.to_s
|
|
|
|
temp << sqo
|
|
|
|
end
|
|
|
|
temp = []
|
|
|
|
sq.survey_question_radiogroups.each do |sqr|
|
|
|
|
sqr['name_translations'] = sqr.name_translations
|
|
|
|
sqr['qid'] = sqr.id.to_s
|
|
|
|
temp << sqr
|
|
|
|
end
|
|
|
|
sq.survey_question_radiogroups = temp
|
|
|
|
@sqs << sq
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
uid = params[:id].split('-').last
|
|
|
|
@survey = QuestionnaireSurvey.find_by(:uid=>uid)
|
|
|
|
@survey.update_user_id = current_user.id
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @survey.update_attributes(survey_params)
|
|
|
|
format.html { redirect_to(admin_surveys_url(:page => params[:page])) }
|
|
|
|
format.xml { head :ok }
|
|
|
|
else
|
|
|
|
format.html { render :action => "edit" }
|
|
|
|
format.xml { render :xml => @survey.errors, :status => :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@survey.destroy
|
|
|
|
redirect_to "/admin/surveys"
|
|
|
|
|
|
|
|
# @survey = QuestionnaireSurvey.find(params[:id])
|
|
|
|
|
|
|
|
# @survey.destroy
|
|
|
|
|
|
|
|
# respond_to do |format|
|
|
|
|
|
|
|
|
# format.html { redirect_to(panel_survey_back_end_surveys_url) }
|
|
|
|
# format.js
|
|
|
|
# end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
|
|
|
if params[:ids]
|
|
|
|
surveys = QuestionnaireSurvey.any_in(:_id => params[:ids]).destroy_all
|
|
|
|
end
|
|
|
|
redirect_to "/admin/surveys"
|
|
|
|
# redirect_to panel_survey_back_end_surveys_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
|
|
|
|
end
|
|
|
|
|
|
|
|
def export
|
|
|
|
|
|
|
|
@chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
|
|
|
|
respond_to do |format|
|
|
|
|
format.xlsx {
|
|
|
|
response.headers['Content-Disposition'] = 'attachment; filename="export.xlsx"'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_answers
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def jump
|
|
|
|
|
|
|
|
@questions = @survey.survey_questions.all
|
|
|
|
|
|
|
|
@jump_to_options = [[ t('survey.not_jump'), 0 ], [t('survey.jump_to_end'), 1]]
|
|
|
|
@jump_to_options += @questions.collect {|q| [ q.title, q.id.to_s ] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def duplicate_it
|
|
|
|
|
|
|
|
@new_survey = QuestionnaireSurvey.new
|
|
|
|
|
|
|
|
@survey.attributes.each do |key, value|
|
|
|
|
unless ['_id', 'created_at', 'updated_at', 'update_user_id'].include? key
|
|
|
|
if @survey.respond_to?(key + '_translations')
|
|
|
|
@new_survey.send(key + '_translations=', value)
|
|
|
|
else
|
|
|
|
@new_survey.write_attribute(key, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@survey.survey_questions.all.each do |question|
|
|
|
|
new_question = @new_survey.survey_questions.new
|
|
|
|
question.attributes.each do |key, value|
|
|
|
|
unless ['_id', 'survey_id'].include? key
|
|
|
|
if question.respond_to?(key + '_translations')
|
|
|
|
new_question.send(key + '_translations=', value)
|
|
|
|
else
|
|
|
|
new_question.write_attribute(key, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
question.survey_question_options.all.each do |option|
|
|
|
|
new_option = new_question.survey_question_options.new
|
|
|
|
option.attributes.each do |key, value|
|
|
|
|
unless ['_id', 'survey_question_id'].include? key
|
|
|
|
if option.respond_to?(key + '_translations')
|
|
|
|
new_option.send(key + '_translations=', value)
|
|
|
|
else
|
|
|
|
new_option.write_attribute(key, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
question.survey_question_radiogroups.all.each do |radiogroup|
|
|
|
|
new_radiogroup = new_question.survey_question_radiogroups.new
|
|
|
|
radiogroup.attributes.each do |key, value|
|
|
|
|
unless ['_id', 'survey_question_id'].include? key
|
|
|
|
if radiogroup.respond_to?(key + '_translations')
|
|
|
|
new_radiogroup.send(key + '_translations=', value)
|
|
|
|
else
|
|
|
|
new_radiogroup.write_attribute(key, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@new_survey.create_user_id = current_user.id
|
|
|
|
@new_survey.update_user_id = current_user.id
|
|
|
|
@new_survey.uid = nil
|
|
|
|
|
|
|
|
@new_survey.save!
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to(admin_surveys_url) }
|
|
|
|
format.xml { render :xml => @survey, :status => :created, :location => @new_survey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_survey
|
|
|
|
@survey = QuestionnaireSurvey.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def survey_params
|
|
|
|
params.require(:questionnaire_survey).permit!
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|