Class: ThinkFeelDoEngine::Reports::ModuleSession

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

Overview

Scenario: A Participant starts using a Tool Module.

Constant Summary collapse

THRESHOLD =
5.minutes

Constants included from ToolModule

ToolModule::URL_ROOT_RE

Class Method Summary collapse

Methods included from ToolModule

included

Class Method Details

.allObject



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

def self.all
  all_module_interactions
    .group_by { |i| i[:participant_id] }
    .map do |_participant_id, interactions|
    # condense adjacent interactions
    filtered_interactions = [nice_times(interactions.first.clone)].compact
    interactions.each_with_index do |interaction, i|
      previous_interaction = interactions[i - 1] || {}
      next if previous_interaction[:module_id] == interaction[:module_id]

      filtered_interaction = interaction.clone

      (i + 1..interactions.length - 1).each do |j|
        next_interaction = interactions[j]
        break if next_interaction[:module_id] != interaction[:module_id]

        next unless next_interaction[:last_page_opened_at] -
                    filtered_interaction[:last_page_opened_at] < THRESHOLD

        filtered_interaction[:last_page_opened_at] =
          next_interaction[:last_page_opened_at]
      end

      filtered_interactions << nice_times(filtered_interaction)
    end

    filtered_interactions.uniq
  end.flatten
end

.all_module_interactionsObject

Returns all events that represent a viewing (render) of a Content Module.



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

def self.all_module_interactions
  modules = modules_map.keys.each_with_object({}) do |key, h|
    if Task.exists?(bit_core_content_module_id: modules_map[key].id)
      h[key] = modules_map[key]
    end
  end

  Participant.not_moderator.select(:id, :study_id).map do |participant|
    events = time_sorted_render_events_for(participant)

    events.map do |e|
      content_module = modules[e.current_url.gsub(URL_ROOT_RE, "")]

      next unless content_module

      last_page_opened = last_page_opened(events, e, content_module.id)

      {
        participant_id: participant.study_id,
        module_id: content_module.id,
        page_headers: e.headers,
        module_selected_at: e.emitted_at,
        last_page_opened_at: last_page_opened[:opened_at],
        did_complete: last_page_opened[:is_last_module_page]
      }
    end.compact
  end.flatten
end

.columnsObject



10
11
12
13
# File 'app/models/think_feel_do_engine/reports/module_session.rb', line 10

def self.columns
  %w( participant_id module_id page_headers module_selected_at
      last_page_opened_at did_complete )
end

.last_module_page?(url, module_id) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/think_feel_do_engine/reports/module_session.rb', line 112

def self.last_module_page?(url, module_id)
  # last provider within module
  last_provider = BitCore::ContentModule.find(module_id)
                                        .content_providers
                                        .order(:position)
                                        .last

  if last_provider.nil?
    false
  else
    provider_re = "modules\/#{module_id}\/" \
                  "providers\/#{last_provider.id}(\/.*)?$"

    !url.match(/#{ provider_re }/).nil?
  end
end

.last_page_opened(events, first_session_event, module_id) ⇒ Object

last event (click) with matching module id



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/think_feel_do_engine/reports/module_session.rb', line 84

def self.last_page_opened(events, first_session_event, module_id)
  latest_event_time = first_session_event.emitted_at
  module_events =
    events
    .drop_while { |e| e.id != first_session_event.id }
    .take_while do |e|
      e.emitted_at - latest_event_time < THRESHOLD &&
        e.current_url.match(%r{modules\/#{ module_id }(\/.*)?$}) &&
        (latest_event_time = e.emitted_at)
    end
  last_event = (module_events.last || first_session_event)

  {
    opened_at: last_event.emitted_at,
    is_last_module_page: last_module_page?(last_event.current_url,
                                           module_id)
  }
end

.nice_times(interaction) ⇒ Object



103
104
105
106
107
108
109
110
# File 'app/models/think_feel_do_engine/reports/module_session.rb', line 103

def self.nice_times(interaction)
  interaction[:module_selected_at] =
    interaction[:module_selected_at].iso8601
  interaction[:last_page_opened_at] =
    interaction[:last_page_opened_at].iso8601

  interaction
end

.time_sorted_render_events_for(participant) ⇒ Object



76
77
78
79
80
81
# File 'app/models/think_feel_do_engine/reports/module_session.rb', line 76

def self.time_sorted_render_events_for(participant)
  EventCapture::Event
    .where(participant_id: participant.id, kind: "render")
    .select(:id, :participant_id, :emitted_at, :payload, :kind)
    .to_a.sort { |a, b| a.emitted_at <=> b.emitted_at }
end