Orbit/vendor/built_in_modules/survey/app/models/survey.rb

139 lines
4.0 KiB
Ruby
Raw Permalink Normal View History

2013-04-29 08:06:45 +00:00
# encoding: utf-8
class Survey
include Mongoid::Document
include Mongoid::Timestamps
ResultChart = 0
ResultExtern = 1
ResultFile = 2
2013-04-29 08:06:45 +00:00
scope :available_for_lang, ->(locale){ where("available_for_#{locale}".to_sym => true) }
scope :can_display, where(is_hidden: false)
field :title, :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 :result_type, :type => Integer, :default => 0
field :extern_link
mount_uploader :upload_file, AssetUploader
field :jump_mode, :type => Boolean, :default => false
field :redirect_mode, :type => Boolean, :default => false
field :redirect_url, :type => String
2013-04-29 08:06:45 +00:00
validates :title, :at_least_one => true
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 time_range
r = "#{self.postdate.to_date}"
2013-05-25 02:05:19 +00:00
r += "- #{self.deadline.to_date}" if self.deadline
2013-04-29 08:06:45 +00:00
r
end
def result
2013-05-25 02:05:19 +00:00
if ( self.result_type == Survey::ResultChart && self.deadline && Time.now > self.deadline ) ||
2013-05-24 14:37:40 +00:00
( self.result_type == Survey::ResultExtern && !self.extern_link.blank? ) ||
( self.result_type == Survey::ResultFile && self.upload_file? )
2013-05-25 02:19:58 +00:00
('<a target="_blank" href="'+Rails.application.routes.url_helpers.result_panel_survey_front_end_survey_path(self, :standalone => true) + '"><img src="/assets/result.png" /></a>').html_safe
2013-05-24 14:37:40 +00:00
else
''
end
end
def write
2013-05-25 02:19:58 +00:00
unless self.deadline && Time.now > self.deadline
('<a target="_blank" href="' + Rails.application.routes.url_helpers.panel_survey_front_end_survey_path(self, :standalone => true) + '"><img src="/assets/write.png" /></a>').html_safe
2013-05-24 14:37:40 +00:00
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
2013-05-11 05:52:27 +00:00
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
2013-04-29 08:06:45 +00:00
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
end