From b1ce9803258fb18783b11fcf3af83f4c44ce6707 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 21 Jan 2016 17:36:12 -0300 Subject: [PATCH] comment_group: export comments --- plugins/comment_group/lib/comment_group_plugin.rb | 2 +- plugins/comment_group/lib/comment_group_plugin/comments_report.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 plugins/comment_group/lib/comment_group_plugin/comments_report.rb diff --git a/plugins/comment_group/lib/comment_group_plugin.rb b/plugins/comment_group/lib/comment_group_plugin.rb index bd90b97..a467ad0 100644 --- a/plugins/comment_group/lib/comment_group_plugin.rb +++ b/plugins/comment_group/lib/comment_group_plugin.rb @@ -30,7 +30,7 @@ class CommentGroupPlugin < Noosfero::Plugin def article_extra_toolbar_buttons(article) user = context.send :user - return [] if !article.comment_group_plugin_enabled? || !article.allow_edit?(user) + return [] if !article.allow_edit?(user) || article.comments.where("comments.group_id is not null").empty? [ { :title => _('Export Comments'), diff --git a/plugins/comment_group/lib/comment_group_plugin/comments_report.rb b/plugins/comment_group/lib/comment_group_plugin/comments_report.rb new file mode 100644 index 0000000..d41881a --- /dev/null +++ b/plugins/comment_group/lib/comment_group_plugin/comments_report.rb @@ -0,0 +1,51 @@ +module CommentGroupPlugin::CommentsReport + + #FIXME make this test + def export_comments_csv(article) + comments_map = article.comments.group_by { |comment| comment.group_id } + @export = [] + doc = Nokogiri::HTML(article.body) + paragraph_id = 1 + doc.css("[data-macro-group_id]").map do |paragraph| + uuid = paragraph.attributes['data-macro-group_id'].value + comments_for_paragraph = comments_map[uuid.to_i] + if comments_for_paragraph.present? + # Put comments for the paragraph + comments_for_paragraph.each do | comment | + @export << create_comment_element(comment, paragraph, paragraph_id) + end + else # There are no comments for this paragraph + @export << create_comment_element(nil, paragraph, paragraph_id) + end + paragraph_id += 1 + end + # Now we need to put all other comments that are not attached to a paragraph + comments_without_paragrah = comments_map[nil] || [] + comments_without_paragrah.each do | comment | + @export << create_comment_element(comment, nil, nil) + end + return _("No comments for article[%{id}]: %{path}\n\n") % {:id => article.id, :path => article.path} if @export.empty? + + column_names = @export.first.keys + CSV.generate do |csv| + csv << column_names + @export.each { |x| csv << x.values } + end + end + + private + + def create_comment_element(comment, paragraph, paragraph_id) + { + paragraph_id: paragraph_id, + paragraph_text: paragraph.present? ? paragraph.text.strip : nil, + comment_id: comment.present? ? comment.id : '-', + comment_reply_to: comment.present? ? comment.reply_of_id : '-', + comment_title: comment.present? ? comment.title : '-', + comment_content: comment.present? ? comment.body : '-', + comment_author_name: comment.present? ? comment.author_name : '-', + comment_author_email: comment.present? ? comment.author_email : '-' + } + end + +end -- libgit2 0.21.2