2014-10-06 05:55:25 +00:00
|
|
|
class SurveysController < ApplicationController
|
|
|
|
|
2014-10-06 08:49:09 +00:00
|
|
|
helper MemberHelper
|
2014-10-06 05:55:25 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
|
|
|
|
questionnairesurveys = QuestionnaireSurvey.any_of( {:postdate.lte => Time.now} ).desc(:postdate).page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
|
2015-11-30 10:11:58 +00:00
|
|
|
current_user = OrbitHelper.current_user
|
2014-10-06 05:55:25 +00:00
|
|
|
surveys = questionnairesurveys.collect do |a|
|
2015-11-30 10:11:58 +00:00
|
|
|
answer_present = (current_user.nil? ? false : (a.survey_answers.where(:user => current_user.id).count > 0 ? true : false))
|
2014-10-06 05:55:25 +00:00
|
|
|
|
|
|
|
if !OrbitHelper.params[:page_no].blank?
|
|
|
|
title = ('<a href="'+ OrbitHelper.url_to_show(a.to_param) + "?method=show_data&page_no=#{OrbitHelper.params[:page_no]}" + '">' + a.title + '</a>').html_safe
|
|
|
|
else
|
|
|
|
title = ('<a href="'+ OrbitHelper.url_to_show(a.to_param) + '?method=show_data">' + a.title + '</a>').html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
"title" => title,
|
|
|
|
"time_range" => QuestionnaireSurvey.time_range(a),
|
|
|
|
"write" => QuestionnaireSurvey.write(a),
|
2015-11-30 10:11:58 +00:00
|
|
|
"result" => QuestionnaireSurvey.result(a),
|
|
|
|
"answer_present" => answer_present
|
2014-10-06 05:55:25 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
{
|
|
|
|
"surveys" => surveys,
|
|
|
|
"extras" => {
|
|
|
|
"widget-title"=>t('survey.survey'),
|
|
|
|
"th_time_range" => t('survey.time_range'),
|
|
|
|
"th_title" => t("survey.title"),
|
|
|
|
"th_write" => t('survey.write'),
|
|
|
|
"th_result" => t('survey.result')
|
|
|
|
},
|
|
|
|
"total_pages" => questionnairesurveys.total_pages
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_data
|
|
|
|
|
|
|
|
params = OrbitHelper.params
|
|
|
|
|
|
|
|
survey = QuestionnaireSurvey.find_by(uid: params[:uid])
|
2015-11-30 10:11:58 +00:00
|
|
|
current_user = OrbitHelper.current_user
|
|
|
|
answer_present = (current_user.nil? ? false : (survey.survey_answers.where(:user => current_user.id).count > 0 ? true : false))
|
2015-11-30 10:21:58 +00:00
|
|
|
answer_present = (!survey.needs_login ? false : answer_present)
|
2015-11-30 10:11:58 +00:00
|
|
|
link_to_answer_set = (answer_present ? "/admin/surveys/#{survey.survey_answers.where(:user => current_user.id).first.id.to_s}/answer_set" : "")
|
2014-10-06 05:55:25 +00:00
|
|
|
{
|
|
|
|
'survey' => survey,
|
2015-11-30 10:11:58 +00:00
|
|
|
'answer_present' => answer_present,
|
|
|
|
'link_to_answer_set' => link_to_answer_set,
|
2014-10-06 05:55:25 +00:00
|
|
|
'time_range' => QuestionnaireSurvey.time_range(survey)
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
|
|
|
|
params = OrbitHelper.params
|
|
|
|
|
|
|
|
survey = QuestionnaireSurvey.find_by(uid: params[:uid])
|
|
|
|
|
|
|
|
|
|
|
|
if survey.redirect_mode
|
|
|
|
|
|
|
|
redirect_url = survey.redirect_url
|
|
|
|
|
|
|
|
{
|
|
|
|
'redirect_url' => redirect_url
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
questions = survey.survey_questions.all.entries
|
2015-11-30 10:11:58 +00:00
|
|
|
current_user = OrbitHelper.current_user
|
|
|
|
answer_present = (current_user.nil? ? false : (survey.survey_answers.where(:user => current_user.id).count > 0 ? true : false))
|
2015-11-30 10:21:58 +00:00
|
|
|
answer_present = (!survey.needs_login ? false : answer_present)
|
2015-11-30 10:11:58 +00:00
|
|
|
link_to_answer_set = (answer_present ? "/admin/surveys/#{survey.survey_answers.where(:user => current_user.id).first.id.to_s}/answer_set" : "")
|
2014-10-06 05:55:25 +00:00
|
|
|
if survey.jump_mode
|
|
|
|
answers = Hash[questions.collect{ |o| [o.id.to_s, o.id.to_s] }]
|
|
|
|
jumpable_questions = questions.select{ |q| q.jumpable? && q.can_set_jump? }
|
|
|
|
jump_tos_map = jumpable_questions.map(&:get_jump_tos).inject(:merge)
|
|
|
|
end
|
|
|
|
{
|
|
|
|
'survey' => survey,
|
|
|
|
'redirect_url' => '',
|
2015-11-30 10:11:58 +00:00
|
|
|
'link_to_answer_set' => link_to_answer_set,
|
2014-10-06 05:55:25 +00:00
|
|
|
'questions' => questions,
|
|
|
|
'answers' => answers,
|
2015-11-30 10:11:58 +00:00
|
|
|
'answer_present' => answer_present,
|
2014-10-06 05:55:25 +00:00
|
|
|
'jump_tos_map' => jump_tos_map
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
|
|
|
uid = params[:id].split('-').last
|
|
|
|
@survey = QuestionnaireSurvey.find_by(:uid=>uid)
|
|
|
|
|
|
|
|
answer = params[:answer]
|
|
|
|
@answer_model = @survey.survey_answers.new
|
2015-11-27 11:55:47 +00:00
|
|
|
@answer_model.user = current_user.id if !current_user.nil?
|
2014-10-06 05:55:25 +00:00
|
|
|
@survey.survey_questions.each do |question|
|
|
|
|
qid = question.id.to_s
|
|
|
|
if question.is_required && answer[qid].blank? && !@survey.jump_mode #&& (! @survey.jump_mode || ( @survey.jump_mode && question.jumpable? ) )
|
|
|
|
@answer_model.errors.add question.title, t('survey_question.required_error')
|
|
|
|
else
|
|
|
|
case question.type
|
|
|
|
when SurveyQuestion::Radio, SurveyQuestion::Select
|
|
|
|
if question.custom_option && answer[qid] == 'custom_option'
|
|
|
|
@answer_model[qid] = answer[qid + '_custom_option']
|
|
|
|
else
|
|
|
|
if answer[qid]
|
|
|
|
@answer_model[qid] = question.survey_question_options.find(answer[qid]).name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when SurveyQuestion::Check
|
|
|
|
@answer_model[qid] = []
|
|
|
|
if answer[qid]
|
|
|
|
answer[qid].each do |oid, value|
|
|
|
|
if value.to_i != 0
|
|
|
|
if question.custom_option && oid == 'custom_option'
|
|
|
|
@answer_model[qid].push answer[qid + '_custom_option']
|
|
|
|
else
|
|
|
|
@answer_model[qid].push question.survey_question_options.find(oid).name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when SurveyQuestion::Radiogroup
|
|
|
|
@answer_model[qid] = {}
|
|
|
|
options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, o.name ] }]
|
|
|
|
radiogroups = Hash[question.survey_question_radiogroups.collect{|rg| [ rg.id.to_s, rg.name] }]
|
|
|
|
if answer[qid]
|
|
|
|
answer[qid].each do |oid, value|
|
|
|
|
unless value.blank?
|
|
|
|
@answer_model[qid][oid] = radiogroups[value]
|
|
|
|
else
|
|
|
|
if question.is_required
|
|
|
|
@answer_model.errors.add question.title, t('survey_question.required_error')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when SurveyQuestion::Oneline, SurveyQuestion::Multiline
|
|
|
|
@answer_model[qid] = answer[qid]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if @answer_model.errors.empty?
|
|
|
|
@answer_model.save!
|
|
|
|
render :answer_success
|
|
|
|
else
|
|
|
|
@survey_answer_error = @answer_model.errors.full_messages.join(',')
|
|
|
|
render :answer_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def result
|
|
|
|
params = OrbitHelper.params
|
|
|
|
|
|
|
|
survey = QuestionnaireSurvey.find_by(uid: params[:uid])
|
|
|
|
# survey.result_type = QuestionnaireSurvey::ResultChart if params[:force_chart]
|
|
|
|
case survey.result_type
|
|
|
|
when QuestionnaireSurvey::ResultChart
|
|
|
|
chart_data, survey_questions, survey_answers = survey.generate_chart_data
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
'survey' => survey,
|
|
|
|
'chart_data' => chart_data,
|
|
|
|
'survey_questions' => survey_questions,
|
|
|
|
'survey_answers' => survey_answers
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|