# encoding: utf-8
class QuestionnaireSurvey
include Mongoid::Document
include Mongoid::Timestamps
include Slug
ResultChart = 0
ResultExtern = 1
ResultFile = 2
ResultCriteria = 3
include OrbitCategory::Categorizable
scope :can_display, ->{where(is_hidden: false)}
field :title, as: :slug_title, type: String, localize: true
field :description, :localize => true
field :create_user_id
field :update_user_id
field :postdate, :type => DateTime
field :deadline, :type => DateTime
field :is_hidden, :type => Boolean, :default => false
field :needs_login, :type => Boolean, :default => false
field :total_points, type: Integer, :default => 0
field :result_type, :type => Integer, :default => 0
field :extern_link
mount_uploader :upload_file, AssetUploader
field :result_criteria, type: Array, :default => []
field :jump_mode, :type => Boolean, :default => false
field :redirect_mode, :type => Boolean, :default => false
field :redirect_url, :type => String
# validates :title, :at_least_one => true
# validates :title, :presence => { :message => I18n.t("survey.title") }
has_many :survey_questions, :autosave => true, :dependent => :destroy
has_many :survey_answers, :dependent => :destroy
has_many :survey_sections, :dependent => :destroy
accepts_nested_attributes_for :survey_questions, :allow_destroy => true
before_save :check_deadline#, :update_avliable_language
has_many :survey_paginations, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :survey_paginations, :allow_destroy => true
def update_user
User.find(update_user_id) rescue nil
end
def update_user=(user)
self.update_user_id = user.id
end
def self.time_range(data)
r = "#{data.postdate.to_date}"
if data.deadline
r += " - #{data.deadline.to_date}"
else
r += " - #{I18n.t(:no_deadline)}"
end
r
end
def self.result(data)
if ( data.result_type == QuestionnaireSurvey::ResultChart && data.deadline && Time.now > data.deadline ) ||
( data.result_type == QuestionnaireSurvey::ResultExtern && !data.extern_link.blank? ) ||
( data.result_type == QuestionnaireSurvey::ResultFile && data.upload_file? )
('').html_safe
else
''
end
end
def self.write(data)
unless data.deadline && Time.now > data.deadline
('').html_safe
else
''
end
end
def generate_chart_data
survey_questions = self.survey_questions.all
survey_answers = self.survey_answers.all.order_by(created_at: :desc)
chart_data = {}
answers =(0...survey_answers.count).map{{}}
SurveysHelper.set_locale(I18n.locale)
survey_questions.each do |question|
qid = question.id.to_s
use_custom_option = question.custom_option_new_option
chart_data[qid] = {}
case question.type
when SurveyQuestion::Radio, SurveyQuestion::Select
options = question.survey_question_options.map{|v| [v.name,v.id.to_s]}
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_value(answer[qid],
options: options,
chart_data: chart_data[qid],
use_custom_option: use_custom_option)
end
when SurveyQuestion::Check
options = question.survey_question_options.map{|v| [v.name,v.id.to_s]}
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_checkbox_value(answer[qid],
options: options,
chart_data: chart_data[qid],
use_custom_option: use_custom_option)
end
when SurveyQuestion::Radiogroup
options = question.survey_question_options.map{|v| [v.name,v.id.to_s]}
radiogroups = question.survey_question_radiogroups.map{|v| [v.name,v.id.to_s]}
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_rg_value(answer[qid],
options: options,
radiogroups: radiogroups,
chart_data: chart_data[qid],
use_custom_option: use_custom_option)
end
when SurveyQuestion::DoubleLevelOption
custom_option_title = question.custom_option_title.to_s
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_double_level_value(answer[qid],
other_title: custom_option_title,
use_custom_option: use_custom_option,
chart_data: chart_data[qid])
end
else
survey_answers.each_with_index do |answer,i|
answers[i][qid] = answer[qid]
end
end
end
[chart_data, survey_questions, answers]
end
def expired?
(self.deadline < Time.now) rescue false
end
protected
def check_deadline
if(!self.deadline.nil? and (self.deadline < self.postdate ))
self.deadline = nil
end
end
# def update_avliable_language
# VALID_LOCALES.each do |locale|
# if (title_translations[locale].blank? rescue true)
# self["available_for_#{locale}".to_sym] = false
# else
# self["available_for_#{locale}".to_sym] = true
# end
# end
# end
# paginates_per 10
end