MOBILE-3320 core: Fix grades link handler

main
Dani Palou 2021-05-05 12:24:57 +02:00
parent f7e531c3d0
commit d10e9f574a
6 changed files with 59 additions and 55 deletions

View File

@ -27,6 +27,9 @@ function buildRoutes(injector: Injector): Routes {
{ {
path: '', path: '',
component: CoreCourseIndexPage, component: CoreCourseIndexPage,
data: {
isCourseIndex: true,
},
children: routes.children, children: routes.children,
}, },
...routes.siblings, ...routes.siblings,

View File

@ -71,7 +71,7 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy {
const index = this.tabs.findIndex((tab) => tab.name == data.name); const index = this.tabs.findIndex((tab) => tab.name == data.name);
if (index >= 0) { if (index >= 0) {
this.tabsComponent?.selectByIndex(index + 1); this.tabsComponent?.selectByIndex(index);
} }
} }
}); });
@ -129,11 +129,9 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy {
// Load the course handlers. // Load the course handlers.
const handlers = await CoreCourseOptionsDelegate.getHandlersToDisplay(this.course!, false, false); const handlers = await CoreCourseOptionsDelegate.getHandlersToDisplay(this.course!, false, false);
this.tabs = [...this.tabs, ...handlers.map(handler => handler.data)];
let tabToLoad: number | undefined; let tabToLoad: number | undefined;
// Add the courseId to the handler component data. // Create the full path.
handlers.forEach((handler, index) => { handlers.forEach((handler, index) => {
handler.data.page = CoreTextUtils.concatenatePaths(this.currentPagePath, handler.data.page); handler.data.page = CoreTextUtils.concatenatePaths(this.currentPagePath, handler.data.page);
handler.data.pageParams = handler.data.pageParams || {}; handler.data.pageParams = handler.data.pageParams || {};
@ -144,6 +142,11 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy {
} }
}); });
this.tabs = [...this.tabs, ...handlers.map(handler => ({
...handler.data,
name: handler.name,
}))];
// Select the tab if needed. // Select the tab if needed.
this.firstTabName = undefined; this.firstTabName = undefined;
if (tabToLoad) { if (tabToLoad) {

View File

@ -43,6 +43,7 @@ import { CoreCourseLogCronHandler } from './handlers/log-cron';
import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins';
import { CoreCourseAutoSyncData, CoreCourseSyncProvider } from './sync'; import { CoreCourseAutoSyncData, CoreCourseSyncProvider } from './sync';
import { CoreTagItem } from '@features/tag/services/tag'; import { CoreTagItem } from '@features/tag/services/tag';
import { CoreNavigator } from '@services/navigator';
const ROOT_CACHE_KEY = 'mmCourse:'; const ROOT_CACHE_KEY = 'mmCourse:';
@ -176,10 +177,14 @@ export class CoreCourseProvider {
* @param courseId Course ID. * @param courseId Course ID.
* @return Whether the current view is a certain course. * @return Whether the current view is a certain course.
*/ */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
currentViewIsCourse(courseId: number): boolean { currentViewIsCourse(courseId: number): boolean {
// @todo implement const route = CoreNavigator.getCurrentRoute({ routeData: { isCourseIndex: true } });
return false;
if (!route) {
return false;
}
return Number(route.snapshot.params.courseId) == courseId;
} }
/** /**

View File

@ -34,6 +34,7 @@ import { CoreDomUtils } from '@services/utils/dom';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
import { makeSingleton, Translate } from '@singletons'; import { makeSingleton, Translate } from '@singletons';
import { CoreError } from '@classes/errors/error'; import { CoreError } from '@classes/errors/error';
import { CoreCourseHelper } from '@features/course/services/course-helper';
/** /**
* Service that provides some features regarding grades information. * Service that provides some features regarding grades information.
@ -458,14 +459,13 @@ export class CoreGradesHelperProvider {
siteId?: string, siteId?: string,
): Promise<void> { ): Promise<void> {
const modal = await CoreDomUtils.showModalLoading(); const modal = await CoreDomUtils.showModalLoading();
let currentUserId: number;
const site = await CoreSites.getSite(siteId);
siteId = site.id;
const currentUserId = site.getUserId();
try { try {
const site = await CoreSites.getSite(siteId);
siteId = site.id;
currentUserId = site.getUserId();
if (!moduleId) { if (!moduleId) {
throw new CoreError('Invalid moduleId'); throw new CoreError('Invalid moduleId');
} }
@ -477,27 +477,27 @@ export class CoreGradesHelperProvider {
throw new CoreError('No grades found.'); throw new CoreError('No grades found.');
} }
// Can get grades. Do it.
const items = await CoreGrades.getGradeItems(courseId, userId, undefined, siteId);
// Find the item of the module.
const item = Array.isArray(items) && items.find((item) => moduleId == item.cmid);
if (!item) {
throw new CoreError('Grade item not found.');
}
// Open the item directly.
const gradeId = item.id;
await CoreUtils.ignoreErrors(
CoreNavigator.navigateToSitePath(`/grades/${courseId}/${gradeId}`, {
siteId,
params: { userId },
}),
);
} catch (error) {
try { try {
// Can get grades. Do it.
const items = await CoreGrades.getGradeItems(courseId, userId, undefined, siteId);
// Find the item of the module.
const item = Array.isArray(items) && items.find((item) => moduleId == item.cmid);
if (!item) {
throw new CoreError('Grade item not found.');
}
// Open the item directly.
const gradeId = item.id;
await CoreUtils.ignoreErrors(
CoreNavigator.navigateToSitePath(`/grades/${courseId}/${gradeId}`, {
siteId,
params: { userId },
}),
);
} catch (error) {
// Cannot get grade items or there's no need to. // Cannot get grade items or there's no need to.
if (userId && userId != currentUserId) { if (userId && userId != currentUserId) {
// View another user grades. Open the grades page directly. // View another user grades. Open the grades page directly.
@ -517,24 +517,12 @@ export class CoreGradesHelperProvider {
return; return;
} }
// @todo
// Open the course with the grades tab selected. // Open the course with the grades tab selected.
// await CoreCourseHelper.getCourse(courseId, siteId).then(async (result) => { await CoreCourseHelper.getAndOpenCourse(courseId, { selectedTab: 'CoreGrades' }, siteId);
// const pageParams = { } catch (error) {
// course: result.course, // Cannot get course for some reason, just open the grades page.
// selectedTab: 'CoreGrades', await CoreNavigator.navigateToSitePath(`/grades/${courseId}`, { siteId });
// };
// // CoreContentLinksHelper.goInSite(navCtrl, 'CoreCourseSectionPage', pageParams, siteId)
// return await CoreUtils.ignoreErrors(CoreNavigator.navigateToSitePath('/course', {
// siteId,
// params: pageParams,
// }));
// });
} }
} catch (error) {
// Cannot get course for some reason, just open the grades page.
await CoreNavigator.navigateToSitePath(`/grades/${courseId}`, { siteId });
} finally { } finally {
modal.dismiss(); modal.dismiss();
} }

View File

@ -64,6 +64,7 @@ export type CoreNavigatorCurrentRouteOptions = Partial<{
params: Params; // Params to get the value from. params: Params; // Params to get the value from.
route: ActivatedRoute; // Current Route. route: ActivatedRoute; // Current Route.
pageComponent: unknown; pageComponent: unknown;
routeData: Record<string, unknown>;
}>; }>;
/** /**
@ -401,18 +402,22 @@ export class CoreNavigatorService {
*/ */
getCurrentRoute(): ActivatedRoute; getCurrentRoute(): ActivatedRoute;
getCurrentRoute(options: CoreNavigatorCurrentRouteOptions): ActivatedRoute | null; getCurrentRoute(options: CoreNavigatorCurrentRouteOptions): ActivatedRoute | null;
getCurrentRoute({ route, pageComponent }: CoreNavigatorCurrentRouteOptions = {}): ActivatedRoute | null { getCurrentRoute({ route, pageComponent, routeData }: CoreNavigatorCurrentRouteOptions = {}): ActivatedRoute | null {
route = route ?? Router.routerState.root; route = route ?? Router.routerState.root;
if (pageComponent && route.component === pageComponent) { if (pageComponent && route.component === pageComponent) {
return route; return route;
} }
if (route.firstChild) { if (routeData && CoreUtils.basicLeftCompare(routeData, route.snapshot.data, 3)) {
return this.getCurrentRoute({ route: route.firstChild, pageComponent }); return route;
} }
return pageComponent ? null : route; if (route.firstChild) {
return this.getCurrentRoute({ route: route.firstChild, pageComponent, routeData });
}
return pageComponent || routeData ? null : route;
} }
/** /**

View File

@ -176,7 +176,7 @@ export class CoreUtilsProvider {
maxLevels: number = 0, maxLevels: number = 0,
level: number = 0, level: number = 0,
undefinedIsNull: boolean = true, undefinedIsNull: boolean = true,
): boolean | undefined { ): boolean {
if (typeof itemA == 'function' || typeof itemB == 'function') { if (typeof itemA == 'function' || typeof itemB == 'function') {
return true; // Don't compare functions. return true; // Don't compare functions.
} else if (typeof itemA == 'object' && typeof itemB == 'object') { } else if (typeof itemA == 'object' && typeof itemB == 'object') {
@ -189,7 +189,7 @@ export class CoreUtilsProvider {
const value = itemA[name]; const value = itemA[name];
if (name == '$$hashKey') { if (name == '$$hashKey') {
// Ignore $$hashKey property since it's a "calculated" property. // Ignore $$hashKey property since it's a "calculated" property.
return; continue;
} }
if (!this.basicLeftCompare(value, itemB[name], maxLevels, level + 1)) { if (!this.basicLeftCompare(value, itemB[name], maxLevels, level + 1)) {