Commit 4694ee41b7f29f5dcbb5f2f2c9c7a761e6d3538b

Authored by Larissa Reis
1 parent c224f8a9

Export CSV

plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb
  1 +require 'csv'
  2 +
1 3 class CustomFormsPluginMyprofileController < MyProfileController
2 4 protect 'post_content', :profile
3 5  
... ... @@ -59,6 +61,22 @@ class CustomFormsPluginMyprofileController &lt; MyProfileController
59 61 def submissions
60 62 @form = CustomFormsPlugin::Form.find(params[:id])
61 63 @submissions = @form.submissions
  64 + respond_to do |format|
  65 + format.html
  66 + format.csv do
  67 + # CSV contains form fields, timestamp, user name and user email
  68 + columns = @form.fields.count + 3
  69 + csv_content = CSV.generate_line(['Timestamp', 'Name', 'Email'] + @form.fields.map(&:name)) + "\n"
  70 + @submissions.each do |s|
  71 + fields = [s.updated_at, s.profile.present? ? s.profile.name : s.author_name, s.profile.present? ? s.profile.email : s.author_email]
  72 + @form.fields.each do |f|
  73 + fields << s.answers.select{|a| a.field == f}.map{|answer| answer.to_s}
  74 + end
  75 + CSV.generate_row(fields, columns, csv_content)
  76 + end
  77 + send_data csv_content, :type => 'text/csv', :filename => "#{@form.name}.csv"
  78 + end
  79 + end
62 80 end
63 81  
64 82 def show_submission
... ...
plugins/custom_forms/lib/custom_forms_plugin/answer.rb
... ... @@ -10,5 +10,11 @@ class CustomFormsPlugin::Answer &lt; Noosfero::Plugin::ActiveRecord
10 10 errors.add(:value, _("is mandatory.").fix_i18n)
11 11 end
12 12 end
  13 +
  14 + def to_s
  15 + return value if value.blank? || field.alternatives.blank?
  16 + selected = value.split(',')
  17 + field.alternatives.select {|alt| selected.include? alt.id.to_s }.map(&:label).join(';')
  18 + end
13 19 end
14 20  
... ...
plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb
... ... @@ -34,5 +34,18 @@ class CustomFormsPlugin::AnswerTest &lt; ActiveSupport::TestCase
34 34 assert !answer.errors.invalid?(:value)
35 35 end
36 36  
  37 + should 'make string representation show answers' do
  38 + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile))
  39 + field = CustomFormsPlugin::Field.create!(:name => 'ProjectName', :form => form)
  40 + answer = CustomFormsPlugin::Answer.new(:field => field, :value => 'MyProject')
  41 +
  42 + field_select = CustomFormsPlugin::Field.create!(:name => 'License', :form => form)
  43 + alt = CustomFormsPlugin::Alternative.create!(:id => 1, :field => field_select, :label => 'GPL')
  44 + answer2 = CustomFormsPlugin::Answer.new(:field => field_select, :value => alt.id.to_s)
  45 +
  46 + assert_equal 'MyProject', answer.to_s
  47 + assert_equal 'GPL', answer2.to_s
  48 + end
  49 +
37 50 end
38 51  
... ...
plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb
... ... @@ -5,6 +5,10 @@
5 5 <% if @form.submissions.empty? %>
6 6 <%= _('There are no submissions for this form.') %>
7 7 <% else %>
  8 + <p>
  9 + <%= _('Download all form responses as') %>:
  10 + <%= link_to '[CSV]', :format => 'csv' %>
  11 + </p>
8 12 <table class="action-table">
9 13 <tr>
10 14 <th style='width: 50%'><%= _('Author') %></th>
... ...