MOBILE-3729 siteplugins: Allow updating coursepagemethod data
parent
337b92aed6
commit
8830b865f6
|
@ -24,10 +24,13 @@ import {
|
||||||
CoreSitePluginsContent,
|
CoreSitePluginsContent,
|
||||||
CoreSitePluginsCourseModuleHandlerData,
|
CoreSitePluginsCourseModuleHandlerData,
|
||||||
CoreSitePluginsPlugin,
|
CoreSitePluginsPlugin,
|
||||||
|
CoreSitePluginsProvider,
|
||||||
} from '@features/siteplugins/services/siteplugins';
|
} from '@features/siteplugins/services/siteplugins';
|
||||||
import { CoreNavigationOptions, CoreNavigator } from '@services/navigator';
|
import { CoreNavigationOptions, CoreNavigator } from '@services/navigator';
|
||||||
import { CoreLogger } from '@singletons/logger';
|
import { CoreLogger } from '@singletons/logger';
|
||||||
import { CoreSitePluginsBaseHandler } from './base-handler';
|
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.
|
* 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) {
|
if (forCoursePage && this.handlerSchema.coursepagemethod && module.visibleoncoursepage !== 0) {
|
||||||
// Call the method to get the course page template.
|
// 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;
|
return handlerData;
|
||||||
|
@ -152,12 +163,16 @@ export class CoreSitePluginsModuleHandler extends CoreSitePluginsBaseHandler imp
|
||||||
* @param module Module.
|
* @param module Module.
|
||||||
* @param courseId Course ID.
|
* @param courseId Course ID.
|
||||||
* @param handlerData Handler data.
|
* @param handlerData Handler data.
|
||||||
|
* @param method Method to call.
|
||||||
|
* @param refresh Whether to refresh the data.
|
||||||
* @return Promise resolved when done.
|
* @return Promise resolved when done.
|
||||||
*/
|
*/
|
||||||
protected async loadCoursePageTemplate(
|
protected async loadCoursePageTemplate(
|
||||||
module: CoreCourseAnyModuleData,
|
module: CoreCourseAnyModuleData,
|
||||||
courseId: number,
|
courseId: number,
|
||||||
handlerData: CoreCourseModuleHandlerData,
|
handlerData: CoreCourseModuleHandlerData,
|
||||||
|
method: string,
|
||||||
|
refresh?: boolean,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// Call the method to get the course page template.
|
// Call the method to get the course page template.
|
||||||
handlerData.loading = true;
|
handlerData.loading = true;
|
||||||
|
@ -167,12 +182,12 @@ export class CoreSitePluginsModuleHandler extends CoreSitePluginsBaseHandler imp
|
||||||
cmid: module.id,
|
cmid: module.id,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (refresh) {
|
||||||
|
await CoreUtils.ignoreErrors(CoreSitePlugins.invalidateContent(this.plugin.component, method, args));
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await CoreSitePlugins.getContent(
|
const result = await CoreSitePlugins.getContent(this.plugin.component, method, args);
|
||||||
this.plugin.component,
|
|
||||||
this.handlerSchema.coursepagemethod!,
|
|
||||||
args,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Use the html returned.
|
// Use the html returned.
|
||||||
handlerData.title = result.templates[0]?.html ?? '';
|
handlerData.title = result.templates[0]?.html ?? '';
|
||||||
|
|
|
@ -18,9 +18,10 @@ import { Md5 } from 'ts-md5';
|
||||||
|
|
||||||
import { CoreSiteWSPreSets } from '@classes/site';
|
import { CoreSiteWSPreSets } from '@classes/site';
|
||||||
import { CoreCompileHtmlComponent } from '@features/compile/components/compile-html/compile-html';
|
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 { CoreNavigator } from '@services/navigator';
|
||||||
import { CoreDomUtils } from '@services/utils/dom';
|
import { CoreDomUtils } from '@services/utils/dom';
|
||||||
|
import { CoreEvents } from '@singletons/events';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Component to render a site plugin content.
|
* 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.openContent = this.openContent.bind(this);
|
||||||
this.jsData.refreshContent = this.refreshContent.bind(this);
|
this.jsData.refreshContent = this.refreshContent.bind(this);
|
||||||
this.jsData.updateContent = this.updateContent.bind(this);
|
this.jsData.updateContent = this.updateContent.bind(this);
|
||||||
|
this.jsData.updateModuleCourseContent = this.updateModuleCourseContent.bind(this);
|
||||||
|
|
||||||
this.onContentLoaded.emit(refresh);
|
this.onContentLoaded.emit(refresh);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -224,4 +226,14 @@ export class CoreSitePluginsPluginContentComponent implements OnInit, DoCheck {
|
||||||
return this.compileComponent?.callComponentFunction(name, params);
|
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 });
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ const ROOT_CACHE_KEY = 'CoreSitePlugins:';
|
||||||
export class CoreSitePluginsProvider {
|
export class CoreSitePluginsProvider {
|
||||||
|
|
||||||
static readonly COMPONENT = 'CoreSitePlugins';
|
static readonly COMPONENT = 'CoreSitePlugins';
|
||||||
|
static readonly UPDATE_COURSE_CONTENT = 'siteplugins_update_course_content';
|
||||||
|
|
||||||
protected logger: CoreLogger;
|
protected logger: CoreLogger;
|
||||||
protected sitePlugins: {[name: string]: CoreSitePluginsHandler} = {}; // Site plugins registered.
|
protected sitePlugins: {[name: string]: CoreSitePluginsHandler} = {}; // Site plugins registered.
|
||||||
|
@ -922,3 +923,24 @@ export type CoreSitePluginsMainMenuHomeHandlerData = CoreSitePluginsHandlerCommo
|
||||||
priority?: number;
|
priority?: number;
|
||||||
ptrenabled?: boolean;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue