define [
  'analytics/compiled/BaseData'
  'timezone'
], (BaseData, tz) ->

  ##
  # Loads the participation data for the student and course. Exposes the data
  # as the 'pageViews' and 'participations' properties once loaded.
  class ParticipationData extends BaseData
    constructor: (student) ->
      course = student.get('course')
      super "/api/v1/courses/#{course.get 'id'}/analytics/users/#{student.get 'id'}/activity"

    populate: (data) ->
      @bins = []

      # maintain one unique bin per date, order of insertion into @bins
      # unimportant
      binMap = {}
      binFor = (date) =>
        if !binMap[date]?
          binMap[date] =
            date: date
            views: 0
            participations: 0
            participation_events: []
          @bins.push binMap[date]
        binMap[date]

      # sort the page view data to the appropriate bins
      for date, views of data.page_views
        # this date is the hour for the bin
        bin = binFor(Date.parse date)
        bin.views += views

      # sort the participation date to the appropriate bins
      for event in data.participations
        event.createdAt = Date.parse event.created_at
        # bin to the hour corresponding to event.createdAt, so that all
        # participations fall in the same bin as their respective page views.
        bin = binFor(tz.parse(tz.format(event.createdAt, '%F %H:00:00')))
        bin.participation_events.push event
        bin.participations += 1
define [
  'analytics/compiled/BaseData'
  'timezone'
], (BaseData, tz) ->

  ##
  # Loads the participation data for the student and course. Exposes the data
  # as the 'pageViews' and 'participations' properties once loaded.
  class ParticipationData extends BaseData
    constructor: (student) ->
      course = student.get('course')
      super "/api/v1/courses/#{course.get 'id'}/analytics/users/#{student.get 'id'}/activity"

    populate: (data) ->
      @bins = []

      # maintain one unique bin per date, order of insertion into @bins
      # unimportant
      binMap = {}
      binFor = (date) =>
        if !binMap[date]?
          binMap[date] =
            date: date
            views: 0
            participations: 0
            participation_events: []
          @bins.push binMap[date]
        binMap[date]

      # sort the page view data to the appropriate bins
      for date, views of data.page_views
        # this date is the day for the bin
        bin = binFor(Date.parse date)
        bin.views += views

      # sort the participation date to the appropriate bins
      for event in data.participations
        event.createdAt = Date.parse event.created_at
        # bin to the day corresponding to event.createdAt, so that all
        # participations fall in the same bin as their respective page views.
        bin = binFor(new Date(event.createdAt.setHours(0,0,0,0)))
        bin.participation_events.push event
        bin.participations += 1
Fix merging of participation and page views
Fix merging of participation and page views

the binning of page views and participation from the api into dates is
fixed.

closes ADMIN-1299

note: there was a change in this file from binning by day -> hour to fix
a bug that looks like was a timezone issue. See CNVS-16464 and
https://gerrit.instructure.com/#/c/44111/.  I believe this sort of
addressed the issue at the time, but was not correct.

test plan:
  - have a course with a student with page views and participation data
    on the same day (you could run @jeremy's fake_analytics.rb script to
    generate randomish data and tweak the results to get dupes)
  - go to /courses/:couse_id/analytics/users/:user_id
  - click the toggle to switch to table view
  > expect the activity by date table to have combined the views and
    participation into the same date. There should be no duplicate
    dates in the table.

Change-Id: Iff76a43c0a7c0a1d3dd5082a6bbde18407916f32
Reviewed-on: https://gerrit.instructure.com/161479
Tested-by: Jenkins
Reviewed-by: Mysti Sadler <431d062aff1e8f25416b3a7b732fe6effdb03f9a@instructure.com>
Reviewed-by: Jeremy Stanley <b3f594e10a9edcf5413cf1190121d45078c62290@instructure.com>
Reviewed-by: Carl Kibler <7c2b40129dd885a147d5b2dda56dda016b9a8f57@instructure.com>
QA-Review: Jeremy Stanley <b3f594e10a9edcf5413cf1190121d45078c62290@instructure.com>
Product-Review: Ed Schiebel <0ca8a83847a5889cf05cb219c0ab09f6b646243b@instructure.com>
