Class: ThinkFeelDoEngine::Reports::VideoSession

Inherits:
Object
  • Object
show all
Defined in:
app/models/think_feel_do_engine/reports/video_session.rb

Overview

Scenario: a Participant plays a video.

Class Method Summary collapse

Class Method Details

.allObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/think_feel_do_engine/reports/video_session.rb', line 11

def self.all
  Participant.not_moderator.select(:id, :study_id).map do |participant|
    video_play_events(participant.id).map do |e|
      m = e.current_url.match(%r{.*\/providers\/(\d+)\/(\d+)$})
      provider_id = m ? m[1] : -1
      position = m ? m[2] : -1
      video = BitCore::ContentProvider
              .find_by(id: provider_id)
              .try(:source_content)
              .try(:slides)
              .try(:find_by_position, position)
      next_e = next_event(e)

      {
        participant_id: participant.study_id,
        video_title: video.try(:title),
        video_started_at: e.emitted_at.iso8601,
        video_stopped_at: next_e.try(:emitted_at).try(:iso8601),
        stopping_action: next_e.try(:kind)
      }
    end
  end.flatten
end

.columnsObject



6
7
8
9
# File 'app/models/think_feel_do_engine/reports/video_session.rb', line 6

def self.columns
  %w( participant_id video_title video_started_at video_stopped_at
      stopping_action )
end

.next_event(event) ⇒ Object

Return the immediately following Event for the Participant that is not a video play event, or nil if none exist.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/think_feel_do_engine/reports/video_session.rb', line 44

def self.next_event(event)
  event = EventCapture::Event.next_event_for(event)

  return if event.nil?
  return event if event && event.kind != "videoPause"

  # if the event is a pause and there's an immediately following finish,
  # return the finish event
  finish_event = EventCapture::Event
                 .where(participant_id: event.participant_id)
                 .where("emitted_at > ? AND emitted_at < ?",
                        event.emitted_at,
                        event.emitted_at + 2.seconds)
                 .order(:emitted_at)
                 .limit(1)
                 .first

  finish_event.try(:kind) == "videoFinish" ? finish_event : event
end

.video_play_events(participant_id) ⇒ Object



35
36
37
38
39
40
# File 'app/models/think_feel_do_engine/reports/video_session.rb', line 35

def self.video_play_events(participant_id)
  EventCapture::Event
    .where(participant_id: participant_id, kind: "videoPlay")
    .select(:participant_id, :kind, :emitted_at, :payload)
    .order(:emitted_at)
end