class ExportedPlan

Constants

VALID_FORMATS

Public Instance Methods

admin_details() click to toggle source
# File app/models/exported_plan.rb, line 71
def admin_details
  @admin_details ||= self.settings(:export).fields[:admin]
end
as_csv() click to toggle source

Export formats

# File app/models/exported_plan.rb, line 77
def as_csv
  CSV.generate do |csv|
    csv << ["Section","Question","Answer","Selected option(s)","Answered by","Answered at"]
    self.sections.each do |section|
      self.questions_for_section(section).each do |question|
        answer = self.plan.answer(question.id)
        options_string = answer.options.collect {|o| o.text}.join('; ')

        csv << [section.title, question.text, sanitize_text(answer.text), options_string, answer.try(:user).try(:name), answer.created_at]
      end
    end
  end
end
as_txt() click to toggle source
# File app/models/exported_plan.rb, line 91
def as_txt
  output = "#{self.plan.project.title}\n\n#{self.plan.version.phase.title}\n"

  self.sections.each do |section|
    output += "\n#{section.title}\n"

    self.questions_for_section(section).each do |question|
      output += "\n#{question.text}\n"
      answer = self.plan.answer(question.id, false)

      if answer.nil? || answer.text.nil? then
        output += "Question not answered.\n"
      else
        output += answer.options.collect {|o| o.text}.join("\n")
        if question.option_comment_display == true then
          output += "\n#{sanitize_text(answer.text)}\n"
        else
          output += "\n"
        end  
      end
    end
  end

  output
end
funder() click to toggle source
# File app/models/exported_plan.rb, line 46
def funder
  org = self.plan.project.dmptemplate.try(:organisation)
  org.name if org.present? && org.organisation_type.try(:name) == I18n.t('helpers.org_type.funder')
end
grant_title() click to toggle source
# File app/models/exported_plan.rb, line 30
def grant_title
  self.plan.project.grant_number
end
institution() click to toggle source
# File app/models/exported_plan.rb, line 51
def institution
  plan.project.organisation.try(:name)
end
principal_investigator() click to toggle source
# File app/models/exported_plan.rb, line 34
def principal_investigator
  self.plan.project.principal_investigator
end
project_data_contact() click to toggle source
# File app/models/exported_plan.rb, line 38
def project_data_contact
  self.plan.project.data_contact
end
project_description() click to toggle source
# File app/models/exported_plan.rb, line 42
def project_description
  self.plan.project.description
end
project_identifier() click to toggle source
# File app/models/exported_plan.rb, line 26
def project_identifier
  self.plan.project.identifier
end
project_name() click to toggle source

Getters to match Settings::Dmptemplate::VALID_ADMIN_FIELDS

# File app/models/exported_plan.rb, line 20
def project_name
  name = self.plan.project.title
  name += " - #{self.plan.title}" if self.plan.project.dmptemplate.phases.count > 1
  name
end
questions_for_section(section_id) click to toggle source
# File app/models/exported_plan.rb, line 67
def questions_for_section(section_id)
  questions.where(section_id: section_id)
end
sections() click to toggle source

sections taken from fields settings

# File app/models/exported_plan.rb, line 56
def sections
  sections = self.plan.sections

  return [] if questions.empty?

  section_ids = questions.pluck(:section_id).uniq
  sections = sections.select {|section| section_ids.member?(section.id) }

  sections.sort_by(&:number)
end

Private Instance Methods

questions() click to toggle source
# File app/models/exported_plan.rb, line 119
def questions
  @questions ||= begin
    question_settings = self.settings(:export).fields[:questions]

    return [] if question_settings.is_a?(Array) && question_settings.empty?

    questions = if question_settings.present? && question_settings != :all
      Question.where(id: question_settings)
    else
      Question.where(section_id: self.plan.sections.collect {|s| s.id })
    end

    questions.order(:number)
  end
end
sanitize_text(text) click to toggle source
# File app/models/exported_plan.rb, line 135
def sanitize_text(text)
  if (!text.nil?) then ActionView::Base.full_sanitizer.sanitize(text.gsub(/&nbsp;/,"")) end
end