From 453d97135947c083eed3f06b1e67f2604c77ac60 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 7 Jan 2019 09:49:53 +0100 Subject: [PATCH] MOBILE-2795 lesson: Prioritize prevent access reasons --- .../mod/lesson/components/index/index.ts | 21 +++++++++--- src/addon/mod/lesson/pages/player/player.ts | 5 +-- src/addon/mod/lesson/providers/lesson.ts | 32 +++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/addon/mod/lesson/components/index/index.ts b/src/addon/mod/lesson/components/index/index.ts index 34170064f..015434ce0 100644 --- a/src/addon/mod/lesson/components/index/index.ts +++ b/src/addon/mod/lesson/components/index/index.ts @@ -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) => { diff --git a/src/addon/mod/lesson/pages/player/player.ts b/src/addon/mod/lesson/pages/player/player.ts index 01fd5b4bf..c40f6ed8c 100644 --- a/src/addon/mod/lesson/pages/player/player.ts +++ b/src/addon/mod/lesson/pages/player/player.ts @@ -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); } } diff --git a/src/addon/mod/lesson/providers/lesson.ts b/src/addon/mod/lesson/providers/lesson.ts index 96578dad8..dd15ed778 100644 --- a/src/addon/mod/lesson/providers/lesson.ts +++ b/src/addon/mod/lesson/providers/lesson.ts @@ -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.