forked from CIT/Vmeda.Online
		
	Merge pull request #1589 from dpalou/MOBILE-2655
MOBILE-2655 course: Decrease WS calls to get course completion
This commit is contained in:
		
						commit
						89ba8ccce3
					
				| @ -165,7 +165,19 @@ export class AddonCourseCompletionProvider { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return this.coursesProvider.getUserCourse(courseId, preferCache).then((course) => { |         return this.coursesProvider.getUserCourse(courseId, preferCache).then((course) => { | ||||||
|             return !(course && typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0); |             if (course) { | ||||||
|  |                 if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) { | ||||||
|  |                     // Completion not enabled for the course.
 | ||||||
|  |                     return false; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 if (typeof course.completionhascriteria != 'undefined' && course.completionhascriteria == 0) { | ||||||
|  |                     // No criteria, cannot view completion.
 | ||||||
|  |                     return false; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             return true; | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -177,27 +189,50 @@ export class AddonCourseCompletionProvider { | |||||||
|      * @return {Promise<boolean>} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise. |      * @return {Promise<boolean>} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise. | ||||||
|      */ |      */ | ||||||
|     isPluginViewEnabledForUser(courseId: number, userId?: number): Promise<boolean> { |     isPluginViewEnabledForUser(courseId: number, userId?: number): Promise<boolean> { | ||||||
|         // Disable emergency cache to be able to detect that the plugin has been disabled (WS will fail).
 |         // Check if user wants to view his own completion.
 | ||||||
|         const preSets: any = { |         const currentUserId = this.sitesProvider.getCurrentSiteUserId(); | ||||||
|             emergencyCache: 0 |         let promise; | ||||||
|         }; |  | ||||||
| 
 | 
 | ||||||
|         return this.getCompletion(courseId, userId, preSets).then(() => { |         if (!userId || userId == currentUserId) { | ||||||
|             return true; |             // Viewing own completion. Get the course to check if it has completion criteria.
 | ||||||
|         }).catch((error) => { |             promise = this.coursesProvider.getUserCourse(courseId, true).then((course): any => { | ||||||
|             if (this.utils.isWebServiceError(error)) { |                 // If the site is returning the completionhascriteria then the user can view his own completion.
 | ||||||
|                 // The WS returned an error, plugin is not enabled.
 |                 // We already checked the value in isPluginViewEnabledForCourse.
 | ||||||
|                 return false; |                 if (course && typeof course.completionhascriteria != 'undefined') { | ||||||
|             } else { |  | ||||||
|                 // Not a WS error. Check if we have a cached value.
 |  | ||||||
|                 preSets.omitExpires = true; |  | ||||||
| 
 |  | ||||||
|                 return this.getCompletion(courseId, userId, preSets).then(() => { |  | ||||||
|                     return true; |                     return true; | ||||||
|                 }).catch(() => { |                 } | ||||||
|  | 
 | ||||||
|  |                 return Promise.reject(null); | ||||||
|  |             }); | ||||||
|  |         } else { | ||||||
|  |             promise = Promise.reject(null); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return promise.catch(() => { | ||||||
|  |             // User not viewing own completion or the site doesn't tell us if the course has criteria.
 | ||||||
|  |             // The only way to know if completion can be viewed is to call the WS.
 | ||||||
|  |             // Disable emergency cache to be able to detect that the plugin has been disabled (WS will fail).
 | ||||||
|  |             const preSets: any = { | ||||||
|  |                 emergencyCache: 0 | ||||||
|  |             }; | ||||||
|  | 
 | ||||||
|  |             return this.getCompletion(courseId, userId, preSets).then(() => { | ||||||
|  |                 return true; | ||||||
|  |             }).catch((error) => { | ||||||
|  |                 if (this.utils.isWebServiceError(error)) { | ||||||
|  |                     // The WS returned an error, plugin is not enabled.
 | ||||||
|                     return false; |                     return false; | ||||||
|                 }); |                 } else { | ||||||
|             } |                     // Not a WS error. Check if we have a cached value.
 | ||||||
|  |                     preSets.omitExpires = true; | ||||||
|  | 
 | ||||||
|  |                     return this.getCompletion(courseId, userId, preSets).then(() => { | ||||||
|  |                         return true; | ||||||
|  |                     }).catch(() => { | ||||||
|  |                         return false; | ||||||
|  |                     }); | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -133,8 +133,13 @@ export class CoreCoursesHelperProvider { | |||||||
|                 } |                 } | ||||||
|                 courses = slice > 0 ? courses.slice(0, slice) : courses; |                 courses = slice > 0 ? courses.slice(0, slice) : courses; | ||||||
| 
 | 
 | ||||||
|                 // Fetch course completion status.
 |                 // Fetch course completion status if needed.
 | ||||||
|                 return Promise.all(courses.map((course) => { |                 return Promise.all(courses.map((course) => { | ||||||
|  |                     if (typeof course.completed != 'undefined') { | ||||||
|  |                         // The WebService already returns the completed status, no need to fetch it.
 | ||||||
|  |                         return Promise.resolve(course); | ||||||
|  |                     } | ||||||
|  | 
 | ||||||
|                     if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) { |                     if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) { | ||||||
|                         // Completion is disabled for this course, there is no need to fetch the completion status.
 |                         // Completion is disabled for this course, there is no need to fetch the completion status.
 | ||||||
|                         return Promise.resolve(course); |                         return Promise.resolve(course); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user