2015-11-03 19:41:06 -05:00
|
|
|
app = require 'core/application'
|
2016-02-25 18:24:16 -05:00
|
|
|
CreateAccountModal = require 'views/core/CreateAccountModal'
|
2015-11-03 19:41:06 -05:00
|
|
|
CocoCollection = require 'collections/CocoCollection'
|
|
|
|
Course = require 'models/Course'
|
2015-11-05 20:31:11 -05:00
|
|
|
Classroom = require 'models/Classroom'
|
|
|
|
User = require 'models/User'
|
2015-11-03 19:41:06 -05:00
|
|
|
CourseInstance = require 'models/CourseInstance'
|
|
|
|
RootView = require 'views/core/RootView'
|
|
|
|
template = require 'templates/courses/student-courses-view'
|
|
|
|
utils = require 'core/utils'
|
|
|
|
|
|
|
|
# TODO: Implement join class
|
|
|
|
# TODO: Implement course instance links
|
|
|
|
|
|
|
|
module.exports = class StudentCoursesView extends RootView
|
|
|
|
id: 'student-courses-view'
|
|
|
|
template: template
|
|
|
|
|
|
|
|
events:
|
|
|
|
'click #join-class-btn': 'onClickJoinClassButton'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super(options)
|
|
|
|
@courseInstances = new CocoCollection([], { url: "/db/user/#{me.id}/course_instances", model: CourseInstance})
|
2015-11-10 15:08:44 -05:00
|
|
|
@courseInstances.comparator = (ci) -> return ci.get('classroomID') + ci.get('courseID')
|
2015-11-03 19:41:06 -05:00
|
|
|
@supermodel.loadCollection(@courseInstances, 'course_instances')
|
2015-11-05 20:31:11 -05:00
|
|
|
@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-22 10:47:28 -05:00
|
|
|
|
2015-11-09 16:29:49 -05:00
|
|
|
onLoaded: ->
|
2015-11-04 19:54:13 -05:00
|
|
|
if (@classCode = utils.getQueryVariable('_cc', false)) and not me.isAnonymous()
|
|
|
|
@joinClass()
|
2015-11-09 16:29:49 -05:00
|
|
|
super()
|
2015-11-04 19:54:13 -05:00
|
|
|
|
|
|
|
onClickJoinClassButton: (e) ->
|
2016-02-25 18:24:16 -05:00
|
|
|
return @openModalView new CreateAccountModal() if me.isAnonymous()
|
2015-11-04 19:54:13 -05:00
|
|
|
@classCode = @$('#classroom-code-input').val()
|
|
|
|
@joinClass()
|
|
|
|
|
|
|
|
joinClass: () ->
|
|
|
|
@state = 'enrolling'
|
|
|
|
@renderSelectors '#join-classroom-form'
|
|
|
|
$.ajax({
|
|
|
|
method: 'POST'
|
|
|
|
url: '/db/classroom/-/members'
|
|
|
|
data: code: @classCode
|
|
|
|
context: @
|
|
|
|
success: @onJoinClassroomSuccess
|
|
|
|
error: (jqxhr, textStatus, errorThrown) ->
|
|
|
|
application.tracker?.trackEvent 'Failed to join classroom with code', status: textStatus
|
|
|
|
@state = 'unknown_error'
|
|
|
|
if jqxhr.status is 422
|
|
|
|
@stateMessage = 'Please enter a code.'
|
|
|
|
else if jqxhr.status is 404
|
|
|
|
@stateMessage = 'Code not found.'
|
|
|
|
else
|
|
|
|
@stateMessage = "#{jqxhr.responseText}"
|
|
|
|
@renderSelectors '#join-classroom-form'
|
|
|
|
})
|
|
|
|
|
|
|
|
onJoinClassroomSuccess: (data, textStatus, jqxhr) ->
|
2015-11-09 16:29:49 -05:00
|
|
|
classroom = new Classroom(data)
|
|
|
|
application.tracker?.trackEvent 'Joined classroom', {
|
2015-11-22 10:47:28 -05:00
|
|
|
classroomID: classroom.id,
|
2015-11-09 16:29:49 -05:00
|
|
|
classroomName: classroom.get('name')
|
|
|
|
ownerID: classroom.get('ownerID')
|
|
|
|
}
|
|
|
|
@classrooms.add(classroom)
|
|
|
|
@render()
|
2015-11-22 10:47:28 -05:00
|
|
|
|
2015-11-09 16:29:49 -05:00
|
|
|
classroomCourseInstances = new CocoCollection([], { url: "/db/course_instance", model: CourseInstance })
|
|
|
|
classroomCourseInstances.fetch({ data: {classroomID: classroom.id} })
|
|
|
|
@listenToOnce classroomCourseInstances, 'sync', ->
|
2015-11-22 10:47:28 -05:00
|
|
|
|
2015-11-09 16:29:49 -05:00
|
|
|
# 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 $.ajax({
|
|
|
|
method: 'POST'
|
|
|
|
url: _.result(courseInstance, 'url') + '/members'
|
|
|
|
data: { userID: me.id }
|
|
|
|
context: @
|
|
|
|
success: (data) ->
|
|
|
|
@courseInstances.add(data)
|
2015-11-10 15:08:44 -05:00
|
|
|
@courseInstances.get(data._id).justJoined = true
|
2015-11-09 16:29:49 -05:00
|
|
|
})
|
|
|
|
$.when(jqxhrs...).done =>
|
|
|
|
@state = ''
|
|
|
|
@render()
|