178 lines
5.4 KiB
CoffeeScript
178 lines
5.4 KiB
CoffeeScript
$ = jQuery
|
|
context = window
|
|
logger = context.JK.logger
|
|
broadcastActions = @BroadcastActions
|
|
|
|
rest = context.JK.Rest()
|
|
|
|
broadcastActions.load.listenAndPromise(rest.getBroadcastNotification);
|
|
|
|
BroadcastStore = Reflux.createStore(
|
|
{
|
|
listenables: broadcastActions
|
|
|
|
currentSession: null
|
|
currentLesson: null
|
|
broadcast: null
|
|
currentLessonTimer: null
|
|
teacherFault: false
|
|
isJamClass: false
|
|
init: ->
|
|
this.listenTo(context.AppStore, this.onAppInit)
|
|
this.listenTo(context.SessionStore, this.onSessionChange)
|
|
this.listenTo(context.NavStore, this.onNavChange)
|
|
|
|
onAppInit: (@app) ->
|
|
|
|
getTimeRemaining: (endtime) ->
|
|
t = new Date(endtime).getTime() - new Date().getTime()
|
|
|
|
originalT = t
|
|
|
|
if t < 0
|
|
seconds = Math.ceil( (t/1000) % 60 );
|
|
minutes = Math.ceil( (t/1000/60) % 60 );
|
|
hours = Math.ceil( (t/(1000*60*60)) % 24 );
|
|
days = Math.ceil( t/(1000*60*60*24) );
|
|
else
|
|
seconds = Math.floor( (t/1000) % 60 );
|
|
minutes = Math.floor( (t/1000/60) % 60 );
|
|
hours = Math.floor( (t/(1000*60*60)) % 24 );
|
|
days = Math.floor( t/(1000*60*60*24) );
|
|
|
|
return {
|
|
'total': originalT,
|
|
'days': days,
|
|
'hours': hours,
|
|
'minutes': minutes,
|
|
'seconds': seconds
|
|
};
|
|
|
|
lessonTick: () ->
|
|
@timeManagement()
|
|
@changed()
|
|
|
|
timeManagement: () ->
|
|
lastCheck = $.extend({}, @currentLesson)
|
|
lessonSession = @currentLesson
|
|
lessonSession.until = @getTimeRemaining(lessonSession.scheduled_start)
|
|
if lessonSession.until.total < 0
|
|
# we are past the start time
|
|
if lessonSession.until.total > -(10 * 60 * 1000) # 10 minutes
|
|
lessonSession.initialWindow = true
|
|
else
|
|
lessonSession.initialWindow = false
|
|
lessonSession.beforeSession = false
|
|
else
|
|
# we are before the due time
|
|
lessonSession.initialWindow = false
|
|
lessonSession.beforeSession = true
|
|
|
|
# if we've transitioned to a new window
|
|
|
|
if !lessonSession.beforeSession && ((lastCheck.initialWindow || !lastCheck.initialWindow?) && !lessonSession.initialWindow)
|
|
logger.debug("BroadcastStore: lesson session 'initial window' transition")
|
|
rest.getLessonAnalysis({id: lessonSession.id}).done((response) => @lessonAnalysisDone(response)).fail(@app.ajaxError)
|
|
|
|
lessonAnalysisDone: (@analysis) ->
|
|
|
|
if !@currentLesson?
|
|
logger.debug("BroadcastStore: ignoring lessonAnalysisDone")
|
|
return
|
|
|
|
if @analysis.status == 'completed'
|
|
logger.debug("BroadcastStore: lesson is over")
|
|
@currentLesson.completed = true
|
|
@currentLesson.success = @analysis.success
|
|
@clearTimer()
|
|
@changed()
|
|
else if @analysis.analysis.reason != 'teacher_fault'
|
|
logger.debug("BroadcastStore: not teacher fault; clearing lesson info")
|
|
@clearLesson()
|
|
else
|
|
logger.debug("BroadcastStore: teacher is at fault")
|
|
@teacherFault = true
|
|
@clearTimer()
|
|
@changed()
|
|
|
|
clearLesson: () ->
|
|
if @currentLesson?
|
|
@currentLesson = null
|
|
@clearTimer()
|
|
@teacherFault = false
|
|
@changed()
|
|
|
|
clearTimer: () ->
|
|
if @currentLessonTimer?
|
|
clearInterval(@currentLessonTimer)
|
|
@currentLessonTimer = null
|
|
|
|
onNavChange: (nav) ->
|
|
path = nav.screenPath.toLowerCase()
|
|
@isJamClass = path.indexOf('jamclass') > -1 || path.indexOf('teacher') > -1
|
|
@changed()
|
|
|
|
onSessionChange: (session) ->
|
|
|
|
@session = session
|
|
currentSession = session.session
|
|
if currentSession? && currentSession.lesson_session? && session.inSession() && currentSession.lesson_session.student_id == context.JK.currentUserId
|
|
|
|
@currentSession = currentSession
|
|
|
|
lessonSession = currentSession.lesson_session
|
|
# so that receivers can check type of info coming at them via one-way events
|
|
lessonSession.isLesson = true
|
|
|
|
if lessonSession.status == 'completed'
|
|
lessonSession.completed = true
|
|
lessonSession.success = lessonSession.success
|
|
#else
|
|
# rest.getLessonAnalysis({id: lessonSession.id}).done((response) => @lessonAnalysisDone(response)).fail(@app.ajaxError)
|
|
|
|
@currentLesson = lessonSession
|
|
@timeManagement()
|
|
logger.debug("BroadcastStore: currentLesson until: ", @currentLesson.until, lessonSession.scheduled_start)
|
|
if !@currentLessonTimer?
|
|
@currentLessonTimer = setInterval((() => @lessonTick()), 1000)
|
|
@changed()
|
|
|
|
else
|
|
@clearLesson()
|
|
|
|
onLoad: () ->
|
|
logger.debug("loading broadcast notification...")
|
|
|
|
onLoadCompleted: (response) ->
|
|
if response.id?
|
|
logger.debug("broadcast notification sync completed")
|
|
@broadcast = response
|
|
@changed()
|
|
|
|
|
|
onLoadFailed: (jqXHR) ->
|
|
if jqXHR.status != 404
|
|
logger.error("broadcast notification sync failed")
|
|
|
|
onHide: () ->
|
|
@broadcast = null
|
|
@changed()
|
|
|
|
changed: () ->
|
|
if @currentLesson?
|
|
@currentLesson.teacherFault = @teacherFault
|
|
@currentLesson.teacherPresent = @session.findParticipantByUserId(@currentLesson.teacher_id)
|
|
if @currentLesson.teacherPresent?
|
|
this.trigger(null)
|
|
else
|
|
this.trigger(@currentLesson)
|
|
else if @isJamClass
|
|
this.trigger({isJamClass: true})
|
|
else
|
|
this.trigger(@broadcast)
|
|
}
|
|
)
|
|
|
|
context.JK.Stores.Broadcast = BroadcastStore
|
|
|