From 4694ee41b7f29f5dcbb5f2f2c9c7a761e6d3538b Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Tue, 5 Nov 2013 14:00:28 -0300 Subject: [PATCH] Export CSV --- plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb | 18 ++++++++++++++++++ plugins/custom_forms/lib/custom_forms_plugin/answer.rb | 6 ++++++ plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb | 13 +++++++++++++ plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb | 4 ++++ 4 files changed, 41 insertions(+), 0 deletions(-) diff --git a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb index 0663fe3..fb91fdc 100644 --- a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb +++ b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb @@ -1,3 +1,5 @@ +require 'csv' + class CustomFormsPluginMyprofileController < MyProfileController protect 'post_content', :profile @@ -59,6 +61,22 @@ class CustomFormsPluginMyprofileController < MyProfileController def submissions @form = CustomFormsPlugin::Form.find(params[:id]) @submissions = @form.submissions + respond_to do |format| + format.html + format.csv do + # CSV contains form fields, timestamp, user name and user email + columns = @form.fields.count + 3 + csv_content = CSV.generate_line(['Timestamp', 'Name', 'Email'] + @form.fields.map(&:name)) + "\n" + @submissions.each do |s| + fields = [s.updated_at, s.profile.present? ? s.profile.name : s.author_name, s.profile.present? ? s.profile.email : s.author_email] + @form.fields.each do |f| + fields << s.answers.select{|a| a.field == f}.map{|answer| answer.to_s} + end + CSV.generate_row(fields, columns, csv_content) + end + send_data csv_content, :type => 'text/csv', :filename => "#{@form.name}.csv" + end + end end def show_submission diff --git a/plugins/custom_forms/lib/custom_forms_plugin/answer.rb b/plugins/custom_forms/lib/custom_forms_plugin/answer.rb index f66561f..d29766f 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/answer.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/answer.rb @@ -10,5 +10,11 @@ class CustomFormsPlugin::Answer < Noosfero::Plugin::ActiveRecord errors.add(:value, _("is mandatory.").fix_i18n) end end + + def to_s + return value if value.blank? || field.alternatives.blank? + selected = value.split(',') + field.alternatives.select {|alt| selected.include? alt.id.to_s }.map(&:label).join(';') + end end diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb index 827642c..f566b66 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/answer_test.rb @@ -34,5 +34,18 @@ class CustomFormsPlugin::AnswerTest < ActiveSupport::TestCase assert !answer.errors.invalid?(:value) end + should 'make string representation show answers' do + form = CustomFormsPlugin::Form.create!(:name => 'Free Software', :profile => fast_create(Profile)) + field = CustomFormsPlugin::Field.create!(:name => 'ProjectName', :form => form) + answer = CustomFormsPlugin::Answer.new(:field => field, :value => 'MyProject') + + field_select = CustomFormsPlugin::Field.create!(:name => 'License', :form => form) + alt = CustomFormsPlugin::Alternative.create!(:id => 1, :field => field_select, :label => 'GPL') + answer2 = CustomFormsPlugin::Answer.new(:field => field_select, :value => alt.id.to_s) + + assert_equal 'MyProject', answer.to_s + assert_equal 'GPL', answer2.to_s + end + end diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb index e03b7e4..50ce9e5 100644 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/submissions.html.erb @@ -5,6 +5,10 @@ <% if @form.submissions.empty? %> <%= _('There are no submissions for this form.') %> <% else %> +

+ <%= _('Download all form responses as') %>: + <%= link_to '[CSV]', :format => 'csv' %> +

-- libgit2 0.21.2
<%= _('Author') %>