1+ define ( [ "coreJS/adapt" , "./ProgressView" ] , function ( Adapt , ProgressView ) {
2+ var progressReporter = new Progress ( ) ;
3+ var progressView = new ProgressView ( { model : progressReporter } ) ;
4+
5+ Adapt . on ( 'app:dataLoaded' , function ( ) {
6+ calculateCompletion ( Adapt , progressReporter ) ;
7+
8+ Adapt . articles . on ( "change:_isComplete" , function ( ) {
9+ calculateCompletion ( Adapt , progressReporter ) ;
10+ } ) ;
11+
12+ Adapt . on ( "pageView:postRender" , function ( view ) {
13+ view . $el . addClass ( "shows-progress" ) ;
14+ view . $el . append ( progressView . $el ) ;
15+ } ) ;
16+ } ) ;
17+ } ) ;
18+
19+ var Progress = Backbone . Model . extend ( {
20+ defaults : {
21+ /**
22+ * The number of units that are completed.
23+ *
24+ * @type {Number }
25+ */
26+ completeUnitCount : 0 ,
27+ /**
28+ * The total number of units.
29+ * "Units" is a generic term of an item being used to track completion (e.g Adapt Pages or Articles).
30+ *
31+ * @type {Number }
32+ */
33+ totalUnitCount : 0 ,
34+ /**
35+ * The number of units left to complete.
36+ *
37+ * @type {Number }
38+ */
39+ remainingUnitCount : 0 ,
40+ /**
41+ * The percent completion on a scale of 0 to 1.
42+ *
43+ * @type {Number }
44+ */
45+ percentComplete : 0 ,
46+ /**
47+ * The percent completion on a scale of 0 to 100 (whole numbers).
48+ *
49+ * @type {Number }
50+ */
51+ displayPercentComplete : 0
52+ } ,
53+
54+ /**
55+ * Updates the progress represented by this progress model.
56+ *
57+ * @param {Number } completeUnitCount The number of units completed.
58+ * @param {Number } totalUnitCount The total number of units.
59+ */
60+ updateProgress : function ( completeUnitCount , totalUnitCount ) {
61+ this . set ( {
62+ completeUnitCount : completeUnitCount ,
63+ remainingUnitCount : totalUnitCount - completeUnitCount ,
64+ totalUnitCount : totalUnitCount ,
65+ percentComplete : completeUnitCount / totalUnitCount ,
66+ displayPercentComplete : Math . round ( completeUnitCount / totalUnitCount * 100 )
67+ } )
68+ }
69+ } ) ;
70+
71+ /**
72+ * Calculates the completion of the provided Adapt course.
73+ *
74+ * @param {AdaptModel } Adapt The Adapt course to report completion on.
75+ * @param {Progress } progressReporter The progress reporter receiving the updated progress information.
76+ */
77+ function calculateCompletion ( Adapt , progressReporter ) {
78+ var availableContentObjects = new Backbone . Collection ( Adapt . articles . where ( { _isAvailable : true , _isOptional : false } ) ) ;
79+
80+ var requireCompletionOf = Adapt . course . get ( "_requireCompletionOf" ) ;
81+ var total = requireCompletionOf === - 1 ? availableContentObjects . length : requireCompletionOf ;
82+
83+ var complete = availableContentObjects . where ( { _isComplete : true } ) . length ;
84+
85+ progressReporter . updateProgress ( complete , total ) ;
86+ }
0 commit comments