Merge pull request #2942 from dpalou/MOBILE-3729

MOBILE-3729 siteplugins: Allow updating coursepagemethod data
main
Pau Ferrer Ocaña 2021-09-10 13:08:50 +02:00 committed by GitHub
commit 6e4bc4b311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 7 deletions

View File

@ -24,10 +24,13 @@ import {
CoreSitePluginsContent,
CoreSitePluginsCourseModuleHandlerData,
CoreSitePluginsPlugin,
CoreSitePluginsProvider,
} from '@features/siteplugins/services/siteplugins';
import { CoreNavigationOptions, CoreNavigator } from '@services/navigator';
import { CoreLogger } from '@singletons/logger';
import { CoreSitePluginsBaseHandler } from './base-handler';
import { CoreEvents } from '@singletons/events';
import { CoreUtils } from '@services/utils/utils';
/**
* Handler to support a module using a site plugin.
@ -105,7 +108,15 @@ export class CoreSitePluginsModuleHandler extends CoreSitePluginsBaseHandler imp
if (forCoursePage && this.handlerSchema.coursepagemethod && module.visibleoncoursepage !== 0) {
// Call the method to get the course page template.
this.loadCoursePageTemplate(module, courseId, handlerData);
const method = this.handlerSchema.coursepagemethod;
this.loadCoursePageTemplate(module, courseId, handlerData, method);
// Allow updating the data via event.
CoreEvents.on(CoreSitePluginsProvider.UPDATE_COURSE_CONTENT, (data) => {
if (data.cmId === module.id) {
this.loadCoursePageTemplate(module, courseId, handlerData, method, !data.alreadyFetched);
}
});
}
return handlerData;
@ -152,12 +163,16 @@ export class CoreSitePluginsModuleHandler extends CoreSitePluginsBaseHandler imp
* @param module Module.
* @param courseId Course ID.
* @param handlerData Handler data.
* @param method Method to call.
* @param refresh Whether to refresh the data.
* @return Promise resolved when done.
*/
protected async loadCoursePageTemplate(
module: CoreCourseAnyModuleData,
courseId: number,
handlerData: CoreCourseModuleHandlerData,
method: string,
refresh?: boolean,
): Promise<void> {
// Call the method to get the course page template.
handlerData.loading = true;
@ -167,12 +182,12 @@ export class CoreSitePluginsModuleHandler extends CoreSitePluginsBaseHandler imp
cmid: module.id,
};
if (refresh) {
await CoreUtils.ignoreErrors(CoreSitePlugins.invalidateContent(this.plugin.component, method, args));
}
try {
const result = await CoreSitePlugins.getContent(
this.plugin.component,
this.handlerSchema.coursepagemethod!,
args,
);
const result = await CoreSitePlugins.getContent(this.plugin.component, method, args);
// Use the html returned.
handlerData.title = result.templates[0]?.html ?? '';

View File

@ -18,9 +18,10 @@ import { Md5 } from 'ts-md5';
import { CoreSiteWSPreSets } from '@classes/site';
import { CoreCompileHtmlComponent } from '@features/compile/components/compile-html/compile-html';
import { CoreSitePlugins, CoreSitePluginsContent } from '@features/siteplugins/services/siteplugins';
import { CoreSitePlugins, CoreSitePluginsContent, CoreSitePluginsProvider } from '@features/siteplugins/services/siteplugins';
import { CoreNavigator } from '@services/navigator';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreEvents } from '@singletons/events';
/**
* Component to render a site plugin content.
@ -111,6 +112,7 @@ export class CoreSitePluginsPluginContentComponent implements OnInit, DoCheck {
this.jsData.openContent = this.openContent.bind(this);
this.jsData.refreshContent = this.refreshContent.bind(this);
this.jsData.updateContent = this.updateContent.bind(this);
this.jsData.updateModuleCourseContent = this.updateModuleCourseContent.bind(this);
this.onContentLoaded.emit(refresh);
} catch (error) {
@ -224,4 +226,14 @@ export class CoreSitePluginsPluginContentComponent implements OnInit, DoCheck {
return this.compileComponent?.callComponentFunction(name, params);
}
/**
* Function only for module plugins using coursepagemethod. Update module data in course page content.
*
* @param cmId Module ID.
* @param alreadyFetched Whether course data has already been fetched (no need to fetch it again).
*/
updateModuleCourseContent(cmId: number, alreadyFetched?: boolean): void {
CoreEvents.trigger(CoreSitePluginsProvider.UPDATE_COURSE_CONTENT, { cmId, alreadyFetched });
}
}

View File

@ -38,6 +38,7 @@ const ROOT_CACHE_KEY = 'CoreSitePlugins:';
export class CoreSitePluginsProvider {
static readonly COMPONENT = 'CoreSitePlugins';
static readonly UPDATE_COURSE_CONTENT = 'siteplugins_update_course_content';
protected logger: CoreLogger;
protected sitePlugins: {[name: string]: CoreSitePluginsHandler} = {}; // Site plugins registered.
@ -922,3 +923,24 @@ export type CoreSitePluginsMainMenuHomeHandlerData = CoreSitePluginsHandlerCommo
priority?: number;
ptrenabled?: boolean;
};
/**
* Event to update course content data for plugins using coursepagemethod.
*/
export type CoreSitePluginsUpdateCourseContentEvent = {
cmId: number; // Module ID to update.
alreadyFetched?: boolean; // Whether course data has already been fetched (no need to fetch it again).
};
declare module '@singletons/events' {
/**
* Augment CoreEventsData interface with events specific to this service.
*
* @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
*/
export interface CoreEventsData {
[CoreSitePluginsProvider.UPDATE_COURSE_CONTENT]: CoreSitePluginsUpdateCourseContentEvent;
}
}