diff --git a/src/core/contentlinks/classes/module-index-handler.ts b/src/core/contentlinks/classes/module-index-handler.ts index 859d72c1d..32037482d 100644 --- a/src/core/contentlinks/classes/module-index-handler.ts +++ b/src/core/contentlinks/classes/module-index-handler.ts @@ -15,6 +15,8 @@ import { CoreContentLinksAction } from '../providers/delegate'; import { CoreContentLinksHandlerBase } from './base-handler'; import { CoreCourseHelperProvider } from '@core/course/providers/helper'; +import { CoreCourseProvider } from '@core/course/providers/course'; +import { CoreDomUtilsProvider } from '@providers/utils/dom'; /** * Handler to handle URLs pointing to the index of a module. @@ -33,15 +35,33 @@ export class CoreContentLinksModuleIndexHandler extends CoreContentLinksHandlerB * @param courseHelper The CoreCourseHelperProvider instance. * @param addon Name of the addon as it's registered in course delegate. It'll be used to check if it's disabled. * @param modName Name of the module (assign, book, ...). + * @param instanceIdParam Param name for instance ID gathering. Only if set. */ - constructor(protected courseHelper: CoreCourseHelperProvider, public addon: string, public modName: string) { + constructor(protected courseHelper: CoreCourseHelperProvider, public addon: string, public modName: string, + protected instanceIdParam?: string) { super(); + const pattern = instanceIdParam ? + '\/mod\/' + modName + '\/view\.php.*([\&\?](' + instanceIdParam + '|id)=\\d+)' : + '\/mod\/' + modName + '\/view\.php.*([\&\?]id=\\d+)'; + // Match the view.php URL with an id param. - this.pattern = new RegExp('\/mod\/' + modName + '\/view\.php.*([\&\?]id=\\d+)'); + this.pattern = new RegExp(pattern); this.featureName = 'CoreCourseModuleDelegate_' + addon; } + /** + * Get the mod params necessary to open an activity. + * + * @param url The URL to treat. + * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param courseId Course ID related to the URL. Optional but recommended. + * @return List of params to pass to navigateToModule / navigateToModuleByInstance. + */ + getModParams(url: string, params: any, courseId?: number): any { + return undefined; + } + /** * Get the list of actions for a link (url). * @@ -55,11 +75,23 @@ export class CoreContentLinksModuleIndexHandler extends CoreContentLinksHandlerB CoreContentLinksAction[] | Promise { courseId = courseId || params.courseid || params.cid; + const modParams = this.getModParams(url, params, courseId); + + if (this.instanceIdParam && typeof params[this.instanceIdParam] != 'undefined') { + const instanceId = parseInt(params[this.instanceIdParam], 10); + + return [{ + action: (siteId, navCtrl?): void => { + this.courseHelper.navigateToModuleByInstance(instanceId, this.modName, siteId, courseId, undefined, + this.useModNameToGetModule, modParams, navCtrl); + } + }]; + } return [{ action: (siteId, navCtrl?): void => { this.courseHelper.navigateToModule(parseInt(params.id, 10), siteId, courseId, undefined, - this.useModNameToGetModule ? this.modName : undefined, undefined, navCtrl); + this.useModNameToGetModule ? this.modName : undefined, modParams, navCtrl); } }]; } diff --git a/src/core/course/providers/helper.ts b/src/core/course/providers/helper.ts index 1ecf7a124..86db51423 100644 --- a/src/core/course/providers/helper.ts +++ b/src/core/course/providers/helper.ts @@ -1114,6 +1114,37 @@ export class CoreCourseHelperProvider { return 'Section-' + section.id; } + /** + * Navigate to a module using instance ID and module name. + * + * @param instanceId Activity instance ID. + * @param modName Module name of the activity. + * @param siteId Site ID. If not defined, current site. + * @param courseId Course ID. If not defined we'll try to retrieve it from the site. + * @param sectionId Section the module belongs to. If not defined we'll try to retrieve it from the site. + * @param useModNameToGetModule If true, the app will retrieve all modules of this type with a single WS call. This reduces the + * number of WS calls, but it isn't recommended for modules that can return a lot of contents. + * @param modParams Params to pass to the module + * @param navCtrl NavController for adding new pages to the current history. Optional for legacy support, but + * generates a warning if omitted. + * @return Promise resolved when done. + */ + navigateToModuleByInstance(instanceId: number, modName: string, siteId?: string, courseId?: number, sectionId?: number, + useModNameToGetModule: boolean = false, modParams?: any, navCtrl?: NavController): Promise { + + const modal = this.domUtils.showModalLoading(); + + return this.courseProvider.getModuleBasicInfoByInstance(instanceId, modName, siteId).then((module) => { + this.navigateToModule(parseInt(module.id, 10), siteId, module.course, sectionId, + useModNameToGetModule ? modName : undefined, modParams, navCtrl); + }).catch((error) => { + this.domUtils.showErrorModalDefault(error, 'core.course.errorgetmodule', true); + }).finally(() => { + // Just in case. In fact we need to dismiss the modal before showing a toast or error message. + modal.dismiss(); + }); + } + /** * Navigate to a module. *