survey/app/models/questionnaire_survey.rb

164 lines
4.6 KiB
Ruby

# encoding: utf-8
class QuestionnaireSurvey
include Mongoid::Document
include Mongoid::Timestamps
include Slug
ResultChart = 0
ResultExtern = 1
ResultFile = 2
ResultCriteria = 3
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
accepts_nested_attributes_for :survey_questions, :allow_destroy => true
before_save :check_deadline#, :update_avliable_language
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? )
('<a target="_blank" href="'+ OrbitHelper.url_to_show(data.to_param) + '?method=result&force_chart=true' + '"><span class="result"><i class="icon-align-left"></i></span></a>').html_safe
else
''
end
end
def self.write(data)
unless data.deadline && Time.now > data.deadline
('<a target="_blank" href="'+ OrbitHelper.url_to_show(data.to_param) + '"><span class="write"><i class="icon-edit"></i></span></a>').html_safe
else
''
end
end
def generate_chart_data
survey_questions = self.survey_questions.all
survey_answers = self.survey_answers.all
chart_data = {}
survey_questions.each do |question|
qid = question.id.to_s
options = question.survey_question_options.map(&:name)
case question.type
when SurveyQuestion::Radio, SurveyQuestion::Select
chart_data[qid] = {}
answers = survey_answers.each do |answer|
if answer[qid]
this_answer = answer[qid]
unless options.include? this_answer
this_answer = I18n.t('survey_question.use_custom_option')
end
chart_data[qid][this_answer] ||= 0
chart_data[qid][this_answer] += 1
end
end
when SurveyQuestion::Check
chart_data[qid] = {}
answers = survey_answers.each do |answer|
if answer[qid]
answer[qid].each do |option|
this_answer = option
unless options.include? this_answer
this_answer = I18n.t('survey_question.use_custom_option')
end
chart_data[qid][this_answer] ||= 0
chart_data[qid][this_answer] += 1
end
end
end
when SurveyQuestion::Radiogroup
chart_data[qid] = {}
answers = survey_answers.each do |answer|
if answer[qid]
answer[qid].each do |option, group|
chart_data[qid][option] ||= {}
chart_data[qid][option][group] ||= 0
chart_data[qid][option][group] += 1
end
end
end
end
end
[chart_data, survey_questions, survey_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