RootView = require 'views/core/RootView' Campaigns = require 'collections/Campaigns' Classroom = require 'models/Classroom' Courses = require 'collections/Courses' Levels = require 'collections/Levels' LevelSessions = require 'collections/LevelSessions' User = require 'models/User' utils = require 'core/utils' CocoCollection = require 'collections/CocoCollection' Classrooms = require 'collections/Classrooms' Users = require 'collections/Users' Course = require 'models/Course' CourseInstance = require 'models/CourseInstance' CourseInstances = require 'collections/CourseInstances' Prepaids = require 'collections/Prepaids' module.exports = class TeacherStudentView extends RootView id: 'teacher-student-view' template: require 'templates/teachers/teacher-student-view' getTitle: -> return @user?.broadName() initialize: (options, classroomID, studentID) -> @classroom = new Classroom({_id: classroomID}) @listenToOnce @classroom, 'sync', @onClassroomSync @supermodel.trackRequest(@classroom.fetch()) @courses = new Courses() @supermodel.trackRequest(@courses.fetch({data: { project: 'name' }})) @levels = new Levels() @supermodel.trackRequest(@levels.fetchForClassroom(classroomID, {data: {project: 'name,original'}})) @user = new User({_id: studentID}) @supermodel.trackRequest(@user.fetch()) super(options) onClassroomSync: -> # Now that we have the classroom from db, can request all level sessions for this classroom @sessions = new LevelSessions() @sessions.comparator = 'changed' # Sort level sessions by chanaged field, ascending @listenTo @sessions, 'sync', @onSessionsSync @supermodel.trackRequests(@sessions.fetchForAllClassroomMembers(@classroom)) onSessionsSync: -> # Now we have some level sessions, and enough data to calculate last played string # This may be called multiple times due to paged server API calls via fetchForAllClassroomMembers return if @destroyed # Don't do anything if page was destroyed after db request @updateLastPlayedString() updateLastPlayedString: -> # Make sure all our data is loaded, @sessions may not even be intialized yet return unless @courses.loaded and @levels.loaded and @sessions?.loaded and @user.loaded # Use lodash to find the last session for our user, @sessions already sorted by changed date session = _.findLast @sessions.models, (s) => s.get('creator') is @user.id return unless session # Find course for this level session, for it's name # Level.original is the original id, used for level versioning, and connects levels to level sessions for versionedCourse in @classroom.get('courses') ? [] for level in versionedCourse.levels if level.original is session.get('level').original # Found the level for our level session in the classroom versioned courses # Find the full course so we can get it's name course = _.find @courses.models, (c) => c.id is versionedCourse._id break # Find level for this level session, for it's name level = @levels.findWhere({original: session.get('level').original}) # Update last played string based on what we found @lastPlayedString = "" @lastPlayedString += course.get('name') if course @lastPlayedString += ", " if course and level @lastPlayedString += level.get('name') if level @lastPlayedString += ", " if @lastPlayedString @lastPlayedString += session.get('changed') # Rerun template/jade file to display new last played string @render()