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(/
" - 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| %> -
- <%= 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') %> -+ <%= 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? %> +| <%= _('Questions')%> | -<%= _('Answers')%> | -
|---|
| <%= _('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? %>
|
<%= _('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%> -<%= _('Question not answered.') -%>
+ <% else %> + <% q_format = question.question_format %> + <% if q_format.option_based? %> +