From 90ac6baa672417fe1b83b942a179c0208ade7a13 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Wed, 2 Dec 2015 16:07:46 -0800 Subject: [PATCH] Fix course instance generation for classrooms --- app/templates/courses/classroom-view.jade | 2 +- app/views/courses/ClassroomView.coffee | 29 ++++++++++++++++++--- app/views/courses/TeacherCoursesView.coffee | 27 ++++++++++++++++++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/app/templates/courses/classroom-view.jade b/app/templates/courses/classroom-view.jade index 8768ca1d8..19e06e0f2 100644 --- a/app/templates/courses/classroom-view.jade +++ b/app/templates/courses/classroom-view.jade @@ -53,7 +53,7 @@ block content .progress .progress-bar(style='width:'+stats.levels.pctDone) else if paidFor - button.enable-btn.btn.btn-info.btn-sm(data-user-id=user.id, data-course-instance-id=courseInstance.id) Enable + button.enable-btn.btn.btn-info.btn-sm(data-user-id=user.id, data-course-instance-cid=courseInstance.cid) Enable if !paidFor .text-center diff --git a/app/views/courses/ClassroomView.coffee b/app/views/courses/ClassroomView.coffee index d5e71e60e..48772e56d 100644 --- a/app/views/courses/ClassroomView.coffee +++ b/app/views/courses/ClassroomView.coffee @@ -50,10 +50,22 @@ module.exports = class ClassroomView extends RootView @supermodel.loadCollection(sessions, 'sessions') courseInstance.sessions = sessions sessions.courseInstance = courseInstance + courseInstance.sessionsByUser = {} @listenToOnce sessions, 'sync', (sessions) -> @sessions.add(sessions.slice()) sessions.courseInstance.sessionsByUser = sessions.groupBy('creator') - + + # generate course instance JIT, in the meantime have models w/out equivalents in the db + for course in @courses.models + query = {courseID: course.id, classroomID: @classroom.id} + courseInstance = @courseInstances.findWhere(query) + if not courseInstance + courseInstance = new CourseInstance(query) + @courseInstances.add(courseInstance) + courseInstance.sessions = new CocoCollection([], {model: LevelSession}) + sessions.courseInstance = courseInstance + courseInstance.sessionsByUser = {} + onLoaded: -> userSessions = @sessions.groupBy('creator') for user in @users.models @@ -105,11 +117,20 @@ module.exports = class ClassroomView extends RootView @openModalView(modal) onClickEnableButton: (e) -> - courseInstance = @courseInstances.get($(e.target).data('course-instance-id')) + courseInstance = @courseInstances.get($(e.target).data('course-instance-cid')) userID = $(e.target).data('user-id') - courseInstance.addMember(userID) $(e.target).attr('disabled', true) - @listenToOnce courseInstance, 'sync', @render + + onCourseInstanceCreated = => + courseInstance.addMember(userID) + @listenToOnce courseInstance, 'sync', @render + + if courseInstance.isNew() + # adding the first student to this course, so generate the course instance for it + courseInstance.save(null, {validate: false}) + courseInstance.once 'sync', onCourseInstanceCreated + else + onCourseInstanceCreated() onClickRemoveStudentLink: (e) -> user = @users.get($(e.target).closest('a').data('user-id')) diff --git a/app/views/courses/TeacherCoursesView.coffee b/app/views/courses/TeacherCoursesView.coffee index 21e59f3e9..81286c1e3 100644 --- a/app/views/courses/TeacherCoursesView.coffee +++ b/app/views/courses/TeacherCoursesView.coffee @@ -60,7 +60,9 @@ module.exports = class TeacherCoursesView extends RootView # TODO: how to get new classroom from modal? @classrooms.add(modal.classroom) # TODO: will this definitely fire after modal saves new classroom? - @listenToOnce modal.classroom, 'sync', @render + @listenToOnce modal.classroom, 'sync', -> + @addFreeCourseInstances() + @render() onClickEditClassroomSmall: (e) -> classroomID = $(e.target).data('classroom-id') @@ -68,3 +70,26 @@ module.exports = class TeacherCoursesView extends RootView modal = new ClassroomSettingsModal({classroom: classroom}) @openModalView(modal) @listenToOnce modal, 'hide', @render + + onLoaded: -> + super() + @addFreeCourseInstances() + + addFreeCourseInstances: -> + # so that when students join the classroom, they can automatically get free courses + # non-free courses are generated when the teacher first adds a student to them + for classroom in @classrooms.models + for course in @courses.models + continue if not course.get('free') + courseInstance = @courseInstances.findWhere({classroomID: classroom.id, courseID: course.id}) + if not courseInstance + courseInstance = new CourseInstance({ + classroomID: classroom.id + courseID: course.id + }) + # TODO: figure out a better way to get around triggering validation errors for properties + # that the server will end up filling in, like an empty members array, ownerID + courseInstance.save(null, {validate: false}) + @courseInstances.add(courseInstance) + @listenToOnce courseInstance, 'sync', @addFreeCourseInstances + return \ No newline at end of file