Class: ThinkFeelDoEngine::Reports::LessonSlideView

Inherits:
Object
  • Object
show all
Includes:
LessonModule
Defined in:
app/models/think_feel_do_engine/reports/lesson_slide_view.rb

Overview

Scenario: a Participant starts viewing a Slide.

Constant Summary

Constants included from LessonModule

ThinkFeelDoEngine::Reports::LessonModule::URL_ROOT_RE

Class Method Summary collapse

Methods included from LessonModule

included

Class Method Details

.allObject



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
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 12

def self.all
  interactions = all_slide_interactions

  # condense adjacent interactions
  filtered_interactions = []

  interactions.each_with_index do |interaction, i|
    previous_interaction = find_previous_interaction(interactions, i)
    next if previous_interaction.nil? ||
            interaction.nil? ||
            previous_interaction[:slide_id] == interaction[:slide_id]

    next_interaction = interactions[i + 1] || {}
    filtered_interaction = interaction.clone

    if next_interaction[:slide_id] == interaction[:slide_id]
      filtered_interaction[:slide_exited_at] =
        next_interaction[:slide_exited_at]
    end

    filtered_interactions << filtered_interaction
  end

  filtered_interactions
end

.all_slide_interactionsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 38

def self.all_slide_interactions
  lessons = lessons_map

  Participant.not_moderator.select(:id, :study_id).map do |participant|
    slide_view_events(lessons, participant.id).map do |lesson_event|
      e = lesson_event[1]
      lesson_id = lesson_event[0]
      begin
        slides = ContentModules::LessonModule.find(lesson_id).slides
        slide = find_slide_by_url(slides, e.current_url)
        {
          participant_id: participant.study_id,
          lesson_id: lesson_id,
          slide_id: slide.try(:id),
          slide_title: slide.try(:title),
          slide_selected_at: e.emitted_at.try(:iso8601),
          slide_exited_at: next_event_at(e).try(:iso8601)
        }
      rescue ActiveRecord::RecordNotFound
        lesson_module_not_found(participant, lesson_id) if defined?(Raven)
      end
    end
  end.flatten
end

.columnsObject



7
8
9
10
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 7

def self.columns
  %w( participant_id lesson_id slide_id slide_title slide_selected_at
      slide_exited_at )
end

.find_previous_interaction(interactions, index) ⇒ Object



108
109
110
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 108

def self.find_previous_interaction(interactions, index)
  index > 0 ? interactions[index - 1] : {}
end

.find_slide_by_url(slides, url) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 89

def self.find_slide_by_url(slides, url)
  slide_position = 1

  if url.match(/modules\/\d+$/).nil?
    slide_position = url[/\d+$/].to_i
  end

  slides.find_by_position(slide_position) unless slides.empty?
end

.lesson_module_not_found(participant, lesson_id) ⇒ Object



99
100
101
102
103
104
105
106
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 99

def self.lesson_module_not_found(participant, lesson_id)
  Raven.capture_message "No slides found for lesson found",
                        extra: {
                          participant_id: participant.try(:id),
                          study_id: participant.try(:study_id),
                          lesson_id: lesson_id
                        }
end

.next_event_at(event) ⇒ Object

Return the time of the Event immediately following if one exists, nil otherwise.



80
81
82
83
84
85
86
87
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 80

def self.next_event_at(event)
  EventCapture::Event
    .where("participant_id = ? AND emitted_at > ?",
           event.participant_id, event.emitted_at)
    .select(:participant_id, :emitted_at)
    .limit(1)
    .first.try(:emitted_at)
end

.slide_view_events(lessons, participant_id) ⇒ Object

Events for a Participant in which a Lesson Slide is viewed.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/think_feel_do_engine/reports/lesson_slide_view.rb', line 64

def self.slide_view_events(lessons, participant_id)
  EventCapture::Event
    .where(participant_id: participant_id, kind: %w( click render ))
    .select(:participant_id, :emitted_at, :payload)
    .order(:emitted_at)
    .to_a.map do |e|
      key = lessons.keys.find do |l|
        !e.current_url.match(/#{ l }(\/.*)?$/).nil?
      end

      key ? [lessons[key], e] : nil
    end.compact
end