MOBILE-2795 lesson: Prioritize prevent access reasons

main
Dani Palou 2019-01-07 09:49:53 +01:00
parent e11cadbfe1
commit 453d971359
3 changed files with 52 additions and 6 deletions

View File

@ -133,6 +133,7 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo
this.accessInfo = info; this.accessInfo = info;
this.canManage = info.canmanage; this.canManage = info.canmanage;
this.canViewReports = info.canviewreports; this.canViewReports = info.canviewreports;
this.preventMessages = [];
if (this.lessonProvider.isLessonOffline(this.lesson)) { if (this.lessonProvider.isLessonOffline(this.lesson)) {
// Handle status. // Handle status.
@ -162,7 +163,8 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo
} }
if (info.preventaccessreasons && info.preventaccessreasons.length) { 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) { if (askPassword) {
// The lesson requires a password. Check if there is one in memory or DB. // 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) => { promises.push(promise.then((password) => {
return this.validatePassword(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(() => { }).catch(() => {
// No password or the validation failed. Show password form. // No password or the validation failed. Show password form.
this.askPassword = true; this.askPassword = true;
this.preventMessages = info.preventaccessreasons; this.preventMessages = [preventReason];
lessonReady = false; lessonReady = false;
})); }));
} else { } else {
// Lesson cannot be started. // Lesson cannot be started.
this.preventMessages = info.preventaccessreasons; this.preventMessages = [preventReason];
lessonReady = false; lessonReady = false;
} }
} }
@ -293,7 +301,6 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo
*/ */
protected lessonReady(refresh?: boolean): void { protected lessonReady(refresh?: boolean): void {
this.askPassword = false; this.askPassword = false;
this.preventMessages = [];
this.leftDuringTimed = this.hasOffline || this.lessonProvider.leftDuringTimed(this.accessInfo); this.leftDuringTimed = this.hasOffline || this.lessonProvider.leftDuringTimed(this.accessInfo);
if (this.password) { if (this.password) {
@ -524,6 +531,12 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo
// Password validated. // Password validated.
this.lessonReady(false); 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. // Log view now that we have the password.
this.logView(); this.logView();
}).catch((error) => { }).catch((error) => {

View File

@ -245,9 +245,10 @@ export class AddonModLessonPlayerPage implements OnInit, OnDestroy {
if (info.preventaccessreasons && info.preventaccessreasons.length) { if (info.preventaccessreasons && info.preventaccessreasons.length) {
// If it's a password protected lesson and we have the password, allow playing it. // 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. // Lesson cannot be played, show message and go back.
return Promise.reject(info.preventaccessreasons[0].message); return Promise.reject(preventReason.message);
} }
} }

View File

@ -2335,6 +2335,38 @@ export class AddonModLessonProvider {
return this.ROOT_CACHE_KEY + 'userRetake:' + lessonId; 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. * Check if a jump is correct.
* Based in Moodle's jumpto_is_correct. * Based in Moodle's jumpto_is_correct.