@@ -25,7 +25,50 @@ angular.module('mm.addons.coursecompletion')
2525 */
2626. factory ( '$mmaCourseCompletionHandlers' , function ( $mmaCourseCompletion , $state , mmCoursesAccessMethods ) {
2727
28- var self = { } ;
28+ // We use "caches" to decrease network usage.
29+ var self = { } ,
30+ viewCompletionEnabledCache = { } ,
31+ coursesNavEnabledCache = { } ;
32+
33+ /**
34+ * Get a cache key to identify a course and a user.
35+ *
36+ * @param {Number } courseId Course ID.
37+ * @param {Number } userId User ID.
38+ * @return {String } Cache key.
39+ */
40+ function getCacheKey ( courseId , userId ) {
41+ return courseId + '#' + userId ;
42+ }
43+
44+ /**
45+ * Clear view completion cache.
46+ * If a courseId and userId are specified, it will only delete the entry for that user and course.
47+ *
48+ * @module mm.addons.coursecompletion
49+ * @ngdoc method
50+ * @name $mmaCourseCompletionHandlers#clearViewCompletionCache
51+ * @param {Number } [courseId] Course ID.
52+ * @param {Number } [userId] User ID.
53+ */
54+ self . clearViewCompletionCache = function ( courseId , userId ) {
55+ if ( courseId && userId ) {
56+ delete viewCompletionEnabledCache [ getCacheKey ( courseId , userId ) ] ;
57+ } else {
58+ viewCompletionEnabledCache = { } ;
59+ }
60+ } ;
61+
62+ /**
63+ * Clear courses nav caches.
64+ *
65+ * @module mm.addons.coursecompletion
66+ * @ngdoc method
67+ * @name $mmaCourseCompletionHandlers#clearCoursesNavCache
68+ */
69+ self . clearCoursesNavCache = function ( ) {
70+ coursesNavEnabledCache = { } ;
71+ } ;
2972
3073 /**
3174 * View user completion handler.
@@ -56,7 +99,14 @@ angular.module('mm.addons.coursecompletion')
5699 */
57100 self . isEnabledForUser = function ( user , courseId ) {
58101 return $mmaCourseCompletion . isPluginViewEnabledForCourse ( courseId ) . then ( function ( ) {
59- return $mmaCourseCompletion . isPluginViewEnabledForUser ( courseId , user . id ) ;
102+ var cacheKey = getCacheKey ( courseId , user . id ) ;
103+ if ( typeof viewCompletionEnabledCache [ cacheKey ] != 'undefined' ) {
104+ return viewCompletionEnabledCache [ cacheKey ] ;
105+ }
106+ return $mmaCourseCompletion . isPluginViewEnabledForUser ( courseId , user . id ) . then ( function ( enabled ) {
107+ viewCompletionEnabledCache [ cacheKey ] = enabled ;
108+ return enabled ;
109+ } ) ;
60110 } ) ;
61111 } ;
62112
@@ -130,7 +180,13 @@ angular.module('mm.addons.coursecompletion')
130180 }
131181 return $mmaCourseCompletion . isPluginViewEnabledForCourse ( courseId ) . then ( function ( ) {
132182 // Check if the user can see his own report, teachers can't.
133- return $mmaCourseCompletion . isPluginViewEnabledForUser ( courseId ) ;
183+ if ( typeof coursesNavEnabledCache [ courseId ] != 'undefined' ) {
184+ return coursesNavEnabledCache [ courseId ] ;
185+ }
186+ return $mmaCourseCompletion . isPluginViewEnabledForUser ( courseId ) . then ( function ( enabled ) {
187+ coursesNavEnabledCache [ courseId ] = enabled ;
188+ return enabled ;
189+ } ) ;
134190 } ) ;
135191 } ;
136192
@@ -166,4 +222,16 @@ angular.module('mm.addons.coursecompletion')
166222 } ;
167223
168224 return self ;
225+ } )
226+
227+ . run ( function ( $mmaCourseCompletionHandlers , $mmEvents , mmCoreEventLogout , mmCoursesEventMyCoursesRefreshed ,
228+ mmUserEventProfileRefreshed ) {
229+ $mmEvents . on ( mmCoreEventLogout , function ( ) {
230+ $mmaCourseCompletionHandlers . clearViewCompletionCache ( ) ;
231+ $mmaCourseCompletionHandlers . clearCoursesNavCache ( ) ;
232+ } ) ;
233+ $mmEvents . on ( mmCoursesEventMyCoursesRefreshed , $mmaCourseCompletionHandlers . clearCoursesNavCache ) ;
234+ $mmEvents . on ( mmUserEventProfileRefreshed , function ( data ) {
235+ $mmaCourseCompletionHandlers . clearViewCompletionCache ( data . courseid , data . userid ) ;
236+ } ) ;
169237} ) ;
0 commit comments