codecombat/app/views/courses/CoursesView.coffee

150 lines
5.8 KiB
CoffeeScript
Raw Normal View History

2015-09-10 13:37:32 -04:00
app = require 'core/application'
RootView = require 'views/core/RootView'
2015-11-03 19:41:06 -05:00
template = require 'templates/courses/courses-view'
AuthModal = require 'views/core/AuthModal'
CreateAccountModal = require 'views/core/CreateAccountModal'
ChangeCourseLanguageModal = require 'views/courses/ChangeCourseLanguageModal'
ChooseLanguageModal = require 'views/courses/ChooseLanguageModal'
CourseInstance = require 'models/CourseInstance'
CocoCollection = require 'collections/CocoCollection'
Course = require 'models/Course'
Classroom = require 'models/Classroom'
Classrooms = require 'collections/Classrooms'
LevelSession = require 'models/LevelSession'
Campaign = require 'models/Campaign'
utils = require 'core/utils'
# TODO: Test everything
2015-08-29 10:15:35 -04:00
module.exports = class CoursesView extends RootView
id: 'courses-view'
template: template
events:
'click #log-in-btn': 'onClickLogInButton'
'click #start-new-game-btn': 'openSignUpModal'
'click #join-class-btn': 'onClickJoinClassButton'
'submit #join-class-form': 'onSubmitJoinClassForm'
'click #change-language-link': 'onClickChangeLanguageLink'
2015-12-04 15:08:08 -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')
@listenToOnce @courseInstances, 'sync', @onCourseInstancesLoaded
@supermodel.loadCollection(@courseInstances)
@classrooms = new CocoCollection([], { url: "/db/classroom", model: Classroom })
@supermodel.loadCollection(@classrooms, { data: {memberID: me.id} })
@ownedClassrooms = new Classrooms()
@ownedClassrooms.fetchMine({data: {project: '_id'}})
@supermodel.trackCollection(@ownedClassrooms)
@courses = new CocoCollection([], { url: "/db/course", model: Course})
@supermodel.loadCollection(@courses)
onCourseInstancesLoaded: ->
map = {}
for courseInstance in @courseInstances.models
courseID = courseInstance.get('courseID')
if map[courseID]
courseInstance.sessions = map[courseID]
continue
map[courseID] = courseInstance.sessions = new CocoCollection([], {
url: courseInstance.url() + '/my-course-level-sessions',
2015-12-04 15:08:08 -05:00
model: LevelSession
})
courseInstance.sessions.comparator = 'changed'
@supermodel.loadCollection(courseInstance.sessions, { data: { project: 'state.complete level.original playtime changed' }})
2015-12-04 15:08:08 -05:00
2016-04-13 12:54:24 -04:00
hocCourseInstance = @courseInstances.findWhere({hourOfCode: true})
if hocCourseInstance
@courseInstances.remove(hocCourseInstance)
2015-12-04 15:08:08 -05:00
onLoaded: ->
2015-11-29 18:20:38 -05:00
super()
if utils.getQueryVariable('_cc', false) and not me.isAnonymous()
@joinClass()
onClickLogInButton: ->
modal = new AuthModal()
@openModalView(modal)
2015-12-04 15:08:08 -05:00
application.tracker?.trackEvent 'Started Student Login', category: 'Courses'
openSignUpModal: ->
modal = new CreateAccountModal()
@openModalView(modal)
2015-12-04 15:08:08 -05:00
application.tracker?.trackEvent 'Started Student Signup', category: 'Courses'
onSubmitJoinClassForm: (e) ->
e.preventDefault()
@joinClass()
2015-12-04 15:08:08 -05:00
onClickJoinClassButton: (e) ->
@joinClass()
joinClass: ->
return if @state
@state = 'enrolling'
@errorMessage = null
@classCode = @$('#class-code-input').val() or utils.getQueryVariable('_cc', false)
if not @classCode
@state = null
@errorMessage = 'Please enter a code.'
@renderSelectors '#join-class-form'
return
@renderSelectors '#join-class-form'
newClassroom = new Classroom()
newClassroom.joinWithCode(@classCode)
newClassroom.on 'sync', @onJoinClassroomSuccess, @
newClassroom.on 'error', @onJoinClassroomError, @
onJoinClassroomError: (classroom, jqxhr, options) ->
@state = null
2015-12-04 15:08:08 -05:00
application.tracker?.trackEvent 'Failed to join classroom with code', category: 'Courses', status: jqxhr.status
if jqxhr.status is 422
@errorMessage = 'Please enter a code.'
else if jqxhr.status is 404
@errorMessage = 'Code not found.'
else
@errorMessage = "#{jqxhr.responseText}"
2015-12-04 15:08:08 -05:00
@renderSelectors '#join-class-form'
Stuff Partially fix ActivateLicensesModal.spec [IN PROGRESS] Don't display deleted users Move userID to classroom.deletedMembers on user delete (not retroactive) Fix PDF links for course guides, remove old PDFs from repo Remove deprecated SalesView Remove underline for not-yet-linked student names Only show class select when there's more than one Ignore case when sorting student names Use student.broadName instead of name for display and sorting Fix initial load not showing progress after joining a course (hacky) Fix text entry for enrollment number input Fix enrollment statistics Fix enrollment stats completely (and add back in per-class unenrolled count) Add deletedMembers to classroom schema More fixes to enrollment stats (don't count nonmember prepaids) Don't use 0 as implicit false for openSpots Update suggested number of credit to buy automatically Fix classroom edit form ignoring cleared values Add alert text when more users selected than enrollments available Alert user when trying to assign course to unenrolled students Alert user when assigning course to nobody Add some tests for TeacherClassView bulk assign alerts Fix TeacherClassView tests failing without demos Use model/collection.fakeRequests :D Remove unused comment Fix handling of improperly sorted deleted users on clientside Add test for moving deleted users to deletedMembers Add script for moving all deleted classroom members to classroom.deletedMembers Completely rewrite tallying up enrollment statistics Fix some tests to not be dependent on logged-in user Address PR comments Fix default number of enrollments to buy Fix i18n for not enough enrollments Use custom error message for classroom name length
2016-04-07 17:55:42 -04:00
onJoinClassroomSuccess: (newClassroom, data, options) ->
application.tracker?.trackEvent 'Joined classroom', {
2015-12-04 15:08:08 -05:00
category: 'Courses'
classCode: @classCode
classroomID: newClassroom.id
classroomName: newClassroom.get('name')
ownerID: newClassroom.get('ownerID')
}
@classrooms.add(newClassroom)
@render()
@classroomJustAdded = newClassroom.id
2015-12-04 15:08:08 -05:00
classroomCourseInstances = new CocoCollection([], { url: "/db/course_instance", model: CourseInstance })
classroomCourseInstances.fetch({ data: {classroomID: newClassroom.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)
courseInstance.sessions = new Backbone.Collection()
@courseInstances.add(courseInstance)
$.when(jqxhrs...).done =>
Stuff Partially fix ActivateLicensesModal.spec [IN PROGRESS] Don't display deleted users Move userID to classroom.deletedMembers on user delete (not retroactive) Fix PDF links for course guides, remove old PDFs from repo Remove deprecated SalesView Remove underline for not-yet-linked student names Only show class select when there's more than one Ignore case when sorting student names Use student.broadName instead of name for display and sorting Fix initial load not showing progress after joining a course (hacky) Fix text entry for enrollment number input Fix enrollment statistics Fix enrollment stats completely (and add back in per-class unenrolled count) Add deletedMembers to classroom schema More fixes to enrollment stats (don't count nonmember prepaids) Don't use 0 as implicit false for openSpots Update suggested number of credit to buy automatically Fix classroom edit form ignoring cleared values Add alert text when more users selected than enrollments available Alert user when trying to assign course to unenrolled students Alert user when assigning course to nobody Add some tests for TeacherClassView bulk assign alerts Fix TeacherClassView tests failing without demos Use model/collection.fakeRequests :D Remove unused comment Fix handling of improperly sorted deleted users on clientside Add test for moving deleted users to deletedMembers Add script for moving all deleted classroom members to classroom.deletedMembers Completely rewrite tallying up enrollment statistics Fix some tests to not be dependent on logged-in user Address PR comments Fix default number of enrollments to buy Fix i18n for not enough enrollments Use custom error message for classroom name length
2016-04-07 17:55:42 -04:00
# This is a hack to work around previous hacks
# TODO: Do joinWithCode properly (before page load)
# TODO: Do data flow properly (so going to the class URL works and we don't need to just refresh)
location.search = ""
# @state = null
# @render()
# location.hash = ''
# f = -> location.hash = '#just-added-text'
# # quick and dirty scroll to just-added classroom
# setTimeout(f, 10)
onClickChangeLanguageLink: ->
2015-12-04 15:08:08 -05:00
application.tracker?.trackEvent 'Student clicked change language', category: 'Courses'
modal = new ChangeCourseLanguageModal()
@openModalView(modal)
2015-12-04 15:08:08 -05:00
modal.once 'hidden', @render, @