From b7c73d1a322d93a58870c709bbcdaa3a9d8f4b8b Mon Sep 17 00:00:00 2001 From: uzairr Date: Mon, 4 Jun 2018 14:32:59 +0500 Subject: [PATCH] Extract course run with highest grades. if a learner has completed multiple course runs of each course then they want to see course runs with highest grades on associated programs. LEARNER-4351 --- .../models/course_card_model.js | 17 +++++++++++++++++ .../learner_dashboard/views/course_card_view.js | 3 +++ 2 files changed, 20 insertions(+) diff --git a/lms/static/js/learner_dashboard/models/course_card_model.js b/lms/static/js/learner_dashboard/models/course_card_model.js index 59eff07470..2807890764 100644 --- a/lms/static/js/learner_dashboard/models/course_card_model.js +++ b/lms/static/js/learner_dashboard/models/course_card_model.js @@ -38,6 +38,23 @@ class CourseCardModel extends Backbone.Model { return desiredCourseRun; } + getCourseRunWithHighestGrade(grades) { + const allEnrolledCourseRuns = this.context.course_runs.filter(run => run.is_enrolled); + if (allEnrolledCourseRuns.length <= 1) { + return null; + } + + allEnrolledCourseRuns.sort((a, b) => (grades[a.key] || 0) - (grades[b.key] || 0)); + return allEnrolledCourseRuns[allEnrolledCourseRuns.length - 1]; + } + + updateCourseRunWithHighestGrade(grades) { + const courseRunWithHighestGrade = this.getCourseRunWithHighestGrade(grades); + if (courseRunWithHighestGrade) { + this.setActiveCourseRun(courseRunWithHighestGrade, this.context.user_preferences); + } + } + isEnrolledInSession() { // Returns true if the user is currently enrolled in a session of the course return this.context.course_runs.find(run => run.is_enrolled) !== undefined; diff --git a/lms/static/js/learner_dashboard/views/course_card_view.js b/lms/static/js/learner_dashboard/views/course_card_view.js index 9ad242ba92..68429ff2c1 100644 --- a/lms/static/js/learner_dashboard/views/course_card_view.js +++ b/lms/static/js/learner_dashboard/views/course_card_view.js @@ -27,6 +27,9 @@ class CourseCardView extends Backbone.View { this.enrollModel.urlRoot = this.urlModel.get('commerce_api_url'); } this.context = options.context || {}; + if (this.context.collectionCourseStatus === 'completed') { + this.model.updateCourseRunWithHighestGrade(this.context.courseData.grades); + } this.grade = this.context.courseData.grades[this.model.get('course_run_key')]; this.grade = this.grade * 100; this.collectionCourseStatus = this.context.collectionCourseStatus || '';