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} })
|
@supermodel.loadCollection(@classrooms, 'classrooms', { data: {memberID: me.id} })
|
||||||
@courses = new CocoCollection([], { url: "/db/course", model: Course})
|
@courses = new CocoCollection([], { url: "/db/course", model: Course})
|
||||||
@supermodel.loadCollection(@courses, 'courses')
|
@supermodel.loadCollection(@courses, 'courses')
|
||||||
|
|
||||||
|
onLoaded: ->
|
||||||
if (@classCode = utils.getQueryVariable('_cc', false)) and not me.isAnonymous()
|
if (@classCode = utils.getQueryVariable('_cc', false)) and not me.isAnonymous()
|
||||||
@joinClass()
|
@joinClass()
|
||||||
|
super()
|
||||||
|
|
||||||
onClickJoinClassButton: (e) ->
|
onClickJoinClassButton: (e) ->
|
||||||
return @openModalView new AuthModal() if me.isAnonymous()
|
return @openModalView new AuthModal() if me.isAnonymous()
|
||||||
|
@ -57,19 +60,32 @@ module.exports = class StudentCoursesView extends RootView
|
||||||
})
|
})
|
||||||
|
|
||||||
onJoinClassroomSuccess: (data, textStatus, jqxhr) ->
|
onJoinClassroomSuccess: (data, textStatus, jqxhr) ->
|
||||||
# application.tracker?.trackEvent 'Redeemed course prepaid code', {prepaidCode: prepaidID}
|
classroom = new Classroom(data)
|
||||||
# me.fetch(cache: false).always =>
|
application.tracker?.trackEvent 'Joined classroom', {
|
||||||
# if data?.length > 0 && data[0].courseID && data[0]._id
|
classroomID: classroom.id,
|
||||||
# courseID = data[0].courseID
|
classroomName: classroom.get('name')
|
||||||
# courseInstanceID = data[0]._id
|
ownerID: classroom.get('ownerID')
|
||||||
# route = "/courses/#{courseID}/#{courseInstanceID}"
|
}
|
||||||
# viewArgs = [{}, courseID, courseInstanceID]
|
@classrooms.add(classroom)
|
||||||
# Backbone.Mediator.publish 'router:navigate',
|
@render()
|
||||||
# route: route
|
|
||||||
# viewClass: 'views/courses/CourseDetailsView'
|
|
||||||
# viewArgs: viewArgs
|
|
||||||
# else
|
|
||||||
# @state = 'unknown_error'
|
|
||||||
# @stateMessage = "Database error."
|
|
||||||
# @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()
|
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
|
ClassroomSchema.statics.jsonSchema = jsonSchema
|
||||||
|
|
||||||
module.exports = Classroom = mongoose.model 'classroom', ClassroomSchema, 'classrooms'
|
module.exports = Classroom = mongoose.model 'classroom', ClassroomSchema, 'classrooms'
|
||||||
|
|
|
@ -52,6 +52,8 @@ CourseInstanceHandler = class CourseInstanceHandler extends Handler
|
||||||
ownsCourseInstance = courseInstance.get('ownerID').equals(req.user.get('_id'))
|
ownsCourseInstance = courseInstance.get('ownerID').equals(req.user.get('_id'))
|
||||||
addingSelf = userID is req.user.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) =>
|
Prepaid.find({ 'redeemers.userID': mongoose.Types.ObjectId(userID) }).count (err, userIsPrepaid) =>
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
Course.findById courseInstance.get('courseID'), (err, course) =>
|
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) =>
|
CourseInstance.find {members: mongoose.Types.ObjectId(memberID)}, (err, courseInstances) =>
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
return @sendSuccess(res, (@formatEntity(req, courseInstance) for courseInstance in courseInstances))
|
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
|
else
|
||||||
super(arguments...)
|
super(arguments...)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue