Commit 6a838b8e85802de448446b68a26903c5875cad77

Authored by Victor Costa
2 parents 2efdf80b b1ce9803

Merge branch 'export-comment-paragraph-rails3' into staging

plugins/comment_group/controllers/profile/comment_group_plugin_profile_controller.rb
... ... @@ -22,4 +22,15 @@ class CommentGroupPluginProfileController < ProfileController
22 22 3
23 23 end
24 24  
  25 + include CommentGroupPlugin::CommentsReport
  26 +
  27 + def export_comments
  28 + article_id = params[:id]
  29 + article = profile.articles.find(article_id)
  30 + result = export_comments_csv(article)
  31 + filename = "comments_for_article#{article_id}_#{DateTime.now.to_i}.csv"
  32 + send_data result,
  33 + :type => 'text/csv; charset=UTF-8; header=present',
  34 + :disposition => "attachment; filename=#{filename}"
  35 + end
25 36 end
... ...
plugins/comment_group/lib/comment_group_plugin.rb
... ... @@ -28,7 +28,17 @@ class CommentGroupPlugin < Noosfero::Plugin
28 28 true
29 29 end
30 30  
31   -
  31 + def article_extra_toolbar_buttons(article)
  32 + user = context.send :user
  33 + return [] if !article.allow_edit?(user) || article.comments.where("comments.group_id is not null").empty?
  34 + [
  35 + {
  36 + :title => _('Export Comments'),
  37 + :url => {:controller => 'comment_group_plugin_profile', :profile => article.profile.identifier, :action => 'export_comments', :id => article.id},
  38 + :icon => :toggle_comment_paragraph
  39 + }
  40 + ]
  41 + end
32 42 end
33 43  
34 44 require_dependency 'comment_group_plugin/macros/allow_comment'
... ...
plugins/comment_group/lib/comment_group_plugin/comments_report.rb 0 → 100644
... ... @@ -0,0 +1,51 @@
  1 +module CommentGroupPlugin::CommentsReport
  2 +
  3 + #FIXME make this test
  4 + def export_comments_csv(article)
  5 + comments_map = article.comments.group_by { |comment| comment.group_id }
  6 + @export = []
  7 + doc = Nokogiri::HTML(article.body)
  8 + paragraph_id = 1
  9 + doc.css("[data-macro-group_id]").map do |paragraph|
  10 + uuid = paragraph.attributes['data-macro-group_id'].value
  11 + comments_for_paragraph = comments_map[uuid.to_i]
  12 + if comments_for_paragraph.present?
  13 + # Put comments for the paragraph
  14 + comments_for_paragraph.each do | comment |
  15 + @export << create_comment_element(comment, paragraph, paragraph_id)
  16 + end
  17 + else # There are no comments for this paragraph
  18 + @export << create_comment_element(nil, paragraph, paragraph_id)
  19 + end
  20 + paragraph_id += 1
  21 + end
  22 + # Now we need to put all other comments that are not attached to a paragraph
  23 + comments_without_paragrah = comments_map[nil] || []
  24 + comments_without_paragrah.each do | comment |
  25 + @export << create_comment_element(comment, nil, nil)
  26 + end
  27 + return _("No comments for article[%{id}]: %{path}\n\n") % {:id => article.id, :path => article.path} if @export.empty?
  28 +
  29 + column_names = @export.first.keys
  30 + CSV.generate do |csv|
  31 + csv << column_names
  32 + @export.each { |x| csv << x.values }
  33 + end
  34 + end
  35 +
  36 + private
  37 +
  38 + def create_comment_element(comment, paragraph, paragraph_id)
  39 + {
  40 + paragraph_id: paragraph_id,
  41 + paragraph_text: paragraph.present? ? paragraph.text.strip : nil,
  42 + comment_id: comment.present? ? comment.id : '-',
  43 + comment_reply_to: comment.present? ? comment.reply_of_id : '-',
  44 + comment_title: comment.present? ? comment.title : '-',
  45 + comment_content: comment.present? ? comment.body : '-',
  46 + comment_author_name: comment.present? ? comment.author_name : '-',
  47 + comment_author_email: comment.present? ? comment.author_email : '-'
  48 + }
  49 + end
  50 +
  51 +end
... ...
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
... ... @@ -47,18 +47,19 @@ class CommentParagraphPlugin &lt; Noosfero::Plugin
47 47 def article_extra_toolbar_buttons(article)
48 48 user = context.send :user
49 49 return [] if !article.comment_paragraph_plugin_enabled? || !article.allow_edit?(user)
50   - [
  50 + buttons = [
51 51 {
52 52 :title => article.comment_paragraph_plugin_activated? ? _('Deactivate Comments') : _('Activate Comments'),
53 53 :url => {:controller => 'comment_paragraph_plugin_myprofile', :profile => article.profile.identifier, :action => 'toggle_activation', :id => article.id},
54 54 :icon => :toggle_comment_paragraph
55   - },
56   - {
  55 + }
  56 + ]
  57 + buttons << {
57 58 :title => _('Export Comments'),
58 59 :url => {:controller => 'comment_paragraph_plugin_profile', :profile => article.profile.identifier, :action => 'export_comments', :id => article.id},
59 60 :icon => :toggle_comment_paragraph
60   - }
61   - ]
  61 + } if article.comment_paragraph_plugin_activated?
  62 + buttons
62 63 end
63 64  
64 65 end
... ...