codecombat/app/views/courses/HourOfCodeView.coffee

110 lines
4.3 KiB
CoffeeScript
Raw Normal View History

2015-11-03 19:41:06 -05:00
app = require 'core/application'
AuthModal = require 'views/core/AuthModal'
CocoCollection = require 'collections/CocoCollection'
Course = require 'models/Course'
CourseInstance = require 'models/CourseInstance'
RootView = require 'views/core/RootView'
template = require 'templates/courses/hour-of-code-view'
utils = require 'core/utils'
LevelSession = require 'models/LevelSession'
Level = require 'models/Level'
ChooseLanguageModal = require 'views/courses/ChooseLanguageModal'
2015-11-20 10:52:35 -05:00
StudentLogInModal = require 'views/courses/StudentLogInModal'
StudentSignUpModal = require 'views/courses/StudentSignUpModal'
auth = require 'core/auth'
2015-11-03 19:41:06 -05:00
module.exports = class HourOfCodeView extends RootView
id: 'hour-of-code-view'
template: template
2015-11-09 19:45:12 -05:00
events:
2015-12-04 14:31:49 -05:00
'click #continue-playing-btn': 'onClickContinuePlayingButton'
'click #start-new-game-btn': 'onClickStartNewGameButton'
2015-11-20 10:52:35 -05:00
'click #log-in-btn': 'onClickLogInButton'
'click #log-out-link': 'onClickLogOutLink'
2015-11-03 19:41:06 -05:00
initialize: ->
2015-11-09 19:45:12 -05:00
@setUpHourOfCode()
@courseInstances = new CocoCollection([], { url: "/db/user/#{me.id}/course_instances", model: CourseInstance})
@listenToOnce @courseInstances, 'sync', @onCourseInstancesLoaded
@courseInstances.comparator = (ci) -> return ci.get('classroomID') + ci.get('courseID')
@supermodel.loadCollection(@courseInstances, 'course_instances', { cache: false })
2015-11-09 19:45:12 -05:00
onCourseInstancesLoaded: ->
@hourOfCodeCourseInstance = @courseInstances.findWhere({hourOfCode: true})
if @hourOfCodeCourseInstance
@sessions = new CocoCollection([], {
url: "/db/course_instance/#{@hourOfCodeCourseInstance.id}/level_sessions"
model: LevelSession
})
@sessions.comparator = 'created'
@listenTo @sessions, 'sync', @onSessionsLoaded
@supermodel.loadCollection(@sessions, 'sessions', { cache: false })
onSessionsLoaded: ->
@lastSession = @sessions.last()
if @lastSession
@lastLevel = new Level()
levelData = @lastSession.get('level')
2016-02-18 11:44:40 -05:00
@supermodel.loadModel(@lastLevel, {
url: "/db/level/#{levelData.original}/version/#{levelData.majorVersion}"
data: {
project: 'name,slug'
}
})
2015-11-09 19:45:12 -05:00
setUpHourOfCode: ->
# If we haven't tracked this player as an hourOfCode player yet, and it's a new account, we do that now.
elapsed = new Date() - new Date(me.get('dateCreated'))
if not me.get('hourOfCode') and (elapsed < 5 * 60 * 1000 or me.get('anonymous'))
2015-11-09 19:45:12 -05:00
me.set('hourOfCode', true)
me.patch()
$('body').append($('<img src="https://code.org/api/hour/begin_codecombat.png" style="visibility: hidden;">'))
2015-12-04 14:31:49 -05:00
window.tracker?.trackEvent 'Hour of Code Begin'
onClickContinuePlayingButton: ->
url = @continuePlayingLink()
window.tracker?.trackEvent 'HoC continue playing ', category: 'HoC', label: url
app.router.navigate(url, { trigger: true })
2015-11-09 19:45:12 -05:00
afterRender: ->
super()
@onClickStartNewGameButton() if @getQueryVariable('go') and not @lastLevel
onClickStartNewGameButton: ->
2015-12-04 14:31:49 -05:00
# User without hour of code course instance, creates one, starts playing
modal = new ChooseLanguageModal({
logoutFirst: @hourOfCodeCourseInstance?
})
@openModalView(modal)
@listenToOnce modal, 'set-language', @startHourOfCodePlay
2015-12-04 14:31:49 -05:00
window.tracker?.trackEvent 'Start New Game', category: 'HoC', label: 'HoC Start New Game'
continuePlayingLink: ->
ci = @hourOfCodeCourseInstance
"/play/level/#{@lastLevel.get('slug')}?course=#{ci.get('courseID')}&course-instance=#{ci.id}"
2015-11-09 19:45:12 -05:00
startHourOfCodePlay: ->
@$('#main-content').hide()
@$('#begin-hoc-area').removeClass('hide')
hocCourseInstance = new CourseInstance()
hocCourseInstance.upsertForHOC({cache: false})
@listenToOnce hocCourseInstance, 'sync', ->
url = hocCourseInstance.firstLevelURL()
document.location.href = url
2015-11-20 10:52:35 -05:00
onClickLogInButton: ->
modal = new StudentLogInModal()
@openModalView(modal)
modal.on 'want-to-create-account', @onWantToCreateAccount, @
2015-12-04 14:31:49 -05:00
window.tracker?.trackEvent 'Started Login', category: 'HoC', label: 'HoC Login'
2015-11-20 10:52:35 -05:00
onWantToCreateAccount: ->
modal = new StudentSignUpModal()
@openModalView(modal)
2015-12-04 14:31:49 -05:00
window.tracker?.trackEvent 'Started Signup', category: 'HoC', label: 'HoC Sign Up'
onClickLogOutLink: ->
2015-12-04 14:31:49 -05:00
window.tracker?.trackEvent 'Log Out', category: 'HoC', label: 'HoC Log Out'
auth.logoutUser()