survey/app/models/survey_question.rb

118 lines
3.2 KiB
Ruby
Raw Permalink Normal View History

2014-10-06 05:55:25 +00:00
class SurveyQuestion
Oneline = 0
Multiline = 1
Radio = 2
Check = 3
Select = 4
Radiogroup = 5
2017-03-03 13:00:52 +00:00
DateTime = 6
2021-07-08 13:31:27 +00:00
DoubleLevelOption = 7
2014-10-06 05:55:25 +00:00
include Mongoid::Document
2021-08-31 11:00:01 +00:00
include Mongoid::Attributes::Dynamic
2021-10-13 09:15:47 +00:00
field :need_update_score, :type => Boolean, :default => false
field :weight, :type => Integer, :default => 1
2014-10-06 05:55:25 +00:00
field :title, :localize => true
field :description, :localize => true
field :is_required, :type => Boolean
field :type, :type => Integer
2021-07-08 13:31:27 +00:00
field :selectable_question, :type => Boolean, :default => false
field :selectable_question_type, :type => Integer, :default => 0
2017-03-03 13:00:52 +00:00
field :datetime_type
2014-10-06 05:55:25 +00:00
# allow custom answer option
field :custom_option, :type => Boolean
2021-07-08 13:31:27 +00:00
field :custom_option_type, :type => Array, :default => [1]
field :custom_option_title, :type => String, :localize => true
field :option_layout_type, :type => Integer, :default => 0
2014-10-06 05:55:25 +00:00
field :sequence, :type => Integer, :default => 0
2021-07-08 13:31:27 +00:00
field :question_type, :type => Integer, :default => 0
2014-10-06 05:55:25 +00:00
belongs_to :questionnaire_survey
embeds_many :survey_question_options
embeds_many :survey_question_radiogroups
accepts_nested_attributes_for :survey_question_options, :allow_destroy => true
accepts_nested_attributes_for :survey_question_radiogroups, :allow_destroy => true
# default_scope asc(:sequence)
default_scope ->{ asc(:sequence) }
2021-07-08 13:31:27 +00:00
before_save do
self.custom_option_type = self.custom_option_type.map(&:to_i)
2021-10-13 09:15:47 +00:00
if self.weight_changed? || (self.survey_question_options.select{|opt| opt.points_changed? || (opt.level2.select{|sub_opt| sub_opt.points_changed?}.count != 0)}.count != 0)
self.need_update_score = true
end
2021-07-08 13:31:27 +00:00
end
2014-10-06 05:55:25 +00:00
def jumpable?
case type
when SurveyQuestion::Radio, SurveyQuestion::Select
true
else
false
end
end
def can_set_jump?
( !custom_option )
end
def get_jump_tos
Hash[survey_question_options.select{ |o| !o.jump_to.blank? }.collect{ |o| [o.id.to_s, o.jump_to] }]
end
2021-07-08 13:31:27 +00:00
def custom_option_new_option
self.custom_option && self.custom_option_type.include?(1)
end
def custom_option_each_option
self.custom_option && self.custom_option_type.include?(0)
end
def custom_option_title
tmp = super()
tmp.blank? ? I18n.t('survey_question.option_title_default') : tmp
end
def layout_float
self.option_layout_type==1
end
def as_json(**xargs)
res = super({:except =>:survey_question_options}.merge(xargs))
res.merge({title_translations: self.title_translations,
description_translations: self[:description],
qid: self[:_id].to_s,
survey_question_options: self.survey_question_options.as_json,
custom_option_title_translations: self.custom_option_title_translations})
end
def is_selectable_radio
self.selectable_question && self.selectable_question_type==1
end
def is_selectable_check
self.selectable_question && self.selectable_question_type==0
end
def level1_radio
self.question_type==0
end
def level1_check
self.question_type==1
end
#def attributes()
# tmp = super()
# tmp.merge({title_translations: tmp[:title],
# description_translations: tmp[:description],
# qid: tmp[:_id].to_s})
#end
2014-10-06 05:55:25 +00:00
end