MOBILE-4616 course: Get course info whatever the user is enrolled or not

main
Pau Ferrer Ocaña 2024-10-21 11:40:26 +02:00
parent d06e9ec1db
commit 1c9d096299
3 changed files with 29 additions and 35 deletions

View File

@ -25,7 +25,7 @@ import {
import { import {
CoreCourseModulePrefetchDelegate, CoreCourseModulePrefetchDelegate,
CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate'; CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate';
import { CoreCourseAnyCourseData, CoreCourses } from '@features/courses/services/courses'; import { CoreCourses } from '@features/courses/services/courses';
import { AccordionGroupChangeEventDetail } from '@ionic/angular'; import { AccordionGroupChangeEventDetail } from '@ionic/angular';
import { CoreLoadings } from '@services/loadings'; import { CoreLoadings } from '@services/loadings';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
@ -612,31 +612,6 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
} }
/**
* Get the course object.
*
* @param courseId Course ID.
* @returns Promise resolved with the course object if found.
*/
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.
* *
@ -646,7 +621,7 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
const course = await this.getCourse(this.courseId); const course = await CoreCourseHelper.getCourseInfo(this.courseId);
if (!course) { if (!course) {
CoreDomUtils.showErrorModal('core.course.errordownloadingcourse', true); CoreDomUtils.showErrorModal('core.course.errordownloadingcourse', true);

View File

@ -21,7 +21,7 @@ import { CoreCourse } from '@features/course/services/course';
import { CoreCourseHelper, CoreCourseModuleData } from '@features/course/services/course-helper'; import { CoreCourseHelper, CoreCourseModuleData } from '@features/course/services/course-helper';
import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate'; import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate';
import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate'; import { CoreCourseModulePrefetchDelegate } from '@features/course/services/module-prefetch-delegate';
import { CoreCourses, CoreEnrolledCourseData } from '@features/courses/services/courses'; import { CoreCourseAnyCourseData } from '@features/courses/services/courses';
import { CoreGradesFormattedRow, CoreGradesFormattedTableRow, CoreGradesHelper } from '@features/grades/services/grades-helper'; import { CoreGradesFormattedRow, CoreGradesFormattedTableRow, CoreGradesHelper } from '@features/grades/services/grades-helper';
import { CoreNetwork } from '@services/network'; import { CoreNetwork } from '@services/network';
import { CoreFilepool } from '@services/filepool'; import { CoreFilepool } from '@services/filepool';
@ -73,7 +73,7 @@ export class CoreCourseModuleSummaryComponent implements OnInit, OnDestroy {
grades?: CoreGradesFormattedRow[]; grades?: CoreGradesFormattedRow[];
blog = false; // If blog is available. blog = false; // If blog is available.
isOnline = false; // If the app is online or not. isOnline = false; // If the app is online or not.
course?: CoreEnrolledCourseData; course?: CoreCourseAnyCourseData;
modicon = ''; modicon = '';
moduleNameTranslated = ''; moduleNameTranslated = '';
isTeacher = false; isTeacher = false;
@ -272,12 +272,7 @@ export class CoreCourseModuleSummaryComponent implements OnInit, OnDestroy {
* Fetch course. * Fetch course.
*/ */
protected async fetchCourse(): Promise<void> { protected async fetchCourse(): Promise<void> {
try { this.course = await CoreCourseHelper.getCourseInfo(this.courseId);
this.course = await CoreCourses.getUserCourse(this.courseId, true);
} catch {
// The user is not enrolled in the course. Use getCourses to see if it's an admin/manager and can see the course.
this.course = await CoreCourses.getCourse(this.courseId);
}
this.isTeacher = await CoreUtils.ignoreErrors(CoreCourseHelper.guessIsTeacher(this.courseId, this.course), false); this.isTeacher = await CoreUtils.ignoreErrors(CoreCourseHelper.guessIsTeacher(this.courseId, this.course), false);
} }

View File

@ -2052,6 +2052,30 @@ export class CoreCourseHelperProvider {
return !!adminOptions[courseId]?.['reports']; return !!adminOptions[courseId]?.['reports'];
} }
/**
* Get the course object whatever the user is enrolled or not..
*
* @param courseId Course ID.
* @returns Promise resolved with the course object if found.
*/
async getCourseInfo(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', courseId);
}
/** /**
* Get the completion status of a module. * Get the completion status of a module.
* *