cirandas.net

ref: master

plugins/work_assignment/lib/work_assignment_plugin/helper.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module WorkAssignmentPlugin::Helper

  def display_author_folder(author_folder, user)
    return if author_folder.children(true).empty?
    content_tag('tr',
      content_tag('td', link_to_last_submission(author_folder, user)) +
      content_tag('td', time_format(author_folder.children.last.created_at)) +
      content_tag('td', author_folder.children.count, :style => 'text-align: center') +
      content_tag('td', content_tag('button', _('View all versions'), :class => 'view-author-versions', 'data-folder-id' => author_folder.id)) +
      content_tag('td', display_privacy_button(author_folder, user))
    ).html_safe +
    author_folder.children.order('created_at DESC').map {|submission| display_submission(submission, user)}.join("\n").html_safe
  end

  def display_submission(submission, user)
    content_tag('tr',
      content_tag('td', link_to_submission(submission, user)) +
      content_tag('td', time_format(submission.created_at))+
      content_tag('td', '') +
      content_tag('td',
        if submission.parent.parent.allow_post_content?(user)
          display_delete_button(submission)
        end
      ),
      :class => "submission-from-#{submission.parent.id}",
      :style => 'display: none'
    )
  end

  def link_to_submission(submission, user)
    if WorkAssignmentPlugin.can_download_submission?(user, submission)
      link_to(submission.name, submission.url)
    else
      submission.name
    end
  end

  def link_to_last_submission(author_folder, user)
    if WorkAssignmentPlugin.can_download_submission?(user, author_folder.children.last)
      link_to(author_folder.name, author_folder.children.last.url)
    else
      author_folder.name
    end
  end

  # FIXME Copied from custom_forms. Consider passing it to core...
  def time_format(time)
    minutes = (time.min == 0) ? '' : ':%M'
    hour = (time.hour == 0 && minutes.blank?) ? '' : ' %H'
    h = hour.blank? ? '' : 'h'
    time.strftime("%Y-%m-%d#{hour+minutes+h}")
  end

  def display_delete_button(article)
    expirable_button article, :delete, _('Delete'),
    {:controller =>'cms', :action => 'destroy', :id => article.id },
    method: :post, 'data-confirm' => delete_article_message(article)
  end

  def display_privacy_button(author_folder, user)
    folder = environment.articles.find_by id: author_folder.id
    work_assignment = folder.parent
    @back_to = url_for(work_assignment.url)

    if(user && work_assignment.allow_visibility_edition &&
      ((author_folder.author_id == user.id && (user.is_member_of? profile)) ||
      user.has_permission?('view_private_content', profile)))

      @tokenized_children = prepare_to_token_input(
                            profile.members.includes(:articles_with_access).find_all{ |m|
                              m.articles_with_access.include?(folder)
                            })
      button :edit, _('Edit'), { :controller => 'work_assignment_plugin_myprofile',
      :action => 'edit_visibility', :article_id => folder.id,
      :tokenized_children => @tokenized_children, :back_to => @back_to}, :method => :post
    end
  end
end