From d874569b27f789f8f1e4c90e1fcc1a7eeb24f7fb Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Thu, 3 Dec 2015 14:51:38 -0800 Subject: [PATCH] Add stats to ClassroomView --- app/templates/courses/classroom-view.jade | 23 ++++++++++++++++++++--- app/views/courses/ClassroomView.coffee | 22 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/app/templates/courses/classroom-view.jade b/app/templates/courses/classroom-view.jade index c1f275692..65f4df653 100644 --- a/app/templates/courses/classroom-view.jade +++ b/app/templates/courses/classroom-view.jade @@ -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 diff --git a/app/views/courses/ClassroomView.coffee b/app/views/courses/ClassroomView.coffee index ab2b95f5f..7bfafc27b 100644 --- a/app/views/courses/ClassroomView.coffee +++ b/app/views/courses/ClassroomView.coffee @@ -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)