MOBILE-2795 lesson: Prioritize prevent access reasons
This commit is contained in:
		
							parent
							
								
									e11cadbfe1
								
							
						
					
					
						commit
						453d971359
					
				| @ -133,6 +133,7 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo | ||||
|             this.accessInfo = info; | ||||
|             this.canManage = info.canmanage; | ||||
|             this.canViewReports = info.canviewreports; | ||||
|             this.preventMessages = []; | ||||
| 
 | ||||
|             if (this.lessonProvider.isLessonOffline(this.lesson)) { | ||||
|                 // Handle status.
 | ||||
| @ -162,7 +163,8 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo | ||||
|             } | ||||
| 
 | ||||
|             if (info.preventaccessreasons && info.preventaccessreasons.length) { | ||||
|                 const askPassword = info.preventaccessreasons.length == 1 && this.lessonProvider.isPasswordProtected(info); | ||||
|                 let preventReason = this.lessonProvider.getPreventAccessReason(info, false); | ||||
|                 const askPassword = preventReason.reason == 'passwordprotectedlesson'; | ||||
| 
 | ||||
|                 if (askPassword) { | ||||
|                     // The lesson requires a password. Check if there is one in memory or DB.
 | ||||
| @ -171,15 +173,21 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo | ||||
| 
 | ||||
|                     promises.push(promise.then((password) => { | ||||
|                         return this.validatePassword(password); | ||||
|                     }).then(() => { | ||||
|                         // Now that we have the password, get the access reason again ignoring the password.
 | ||||
|                         preventReason = this.lessonProvider.getPreventAccessReason(info, true); | ||||
|                         if (preventReason) { | ||||
|                             this.preventMessages = [preventReason]; | ||||
|                         } | ||||
|                     }).catch(() => { | ||||
|                         // No password or the validation failed. Show password form.
 | ||||
|                         this.askPassword = true; | ||||
|                         this.preventMessages = info.preventaccessreasons; | ||||
|                         this.preventMessages = [preventReason]; | ||||
|                         lessonReady = false; | ||||
|                     })); | ||||
|                 } else  { | ||||
|                     // Lesson cannot be started.
 | ||||
|                     this.preventMessages = info.preventaccessreasons; | ||||
|                     this.preventMessages = [preventReason]; | ||||
|                     lessonReady = false; | ||||
|                 } | ||||
|             } | ||||
| @ -293,7 +301,6 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo | ||||
|      */ | ||||
|     protected lessonReady(refresh?: boolean): void { | ||||
|         this.askPassword = false; | ||||
|         this.preventMessages = []; | ||||
|         this.leftDuringTimed = this.hasOffline || this.lessonProvider.leftDuringTimed(this.accessInfo); | ||||
| 
 | ||||
|         if (this.password) { | ||||
| @ -524,6 +531,12 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo | ||||
|             // Password validated.
 | ||||
|             this.lessonReady(false); | ||||
| 
 | ||||
|             // Now that we have the password, get the access reason again ignoring the password.
 | ||||
|             const preventReason = this.lessonProvider.getPreventAccessReason(this.accessInfo, true); | ||||
|             if (preventReason) { | ||||
|                 this.preventMessages = [preventReason]; | ||||
|             } | ||||
| 
 | ||||
|             // Log view now that we have the password.
 | ||||
|             this.logView(); | ||||
|         }).catch((error) => { | ||||
|  | ||||
| @ -245,9 +245,10 @@ export class AddonModLessonPlayerPage implements OnInit, OnDestroy { | ||||
| 
 | ||||
|             if (info.preventaccessreasons && info.preventaccessreasons.length) { | ||||
|                 // If it's a password protected lesson and we have the password, allow playing it.
 | ||||
|                 if (!this.password || info.preventaccessreasons.length > 1 || !this.lessonProvider.isPasswordProtected(info)) { | ||||
|                 const preventReason = this.lessonProvider.getPreventAccessReason(info, !!this.password); | ||||
|                 if (preventReason) { | ||||
|                     // Lesson cannot be played, show message and go back.
 | ||||
|                     return Promise.reject(info.preventaccessreasons[0].message); | ||||
|                     return Promise.reject(preventReason.message); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|  | ||||
| @ -2335,6 +2335,38 @@ export class AddonModLessonProvider { | ||||
|         return this.ROOT_CACHE_KEY + 'userRetake:' + lessonId; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get the prevent access reason to display for a certain lesson. | ||||
|      * | ||||
|      * @param {any} info Lesson access info. | ||||
|      * @param {boolean} [ignorePassword] Whether password protected reason should be ignored (user already entered the password). | ||||
|      * @return {any} Prevent access reason. | ||||
|      */ | ||||
|     getPreventAccessReason(info: any, ignorePassword?: boolean): any { | ||||
|         let result; | ||||
| 
 | ||||
|         if (info && info.preventaccessreasons) { | ||||
|             for (let i = 0; i < info.preventaccessreasons.length; i++) { | ||||
|                 const entry = info.preventaccessreasons[i]; | ||||
| 
 | ||||
|                 if (entry.reason == 'lessonopen' || entry.reason == 'lessonclosed') { | ||||
|                     // Time restrictions are the most prioritary, return it.
 | ||||
|                     return entry; | ||||
|                 } else if (entry.reason == 'passwordprotectedlesson') { | ||||
|                     if (!ignorePassword) { | ||||
|                         // Treat password before all other reasons.
 | ||||
|                         result = entry; | ||||
|                     } | ||||
|                 } else if (!result) { | ||||
|                     // Rest of cases, just return any of them.
 | ||||
|                     result = entry; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Check if a jump is correct. | ||||
|      * Based in Moodle's jumpto_is_correct. | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user