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
|
2021-07-08 13:31:27 +00:00
|
|
|
@categories = current_user.approved_categories.select{|c| c.module_app_id == @module_app.id} rescue []
|
|
|
|
@filter_fields = filter_fields(@categories,[])
|
2014-10-06 05:55:25 +00:00
|
|
|
@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"}
|
|
|
|
|
2021-07-08 13:31:27 +00:00
|
|
|
@surveys = QuestionnaireSurvey.all.order_by(sort).with_categories(filters("category"))
|
2014-10-06 05:55:25 +00:00
|
|
|
|
|
|
|
@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
|
2015-11-30 10:11:58 +00:00
|
|
|
if can_edit_or_delete?(@survey)
|
|
|
|
@table_fields = ['survey.taken_by', 'survey.taken_date']
|
|
|
|
@survey_answers = @survey.survey_answers.desc(:created_at).page(params[:page]).per(15)
|
|
|
|
else
|
|
|
|
render_401
|
|
|
|
end
|
2015-11-27 11:55:47 +00:00
|
|
|
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)
|
2016-08-18 13:18:02 +00:00
|
|
|
total = 0
|
|
|
|
@survey.survey_questions.each do |ques|
|
|
|
|
ques.survey_question_options.each do |opt|
|
|
|
|
p = (opt.points == nil ? 0 : opt.points) rescue 0
|
|
|
|
total = total + p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@survey.total_points = total
|
2014-10-06 05:55:25 +00:00
|
|
|
@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"
|
|
|
|
|
2021-07-08 13:31:27 +00:00
|
|
|
@sqs = @survey.survey_questions.as_json
|
2014-10-06 05:55:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
uid = params[:id].split('-').last
|
|
|
|
@survey = QuestionnaireSurvey.find_by(:uid=>uid)
|
|
|
|
@survey.update_user_id = current_user.id
|
2016-08-18 13:18:02 +00:00
|
|
|
total = 0
|
|
|
|
@survey.survey_questions.each do |ques|
|
|
|
|
ques.survey_question_options.each do |opt|
|
|
|
|
p = (opt.points == nil ? 0 : opt.points) rescue 0
|
|
|
|
total = total + p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@survey.total_points = total
|
2014-10-06 05:55:25 +00:00
|
|
|
respond_to do |format|
|
|
|
|
if @survey.update_attributes(survey_params)
|
2016-08-18 13:18:02 +00:00
|
|
|
if params[:et] == "result"
|
|
|
|
format.html { redirect_to(set_answers_admin_survey_path(@survey.id)) }
|
|
|
|
else
|
|
|
|
format.html { redirect_to(admin_surveys_url(:page => params[:page])) }
|
|
|
|
end
|
2014-10-06 05:55:25 +00:00
|
|
|
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
|
|
|
|
|
2017-04-11 06:10:01 +00:00
|
|
|
def checkforthread
|
2017-04-11 10:43:22 +00:00
|
|
|
running = !File.exists?("public/uploads/survey_export/#{params[:survey_id]}/#{params[:survey_title]}.xlsx")
|
|
|
|
render :json => {"status" => running}.to_json
|
2017-04-11 06:10:01 +00:00
|
|
|
end
|
2014-10-06 05:55:25 +00:00
|
|
|
|
2017-04-11 06:10:01 +00:00
|
|
|
def export
|
2017-04-11 10:43:22 +00:00
|
|
|
I18n.locale = :zh_tw
|
|
|
|
title = @survey.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')
|
|
|
|
f = "public/uploads/survey_export/#{@survey.id}/#{title}.xlsx"
|
|
|
|
File.delete(f) if File.exists?(f)
|
2021-07-08 13:31:27 +00:00
|
|
|
I18n.locale = locale
|
2017-04-11 10:43:22 +00:00
|
|
|
Thread.new do
|
2021-07-08 13:31:27 +00:00
|
|
|
begin
|
|
|
|
SurveysHelper.generate_xlsx(@survey.id,locale)
|
|
|
|
rescue => e
|
|
|
|
puts [e,e.backtrace]
|
|
|
|
end
|
|
|
|
#system "rake survey_tasks:prepare_download[#{@survey.id}] >> #{Rails.root}/log/rake.log &"
|
2014-10-06 05:55:25 +00:00
|
|
|
end
|
2017-04-11 10:43:22 +00:00
|
|
|
|
|
|
|
render :json => {"success" => true, "title" => title}.to_json
|
2017-04-11 06:10:01 +00:00
|
|
|
# @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
|
2014-10-06 05:55:25 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_answers
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-01-03 08:36:00 +00:00
|
|
|
def set_sections
|
|
|
|
@sections = QuestionnaireSurvey.find(params[:id]).survey_sections
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_section
|
|
|
|
@section = SurveySection.new
|
|
|
|
@survey = QuestionnaireSurvey.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2021-07-08 13:31:27 +00:00
|
|
|
def pagination_setting
|
|
|
|
@survey = QuestionnaireSurvey.find(params[:id])
|
|
|
|
@min_section = 1
|
|
|
|
@survey_sections_len = @survey.survey_sections.count
|
|
|
|
@max_section = @survey_sections_len
|
|
|
|
if @max_section<@min_section
|
|
|
|
@max_section = @min_section
|
|
|
|
end
|
|
|
|
@pagination_settings = @survey.survey_paginations
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_pagination_setting
|
|
|
|
@survey = QuestionnaireSurvey.find(params[:id])
|
|
|
|
survey_params = params.require(:survey).permit!
|
|
|
|
@survey.update_attributes(survey_params)
|
|
|
|
redirect_to set_sections_admin_survey_path(params[:id])
|
|
|
|
end
|
|
|
|
|
2018-01-03 08:36:00 +00:00
|
|
|
def edit_section
|
|
|
|
@section = SurveySection.find(params[:id])
|
|
|
|
@survey = @section.questionnaire_survey
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_section
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_section
|
|
|
|
section = SurveySection.create(survey_section_params)
|
|
|
|
redirect_to set_sections_admin_survey_path(section.questionnaire_survey.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_section
|
|
|
|
section = SurveySection.find(params[:id])
|
|
|
|
section.update_attributes(survey_section_params)
|
|
|
|
redirect_to set_sections_admin_survey_path(section.questionnaire_survey.id)
|
|
|
|
end
|
|
|
|
|
2014-10-06 05:55:25 +00:00
|
|
|
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
|
|
|
|
|
2021-08-31 11:00:01 +00:00
|
|
|
@new_survey = QuestionnaireSurvey.new(:copy_id=>@survey.id,:except_clone_relations=>["survey_answers"])
|
2014-10-06 05:55:25 +00:00
|
|
|
@new_survey.create_user_id = current_user.id
|
|
|
|
@new_survey.update_user_id = current_user.id
|
|
|
|
@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
|
2016-08-18 13:18:02 +00:00
|
|
|
p = params.require(:questionnaire_survey).permit!
|
|
|
|
if !p[:result_criteria].blank?
|
|
|
|
x = p[:result_criteria].collect{|y| y.last}
|
|
|
|
p[:result_criteria] = x
|
|
|
|
end
|
|
|
|
p
|
2014-10-06 05:55:25 +00:00
|
|
|
end
|
2018-01-03 08:36:00 +00:00
|
|
|
|
|
|
|
def survey_section_params
|
|
|
|
params.require(:survey_section).permit!
|
|
|
|
end
|
2014-10-06 05:55:25 +00:00
|
|
|
|
|
|
|
end
|