MOBILE-4009 storage: Fix downloading guest courses

main
Pau Ferrer Ocaña 2023-06-28 11:51:39 +02:00
parent e8e2b94b15
commit c819f2eedd
1 changed files with 23 additions and 7 deletions

View File

@ -24,8 +24,7 @@ import {
import { import {
CoreCourseModulePrefetchDelegate, CoreCourseModulePrefetchDelegate,
CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate'; CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate';
import { CoreCourses } from '@features/courses/services/courses'; import { CoreCourseAnyCourseData, CoreCourses } from '@features/courses/services/courses';
import { CoreCoursesHelper } from '@features/courses/services/courses-helper';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
@ -660,6 +659,25 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
} }
protected async getCourse(courseId: number): Promise<CoreCourseAnyCourseData | undefined> {
try {
// Check if user is enrolled. If enrolled, no guest access.
return await CoreCourses.getUserCourse(courseId, true);
} catch {
// Ignore errors.
}
try {
// The user is not enrolled in the course. Use getCourses to see if it's an admin/manager and can see the course.
return await CoreCourses.getCourse(courseId);
} catch {
// Ignore errors.
}
return await CoreCourses.getCourseByField('id', this.courseId);
}
/** /**
* Prefetch the whole course. * Prefetch the whole course.
* *
@ -669,12 +687,10 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
const courses = await CoreCourses.getUserCourses(true); const course = await this.getCourse(this.courseId);
let course = courses.find((course) => course.id == this.courseId);
if (!course) {
course = await CoreCourses.getCourse(this.courseId);
}
if (!course) { if (!course) {
CoreDomUtils.showErrorModal('core.course.errordownloadingcourse', true);
return; return;
} }