Commit b1ce9803258fb18783b11fcf3af83f4c44ce6707
1 parent
0f801692
Exists in
staging
and in
3 other branches
comment_group: export comments
Showing
2 changed files
with
52 additions
and
1 deletions
Show diff stats
plugins/comment_group/lib/comment_group_plugin.rb
| @@ -30,7 +30,7 @@ class CommentGroupPlugin < Noosfero::Plugin | @@ -30,7 +30,7 @@ class CommentGroupPlugin < Noosfero::Plugin | ||
| 30 | 30 | ||
| 31 | def article_extra_toolbar_buttons(article) | 31 | def article_extra_toolbar_buttons(article) |
| 32 | user = context.send :user | 32 | user = context.send :user |
| 33 | - return [] if !article.comment_group_plugin_enabled? || !article.allow_edit?(user) | 33 | + return [] if !article.allow_edit?(user) || article.comments.where("comments.group_id is not null").empty? |
| 34 | [ | 34 | [ |
| 35 | { | 35 | { |
| 36 | :title => _('Export Comments'), | 36 | :title => _('Export Comments'), |
plugins/comment_group/lib/comment_group_plugin/comments_report.rb
0 → 100644
| @@ -0,0 +1,51 @@ | @@ -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 |