diff --git a/app/models/exported_plan.rb b/app/models/exported_plan.rb index 368f077..0bd9ef1 100644 --- a/app/models/exported_plan.rb +++ b/app/models/exported_plan.rb @@ -80,9 +80,8 @@ end def sections - phase_id = self.phase_id ||= self.plan.template.phases.first.id # Use the first phase if none was specified - sections = Phase.find(phase_id).sections - sections.sort_by(&:number) + self.phase_id ||= self.plan.template.phases.first.id + Section.where({phase_id: phase_id}).order(:number) end def questions_for_section(section_id) @@ -104,16 +103,25 @@ 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) - q_format = question.question_format - if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') - options_string = answer.options.collect {|o| o.text}.join('; ') - else - options_string = '' + questions = self.questions_for_section(section) + if questions.present? + questions.each do |question| + answer = self.plan.answer(question.id) + q_format = question.question_format + if q_format.option_based? + options_string = answer.question_options.collect {|o| o.text}.join('; ') + else + options_string = '' + end + csv << [ + section.title, + sanitize_text(question.text), + question.option_comment_display ? sanitize_text(answer.text) : '', + options_string, + user.name, + answer.updated_at + ] end - csv << [section.title, sanitize_text(question.text), sanitize_text(answer.text), options_string, user.name, answer.updated_at] end end end @@ -122,7 +130,6 @@ def as_txt output = "#{self.plan.title}\n\n#{self.plan.template.title}\n" output += "\n"+_('Details')+"\n\n" - puts 'admin_details: '+self.admin_details.inspect self.admin_details.each do |at| value = self.send(at) @@ -134,35 +141,35 @@ end self.sections.each do |section| - output += "\n#{section.title}\n" + questions = self.questions_for_section(section) + if questions.present? + output += "\n#{section.title}\n" + questions.each do |question| + qtext = sanitize_text( question.text.gsub(/
  • /, ' * ') ) + output += "\n* #{qtext}" + answer = self.plan.answer(question.id, false) - self.questions_for_section(section).each do |question| - qtext = sanitize_text( question.text.gsub(/
  • /, ' * ') ) - output += "\n* #{qtext}" - answer = self.plan.answer(question.id, false) - - if answer.nil? - output += _('Question not answered.')+ "\n" - else - q_format = question.question_format - if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') - output += answer.options.collect {|o| o.text}.join("\n") - if question.option_comment_display + if answer.nil? + output += _('Question not answered.')+ "\n" + else + q_format = question.question_format + if q_format.option_based? + output += answer.question_options.collect {|o| o.text}.join("\n") + if question.option_comment_display + output += "\n#{sanitize_text(answer.text)}\n" + end + else output += "\n#{sanitize_text(answer.text)}\n" end - else - output += "\n#{sanitize_text(answer.text)}\n" end end end end - output end private - + # Returns an Array of question_ids for the exported settings stored for a plan def questions question_settings = self.settings(:export).fields[:questions] @questions ||= if question_settings.present? diff --git a/app/views/plans/export.docx.caracal b/app/views/plans/export.docx.caracal deleted file mode 100644 index 778eb3c..0000000 --- a/app/views/plans/export.docx.caracal +++ /dev/null @@ -1,256 +0,0 @@ -#----------------------------------------------------- -# page settings -#----------------------------------------------------- - -docx.page_numbers true do - align :center -end - -docx.font do - name 'Arial' -end - -docx.p do - size 32 -end - - -#--------------------------------------------- -# Structure for docx format -#--------------------------------------------- - -docx.h1 @exported_plan.plan.title.upcase, font: 'Arial', color: '000000' -docx.h2 @plan.template.title.upcase, font: 'Arial', color: '000000' - -#---- PLAN ADMIN DETAILS ----- -if @exported_plan.admin_details.present? - docx.p - docx.h3 _('Admin Details').upcase , italic: false, font: 'Arial', color: '000000' - @exported_plan.admin_details.each do |field| - value = @exported_plan.send(field) - if value.present? - docx.p do |p| - p.text admin_field_t(field.to_s), bold: true, color: '000000' - p.text ': ', bold: true, color: '000000' - p.text value - end - end - end -end - - -#---- PLAN SECTIONS, QUESTIONS AND ANSWERS ----- -@exported_plan.sections.each do |section| - docx.p - docx.h3 section.title.upcase, italic: false, font: 'Arial', color: '000000' - - @exported_plan.questions_for_section(section.id).each do |question| - docx.p strip_tags(question.text.gsub(/
  • /, ' * ')), bold: true - - answer = @exported_plan.plan.answer(question.id, false) - if answer.nil? - docx.p _('Question not answered'), italic: true - else - q_format = question.question_format - if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') then - answer.options.each do |option| - docx.ul do - if !option.text.nil? - li option.text - end - end - end - end - - if q_format.title == _('Date') || - q_format.title == _('Text field') || - q_format.title == _('Text area') - docx.p strip_tags(answer.text) - end - - if !answer.text.nil? && question.option_comment_display == true then - search_answer = Nokogiri::HTML::DocumentFragment.parse "
    " - search_answer.at(".//div").inner_html = answer.text - - if search_answer.css('table').present? then - table_content = search_answer.css('table').css('tbody') - - if table_content.size > 0 then - table_array = Array.new - - table_content.css('tr').each do |tr| - row_th_array = Array.new - row_td_array = Array.new - - if tr.search('th') then - tr.search('th').each do |cell| - row_th_array << cell.text.to_s - end - end - - if tr.search('td') then - tr.search('td').each do |td_cell| - if !td_cell.text.to_s.blank? then - new_val = Nokogiri::HTML::DocumentFragment.parse "

    " - new_val.at(".//p").inner_html = td_cell - td_tags = new_val.at_css('td').children.map {|x| x.name.strip} - td_text = new_val.at_css('td').children.map {|x| x.text.strip} - - start_c = 0 - end_c = td_tags.size - - c1 = Caracal::Core::Models::TableCellModel.new do - while start_c < end_c do - p do - #-- TEXT - if td_tags[start_c] == 'text' then - text td_text[start_c] - else - #-- LINK - if td_tags[start_c] == 'a' then - l_address = "" - l_text = td_text[start_c] - #-- GET HREF - td_cell.search('a').each do |link| - if link.content == td_text[start_c] then - l_address = link["href"] - end - end - link td_text[start_c], l_address - #-- BOLD TEXT - else - if td_tags[start_c] == 'strong' && td_text[start_c] != "" then - text td_text[start_c], bold: true - text ' ' - #-- ITALIC TEXT - else - if td_tags[start_c] == 'em' && td_text[start_c] != "" then - text td_text[start_c], italic: true - text ' ' - end - end - end - end - end - start_c += 1 - end - end - row_td_array << c1 - else - row_td_array << td_cell.text.to_s - end - end - end - - #--- check if all cells are empty - if row_td_array.size > 0 then - if row_td_array.all?(&:blank?) then - row_td_array = [] - end - end - - if !row_th_array.empty? then - table_array << row_th_array - end - if !row_td_array.empty? then - table_array << row_td_array - end - - end - - #--- build the table layout - docx.table table_array, border_size: 4 do - cell_style rows[0], bold: true, background: 'fbb400' - cell_style cells, size: 18, margins: { top: 100, bottom: 0, left: 100, right: 100 } - end - - end - - else - higher_level = search_answer.search('div.container').children.map {|x| x.name.strip} - all_text = search_answer.search('div.container').children.map {|x| x.inner_html.strip} - high_count = 0 - high_end_count = higher_level.size - - while high_count < high_end_count do - - if higher_level[high_count] == 'p' then - inner_txt_with_tags = Nokogiri::HTML::DocumentFragment.parse "

    " - inner_txt_with_tags.at(".//p").inner_html = all_text[high_count] - tags_type = inner_txt_with_tags.at_css('p').children.map {|x| x.name.strip} - inner_txt = inner_txt_with_tags.at_css('p').children.map {|x| x.text.strip} - - start_count = 0 - end_counter = tags_type.size - - docx.p do - while start_count < end_counter do - text_val = inner_txt[start_count].to_s - #-- TEXT - if tags_type[start_count] == 'text' && text_val != "" then - text text_val - text ' ' - #-- LINK - else - if tags_type[start_count] == 'a' && text_val != "" then - l_text = text_val - l_address = "" - # all links - search_answer.search('a').each do |link| - if link.content == text_val then - l_address = link["href"] - end - end - link text_val, l_address - #-- BOLD TEXT - else - if tags_type[start_count] == 'strong' && text_val != "" then - text text_val, bold: true - text ' ' - #-- ITALIC TEXT - else - if tags_type[start_count] == 'em' && text_val != "" then - text text_val, italic: true - text ' ' - end - end - end - end - start_count +=1 - end - end - #-- END OF P - else - if higher_level[high_count] == 'ul' then - ul_text = search_answer.search('ul').children.map {|x| x.text.strip} - docx.ul do - ul_text.each do |txt| - if !txt.blank? - li txt - end - end - end - else - if higher_level[high_count] == 'ol' then - ol_text = search_answer.search('ol').children.map {|x| x.text.strip} - docx.ol do - ol_text.each do |txt| - if !txt.blank? - li txt - end - end - end - end - end - end - - high_count += 1 - end - end - end - end - #-- add a new line - docx.p - end -end diff --git a/app/views/plans/export.docx.erb b/app/views/plans/export.docx.erb index 12d289f..7f9c2c2 100644 --- a/app/views/plans/export.docx.erb +++ b/app/views/plans/export.docx.erb @@ -16,31 +16,32 @@ <% @exported_plan.sections.each do |section| %> -

    <%= section.title %>

    - <% questions = @exported_plan.questions_for_section(section.id) %> - <% questions.each do |question| %> -

    - <%= raw question.text %> -

    - - <% answer = @plan.answer(question.id, false) %> - <% if answer.nil? then %> -

    <%= _('Question not answered') %>

    - <% else %> - <% q_format = question.question_format %> - <% if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') %> - - <% if question.option_comment_display %> - <%= raw answer.text %> - <% end %> - <%else%> - <%= raw answer.text %> - <%end%> - <% end %> - <% end %> -<% end %> + <% questions = @exported_plan.questions_for_section(section.id) + if questions.present? + %> +

    <%= section.title %>

    + <% questions.each do |question| %> +

    + <%= raw question.text %> +

    + <% answer = @plan.answer(question.id, false) %> + <% if answer.nil? %> +

    <%= _('Question not answered') %>

    + <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> + + <% if question.option_comment_display %> + <%= raw answer.text %> + <% end %> + <% else %> + <%= raw answer.text %> + <% end %> + <% end%> + <% end %> + <% end %> +<% end %> diff --git a/app/views/plans/export.html.erb b/app/views/plans/export.html.erb index 8e79cc4..27a6b1b 100644 --- a/app/views/plans/export.html.erb +++ b/app/views/plans/export.html.erb @@ -29,47 +29,48 @@ <% end %> <% @exported_plan.sections.each do |section| %> -

    <%= section.title %>

    - - - - - - - - - <% questions = @exported_plan.questions_for_section(section.id) %> - <% questions.each do |question| %> + <% questions = @exported_plan.questions_for_section(section.id) + if questions.present? %> +

    <%= section.title %>

    +
    <%= _('Questions')%><%= _('Answers')%>
    + + + + + + + + <% questions.each do |question| %> <% end %> - -
    <%= _('Questions')%><%= _('Answers')%>

    - <%= raw question.text %>

    <% answer = @plan.answer(question.id, false) %> - <% if answer.nil? then %> + <% if answer.nil? %>

    <%= _('Question not answered') %>

    <% else %> <% q_format = question.question_format %> - <% if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') then%> + <% if q_format.option_based? %>
      - <% answer.options.each do |option| %> + <% answer.question_options.each do |option| %>
    • <%= option.text %>
    • <% end %>
    - <% if question.option_comment_display == true then%> + <% if question.option_comment_display == true %> <%= raw answer.text %> <% end %> - <%else%> + <% else%> <%= raw answer.text %> - <%end%> + <% end%> <% end %>
    - <% end %> + + + <% end %> + <% end %>
    diff --git a/app/views/plans/export.pdf.erb b/app/views/plans/export.pdf.erb index 874acd4..b9ab084 100644 --- a/app/views/plans/export.pdf.erb +++ b/app/views/plans/export.pdf.erb @@ -29,40 +29,39 @@ <% end %> <% @exported_plan.sections.each do |section| %> -

    <%= section.title %>

    - <% questions = @exported_plan.questions_for_section(section.id) %> - <% questions.each_with_index do |question, idx| %> -
    - <% unless idx == 0 && question.text == section.title %> + <% questions = @exported_plan.questions_for_section(section.id) + if questions.present? + %> +

    <%= section.title %>

    + <% questions.each do |question| %> +

    <%= raw question.text %>

    - <% end %> - <% answer = @plan.answer(question.id, false) %> - <% if answer.nil? then %> -

    <%= _('Question not answered.') -%>

    - <% else %> - <% q_format = question.question_format%> - - <% if q_format.title == _('Check box') || q_format.title == _('Multi select box') || - q_format.title == _('Radio buttons') || q_format.title == _('Dropdown') then%> -
      - <% answer.options.each do |option| %> -
    • <%= option.text %>
    • - <% end %> -
    - - <% if question.option_comment_display == true then%> + <% answer = @plan.answer(question.id, false) %> + <% if answer.nil? then %> +

    <%= _('Question not answered.') -%>

    + <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> +
      + <% answer.question_options.each do |option| %> +
    • <%= option.text %>
    • + <% end %> +
    + + <% if question.option_comment_display == true then%> + <% if !answer.text.nil? then %> + <%= raw answer.text.gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") %> + <%end%> + <%end%> + <%else%> + <% if !answer.text.nil? then %> <%= raw answer.text.gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") %> <%end%> - <%end%> - <%else%> - - <% if !answer.text.nil? then %> - <%= raw answer.text.gsub(/(\s||<\/td>| )*(<\/tr>|)/,"") %> - <%end%> - <% end %> - <% end %> -
    + <% end %> + <% end %> +
    + <% end %> <% end %> <% end %>