mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
When a student joins a classroom, they also join all course instances for that classroom which are free to join
This commit is contained in:
parent
91b8f9bb40
commit
f7a41dc91a
3 changed files with 49 additions and 17 deletions
|
@ -27,8 +27,11 @@ module.exports = class StudentCoursesView extends RootView
|
|||
@supermodel.loadCollection(@classrooms, 'classrooms', { data: {memberID: me.id} })
|
||||
@courses = new CocoCollection([], { url: "/db/course", model: Course})
|
||||
@supermodel.loadCollection(@courses, 'courses')
|
||||
|
||||
onLoaded: ->
|
||||
if (@classCode = utils.getQueryVariable('_cc', false)) and not me.isAnonymous()
|
||||
@joinClass()
|
||||
super()
|
||||
|
||||
onClickJoinClassButton: (e) ->
|
||||
return @openModalView new AuthModal() if me.isAnonymous()
|
||||
|
@ -57,19 +60,32 @@ module.exports = class StudentCoursesView extends RootView
|
|||
})
|
||||
|
||||
onJoinClassroomSuccess: (data, textStatus, jqxhr) ->
|
||||
# application.tracker?.trackEvent 'Redeemed course prepaid code', {prepaidCode: prepaidID}
|
||||
# me.fetch(cache: false).always =>
|
||||
# if data?.length > 0 && data[0].courseID && data[0]._id
|
||||
# courseID = data[0].courseID
|
||||
# courseInstanceID = data[0]._id
|
||||
# route = "/courses/#{courseID}/#{courseInstanceID}"
|
||||
# viewArgs = [{}, courseID, courseInstanceID]
|
||||
# Backbone.Mediator.publish 'router:navigate',
|
||||
# route: route
|
||||
# viewClass: 'views/courses/CourseDetailsView'
|
||||
# viewArgs: viewArgs
|
||||
# else
|
||||
# @state = 'unknown_error'
|
||||
# @stateMessage = "Database error."
|
||||
# @render?()
|
||||
|
||||
classroom = new Classroom(data)
|
||||
application.tracker?.trackEvent 'Joined classroom', {
|
||||
classroomID: classroom.id,
|
||||
classroomName: classroom.get('name')
|
||||
ownerID: classroom.get('ownerID')
|
||||
}
|
||||
@classrooms.add(classroom)
|
||||
@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 $.ajax({
|
||||
method: 'POST'
|
||||
url: _.result(courseInstance, 'url') + '/members'
|
||||
data: { userID: me.id }
|
||||
context: @
|
||||
success: (data) ->
|
||||
@courseInstances.add(data)
|
||||
})
|
||||
$.when(jqxhrs...).done =>
|
||||
@state = ''
|
||||
@render()
|
||||
|
|
|
@ -31,6 +31,12 @@ ClassroomSchema.pre('save', (next) ->
|
|||
next()
|
||||
)
|
||||
|
||||
ClassroomSchema.methods.isOwner = (userID) ->
|
||||
return userID.equals(@get('ownerID'))
|
||||
|
||||
ClassroomSchema.methods.isMember = (userID) ->
|
||||
return _.any @get('members') or [], (memberID) -> userID.equals(memberID)
|
||||
|
||||
ClassroomSchema.statics.jsonSchema = jsonSchema
|
||||
|
||||
module.exports = Classroom = mongoose.model 'classroom', ClassroomSchema, 'classrooms'
|
||||
|
|
|
@ -51,7 +51,9 @@ CourseInstanceHandler = class CourseInstanceHandler extends Handler
|
|||
return @sendForbiddenError(res) unless _.any(classroom.get('members'), (memberID) -> memberID.toString() is userID)
|
||||
ownsCourseInstance = courseInstance.get('ownerID').equals(req.user.get('_id'))
|
||||
addingSelf = userID is req.user.id
|
||||
return @sendForbiddenError(res) unless ownsCourseInstance or addingSelf
|
||||
return @sendForbiddenError(res) unless ownsCourseInstance or addingSelf
|
||||
alreadyInCourseInstance = _.any courseInstance.get('members') or [], (memberID) -> memberID.toString() is userID
|
||||
return @sendSuccess(res, @formatEntity(req, courseInstance)) if alreadyInCourseInstance
|
||||
Prepaid.find({ 'redeemers.userID': mongoose.Types.ObjectId(userID) }).count (err, userIsPrepaid) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
Course.findById courseInstance.get('courseID'), (err, course) =>
|
||||
|
@ -193,6 +195,14 @@ CourseInstanceHandler = class CourseInstanceHandler extends Handler
|
|||
CourseInstance.find {members: mongoose.Types.ObjectId(memberID)}, (err, courseInstances) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
return @sendSuccess(res, (@formatEntity(req, courseInstance) for courseInstance in courseInstances))
|
||||
else if classroomID = req.query.classroomID
|
||||
return @sendForbiddenError(res) unless req.user
|
||||
return @sendBadInputError(res, 'Bad memberID') unless utils.isID classroomID
|
||||
Classroom.findById classroomID, (err, classroom) =>
|
||||
return @sendForbiddenError(res) unless classroom.isMember(req.user._id) or classroom.isOwner(req.user._id)
|
||||
CourseInstance.find {classroomID: mongoose.Types.ObjectId(classroomID)}, (err, courseInstances) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
return @sendSuccess(res, (@formatEntity(req, courseInstance) for courseInstance in courseInstances))
|
||||
else
|
||||
super(arguments...)
|
||||
|
||||
|
|
Loading…
Reference in a new issue