Module: Course::Discussion::TopicsHelper

Included in:
TopicsComponent
Defined in:
app/helpers/course/discussion/topics_helper.rb

Instance Method Summary collapse

Instance Method Details

#all_staff_unread_countInteger

Returns the count of topics pending staff reply.

Returns:

  • (Integer)

    Returns the count of topics pending staff reply.


25
26
27
28
# File 'app/helpers/course/discussion/topics_helper.rb', line 25

def all_staff_unread_count
  @staff_unread ||= current_course.discussion_topics.
              globally_displayed.pending_staff_reply.distinct.count
end

#all_student_unread_countInteger

Returns the count of unread topics for student course users. Otherwise, return 0.

Returns:

  • (Integer)

    Returns the count of unread topics


61
62
63
64
65
66
67
68
69
# File 'app/helpers/course/discussion/topics_helper.rb', line 61

def all_student_unread_count
  @student_unread ||=
    if current_course_user&.student?
      current_course.discussion_topics.globally_displayed.from_user(current_user.id).
        unread_by(current_user).distinct.count
    else
      0
    end
end

#display_code_lines(file, line_start, line_end) ⇒ String

Display code lines in file.

Parameters:

Returns:

  • (String)

    A HTML fragment containing the code lines.


17
18
19
20
# File 'app/helpers/course/discussion/topics_helper.rb', line 17

def display_code_lines(file, line_start, line_end)
  code = file.lines((line_start - 1)..(line_end - 1)).join("\n")
  format_code_block(code, file.answer.question.actable.language, [line_start, 1].max)
end

#from_user(topic, my_student_ids) ⇒ Object

TODO: Consider adding creator_id column to the programming file annotation table. See https://github.com/Coursemology/coursemology2/issues/2880.

This replaces what the from_user scopes in the specific models were doing when getting my_students_unread_count, for better performance.


47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/course/discussion/topics_helper.rb', line 47

def from_user(topic, my_student_ids)
  case topic.actable_type
  when 'Course::Assessment::SubmissionQuestion'
    my_student_ids.include?(topic&.actable&.submission&.creator&.id)
  when 'Course::Video::Topic'
    my_student_ids.include?(topic&.actable&.creator&.id)
  when 'Course::Assessment::Answer::ProgrammingFileAnnotation'
    my_student_ids.include?(topic&.actable&.file&.answer&.submission&.creator&.id)
  end
end

83
84
85
86
87
# File 'app/helpers/course/discussion/topics_helper.rb', line 83

def link_to_mark_as_read(topic)
  return unless topic.unread?(current_user)
  link_to t('course.discussion.topics.mark_as_read'),
          mark_as_read_course_topic_path(current_course, topic), method: :patch
end

71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/course/discussion/topics_helper.rb', line 71

def link_to_toggle_pending(topic)
  if topic.pending_staff_reply?
    link_to t('course.discussion.topics.unmark_as_pending'),
            toggle_pending_course_topic_path(current_course, topic, pending: false),
            method: :patch, remote: true
  else
    link_to t('course.discussion.topics.mark_as_pending'),
            toggle_pending_course_topic_path(current_course, topic, pending: true),
            method: :patch, remote: true
  end
end

#my_students_unread_countObject


30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/course/discussion/topics_helper.rb', line 30

def my_students_unread_count
  @my_students_unread ||=
    if current_course_user
      my_student_ids = current_course_user.my_students.pluck(:user_id)
      topics = current_course.discussion_topics.globally_displayed.pending_staff_reply.distinct.
               includes(:actable)
      topics.select { |topic| from_user(topic, my_student_ids) }.count
    else
      0
    end
end

#topics_titleString|nil

Sanitize the title in settings.

Returns:

  • (String|nil)

    The formatted title.


6
7
8
9
# File 'app/helpers/course/discussion/topics_helper.rb', line 6

def topics_title
  # We don't want to return a blank string so this check is necessary.
  @settings.title ? format_inline_text(@settings.title) : nil
end