2014-12-23 09:42:24 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-12-29 15:46:14 -05:00
|
|
|
Level = require 'models/Level'
|
2014-12-23 09:42:24 -05:00
|
|
|
|
|
|
|
module.exports = class CampaignLevelView extends CocoView
|
|
|
|
id: 'campaign-level-view'
|
|
|
|
template: require 'templates/editor/campaign/campaign-level-view'
|
2014-12-29 15:46:14 -05:00
|
|
|
|
2014-12-23 09:42:24 -05:00
|
|
|
events:
|
|
|
|
'click .close': 'onClickClose'
|
2014-12-29 15:46:14 -05:00
|
|
|
|
2014-12-23 09:42:24 -05:00
|
|
|
constructor: (options, @level) ->
|
|
|
|
super(options)
|
2014-12-29 15:46:14 -05:00
|
|
|
@fullLevel = new Level _id: @level.id
|
|
|
|
@fullLevel.fetch()
|
|
|
|
@listenToOnce @fullLevel, 'sync', => @render?()
|
|
|
|
|
2014-12-31 14:49:17 -05:00
|
|
|
@levelSlug = @level.get('slug')
|
|
|
|
@getLevelCompletions()
|
|
|
|
|
2014-12-23 09:42:24 -05:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
2014-12-29 15:46:14 -05:00
|
|
|
c.level = if @fullLevel.loaded then @fullLevel else @level
|
2014-12-31 14:49:17 -05:00
|
|
|
c.levelCompletions = @levelCompletions
|
2014-12-23 09:42:24 -05:00
|
|
|
c
|
|
|
|
|
|
|
|
onClickClose: ->
|
2014-12-29 15:46:14 -05:00
|
|
|
@$el.addClass('hidden')
|
|
|
|
@trigger 'hidden'
|
2014-12-31 14:49:17 -05:00
|
|
|
|
|
|
|
getLevelCompletions: ->
|
|
|
|
# Fetch last 7 days of level completion counts
|
|
|
|
success = (data) =>
|
|
|
|
return if @destroyed
|
|
|
|
data.sort (a, b) -> if a.created < b.created then 1 else -1
|
|
|
|
mapFn = (item) ->
|
|
|
|
item.rate = (item.finished / item.started * 100).toFixed(2)
|
|
|
|
item
|
|
|
|
@levelCompletions = _.map data, mapFn, @
|
|
|
|
@render()
|
|
|
|
|
|
|
|
startDay = new Date()
|
|
|
|
startDay.setDate(startDay.getUTCDate() - 6)
|
|
|
|
startDay = startDay.getUTCFullYear() + '-' + (startDay.getUTCMonth() + 1) + '-' + startDay.getUTCDate()
|
|
|
|
|
|
|
|
# TODO: Why do we need this url dash?
|
|
|
|
request = @supermodel.addRequestResource 'level_completions', {
|
|
|
|
url: '/db/analytics_log_event/-/level_completions'
|
|
|
|
data: {startDay: startDay, slugs: [@levelSlug]}
|
|
|
|
method: 'POST'
|
|
|
|
success: success
|
|
|
|
}, 0
|
|
|
|
request.load()
|