2015-09-10 13:37:32 -04:00
|
|
|
app = require 'core/application'
|
2015-10-09 12:32:46 -04:00
|
|
|
AuthModal = require 'views/core/AuthModal'
|
2015-09-10 13:37:32 -04:00
|
|
|
RootView = require 'views/core/RootView'
|
2015-11-03 19:41:06 -05:00
|
|
|
template = require 'templates/courses/courses-view'
|
2015-11-21 14:38:34 -05:00
|
|
|
StudentLogInModal = require 'views/courses/StudentLogInModal'
|
|
|
|
StudentSignUpModal = require 'views/courses/StudentSignUpModal'
|
2015-11-23 12:52:15 -05:00
|
|
|
ChangeCourseLanguageModal = require 'views/courses/ChangeCourseLanguageModal'
|
2015-11-21 14:38:34 -05:00
|
|
|
CourseInstance = require 'models/CourseInstance'
|
2015-11-22 03:08:46 -05:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
Course = require 'models/Course'
|
|
|
|
Classroom = require 'models/Classroom'
|
|
|
|
LevelSession = require 'models/LevelSession'
|
2015-11-23 11:46:08 -05:00
|
|
|
Campaign = require 'models/Campaign'
|
|
|
|
|
|
|
|
# TODO: Test everything
|
2015-10-12 19:25:23 -04:00
|
|
|
|
2015-08-29 10:15:35 -04:00
|
|
|
module.exports = class CoursesView extends RootView
|
|
|
|
id: 'courses-view'
|
|
|
|
template: template
|
2015-11-21 14:38:34 -05:00
|
|
|
|
|
|
|
events:
|
|
|
|
'click #log-in-btn': 'onClickLogInButton'
|
|
|
|
'click #start-new-game-btn': 'onClickStartNewGameButton'
|
2015-11-23 11:46:08 -05:00
|
|
|
'click #join-class-btn': 'onClickJoinClassButton'
|
|
|
|
'submit #join-class-form': 'onSubmitJoinClassForm'
|
2015-11-23 12:52:15 -05:00
|
|
|
'click #change-language-link': 'onClickChangeLanguageLink'
|
2015-11-23 11:46:08 -05:00
|
|
|
|
2015-11-22 03:08:46 -05:00
|
|
|
initialize: ->
|
|
|
|
@courseInstances = new CocoCollection([], { url: "/db/user/#{me.id}/course_instances", model: CourseInstance})
|
|
|
|
@courseInstances.comparator = (ci) -> return ci.get('classroomID') + ci.get('courseID')
|
|
|
|
@supermodel.loadCollection(@courseInstances, 'course_instances')
|
|
|
|
@classrooms = new CocoCollection([], { url: "/db/classroom", model: Classroom })
|
|
|
|
@supermodel.loadCollection(@classrooms, 'classrooms', { data: {memberID: me.id} })
|
|
|
|
@courses = new CocoCollection([], { url: "/db/course", model: Course})
|
|
|
|
@supermodel.loadCollection(@courses, 'courses')
|
2015-11-23 11:46:08 -05:00
|
|
|
@campaigns = new CocoCollection([], { url: "/db/campaigns", model: Campaign })
|
|
|
|
# TODO: fetch only course campaigns
|
|
|
|
@supermodel.loadCollection(@courses, 'courses')
|
|
|
|
|
2015-11-22 03:08:46 -05:00
|
|
|
onLoaded: ->
|
2015-11-23 11:46:08 -05:00
|
|
|
for courseInstance in @courseInstances.models
|
|
|
|
# TODO: fetch sessions for given course instance
|
|
|
|
# TODO: make sure we only fetch one per courseID
|
|
|
|
courseInstance.sessions = new CocoCollection([], { url: '???' })
|
|
|
|
courseInstance.sessions.allDone = ->
|
|
|
|
# TODO: should return if all non-arena courses are complete
|
|
|
|
|
2015-11-22 03:08:46 -05:00
|
|
|
@hocCourseInstance = @courseInstances.findWhere({hourOfCode: true})
|
|
|
|
if @hocCourseInstance
|
|
|
|
@courseInstances.remove(@hocCourseInstance)
|
|
|
|
@sessions = new CocoCollection([], { url: @hocCourseInstance.url() + '/my-course-level-sessions', model: LevelSession })
|
2015-11-23 11:46:08 -05:00
|
|
|
@sessions.comparator = 'changed'
|
2015-11-22 03:08:46 -05:00
|
|
|
@supermodel.loadCollection(@sessions, 'sessions')
|
|
|
|
super()
|
|
|
|
|
2015-11-21 14:38:34 -05:00
|
|
|
onClickStartNewGameButton: ->
|
|
|
|
@openSignUpModal()
|
|
|
|
|
|
|
|
onClickLogInButton: ->
|
|
|
|
modal = new StudentLogInModal()
|
|
|
|
@openModalView(modal)
|
|
|
|
modal.on 'want-to-create-account', @openSignUpModal, @
|
|
|
|
|
|
|
|
openSignUpModal: ->
|
|
|
|
modal = new StudentSignUpModal({ willPlay: true })
|
|
|
|
@openModalView(modal)
|
|
|
|
modal.once 'click-skip-link', @startHourOfCodePlay, @
|
|
|
|
|
|
|
|
startHourOfCodePlay: ->
|
|
|
|
@$('#main-content').hide()
|
|
|
|
@$('#begin-hoc-area').removeClass('hide')
|
|
|
|
hocCourseInstance = new CourseInstance()
|
|
|
|
hocCourseInstance.upsertForHOC()
|
|
|
|
@listenToOnce hocCourseInstance, 'sync', ->
|
|
|
|
url = hocCourseInstance.firstLevelURL()
|
|
|
|
app.router.navigate(url, { trigger: true })
|
2015-11-23 11:46:08 -05:00
|
|
|
|
|
|
|
onSubmitJoinClassForm: (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
@joinClass()
|
|
|
|
|
|
|
|
onClickJoinClassButton: (e) ->
|
|
|
|
@joinClass()
|
|
|
|
|
|
|
|
joinClass: ->
|
|
|
|
@state = 'enrolling'
|
|
|
|
@classCode = @$('#classroom-code-input').val() or utils.getQueryVariable('_cc', false)
|
|
|
|
return unless @classCode
|
|
|
|
@renderSelectors '#join-classroom-form'
|
|
|
|
newClassroom = new Classroom()
|
|
|
|
newClassroom.joinWithCode(@classCode)
|
|
|
|
newClassroom.on 'sync', @onJoinClassroomSuccess, @
|
|
|
|
newClassroom.on 'error', @onJoinClassroomError, @
|
|
|
|
|
|
|
|
onJoinClassroomError: (classroom, jqxhr, options) ->
|
|
|
|
application.tracker?.trackEvent 'Failed to join classroom with code', status: jqxhr.status
|
|
|
|
@state = 'unknown_error'
|
|
|
|
if jqxhr.status is 422
|
|
|
|
@errorMessage = 'Please enter a code.'
|
|
|
|
else if jqxhr.status is 404
|
|
|
|
@errorMessage = 'Code not found.'
|
|
|
|
else
|
|
|
|
@errorMessage = "#{jqxhr.responseText}"
|
|
|
|
@renderSelectors '#join-classroom-form'
|
|
|
|
|
|
|
|
onJoinClassroomSuccess: (newClassroom, jqxhr, options) ->
|
|
|
|
application.tracker?.trackEvent 'Joined classroom', {
|
|
|
|
classroomID: newClassroom.id,
|
|
|
|
classroomName: newClassroom.get('name')
|
|
|
|
ownerID: newClassroom.get('ownerID')
|
|
|
|
}
|
|
|
|
@classrooms.add(newClassroom)
|
|
|
|
newClassroom.justAdded = true
|
|
|
|
@render()
|
|
|
|
|
|
|
|
classroomCourseInstances = new CocoCollection([], { url: "/db/course_instance", model: CourseInstance })
|
|
|
|
classroomCourseInstances.fetch({ data: {classroomID: classroom.id} })
|
|
|
|
@listenToOnce classroomCourseInstances, 'sync', ->
|
|
|
|
# join any course instances in the classroom which are free to join
|
|
|
|
jqxhrs = []
|
|
|
|
for courseInstance in classroomCourseInstances.models
|
|
|
|
course = @courses.get(courseInstance.get('courseID'))
|
|
|
|
if course.get('free')
|
|
|
|
jqxhrs.push = courseInstance.addMember(me.id)
|
|
|
|
@courseInstances.add(courseInstance)
|
|
|
|
$.when(jqxhrs...).done =>
|
|
|
|
@state = ''
|
|
|
|
@render()
|
2015-11-23 12:52:15 -05:00
|
|
|
delete newClassroom.justAdded
|
|
|
|
|
|
|
|
onClickChangeLanguageLink: ->
|
|
|
|
modal = new ChangeCourseLanguageModal()
|
|
|
|
@openModalView(modal)
|