Add stats to ClassroomView

This commit is contained in:
Scott Erickson 2015-12-03 14:51:38 -08:00
parent b2ed489197
commit d874569b27
2 changed files with 40 additions and 5 deletions

View file

@ -17,7 +17,24 @@ block content
if view.classroom.get('description')
p= view.classroom.get('description')
// TODO: Add classroom statistics (grab from CourseDetailsView)
h3(data-i18n="courses.stats")
table.progress-stats-container
- var stats = view.classStats()
tr
td(data-i18n="courses.total_students")
td= _.size(view.classroom.get('members'))
tr
td(data-i18n="courses.average_time")
td= stats.averagePlaytime
tr
td(data-i18n="courses.total_time")
td= stats.totalPlaytime
tr
td(data-i18n="courses.average_levels")
td= stats.averageLevelsComplete
tr
td(data-i18n="courses.total_levels")
td= stats.totalLevelsComplete
h1
| Students
@ -36,8 +53,8 @@ block content
span.spl remove student
h2= user.broadName()
- var lastPlayedString = view.makeLastPlayedString(user);
- var playtime = view.makePlaytimeString(user);
- var lastPlayedString = view.userLastPlayedString(user);
- var playtime = view.userPlaytimeString(user);
if lastPlayedString || playtime
#student-stats-row.row
if lastPlayedString

View file

@ -44,6 +44,7 @@ module.exports = class ClassroomView extends RootView
@users.comparator = (user) => user.broadName().toLowerCase()
@supermodel.loadCollection(@users, 'users')
@listenToOnce @courseInstances, 'sync', @onCourseInstancesSync
@sessions = new CocoCollection([], { model: LevelSession })
onCourseInstancesSync: ->
@sessions = new CocoCollection([], { model: LevelSession })
@ -115,7 +116,7 @@ module.exports = class ClassroomView extends RootView
@openModalView(modal)
@listenToOnce modal, 'hidden', @render
makeLastPlayedString: (user) ->
userLastPlayedString: (user) ->
session = user.sessions.last()
return '' if not session
campaign = session.collection.campaign
@ -123,11 +124,28 @@ module.exports = class ClassroomView extends RootView
campaignLevel = campaign.get('levels')[levelOriginal]
return "#{campaign.get('fullName')}, #{campaignLevel.name}"
makePlaytimeString: (user) ->
userPlaytimeString: (user) ->
playtime = _.reduce user.sessions.pluck('playtime'), (s1, s2) -> (s1 or 0) + (s2 or 0)
return '' unless playtime
return moment.duration(playtime, 'seconds').humanize()
classStats: ->
stats = {}
playtime = 0
total = 0
for session in @sessions.models
pt = session.get('playtime') or 0
playtime += pt
total += 1
stats.averagePlaytime = if playtime and total then moment.duration(playtime / total, "seconds").humanize() else 0
stats.totalPlaytime = if playtime then moment.duration(playtime, "seconds").humanize() else 0
completeSessions = @sessions.filter (s) -> s.get('state').complete
stats.averageLevelsComplete = if @users.size() then (_.size(completeSessions) / @users.size()).toFixed(1) else 'N/A'
stats.totalLevelsComplete = _.size(completeSessions)
return stats
onClickAddStudentsButton: (e) ->
modal = new InviteToClassroomModal({classroom: @classroom})
@openModalView(modal)