MOBILE-2317 core: Migrate delegates to CoreDelegate
parent
debfb5c6c6
commit
2319cf8074
|
@ -19,7 +19,7 @@ import { CoreEventsProvider } from '../providers/events';
|
|||
|
||||
export interface CoreDelegateHandler {
|
||||
/**
|
||||
* Name of the handler, or name and sub context (mmaMessages, mmaMessage:blockContact, ...).
|
||||
* Name of the handler, or name and sub context (AddonMessages, AddonMessages:blockContact, ...).
|
||||
* @type {string}
|
||||
*/
|
||||
name: string;
|
||||
|
@ -29,7 +29,7 @@ export interface CoreDelegateHandler {
|
|||
* @return {boolean|Promise<boolean>} Whether or not the handler is enabled on a site level.
|
||||
*/
|
||||
isEnabled(): boolean | Promise<boolean>;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Superclass to help creating delegates
|
||||
|
@ -80,16 +80,20 @@ export class CoreDelegate {
|
|||
* @param {string} delegateName Delegate name used for logging purposes.
|
||||
* @param {CoreLoggerProvider} loggerProvider CoreLoggerProvider instance, cannot be directly injected.
|
||||
* @param {CoreSitesProvider} sitesProvider CoreSitesProvider instance, cannot be directly injected.
|
||||
* @param {CoreEventsProvider} [eventsProvider] CoreEventsProvider instance, cannot be directly injected.
|
||||
* If not set, no events will be fired.
|
||||
*/
|
||||
constructor(delegateName: string, protected loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider,
|
||||
protected eventsProvider: CoreEventsProvider) {
|
||||
protected eventsProvider?: CoreEventsProvider) {
|
||||
this.logger = this.loggerProvider.getInstance(delegateName);
|
||||
|
||||
if (eventsProvider) {
|
||||
// Update handlers on this cases.
|
||||
eventsProvider.on(CoreEventsProvider.LOGIN, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.SITE_UPDATED, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.REMOTE_ADDONS_LOADED, this.updateHandlers.bind(this));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a certain function in a enabled handler.
|
||||
|
|
|
@ -19,24 +19,12 @@ import { CoreLoggerProvider } from '../../../providers/logger';
|
|||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
import { CoreCourseProvider } from './course';
|
||||
import { CoreCourseFormatDefaultHandler } from './default-format';
|
||||
import { CoreDelegate, CoreDelegateHandler } from '../../../classes/delegate';
|
||||
|
||||
/**
|
||||
* Interface that all course format handlers must implement.
|
||||
*/
|
||||
export interface CoreCourseFormatHandler {
|
||||
/**
|
||||
* Name of the format. It should match the "format" returned in core_course_get_courses.
|
||||
* @type {string}
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Whether or not the handler is enabled on a site level.
|
||||
*
|
||||
* @return {boolean|Promise<boolean>} True or promise resolved with true if enabled.
|
||||
*/
|
||||
isEnabled(): boolean|Promise<boolean>;
|
||||
|
||||
export interface CoreCourseFormatHandler extends CoreDelegateHandler {
|
||||
/**
|
||||
* Get the title to use in course page. If not defined, course fullname.
|
||||
* This function will be called without sections first, and then call it again when the sections are retrieved.
|
||||
|
@ -136,25 +124,20 @@ export interface CoreCourseFormatHandler {
|
|||
* @return {Promise<any>} Promise resolved when the data is invalidated.
|
||||
*/
|
||||
invalidateData?(course: any, sections: any[]): Promise<any>;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Service to interact with course formats. Provides the functions to register and interact with the addons.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreCourseFormatDelegate {
|
||||
protected logger;
|
||||
export class CoreCourseFormatDelegate extends CoreDelegate {
|
||||
protected handlers: { [s: string]: CoreCourseFormatHandler } = {}; // All registered handlers.
|
||||
protected enabledHandlers: { [s: string]: CoreCourseFormatHandler } = {}; // Handlers enabled for the current site.
|
||||
protected lastUpdateHandlersStart: number;
|
||||
protected featurePrefix = 'CoreCourseFormatHandler_';
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider,
|
||||
private defaultHandler: CoreCourseFormatDefaultHandler) {
|
||||
this.logger = logger.getInstance('CoreCoursesCourseFormatDelegate');
|
||||
|
||||
eventsProvider.on(CoreEventsProvider.LOGIN, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.SITE_UPDATED, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.REMOTE_ADDONS_LOADED, this.updateHandlers.bind(this));
|
||||
constructor(loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider,
|
||||
protected defaultHandler: CoreCourseFormatDefaultHandler) {
|
||||
super('CoreCoursesCourseFormatDelegate', loggerProvider, sitesProvider, eventsProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -250,6 +233,7 @@ export class CoreCourseFormatDelegate {
|
|||
if (sections[0].id != CoreCourseProvider.ALL_SECTIONS_ID) {
|
||||
return sections[0];
|
||||
}
|
||||
|
||||
return sections[1];
|
||||
});
|
||||
}
|
||||
|
@ -286,20 +270,6 @@ export class CoreCourseFormatDelegate {
|
|||
return this.executeFunction(course.format, 'invalidateData', [course, sections]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a time belongs to the last update handlers call.
|
||||
* This is to handle the cases where updateHandlers don't finish in the same order as they're called.
|
||||
*
|
||||
* @param {number} time Time to check.
|
||||
* @return {boolean} Whether it's the last call.
|
||||
*/
|
||||
isLastUpdateCall(time: number) : boolean {
|
||||
if (!this.lastUpdateHandlersStart) {
|
||||
return true;
|
||||
}
|
||||
return time == this.lastUpdateHandlersStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a course.
|
||||
*
|
||||
|
@ -311,81 +281,7 @@ export class CoreCourseFormatDelegate {
|
|||
if (this.enabledHandlers[course.format] && this.enabledHandlers[course.format].openCourse) {
|
||||
return this.enabledHandlers[course.format].openCourse(navCtrl, course);
|
||||
}
|
||||
|
||||
return navCtrl.push('CoreCourseSectionPage', { course: course });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a handler.
|
||||
*
|
||||
* @param {CoreCourseFormatHandler} handler The handler to register.
|
||||
* @return {boolean} True if registered successfully, false otherwise.
|
||||
*/
|
||||
registerHandler(handler: CoreCourseFormatHandler) : boolean {
|
||||
if (typeof this.handlers[handler.name] !== 'undefined') {
|
||||
this.logger.log(`Addon '${handler.name}' already registered`);
|
||||
return false;
|
||||
}
|
||||
this.logger.log(`Registered addon '${handler.name}'`);
|
||||
this.handlers[handler.name] = handler;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handler for the current site.
|
||||
*
|
||||
* @param {CoreCourseFormatHandler} handler The handler to check.
|
||||
* @param {number} time Time this update process started.
|
||||
* @return {Promise<void>} Resolved when done.
|
||||
*/
|
||||
protected updateHandler(handler: CoreCourseFormatHandler, time: number) : Promise<void> {
|
||||
let promise,
|
||||
siteId = this.sitesProvider.getCurrentSiteId(),
|
||||
currentSite = this.sitesProvider.getCurrentSite();
|
||||
|
||||
if (!this.sitesProvider.isLoggedIn()) {
|
||||
promise = Promise.reject(null);
|
||||
} else if (currentSite.isFeatureDisabled('CoreCourseFormatHandler_' + handler.name)) {
|
||||
promise = Promise.resolve(false);
|
||||
} else {
|
||||
promise = Promise.resolve(handler.isEnabled());
|
||||
}
|
||||
|
||||
// Checks if the handler is enabled.
|
||||
return promise.catch(() => {
|
||||
return false;
|
||||
}).then((enabled: boolean) => {
|
||||
// Verify that this call is the last one that was started.
|
||||
// Check that site hasn't changed since the check started.
|
||||
if (this.isLastUpdateCall(time) && this.sitesProvider.getCurrentSiteId() === siteId) {
|
||||
if (enabled) {
|
||||
this.enabledHandlers[handler.name] = handler;
|
||||
} else {
|
||||
delete this.enabledHandlers[handler.name];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handlers for the current site.
|
||||
*
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
protected updateHandlers() : Promise<any> {
|
||||
let promises = [],
|
||||
now = Date.now();
|
||||
|
||||
this.logger.debug('Updating handlers for current site.');
|
||||
|
||||
this.lastUpdateHandlersStart = now;
|
||||
|
||||
// Loop over all the handlers.
|
||||
for (let name in this.handlers) {
|
||||
promises.push(this.updateHandler(this.handlers[name], now));
|
||||
}
|
||||
|
||||
return Promise.all(promises).catch(() => {
|
||||
// Never reject.
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,30 +19,12 @@ import { CoreLoggerProvider } from '../../../providers/logger';
|
|||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
import { CoreCourseProvider } from './course';
|
||||
import { CoreSite } from '../../../classes/site';
|
||||
import { CoreDelegate, CoreDelegateHandler } from '../../../classes/delegate';
|
||||
|
||||
/**
|
||||
* Interface that all course module handlers must implement.
|
||||
*/
|
||||
export interface CoreCourseModuleHandler {
|
||||
/**
|
||||
* A name to identify the addon.
|
||||
* @type {string}
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Name of the module. It should match the "modname" of the module returned in core_course_get_contents.
|
||||
* @type {string}
|
||||
*/
|
||||
modname: string;
|
||||
|
||||
/**
|
||||
* Whether or not the handler is enabled on a site level.
|
||||
*
|
||||
* @return {boolean|Promise<boolean>} True or promise resolved with true if enabled.
|
||||
*/
|
||||
isEnabled(): boolean|Promise<boolean>;
|
||||
|
||||
export interface CoreCourseModuleHandler extends CoreDelegateHandler {
|
||||
/**
|
||||
* Get the data required to display the module in the course contents view.
|
||||
*
|
||||
|
@ -107,7 +89,7 @@ export interface CoreCourseModuleHandlerData {
|
|||
* @param {NavOptions} [options] Options for the navigation.
|
||||
*/
|
||||
action?(event: Event, navCtrl: NavController, module: any, courseId: number, options?: NavOptions): void;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A button to display in a module item.
|
||||
|
@ -158,19 +140,14 @@ export interface CoreCourseModuleHandlerButton {
|
|||
* Delegate to register module handlers.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreCourseModuleDelegate {
|
||||
protected logger;
|
||||
export class CoreCourseModuleDelegate extends CoreDelegate {
|
||||
protected handlers: { [s: string]: CoreCourseModuleHandler } = {}; // All registered handlers.
|
||||
protected enabledHandlers: { [s: string]: CoreCourseModuleHandler } = {}; // Handlers enabled for the current site.
|
||||
protected lastUpdateHandlersStart: number;
|
||||
protected featurePrefix = '$mmCourseDelegate_';
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider,
|
||||
private courseProvider: CoreCourseProvider) {
|
||||
this.logger = logger.getInstance('CoreCourseModuleDelegate');
|
||||
|
||||
eventsProvider.on(CoreEventsProvider.LOGIN, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.SITE_UPDATED, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.REMOTE_ADDONS_LOADED, this.updateHandlers.bind(this));
|
||||
constructor(loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider,
|
||||
protected courseProvider: CoreCourseProvider) {
|
||||
super('CoreCourseModuleDelegate', loggerProvider, sitesProvider, eventsProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -230,16 +207,6 @@ export class CoreCourseModuleDelegate {
|
|||
}
|
||||
|
||||
return defaultData;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a module has a registered handler (not necessarily enabled).
|
||||
*
|
||||
* @param {string} modname The name of the module type.
|
||||
* @return {boolean} If the controller is installed or not.
|
||||
*/
|
||||
hasHandler(modname: string) : boolean {
|
||||
return typeof this.handlers[modname] !== 'undefined';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -263,100 +230,12 @@ export class CoreCourseModuleDelegate {
|
|||
* @return {boolean} Whether module is disabled.
|
||||
*/
|
||||
isModuleDisabledInSite(modname: string, site?: CoreSite): boolean {
|
||||
if (typeof this.handlers[modname] != 'undefined') {
|
||||
site = site || this.sitesProvider.getCurrentSite();
|
||||
|
||||
if (typeof this.handlers[modname] != 'undefined') {
|
||||
return site.isFeatureDisabled('$mmCourseDelegate_' + this.handlers[modname].name);
|
||||
return this.isFeatureDisabled(this.handlers[modname], site);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a time belongs to the last update handlers call.
|
||||
* This is to handle the cases where updateHandlers don't finish in the same order as they're called.
|
||||
*
|
||||
* @param {number} time Time to check.
|
||||
* @return {boolean} Whether it's the last call.
|
||||
*/
|
||||
isLastUpdateCall(time: number) : boolean {
|
||||
if (!this.lastUpdateHandlersStart) {
|
||||
return true;
|
||||
}
|
||||
return time == this.lastUpdateHandlersStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a handler.
|
||||
*
|
||||
* @param {CoreCourseModuleHandler} handler The handler to register.
|
||||
* @return {boolean} True if registered successfully, false otherwise.
|
||||
*/
|
||||
registerHandler(handler: CoreCourseModuleHandler) : boolean {
|
||||
if (typeof this.handlers[handler.modname] !== 'undefined') {
|
||||
this.logger.log('There is an addon named \'' + this.handlers[handler.modname].name +
|
||||
'\' already registered as handler for ' + handler.modname);
|
||||
return false;
|
||||
}
|
||||
this.logger.log(`Registered addon '${handler.name}' for '${handler.modname}'`);
|
||||
this.handlers[handler.modname] = handler;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handler for the current site.
|
||||
*
|
||||
* @param {CoreCourseModuleHandler} handler The handler to check.
|
||||
* @param {number} time Time this update process started.
|
||||
* @return {Promise<void>} Resolved when done.
|
||||
*/
|
||||
protected updateHandler(handler: CoreCourseModuleHandler, time: number) : Promise<void> {
|
||||
let promise,
|
||||
siteId = this.sitesProvider.getCurrentSiteId(),
|
||||
currentSite = this.sitesProvider.getCurrentSite();
|
||||
|
||||
if (!this.sitesProvider.isLoggedIn()) {
|
||||
promise = Promise.reject(null);
|
||||
} else if (currentSite.isFeatureDisabled('$mmCourseDelegate_' + handler.name)) {
|
||||
promise = Promise.resolve(false);
|
||||
} else {
|
||||
promise = Promise.resolve(handler.isEnabled());
|
||||
}
|
||||
|
||||
// Checks if the handler is enabled.
|
||||
return promise.catch(() => {
|
||||
return false;
|
||||
}).then((enabled: boolean) => {
|
||||
// Verify that this call is the last one that was started.
|
||||
if (this.isLastUpdateCall(time) && this.sitesProvider.getCurrentSiteId() === siteId) {
|
||||
if (enabled) {
|
||||
this.enabledHandlers[handler.modname] = handler;
|
||||
} else {
|
||||
delete this.enabledHandlers[handler.modname];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handlers for the current site.
|
||||
*
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
protected updateHandlers() : Promise<any> {
|
||||
let promises = [],
|
||||
now = Date.now();
|
||||
|
||||
this.logger.debug('Updating handlers for current site.');
|
||||
|
||||
this.lastUpdateHandlersStart = now;
|
||||
|
||||
// Loop over all the handlers.
|
||||
for (let name in this.handlers) {
|
||||
promises.push(this.updateHandler(this.handlers[name], now));
|
||||
}
|
||||
|
||||
return Promise.all(promises).catch(() => {
|
||||
// Never reject.
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import { CoreSiteWSPreSets } from '../../../classes/site';
|
|||
import { CoreConstants } from '../../constants';
|
||||
import { Md5 } from 'ts-md5/dist/md5';
|
||||
import { Subject, BehaviorSubject, Subscription } from 'rxjs';
|
||||
import { CoreDelegate, CoreDelegateHandler } from '../../../classes/delegate';
|
||||
|
||||
/**
|
||||
* Progress of downloading a list of modules.
|
||||
|
@ -54,19 +55,7 @@ export type CoreCourseModulesProgressFunction = (data: CoreCourseModulesProgress
|
|||
/**
|
||||
* Interface that all course prefetch handlers must implement.
|
||||
*/
|
||||
export interface CoreCourseModulePrefetchHandler {
|
||||
/**
|
||||
* A name to identify the addon.
|
||||
* @type {string}
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Name of the module. It should match the "modname" of the module returned in core_course_get_contents.
|
||||
* @type {string}
|
||||
*/
|
||||
modname: string;
|
||||
|
||||
export interface CoreCourseModulePrefetchHandler extends CoreDelegateHandler {
|
||||
/**
|
||||
* The handler's component.
|
||||
* @type {string}
|
||||
|
@ -80,13 +69,6 @@ export interface CoreCourseModulePrefetchHandler {
|
|||
*/
|
||||
updatesNames?: RegExp;
|
||||
|
||||
/**
|
||||
* Whether or not the handler is enabled on a site level.
|
||||
*
|
||||
* @return {boolean|Promise<boolean>} A boolean, or a promise resolved with a boolean, indicating if the handler is enabled.
|
||||
*/
|
||||
isEnabled() : boolean|Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Get the download size of a module.
|
||||
*
|
||||
|
@ -194,13 +176,13 @@ export interface CoreCourseModulePrefetchHandler {
|
|||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
removeFiles?(module: any, courseId: number): Promise<any>;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate to register module prefetch handlers.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreCourseModulePrefetchDelegate {
|
||||
export class CoreCourseModulePrefetchDelegate extends CoreDelegate {
|
||||
// Variables for database.
|
||||
protected CHECK_UPDATES_TIMES_TABLE = 'check_updates_times';
|
||||
protected checkUpdatesTableSchema = {
|
||||
|
@ -217,31 +199,33 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
notNull: true
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
protected ROOT_CACHE_KEY = 'mmCourse:';
|
||||
|
||||
protected logger;
|
||||
protected handlers: { [s: string]: CoreCourseModulePrefetchHandler } = {}; // All registered handlers.
|
||||
protected enabledHandlers: { [s: string]: CoreCourseModulePrefetchHandler } = {}; // Handlers enabled for the current site.
|
||||
protected statusCache = new CoreCache();
|
||||
protected lastUpdateHandlersStart: number;
|
||||
|
||||
// Promises for check updates, to prevent performing the same request twice at the same time.
|
||||
protected courseUpdatesPromises: { [s: string]: { [s: string]: Promise<any> } } = {};
|
||||
|
||||
// Promises and observables for prefetching, to prevent downloading the same section twice at the same time
|
||||
// and notify the progress of the download.
|
||||
protected prefetchData: {[s: string]: {[s: string]: {
|
||||
protected prefetchData: {
|
||||
[s: string]: {
|
||||
[s: string]: {
|
||||
promise: Promise<any>,
|
||||
observable: Subject<CoreCourseModulesProgress>,
|
||||
subscriptions: Subscription[]
|
||||
}}} = {};
|
||||
}
|
||||
}
|
||||
} = {};
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private eventsProvider: CoreEventsProvider,
|
||||
constructor(loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider, protected eventsProvider: CoreEventsProvider,
|
||||
private courseProvider: CoreCourseProvider, private filepoolProvider: CoreFilepoolProvider,
|
||||
private timeUtils: CoreTimeUtilsProvider, private utils: CoreUtilsProvider, private fileProvider: CoreFileProvider) {
|
||||
this.logger = logger.getInstance('CoreCourseModulePrefetchDelegate');
|
||||
super('CoreCourseModulePrefetchDelegate', loggerProvider, sitesProvider);
|
||||
|
||||
this.sitesProvider.createTableFromSchema(this.checkUpdatesTableSchema);
|
||||
}
|
||||
|
@ -309,7 +293,7 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
result.toCheck.push({
|
||||
contextlevel: 'module',
|
||||
id: module.id,
|
||||
since: data.downloadTime || 0
|
||||
since: data.downloadTime || 0
|
||||
});
|
||||
} else {
|
||||
// Cannot use check updates, add it to the cannotUse array.
|
||||
|
@ -430,7 +414,7 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
preSets.omitExpires = true;
|
||||
|
||||
return site.read('core_course_check_updates', params, preSets).then((response) => {
|
||||
if (!response || typeof response.instances == 'undefined') {
|
||||
if (!response || typeof response.instances == 'undefined') {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
|
@ -450,7 +434,6 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
return this.courseUpdatesPromises[siteId][id];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check for updates in a course.
|
||||
*
|
||||
|
@ -934,20 +917,6 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
return !!(this.prefetchData[siteId] && this.prefetchData[siteId][id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a time belongs to the last update handlers call.
|
||||
* This is to handle the cases where updateHandlers don't finish in the same order as they're called.
|
||||
*
|
||||
* @param {number} time Time to check.
|
||||
* @return {boolean} Whether it's the last call.
|
||||
*/
|
||||
isLastUpdateCall(time: number) : boolean {
|
||||
if (!this.lastUpdateHandlersStart) {
|
||||
return true;
|
||||
}
|
||||
return time == this.lastUpdateHandlersStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a module is downloadable.
|
||||
*
|
||||
|
@ -998,7 +967,7 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
if (handler && handler.hasUpdates) {
|
||||
// Handler implements its own function to check the updates, use it.
|
||||
return Promise.resolve(handler.hasUpdates(module, courseId, moduleUpdates));
|
||||
} else if (!moduleUpdates || !moduleUpdates.updates || !moduleUpdates.updates.length) {
|
||||
} else if (!moduleUpdates || !moduleUpdates.updates || !moduleUpdates.updates.length) {
|
||||
// Module doesn't have any update.
|
||||
return Promise.resolve(false);
|
||||
} else if (handler && handler.updatesNames && handler.updatesNames.test) {
|
||||
|
@ -1115,23 +1084,6 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
return prefetchData.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a handler.
|
||||
*
|
||||
* @param {CoreCourseModulePrefetchHandler} handler The handler to register.
|
||||
* @return {boolean} True if registered successfully, false otherwise.
|
||||
*/
|
||||
registerHandler(handler: CoreCourseModulePrefetchHandler) : boolean {
|
||||
if (typeof this.handlers[handler.modname] !== 'undefined') {
|
||||
this.logger.log('There is an addon named \'' + this.handlers[handler.modname].name +
|
||||
'\' already registered as a prefetch handler for ' + handler.modname);
|
||||
return false;
|
||||
}
|
||||
this.logger.log(`Registered addon '${handler.name}' as a prefetch handler for '${handler.modname}'`);
|
||||
this.handlers[handler.modname] = handler;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove module Files from handler.
|
||||
*
|
||||
|
@ -1219,60 +1171,6 @@ export class CoreCourseModulePrefetchDelegate {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the enabled handlers for the current site.
|
||||
*
|
||||
* @param {CoreCourseModulePrefetchHandler} handler The handler to treat.
|
||||
* @param {number} time Time this update process started.
|
||||
* @return {Promise<void>} Resolved when done.
|
||||
*/
|
||||
updateHandler(handler: CoreCourseModulePrefetchHandler, time: number) : Promise<void> {
|
||||
let promise,
|
||||
siteId = this.sitesProvider.getCurrentSiteId();
|
||||
|
||||
if (!siteId) {
|
||||
promise = Promise.reject(null);
|
||||
} else {
|
||||
promise = Promise.resolve(handler.isEnabled());
|
||||
}
|
||||
|
||||
// Checks if the prefetch is enabled.
|
||||
return promise.catch(() => {
|
||||
return false;
|
||||
}).then((enabled: boolean) => {
|
||||
// Verify that this call is the last one that was started.
|
||||
// Check that site hasn't changed since the check started.
|
||||
if (this.isLastUpdateCall(time) && this.sitesProvider.getCurrentSiteId() == siteId) {
|
||||
if (enabled) {
|
||||
this.enabledHandlers[handler.modname] = handler;
|
||||
} else {
|
||||
delete this.enabledHandlers[handler.modname];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handlers for the current site.
|
||||
*
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
updateHandlers() : Promise<any> {
|
||||
const promises = [],
|
||||
now = Date.now();
|
||||
|
||||
this.lastUpdateHandlersStart = now;
|
||||
|
||||
// Loop over all the handlers.
|
||||
for (let name in this.handlers) {
|
||||
promises.push(this.updateHandler(this.handlers[name], now));
|
||||
}
|
||||
|
||||
return Promise.all(promises).catch(() => {
|
||||
// Never reject.
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the status of a module in the "cache".
|
||||
*
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
// limitations under the License.
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CoreDelegate, CoreDelegateHandler } from '../../../classes/delegate';
|
||||
import { CoreEventsProvider } from '../../../providers/events';
|
||||
import { CoreLoggerProvider } from '../../../providers/logger';
|
||||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
|
@ -22,26 +23,13 @@ import { CoreCoursesProvider } from './courses';
|
|||
/**
|
||||
* Interface that all courses handlers must implement.
|
||||
*/
|
||||
export interface CoreCoursesHandler {
|
||||
/**
|
||||
* Name of the handler.
|
||||
* @type {string}
|
||||
*/
|
||||
name: string;
|
||||
|
||||
export interface CoreCoursesHandler extends CoreDelegateHandler {
|
||||
/**
|
||||
* The highest priority is displayed first.
|
||||
* @type {number}
|
||||
*/
|
||||
priority: number;
|
||||
|
||||
/**
|
||||
* Whether or not the handler is enabled on a site level.
|
||||
*
|
||||
* @return {boolean|Promise<boolean>} True or promise resolved with true if enabled.
|
||||
*/
|
||||
isEnabled(): boolean|Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Whether or not the handler is enabled for a certain course.
|
||||
* For perfomance reasons, do NOT call WebServices in here, call them in shouldDisplayForCourse.
|
||||
|
@ -90,7 +78,7 @@ export interface CoreCoursesHandler {
|
|||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
prefetch?(course: any): Promise<any>;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Data needed to render a course handler. It's returned by the handler.
|
||||
|
@ -120,7 +108,7 @@ export interface CoreCoursesHandlerData {
|
|||
* @param {any} course The course.
|
||||
*/
|
||||
action(course: any): void;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Data returned by the delegate for each handler.
|
||||
|
@ -145,29 +133,29 @@ export interface CoreCoursesHandlerToDisplay {
|
|||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
prefetch?(course: any): Promise<any>;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Service to interact with plugins to be shown in each course.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreCoursesDelegate {
|
||||
protected logger;
|
||||
export class CoreCoursesDelegate extends CoreDelegate {
|
||||
protected handlers: { [s: string]: CoreCoursesHandler } = {}; // All registered handlers.
|
||||
protected enabledHandlers: { [s: string]: CoreCoursesHandler } = {}; // Handlers enabled for the current site.
|
||||
protected loaded: { [courseId: number]: boolean } = {};
|
||||
protected lastUpdateHandlersStart: number;
|
||||
protected lastUpdateHandlersForCoursesStart: any = {};
|
||||
protected coursesHandlers: {[courseId: number]: {
|
||||
access?: any, navOptions?: any, admOptions?: any, deferred?: PromiseDefer, enabledHandlers?: CoreCoursesHandler[]}} = {};
|
||||
protected coursesHandlers: {
|
||||
[courseId: number]: {
|
||||
access?: any, navOptions?: any, admOptions?: any, deferred?: PromiseDefer, enabledHandlers?: CoreCoursesHandler[]
|
||||
}
|
||||
} = {};
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private eventsProvider: CoreEventsProvider,
|
||||
private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider) {
|
||||
this.logger = logger.getInstance('CoreMainMenuDelegate');
|
||||
protected featurePrefix = '$mmCoursesDelegate_';
|
||||
|
||||
constructor(loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider, private utils: CoreUtilsProvider,
|
||||
protected eventsProvider: CoreEventsProvider, private coursesProvider: CoreCoursesProvider) {
|
||||
super('CoreMainMenuDelegate', loggerProvider, sitesProvider, eventsProvider);
|
||||
|
||||
eventsProvider.on(CoreEventsProvider.LOGIN, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.SITE_UPDATED, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.REMOTE_ADDONS_LOADED, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.LOGOUT, () => {
|
||||
this.clearCoursesHandlers();
|
||||
});
|
||||
|
@ -205,7 +193,7 @@ export class CoreCoursesDelegate {
|
|||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
clearAndInvalidateCoursesOptions(courseId?: number): Promise<any> {
|
||||
var promises = [];
|
||||
let promises = [];
|
||||
|
||||
this.eventsProvider.trigger(CoreCoursesProvider.EVENT_MY_COURSES_REFRESHED);
|
||||
|
||||
|
@ -247,7 +235,7 @@ export class CoreCoursesDelegate {
|
|||
refresh = false;
|
||||
}
|
||||
|
||||
if (refresh || !this.coursesHandlers[courseId] || this.coursesHandlers[courseId].access.type != accessData.type) {
|
||||
if (refresh || !this.coursesHandlers[courseId] || this.coursesHandlers[courseId].access.type != accessData.type) {
|
||||
if (!this.coursesHandlers[courseId]) {
|
||||
this.coursesHandlers[courseId] = {};
|
||||
}
|
||||
|
@ -404,20 +392,6 @@ export class CoreCoursesDelegate {
|
|||
return this.utils.allPromises(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a time belongs to the last update handlers call.
|
||||
* This is to handle the cases where updateHandlers don't finish in the same order as they're called.
|
||||
*
|
||||
* @param {number} time Time to check.
|
||||
* @return {boolean} Whether it's the last call.
|
||||
*/
|
||||
isLastUpdateCall(time: number) : boolean {
|
||||
if (!this.lastUpdateHandlersStart) {
|
||||
return true;
|
||||
}
|
||||
return time == this.lastUpdateHandlersStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a time belongs to the last update handlers for course call.
|
||||
* This is to handle the cases where updateHandlersForCourse don't finish in the same order as they're called.
|
||||
|
@ -451,92 +425,14 @@ export class CoreCoursesDelegate {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a handler.
|
||||
*
|
||||
* @param {CoreCoursesHandler} handler The handler to register.
|
||||
* @return {boolean} True if registered successfully, false otherwise.
|
||||
*/
|
||||
registerHandler(handler: CoreCoursesHandler) : boolean {
|
||||
if (typeof this.handlers[handler.name] !== 'undefined') {
|
||||
this.logger.log(`Addon '${handler.name}' already registered`);
|
||||
return false;
|
||||
}
|
||||
this.logger.log(`Registered addon '${handler.name}'`);
|
||||
this.handlers[handler.name] = handler;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handler for the current site.
|
||||
*
|
||||
* @param {CoreInitHandler} handler The handler to check.
|
||||
* @param {number} time Time this update process started.
|
||||
* @return {Promise<void>} Resolved when done.
|
||||
*/
|
||||
protected updateHandler(handler: CoreCoursesHandler, time: number) : Promise<void> {
|
||||
let promise,
|
||||
siteId = this.sitesProvider.getCurrentSiteId(),
|
||||
currentSite = this.sitesProvider.getCurrentSite();
|
||||
|
||||
if (!this.sitesProvider.isLoggedIn()) {
|
||||
promise = Promise.reject(null);
|
||||
} else if (currentSite.isFeatureDisabled('$mmCoursesDelegate_' + handler.name)) {
|
||||
promise = Promise.resolve(false);
|
||||
} else {
|
||||
promise = Promise.resolve(handler.isEnabled());
|
||||
}
|
||||
|
||||
// Checks if the handler is enabled.
|
||||
return promise.catch(() => {
|
||||
return false;
|
||||
}).then((enabled: boolean) => {
|
||||
// Verify that this call is the last one that was started.
|
||||
// Check that site hasn't changed since the check started.
|
||||
if (this.isLastUpdateCall(time) && this.sitesProvider.getCurrentSiteId() === siteId) {
|
||||
if (enabled) {
|
||||
this.enabledHandlers[handler.name] = handler;
|
||||
} else {
|
||||
delete this.enabledHandlers[handler.name];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handlers for the current site.
|
||||
*
|
||||
* @return {Promise<void>} Resolved when done.
|
||||
*/
|
||||
protected updateHandlers() : Promise<void> {
|
||||
let promises = [],
|
||||
siteId = this.sitesProvider.getCurrentSiteId(),
|
||||
now = Date.now();
|
||||
|
||||
this.logger.debug('Updating handlers for current site.');
|
||||
|
||||
this.lastUpdateHandlersStart = now;
|
||||
|
||||
// Loop over all the handlers.
|
||||
for (let name in this.handlers) {
|
||||
promises.push(this.updateHandler(this.handlers[name], now));
|
||||
}
|
||||
|
||||
return Promise.all(promises).then(() => {
|
||||
return true;
|
||||
}, () => {
|
||||
// Never reject.
|
||||
return true;
|
||||
}).then(() => {
|
||||
// Verify that this call is the last one that was started.
|
||||
if (this.isLastUpdateCall(now) && this.sitesProvider.getCurrentSiteId() === siteId) {
|
||||
updateData(siteId?: string) {
|
||||
if (this.sitesProvider.getCurrentSiteId() === siteId) {
|
||||
// Update handlers for all courses.
|
||||
for (let courseId in this.coursesHandlers) {
|
||||
let handler = this.coursesHandlers[courseId];
|
||||
this.updateHandlersForCourse(parseInt(courseId, 10), handler.access, handler.navOptions, handler.admOptions);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -590,5 +486,5 @@ export class CoreCoursesDelegate {
|
|||
this.coursesHandlers[courseId].deferred.resolve();
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,30 +16,18 @@ import { Injectable } from '@angular/core';
|
|||
import { CoreEventsProvider } from '../../../providers/events';
|
||||
import { CoreLoggerProvider } from '../../../providers/logger';
|
||||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
import { CoreDelegate, CoreDelegateHandler } from '../../../classes/delegate';
|
||||
|
||||
/**
|
||||
* Interface that all handlers must implement.
|
||||
*/
|
||||
export interface CoreFileUploaderHandler {
|
||||
/**
|
||||
* A name to identify the addon.
|
||||
* @type {string}
|
||||
*/
|
||||
name: string;
|
||||
|
||||
export interface CoreFileUploaderHandler extends CoreDelegateHandler {
|
||||
/**
|
||||
* Handler's priority. The highest priority, the highest position.
|
||||
* @type {string}
|
||||
*/
|
||||
priority?: number;
|
||||
|
||||
/**
|
||||
* Whether or not the handler is enabled on a site level.
|
||||
*
|
||||
* @return {boolean|Promise<boolean>} True or promise resolved with true if enabled.
|
||||
*/
|
||||
isEnabled(): boolean|Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Given a list of mimetypes, return the ones that are supported by the handler.
|
||||
*
|
||||
|
@ -54,7 +42,7 @@ export interface CoreFileUploaderHandler {
|
|||
* @return {CoreFileUploaderHandlerData} Data.
|
||||
*/
|
||||
getData(): CoreFileUploaderHandlerData;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Data needed to render the handler in the file picker. It must be returned by the handler.
|
||||
|
@ -99,7 +87,7 @@ export interface CoreFileUploaderHandlerData {
|
|||
* @param {string[]} [mimetypes] List of supported mimetypes. If undefined, all mimetypes supported.
|
||||
*/
|
||||
afterRender?(maxSize: number, upload: boolean, allowOffline: boolean, mimetypes: string[]): void;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The result of clicking a handler.
|
||||
|
@ -134,7 +122,7 @@ export interface CoreFileUploaderHandlerResult {
|
|||
* @type {any}
|
||||
*/
|
||||
result?: any;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Data returned by the delegate for each handler.
|
||||
|
@ -146,30 +134,25 @@ export interface CoreFileUploaderHandlerDataToReturn extends CoreFileUploaderHan
|
|||
*/
|
||||
priority?: number;
|
||||
|
||||
|
||||
/**
|
||||
* Supported mimetypes.
|
||||
* @type {string[]}
|
||||
*/
|
||||
mimetypes?: string[];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate to register handlers to be shown in the file picker.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreFileUploaderDelegate {
|
||||
protected logger;
|
||||
export class CoreFileUploaderDelegate extends CoreDelegate {
|
||||
protected handlers: { [s: string]: CoreFileUploaderHandler } = {}; // All registered handlers.
|
||||
protected enabledHandlers: { [s: string]: CoreFileUploaderHandler } = {}; // Handlers enabled for the current site.
|
||||
protected lastUpdateHandlersStart: number;
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, eventsProvider: CoreEventsProvider) {
|
||||
this.logger = logger.getInstance('CoreFileUploaderDelegate');
|
||||
constructor(loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider,
|
||||
protected eventsProvider: CoreEventsProvider) {
|
||||
super('CoreFileUploaderDelegate', loggerProvider, sitesProvider, eventsProvider);
|
||||
|
||||
eventsProvider.on(CoreEventsProvider.LOGIN, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.SITE_UPDATED, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.REMOTE_ADDONS_LOADED, this.updateHandlers.bind(this));
|
||||
eventsProvider.on(CoreEventsProvider.LOGOUT, this.clearSiteHandlers.bind(this));
|
||||
}
|
||||
|
||||
|
@ -215,89 +198,4 @@ export class CoreFileUploaderDelegate {
|
|||
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a time belongs to the last update handlers call.
|
||||
* This is to handle the cases where updateHandlers don't finish in the same order as they're called.
|
||||
*
|
||||
* @param {number} time Time to check.
|
||||
* @return {boolean} Whether it's the last call.
|
||||
*/
|
||||
isLastUpdateCall(time: number) : boolean {
|
||||
if (!this.lastUpdateHandlersStart) {
|
||||
return true;
|
||||
}
|
||||
return time == this.lastUpdateHandlersStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a handler.
|
||||
*
|
||||
* @param {CoreFileUploaderHandler} handler The handler to register.
|
||||
* @return {boolean} True if registered successfully, false otherwise.
|
||||
*/
|
||||
registerHandler(handler: CoreFileUploaderHandler) : boolean {
|
||||
if (typeof this.handlers[handler.name] !== 'undefined') {
|
||||
this.logger.log(`Addon '${handler.name}' already registered`);
|
||||
return false;
|
||||
}
|
||||
this.logger.log(`Registered addon '${handler.name}'`);
|
||||
this.handlers[handler.name] = handler;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handler for the current site.
|
||||
*
|
||||
* @param {CoreFileUploaderHandler} handler The handler to check.
|
||||
* @param {number} time Time this update process started.
|
||||
* @return {Promise<void>} Resolved when done.
|
||||
*/
|
||||
protected updateHandler(handler: CoreFileUploaderHandler, time: number) : Promise<void> {
|
||||
let promise,
|
||||
siteId = this.sitesProvider.getCurrentSiteId();
|
||||
|
||||
if (!this.sitesProvider.isLoggedIn()) {
|
||||
promise = Promise.reject(null);
|
||||
} else {
|
||||
promise = Promise.resolve(handler.isEnabled());
|
||||
}
|
||||
|
||||
// Checks if the handler is enabled.
|
||||
return promise.catch(() => {
|
||||
return false;
|
||||
}).then((enabled: boolean) => {
|
||||
// Verify that this call is the last one that was started.
|
||||
if (this.isLastUpdateCall(time) && this.sitesProvider.getCurrentSiteId() === siteId) {
|
||||
if (enabled) {
|
||||
this.enabledHandlers[handler.name] = handler;
|
||||
} else {
|
||||
delete this.enabledHandlers[handler.name];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the handlers for the current site.
|
||||
*
|
||||
* @return {Promise<any>} Resolved when done.
|
||||
*/
|
||||
protected updateHandlers() : Promise<any> {
|
||||
let promises = [],
|
||||
now = Date.now();
|
||||
|
||||
this.logger.debug('Updating handlers for current site.');
|
||||
|
||||
this.lastUpdateHandlersStart = now;
|
||||
|
||||
// Loop over all the handlers.
|
||||
for (let name in this.handlers) {
|
||||
promises.push(this.updateHandler(this.handlers[name], now));
|
||||
}
|
||||
|
||||
return Promise.all(promises).catch(() => {
|
||||
// Never reject.
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ export interface CoreUserProfileHandler extends CoreDelegateHandler {
|
|||
* @return {CoreUserProfileHandlerData} Data to be shown.
|
||||
*/
|
||||
getDisplayData(user: any, courseId: number): CoreUserProfileHandlerData;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CoreUserProfileHandlerData {
|
||||
/**
|
||||
|
@ -94,7 +94,7 @@ export interface CoreUserProfileHandlerData {
|
|||
* @return {any} Action to be done.
|
||||
*/
|
||||
action?($event: any, user: any, courseId: number): any;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Service to interact with plugins to be shown in user profile. Provides functions to register a plugin
|
||||
|
|
Loading…
Reference in New Issue