app/reports/questionnaire_pdf.rb
def section_to_pdf language, section, user, fields, answers, url_prefix, short_version, loop_sources_items, loop_item=nil, looping_identifier=nil
    #don't print anything for this section if it's a short version and there are no answers for this particular section
    return if short_version && (answers.empty? || !section.any_answers_from?(user, loop_sources_items, loop_item, looping_identifier))
    conditions_met_or_inexistent = section.depends_on_option.present? ? section.dependency_condition_met?(user, looping_identifier) : true
    #If section is hidden and is a looping section and the loop_item is present, print the loop_item name since there is no drop down list like in the Web Sumbission page
    if section.is_hidden? && section.looping? && loop_item.present?
      text "#{loop_item.item_name(language)}", :size => 11, :style => :bold, :inline_format => true
    end
    if !section.is_hidden?
      field_to_use = fields[:sections_field][section.id.to_s] && fields[:sections_field][section.id.to_s].title.present? ? fields[:sections_field][section.id.to_s] : fields[:sections_field_default][section.id.to_s]
      size_to_print = section.root? ? 14 : 11
      if section.looping? && loop_item.present?
        text "#{OrtSanitize.white_space_cleanse(field_to_use.loop_title(nil, loop_item))}", :size => size_to_print, :style => :bold, :inline_format => true
      else
        text "#{OrtSanitize.white_space_cleanse(field_to_use.title)}", :size => size_to_print, :style => :bold, :inline_format => true
      end
      field_to_use = fields[:sections_field][section.id.to_s] && fields[:sections_field][section.id.to_s].description.present? ? fields[:sections_field][section.id.to_s] : fields[:sections_field_default][section.id.to_s]
      images_urls = []
      if field_to_use && field_to_use.description.present?
        #text "#{Sanitize.clean(field_to_use.description)}"
        text "#{OrtSanitize.white_space_cleanse((section.section_extras.present? && loop_item.present?) ?  field_to_use.replace_variables(:description, language, loop_sources_items, loop_item, images_urls) : field_to_use.description)}", :size => 10, :inline_format => true
      end
      display_images images_urls unless images_urls.empty?
      move_down 7
    end
    section.questionnaire_part.children_parts_sorted.each do |child|
      if child.is_a?(Question)
        answer = conditions_met_or_inexistent ? Question.get_answer(answers, child.id, looping_identifier) : nil
        unless short_version && answer.nil?
          question_to_pdf language, child, answer, loop_item, fields, loop_sources_items, url_prefix, short_version
        end
      else
        #if children is of looping type
        if child.looping?
          #get the items through which it is going to loop on
          items = child.next_loop_items loop_item, loop_sources_items
          #for each of the items"
          items.each do |item|
            #check if the section is available for the user
            if child.available_for? user, item
              #update the loop_sources history.
              loop_sources_items[child.loop_source.id.to_s] = item
              new_looping_identifier = looping_identifier ? "#{looping_identifier}#{LoopItem::LOOPING_ID_SEPARATOR}#{item.id}" : item.id.to_s
              #call the function for the children, sending the necessary items.
              section_to_pdf language, child, user, fields, answers, url_prefix, short_version, loop_sources_items, item, new_looping_identifier
            end
          end
        else
          #if it is not a looping item just call the function and send the same items...
          section_to_pdf language, child, user, fields, answers, url_prefix, short_version, loop_sources_items, loop_item, looping_identifier
        end
      end
    end
    if section.looping? && section.section_extras.present? && cursor != 720.0
      start_new_page# if section.looping? && section.section_extras.present?
      #puts "the cursor is here #{cursor}"
    end
  end
###
# user: the current user , or the user whose questionnaire is being converted to pdf
# section: the section that is being treated
# answers: all the answers of the user for this questionnaire
# loop_item: current loop_item, in case this is a looping section, nil if there's not one
# loop_sources_items: hash to keep track of which item was last used  for each loop_source
the current user , or the user whose questionnaire is being converted to pdf
section: the section that is being treated
answers: all the answers of the user for this questionnaire
loop_item: current loop_item, in case this is a looping section, nil if there's not one
loop_sources_items: hash to keep track of which item was last used  for each loop_source
the current user or the user whose questionnaire is being converted to pdf section : the section that is being treated answers : all the answers of the user for this questionnaire loop_item : current loop_item in case this is a looping section nil if there ' s not one loop_sources_items : hash to keep track of which item was last used for each loop_source
def section_to_pdf language, section, user, fields, answers, url_prefix, short_version, loop_sources_items, loop_item=nil, looping_identifier=nil
    return if short_version && (answers.empty? || !section.any_answers_from?(user, loop_sources_items, loop_item, looping_identifier))
    conditions_met_or_inexistent = section.depends_on_option.present? ? section.dependency_condition_met?(user, looping_identifier) : true
    if section.is_hidden? && section.looping? && loop_item.present?
      text "#{loop_item.item_name(language)}", :size => 11, :style => :bold, :inline_format => true
    end
    if !section.is_hidden?
      field_to_use = fields[:sections_field][section.id.to_s] && fields[:sections_field][section.id.to_s].title.present? ? fields[:sections_field][section.id.to_s] : fields[:sections_field_default][section.id.to_s]
      size_to_print = section.root? ? 14 : 11
      if section.looping? && loop_item.present?
        text "#{OrtSanitize.white_space_cleanse(field_to_use.loop_title(nil, loop_item))}", :size => size_to_print, :style => :bold, :inline_format => true
      else
        text "#{OrtSanitize.white_space_cleanse(field_to_use.title)}", :size => size_to_print, :style => :bold, :inline_format => true
      end
      field_to_use = fields[:sections_field][section.id.to_s] && fields[:sections_field][section.id.to_s].description.present? ? fields[:sections_field][section.id.to_s] : fields[:sections_field_default][section.id.to_s]
      images_urls = []
      if field_to_use && field_to_use.description.present?
        text "#{OrtSanitize.white_space_cleanse((section.section_extras.present? && loop_item.present?) ?  field_to_use.replace_variables(:description, language, loop_sources_items, loop_item, images_urls) : field_to_use.description)}", :size => 10, :inline_format => true
      end
      display_images images_urls unless images_urls.empty?
      move_down 7
    end
    section.questionnaire_part.children_parts_sorted.each do |child|
      if child.is_a?(Question)
        answer = conditions_met_or_inexistent ? Question.get_answer(answers, child.id, looping_identifier) : nil
        unless short_version && answer.nil?
          question_to_pdf language, child, answer, loop_item, fields, loop_sources_items, url_prefix, short_version
        end
      else
        if child.looping?
          items = child.next_loop_items loop_item, loop_sources_items
          items.each do |item|
            if child.available_for? user, item
              loop_sources_items[child.loop_source.id.to_s] = item
              new_looping_identifier = looping_identifier ? "#{looping_identifier}#{LoopItem::LOOPING_ID_SEPARATOR}#{item.id}" : item.id.to_s
              section_to_pdf language, child, user, fields, answers, url_prefix, short_version, loop_sources_items, item, new_looping_identifier
            end
          end
        else
          section_to_pdf language, child, user, fields, answers, url_prefix, short_version, loop_sources_items, loop_item, looping_identifier
        end
      end
    end
    if section.looping? && section.section_extras.present? && cursor != 720.0
      start_new_page
    end
  end
def section_to_pdf language , section , user , fields , answers , url_prefix , short_version , loop_sources_items , loop_item = nil , looping_identifier = nil return if short_version && ( answers . empty? || ! section . any_answers_from? ( user , loop_sources_items , loop_item , looping_identifier ) ) conditions_met_or_inexistent = section . depends_on_option . present? ? section . dependency_condition_met? ( user , looping_identifier ) : true if section . is_hidden? && section . looping? && loop_item . present? text "#{loop_item.item_name(language)}" , :size => 11 , :style => :bold , :inline_format => true end if ! section . is_hidden? field_to_use = fields [ :sections_field ] [ section . id . to_s ] && fields [ :sections_field ] [ section . id . to_s ] . title . present? ? fields [ :sections_field ] [ section . id . to_s ] : fields [ :sections_field_default ] [ section . id . to_s ] size_to_print = section . root? ? 14 : 11 if section . looping? && loop_item . present? text "#{OrtSanitize.white_space_cleanse(field_to_use.loop_title(nil, loop_item))}" , :size => size_to_print , :style => :bold , :inline_format => true else text "#{OrtSanitize.white_space_cleanse(field_to_use.title)}" , :size => size_to_print , :style => :bold , :inline_format => true end field_to_use = fields [ :sections_field ] [ section . id . to_s ] && fields [ :sections_field ] [ section . id . to_s ] . description . present? ? fields [ :sections_field ] [ section . id . to_s ] : fields [ :sections_field_default ] [ section . id . to_s ] images_urls = [ ] if field_to_use && field_to_use . description . present? text "#{OrtSanitize.white_space_cleanse((section.section_extras.present? && loop_item.present?) ?  field_to_use.replace_variables(:description, language, loop_sources_items, loop_item, images_urls) : field_to_use.description)}" , :size => 10 , :inline_format => true end display_images images_urls unless images_urls . empty? move_down 7 end section . questionnaire_part . children_parts_sorted . each do | child | if child . is_a? ( Question ) answer = conditions_met_or_inexistent ? Question . get_answer ( answers , child . id , looping_identifier ) : nil unless short_version && answer . nil? question_to_pdf language , child , answer , loop_item , fields , loop_sources_items , url_prefix , short_version end else if child . looping? items = child . next_loop_items loop_item , loop_sources_items items . each do | item | if child . available_for? user , item loop_sources_items [ child . loop_source . id . to_s ] = item new_looping_identifier = looping_identifier ? "#{looping_identifier}#{LoopItem::LOOPING_ID_SEPARATOR}#{item.id}" : item . id . to_s section_to_pdf language , child , user , fields , answers , url_prefix , short_version , loop_sources_items , item , new_looping_identifier end end else section_to_pdf language , child , user , fields , answers , url_prefix , short_version , loop_sources_items , loop_item , looping_identifier end end end if section . looping? && section . section_extras . present? && cursor != 720.0 start_new_page end end
user: the current user , or the user whose questionnaire is being converted to pdf
section: the section that is being treated
answers: all the answers of the user for this questionnaire
loop_item: current loop_item, in case this is a looping section, nil if there's not one
loop_sources_items: hash to keep track of which item was last used  for each loop_source
user : the current user or the user whose questionnaire is being converted to pdf section : the section that is being treated answers : all the answers of the user for this questionnaire loop_item : current loop_item in case this is a looping section nil if there ' s not one loop_sources_items : hash to keep track of which item was last used for each loop_source
#don't print anything for this section if it's a short version and there are no answers for this particular section #If section is hidden and is a looping section and the loop_item is present, print the loop_item name since there is no drop down list like in the Web Sumbission page #text "#{Sanitize.clean(field_to_use.description)}" #if children is of looping type #get the items through which it is going to loop on #for each of the items" #check if the section is available for the user #update the loop_sources history. #call the function for the children, sending the necessary items. #if it is not a looping item just call the function and send the same items... # if section.looping? && section.section_extras.present? #puts "the cursor is here #{cursor}"
language
section
user
fields
answers
url_prefix
short_version
loop_sources_items
loop_item
looping_identifier