Skip to content

Commit 1c9d096

Browse files
committed
MOBILE-4616 course: Get course info whatever the user is enrolled or not
1 parent d06e9ec commit 1c9d096

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

src/addons/storagemanager/pages/course-storage/course-storage.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import {
2626
CoreCourseModulePrefetchDelegate,
2727
CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate';
28-
import { CoreCourseAnyCourseData, CoreCourses } from '@features/courses/services/courses';
28+
import { CoreCourses } from '@features/courses/services/courses';
2929
import { AccordionGroupChangeEventDetail } from '@ionic/angular';
3030
import { CoreLoadings } from '@services/loadings';
3131
import { CoreNavigator } from '@services/navigator';
@@ -612,31 +612,6 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
612612
this.changeDetectorRef.markForCheck();
613613
}
614614

615-
/**
616-
* Get the course object.
617-
*
618-
* @param courseId Course ID.
619-
* @returns Promise resolved with the course object if found.
620-
*/
621-
protected async getCourse(courseId: number): Promise<CoreCourseAnyCourseData | undefined> {
622-
try {
623-
// Check if user is enrolled. If enrolled, no guest access.
624-
return await CoreCourses.getUserCourse(courseId, true);
625-
} catch {
626-
// Ignore errors.
627-
}
628-
629-
try {
630-
// The user is not enrolled in the course. Use getCourses to see if it's an admin/manager and can see the course.
631-
return await CoreCourses.getCourse(courseId);
632-
} catch {
633-
// Ignore errors.
634-
}
635-
636-
return await CoreCourses.getCourseByField('id', this.courseId);
637-
638-
}
639-
640615
/**
641616
* Prefetch the whole course.
642617
*
@@ -646,7 +621,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
646621
event.stopPropagation();
647622
event.preventDefault();
648623

649-
const course = await this.getCourse(this.courseId);
624+
const course = await CoreCourseHelper.getCourseInfo(this.courseId);
650625
if (!course) {
651626
CoreDomUtils.showErrorModal('core.course.errordownloadingcourse', true);
652627

src/core/features/course/components/module-summary/module-summary.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { CoreCourse } from '@features/course/services/course';
2121
import { CoreCourseHelper, CoreCourseModuleData } from '@features/course/services/course-helper';
2222
import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate';
2323
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
24-
import { CoreCourses, CoreEnrolledCourseData } from '@features/courses/services/courses';
24+
import { CoreCourseAnyCourseData } from '@features/courses/services/courses';
2525
import { CoreGradesFormattedRow, CoreGradesFormattedTableRow, CoreGradesHelper } from '@features/grades/services/grades-helper';
2626
import { CoreNetwork } from '@services/network';
2727
import { CoreFilepool } from '@services/filepool';
@@ -73,7 +73,7 @@ export class CoreCourseModuleSummaryComponent implements OnInit, OnDestroy {
7373
grades?: CoreGradesFormattedRow[];
7474
blog = false; // If blog is available.
7575
isOnline = false; // If the app is online or not.
76-
course?: CoreEnrolledCourseData;
76+
course?: CoreCourseAnyCourseData;
7777
modicon = '';
7878
moduleNameTranslated = '';
7979
isTeacher = false;
@@ -272,12 +272,7 @@ export class CoreCourseModuleSummaryComponent implements OnInit, OnDestroy {
272272
* Fetch course.
273273
*/
274274
protected async fetchCourse(): Promise<void> {
275-
try {
276-
this.course = await CoreCourses.getUserCourse(this.courseId, true);
277-
} catch {
278-
// The user is not enrolled in the course. Use getCourses to see if it's an admin/manager and can see the course.
279-
this.course = await CoreCourses.getCourse(this.courseId);
280-
}
275+
this.course = await CoreCourseHelper.getCourseInfo(this.courseId);
281276

282277
this.isTeacher = await CoreUtils.ignoreErrors(CoreCourseHelper.guessIsTeacher(this.courseId, this.course), false);
283278
}

src/core/features/course/services/course-helper.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,30 @@ export class CoreCourseHelperProvider {
20522052
return !!adminOptions[courseId]?.['reports'];
20532053
}
20542054

2055+
/**
2056+
* Get the course object whatever the user is enrolled or not..
2057+
*
2058+
* @param courseId Course ID.
2059+
* @returns Promise resolved with the course object if found.
2060+
*/
2061+
async getCourseInfo(courseId: number): Promise<CoreCourseAnyCourseData | undefined> {
2062+
try {
2063+
// Check if user is enrolled. If enrolled, no guest access.
2064+
return await CoreCourses.getUserCourse(courseId, true);
2065+
} catch {
2066+
// Ignore errors.
2067+
}
2068+
2069+
try {
2070+
// The user is not enrolled in the course. Use getCourses to see if it's an admin/manager and can see the course.
2071+
return await CoreCourses.getCourse(courseId);
2072+
} catch {
2073+
// Ignore errors.
2074+
}
2075+
2076+
return await CoreCourses.getCourseByField('id', courseId);
2077+
}
2078+
20552079
/**
20562080
* Get the completion status of a module.
20572081
*

0 commit comments

Comments
 (0)