Merge pull request #4129 from NoelDeMartin/MOBILE-4596
MOBILE-4596: Decouple data and wiki addons from initial bundle
This commit is contained in:
		
						commit
						cefe0e4ac1
					
				@ -15,7 +15,8 @@
 | 
			
		||||
import { Input, Output, OnInit, OnChanges, SimpleChange, EventEmitter, Component } from '@angular/core';
 | 
			
		||||
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
 | 
			
		||||
import { CoreFormFields } from '@singletons/form';
 | 
			
		||||
import { AddonModDataData, AddonModDataEntryField, AddonModDataField, AddonModDataTemplateMode } from '../services/data';
 | 
			
		||||
import { AddonModDataData, AddonModDataEntryField, AddonModDataField } from '../services/data';
 | 
			
		||||
import { AddonModDataTemplateMode } from '../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Base class for component to render a field.
 | 
			
		||||
 | 
			
		||||
@ -20,17 +20,20 @@ import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSites } from '@services/sites';
 | 
			
		||||
import { CoreEvents } from '@singletons/events';
 | 
			
		||||
import {
 | 
			
		||||
    AddonModDataAction,
 | 
			
		||||
    AddonModDataData,
 | 
			
		||||
    AddonModDataEntry,
 | 
			
		||||
    AddonModDataGetDataAccessInformationWSResponse,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
} from '../../services/data';
 | 
			
		||||
import { AddonModDataHelper } from '../../services/data-helper';
 | 
			
		||||
import { AddonModDataOffline } from '../../services/data-offline';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { AddonModDataActionsMenuComponent, AddonModDataActionsMenuItem } from '../actionsmenu/actionsmenu';
 | 
			
		||||
import { ADDON_MOD_DATA_ENTRY_CHANGED, ADDON_MOD_DATA_PAGE_NAME } from '../../constants';
 | 
			
		||||
import {
 | 
			
		||||
    ADDON_MOD_DATA_ENTRY_CHANGED,
 | 
			
		||||
    ADDON_MOD_DATA_PAGE_NAME,
 | 
			
		||||
    AddonModDataAction,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
} from '../../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Component that displays a database action.
 | 
			
		||||
 | 
			
		||||
@ -17,8 +17,9 @@ import { FormGroup } from '@angular/forms';
 | 
			
		||||
import { CoreDynamicComponent } from '@components/dynamic-component/dynamic-component';
 | 
			
		||||
import { CoreFormFields } from '@singletons/form';
 | 
			
		||||
import { AddonModDataEntryFieldInitialized, AddonModDataFieldPluginBaseComponent } from '../../classes/base-field-plugin-component';
 | 
			
		||||
import { AddonModDataData, AddonModDataField, AddonModDataTemplateMode } from '../../services/data';
 | 
			
		||||
import { AddonModDataData, AddonModDataField } from '../../services/data';
 | 
			
		||||
import { AddonModDataFieldsDelegate } from '../../services/data-fields-delegate';
 | 
			
		||||
import { AddonModDataTemplateMode } from '../../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Component that displays a database field plugin.
 | 
			
		||||
 | 
			
		||||
@ -31,8 +31,6 @@ import { CoreEventObserver, CoreEvents } from '@singletons/events';
 | 
			
		||||
import {
 | 
			
		||||
    AddonModData,
 | 
			
		||||
    AddonModDataEntry,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
    AddonModDataField,
 | 
			
		||||
    AddonModDataGetDataAccessInformationWSResponse,
 | 
			
		||||
    AddonModDataData,
 | 
			
		||||
@ -40,7 +38,7 @@ import {
 | 
			
		||||
} from '../../services/data';
 | 
			
		||||
import { AddonModDataHelper, AddonModDatDisplayFieldsOptions } from '../../services/data-helper';
 | 
			
		||||
import { AddonModDataAutoSyncData, AddonModDataSyncResult } from '../../services/data-sync';
 | 
			
		||||
import { AddonModDataPrefetchHandler } from '../../services/handlers/prefetch';
 | 
			
		||||
import { AddonModDataPrefetchHandler } from '../../services/handlers/prefetch-lazy';
 | 
			
		||||
import { AddonModDataComponentsCompileModule } from '../components-compile.module';
 | 
			
		||||
import { AddonModDataSearchComponent } from '../search/search';
 | 
			
		||||
import { CoreUrlUtils } from '@services/utils/url';
 | 
			
		||||
@ -51,6 +49,8 @@ import {
 | 
			
		||||
    ADDON_MOD_DATA_ENTRIES_PER_PAGE,
 | 
			
		||||
    ADDON_MOD_DATA_ENTRY_CHANGED,
 | 
			
		||||
    ADDON_MOD_DATA_PAGE_NAME,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
} from '../../constants';
 | 
			
		||||
 | 
			
		||||
const contentToken = '<!-- CORE-DATABASE-CONTENT-GOES-HERE -->';
 | 
			
		||||
 | 
			
		||||
@ -23,13 +23,13 @@ import { ModalController } from '@singletons';
 | 
			
		||||
import {
 | 
			
		||||
    AddonModDataField,
 | 
			
		||||
    AddonModDataData,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataSearchEntriesAdvancedField,
 | 
			
		||||
} from '../../services/data';
 | 
			
		||||
import { AddonModDataFieldsDelegate } from '../../services/data-fields-delegate';
 | 
			
		||||
import { AddonModDataHelper } from '../../services/data-helper';
 | 
			
		||||
import { AddonModDataComponentsCompileModule } from '../components-compile.module';
 | 
			
		||||
import { AddonModDataSearchDataParams } from '../index';
 | 
			
		||||
import { AddonModDataTemplateType } from '../../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Page that displays the search modal.
 | 
			
		||||
 | 
			
		||||
@ -21,3 +21,46 @@ export const ADDON_MOD_DATA_ENTRIES_PER_PAGE = 25;
 | 
			
		||||
 | 
			
		||||
export const ADDON_MOD_DATA_ENTRY_CHANGED = 'addon_mod_data_entry_changed';
 | 
			
		||||
export const ADDON_MOD_DATA_AUTO_SYNCED = 'addon_mod_data_autom_synced';
 | 
			
		||||
 | 
			
		||||
// Handlers.
 | 
			
		||||
export const ADDON_MOD_DATA_PREFETCH_NAME = 'AddonModData';
 | 
			
		||||
export const ADDON_MOD_DATA_PREFETCH_MODNAME = 'data';
 | 
			
		||||
export const ADDON_MOD_DATA_PREFETCH_COMPONENT = ADDON_MOD_DATA_COMPONENT;
 | 
			
		||||
 | 
			
		||||
export enum AddonModDataAction {
 | 
			
		||||
    ADD = 'add',
 | 
			
		||||
    EDIT = 'edit',
 | 
			
		||||
    DELETE = 'delete',
 | 
			
		||||
    APPROVE = 'approve',
 | 
			
		||||
    DISAPPROVE = 'disapprove',
 | 
			
		||||
    USER = 'user',
 | 
			
		||||
    USERPICTURE = 'userpicture',
 | 
			
		||||
    MORE = 'more',
 | 
			
		||||
    MOREURL = 'moreurl',
 | 
			
		||||
    COMMENTS = 'comments',
 | 
			
		||||
    TIMEADDED = 'timeadded',
 | 
			
		||||
    TIMEMODIFIED = 'timemodified',
 | 
			
		||||
    TAGS = 'tags',
 | 
			
		||||
    APPROVALSTATUS = 'approvalstatus',
 | 
			
		||||
    APPROVALSTATUSCLASS = 'approvalstatusclass',
 | 
			
		||||
    DELCHECK = 'delcheck', // Unused.
 | 
			
		||||
    EXPORT = 'export', // Unused.
 | 
			
		||||
    ACTIONSMENU = 'actionsmenu',
 | 
			
		||||
    ID = 'id',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export enum AddonModDataTemplateType {
 | 
			
		||||
    LIST_HEADER = 'listtemplateheader',
 | 
			
		||||
    LIST = 'listtemplate',
 | 
			
		||||
    LIST_FOOTER = 'listtemplatefooter',
 | 
			
		||||
    ADD = 'addtemplate',
 | 
			
		||||
    SEARCH = 'asearchtemplate',
 | 
			
		||||
    SINGLE = 'singletemplate',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export enum AddonModDataTemplateMode {
 | 
			
		||||
    LIST = 'list',
 | 
			
		||||
    EDIT = 'edit',
 | 
			
		||||
    SHOW = 'show',
 | 
			
		||||
    SEARCH = 'search',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -22,15 +22,15 @@ import { CoreTagAreaDelegate } from '@features/tag/services/tag-area-delegate';
 | 
			
		||||
import { CoreCronDelegate } from '@services/cron';
 | 
			
		||||
import { CORE_SITE_SCHEMAS } from '@services/sites';
 | 
			
		||||
import { ADDON_MOD_DATA_OFFLINE_SITE_SCHEMA } from './services/database/data';
 | 
			
		||||
import { AddonModDataApproveLinkHandler } from './services/handlers/approve-link';
 | 
			
		||||
import { AddonModDataDeleteLinkHandler } from './services/handlers/delete-link';
 | 
			
		||||
import { AddonModDataEditLinkHandler } from './services/handlers/edit-link';
 | 
			
		||||
import { getApproveLinkHandlerInstance } from './services/handlers/approve-link';
 | 
			
		||||
import { getDeleteLinkHandlerInstance } from './services/handlers/delete-link';
 | 
			
		||||
import { getEditLinkHandlerInstance } from './services/handlers/edit-link';
 | 
			
		||||
import { AddonModDataIndexLinkHandler } from './services/handlers/index-link';
 | 
			
		||||
import { AddonModDataListLinkHandler } from './services/handlers/list-link';
 | 
			
		||||
import { AddonModDataModuleHandler } from './services/handlers/module';
 | 
			
		||||
import { AddonModDataPrefetchHandler } from './services/handlers/prefetch';
 | 
			
		||||
import { AddonModDataShowLinkHandler } from './services/handlers/show-link';
 | 
			
		||||
import { AddonModDataSyncCronHandler } from './services/handlers/sync-cron';
 | 
			
		||||
import { getPrefetchHandlerInstance } from './services/handlers/prefetch';
 | 
			
		||||
import { getShowLinkHandlerInstance } from './services/handlers/show-link';
 | 
			
		||||
import { getCronHandlerInstance } from './services/handlers/sync-cron';
 | 
			
		||||
import { AddonModDataTagAreaHandler } from './services/handlers/tag-area';
 | 
			
		||||
import { AddonModDataFieldModule } from './fields/field.module';
 | 
			
		||||
import { CoreCourseHelper } from '@features/course/services/course-helper';
 | 
			
		||||
@ -58,15 +58,16 @@ const routes: Routes = [
 | 
			
		||||
            provide: APP_INITIALIZER,
 | 
			
		||||
            multi: true,
 | 
			
		||||
            useValue: () => {
 | 
			
		||||
                CoreCourseModulePrefetchDelegate.registerHandler(getPrefetchHandlerInstance());
 | 
			
		||||
                CoreCronDelegate.register(getCronHandlerInstance());
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(getApproveLinkHandlerInstance());
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(getDeleteLinkHandlerInstance());
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(getShowLinkHandlerInstance());
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(getEditLinkHandlerInstance());
 | 
			
		||||
 | 
			
		||||
                CoreCourseModuleDelegate.registerHandler(AddonModDataModuleHandler.instance);
 | 
			
		||||
                CoreCourseModulePrefetchDelegate.registerHandler(AddonModDataPrefetchHandler.instance);
 | 
			
		||||
                CoreCronDelegate.register(AddonModDataSyncCronHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModDataIndexLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModDataListLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModDataApproveLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModDataDeleteLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModDataShowLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModDataEditLinkHandler.instance);
 | 
			
		||||
                CoreTagAreaDelegate.registerHandler(AddonModDataTagAreaHandler.instance);
 | 
			
		||||
 | 
			
		||||
                CoreCourseHelper.registerModuleReminderClick(ADDON_MOD_DATA_COMPONENT);
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,6 @@ import {
 | 
			
		||||
    AddonModDataData,
 | 
			
		||||
    AddonModDataField,
 | 
			
		||||
    AddonModData,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataEntry,
 | 
			
		||||
    AddonModDataEntryFields,
 | 
			
		||||
    AddonModDataEditEntryResult,
 | 
			
		||||
@ -44,7 +43,7 @@ import { AddonModDataEntryFieldInitialized } from '../../classes/base-field-plug
 | 
			
		||||
import { CoreTextUtils } from '@services/utils/text';
 | 
			
		||||
import { CoreTime } from '@singletons/time';
 | 
			
		||||
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
 | 
			
		||||
import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRY_CHANGED } from '../../constants';
 | 
			
		||||
import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRY_CHANGED, AddonModDataTemplateType } from '../../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Page that displays the view edit page.
 | 
			
		||||
 | 
			
		||||
@ -30,8 +30,6 @@ import {
 | 
			
		||||
    AddonModDataData,
 | 
			
		||||
    AddonModDataGetDataAccessInformationWSResponse,
 | 
			
		||||
    AddonModDataField,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
    AddonModDataEntry,
 | 
			
		||||
} from '../../services/data';
 | 
			
		||||
import { AddonModDataHelper } from '../../services/data-helper';
 | 
			
		||||
@ -42,6 +40,8 @@ import {
 | 
			
		||||
    ADDON_MOD_DATA_COMPONENT,
 | 
			
		||||
    ADDON_MOD_DATA_ENTRIES_PER_PAGE,
 | 
			
		||||
    ADDON_MOD_DATA_ENTRY_CHANGED,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
} from '../../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 | 
			
		||||
@ -31,19 +31,22 @@ import {
 | 
			
		||||
    AddonModDataSearchEntriesOptions,
 | 
			
		||||
    AddonModDataEntries,
 | 
			
		||||
    AddonModDataEntryFields,
 | 
			
		||||
    AddonModDataAction,
 | 
			
		||||
    AddonModDataGetEntryFormatted,
 | 
			
		||||
    AddonModDataData,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataGetDataAccessInformationWSResponse,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
    AddonModDataField,
 | 
			
		||||
    AddonModDataEntryWSField,
 | 
			
		||||
} from './data';
 | 
			
		||||
import { AddonModDataFieldsDelegate } from './data-fields-delegate';
 | 
			
		||||
import { AddonModDataOffline, AddonModDataOfflineAction } from './data-offline';
 | 
			
		||||
import { CoreFileEntry } from '@services/file-helper';
 | 
			
		||||
import { ADDON_MOD_DATA_COMPONENT, ADDON_MOD_DATA_ENTRY_CHANGED } from '../constants';
 | 
			
		||||
import {
 | 
			
		||||
    ADDON_MOD_DATA_COMPONENT,
 | 
			
		||||
    ADDON_MOD_DATA_ENTRY_CHANGED,
 | 
			
		||||
    AddonModDataAction,
 | 
			
		||||
    AddonModDataTemplateType,
 | 
			
		||||
    AddonModDataTemplateMode,
 | 
			
		||||
} from '../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Service that provides helper functions for datas.
 | 
			
		||||
 | 
			
		||||
@ -20,8 +20,9 @@ import { CoreTextUtils } from '@services/utils/text';
 | 
			
		||||
import { CoreUtils } from '@services/utils/utils';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { CorePath } from '@singletons/path';
 | 
			
		||||
import { AddonModDataAction, AddonModDataEntryWSField } from './data';
 | 
			
		||||
import { AddonModDataEntryWSField } from './data';
 | 
			
		||||
import { AddonModDataEntryDBRecord, DATA_ENTRY_TABLE } from './database/data';
 | 
			
		||||
import { AddonModDataAction } from '../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Service to handle Offline data.
 | 
			
		||||
 | 
			
		||||
@ -29,10 +29,10 @@ import { CoreTextUtils } from '@services/utils/text';
 | 
			
		||||
import { CoreUtils } from '@services/utils/utils';
 | 
			
		||||
import { Translate, makeSingleton } from '@singletons';
 | 
			
		||||
import { CoreEvents } from '@singletons/events';
 | 
			
		||||
import { AddonModData, AddonModDataData, AddonModDataAction } from './data';
 | 
			
		||||
import { AddonModData, AddonModDataData } from './data';
 | 
			
		||||
import { AddonModDataHelper } from './data-helper';
 | 
			
		||||
import { AddonModDataOffline, AddonModDataOfflineAction } from './data-offline';
 | 
			
		||||
import { ADDON_MOD_DATA_AUTO_SYNCED, ADDON_MOD_DATA_COMPONENT } from '../constants';
 | 
			
		||||
import { ADDON_MOD_DATA_AUTO_SYNCED, ADDON_MOD_DATA_COMPONENT, AddonModDataAction } from '../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Service to sync databases.
 | 
			
		||||
 | 
			
		||||
@ -35,6 +35,7 @@ import {
 | 
			
		||||
    ADDON_MOD_DATA_COMPONENT,
 | 
			
		||||
    ADDON_MOD_DATA_ENTRIES_PER_PAGE,
 | 
			
		||||
    ADDON_MOD_DATA_ENTRY_CHANGED,
 | 
			
		||||
    AddonModDataAction,
 | 
			
		||||
} from '../constants';
 | 
			
		||||
 | 
			
		||||
declare module '@singletons/events' {
 | 
			
		||||
@ -50,44 +51,6 @@ declare module '@singletons/events' {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export enum AddonModDataAction {
 | 
			
		||||
    ADD = 'add',
 | 
			
		||||
    EDIT = 'edit',
 | 
			
		||||
    DELETE = 'delete',
 | 
			
		||||
    APPROVE = 'approve',
 | 
			
		||||
    DISAPPROVE = 'disapprove',
 | 
			
		||||
    USER = 'user',
 | 
			
		||||
    USERPICTURE = 'userpicture',
 | 
			
		||||
    MORE = 'more',
 | 
			
		||||
    MOREURL = 'moreurl',
 | 
			
		||||
    COMMENTS = 'comments',
 | 
			
		||||
    TIMEADDED = 'timeadded',
 | 
			
		||||
    TIMEMODIFIED = 'timemodified',
 | 
			
		||||
    TAGS = 'tags',
 | 
			
		||||
    APPROVALSTATUS = 'approvalstatus',
 | 
			
		||||
    APPROVALSTATUSCLASS = 'approvalstatusclass',
 | 
			
		||||
    DELCHECK = 'delcheck', // Unused.
 | 
			
		||||
    EXPORT = 'export', // Unused.
 | 
			
		||||
    ACTIONSMENU = 'actionsmenu',
 | 
			
		||||
    ID = 'id',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export enum AddonModDataTemplateType {
 | 
			
		||||
    LIST_HEADER = 'listtemplateheader',
 | 
			
		||||
    LIST = 'listtemplate',
 | 
			
		||||
    LIST_FOOTER = 'listtemplatefooter',
 | 
			
		||||
    ADD = 'addtemplate',
 | 
			
		||||
    SEARCH = 'asearchtemplate',
 | 
			
		||||
    SINGLE = 'singletemplate',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export enum AddonModDataTemplateMode {
 | 
			
		||||
    LIST = 'list',
 | 
			
		||||
    EDIT = 'edit',
 | 
			
		||||
    SHOW = 'show',
 | 
			
		||||
    SEARCH = 'search',
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Service that provides some features for databases.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { CoreSiteSchema } from '@services/sites';
 | 
			
		||||
import { AddonModDataAction } from '../data';
 | 
			
		||||
import { AddonModDataAction } from '../../constants';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Database variables for AddonModDataOfflineProvider.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										52
									
								
								src/addons/mod/data/services/handlers/approve-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/addons/mod/data/services/handlers/approve-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataHelper } from '../data-helper';
 | 
			
		||||
import { AddonModDataApproveLinkHandlerService } from '@addons/mod/data/services/handlers/approve-link';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database approve/disapprove entry.
 | 
			
		||||
 * Match mod/data/view.php?d=6&approve=5 with a valid data id and entryid.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataApproveLinkHandlerLazyService extends AddonModDataApproveLinkHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>, courseId?: number): Promise<void> {
 | 
			
		||||
        const dataId = parseInt(params.d, 10);
 | 
			
		||||
        const entryId = parseInt(params.approve, 10) || parseInt(params.disapprove, 10);
 | 
			
		||||
        const approve = parseInt(params.approve, 10) ? true : false;
 | 
			
		||||
 | 
			
		||||
        await AddonModDataHelper.approveOrDisapproveEntry(dataId, entryId, approve, courseId, siteId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined || (params.approve === undefined && params.disapprove === undefined)) {
 | 
			
		||||
            // Required fields not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModDataApproveLinkHandler = makeSingleton(AddonModDataApproveLinkHandlerLazyService);
 | 
			
		||||
@ -12,18 +12,12 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants';
 | 
			
		||||
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
 | 
			
		||||
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataHelper } from '../data-helper';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME } from '../../constants';
 | 
			
		||||
import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import type { AddonModDataApproveLinkHandlerLazyService } from '@addons/mod/data/services/handlers/approve-link-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database approve/disapprove entry.
 | 
			
		||||
 * Match mod/data/view.php?d=6&approve=5 with a valid data id and entryid.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataApproveLinkHandlerService extends CoreContentLinksHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModDataApproveLinkHandler';
 | 
			
		||||
@ -36,27 +30,41 @@ export class AddonModDataApproveLinkHandlerService extends CoreContentLinksHandl
 | 
			
		||||
     */
 | 
			
		||||
    getActions(siteIds: string[], url: string, params: Record<string, string>, courseId?: number): CoreContentLinksAction[] {
 | 
			
		||||
        return [{
 | 
			
		||||
            action: async (siteId): Promise<void> => {
 | 
			
		||||
                const dataId = parseInt(params.d, 10);
 | 
			
		||||
                const entryId = parseInt(params.approve, 10) || parseInt(params.disapprove, 10);
 | 
			
		||||
                const approve = parseInt(params.approve, 10) ? true : false;
 | 
			
		||||
 | 
			
		||||
                await AddonModDataHelper.approveOrDisapproveEntry(dataId, entryId, approve, courseId, siteId);
 | 
			
		||||
            },
 | 
			
		||||
            action: (siteId) => this.handleAction(siteId, params, courseId),
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     * Handle link action.
 | 
			
		||||
     *
 | 
			
		||||
     * @param siteId Site id.
 | 
			
		||||
     * @param params Params.
 | 
			
		||||
     * @param courseId Course id.
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined || (params.approve === undefined && params.disapprove === undefined)) {
 | 
			
		||||
            // Required fields not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>, courseId?: number): Promise<void> {
 | 
			
		||||
        // Stub to override.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataApproveLinkHandler = makeSingleton(AddonModDataApproveLinkHandlerService);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get approve link handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Link handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getApproveLinkHandlerInstance(): CoreContentLinksHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModDataApproveLinkHandlerLazyService,
 | 
			
		||||
        AddonModDataApproveLinkHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModDataApproveLinkHandler } = await import('./approve-link-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModDataApproveLinkHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModDataApproveLinkHandlerService());
 | 
			
		||||
    lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										50
									
								
								src/addons/mod/data/services/handlers/delete-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								src/addons/mod/data/services/handlers/delete-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataHelper } from '../data-helper';
 | 
			
		||||
import { AddonModDataDeleteLinkHandlerService } from '@addons/mod/data/services/handlers/delete-link';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database delete entry.
 | 
			
		||||
 * Match mod/data/view.php?d=6&delete=5 with a valid data id and entryid.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataDeleteLinkHandlerLazyService extends AddonModDataDeleteLinkHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>, courseId?: number): Promise<void> {
 | 
			
		||||
        const dataId = parseInt(params.d, 10);
 | 
			
		||||
        const entryId = parseInt(params.delete, 10);
 | 
			
		||||
 | 
			
		||||
        await AddonModDataHelper.showDeleteEntryModal(dataId, entryId, courseId, siteId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined || params.delete === undefined) {
 | 
			
		||||
            // Required fields not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataDeleteLinkHandler = makeSingleton(AddonModDataDeleteLinkHandlerLazyService);
 | 
			
		||||
@ -12,18 +12,12 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants';
 | 
			
		||||
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
 | 
			
		||||
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataHelper } from '../data-helper';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME } from '../../constants';
 | 
			
		||||
import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import type { AddonModDataDeleteLinkHandlerLazyService } from '@addons/mod/data/services/handlers/delete-link-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database delete entry.
 | 
			
		||||
 * Match mod/data/view.php?d=6&delete=5 with a valid data id and entryid.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataDeleteLinkHandlerService extends CoreContentLinksHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModDataDeleteLinkHandler';
 | 
			
		||||
@ -35,26 +29,41 @@ export class AddonModDataDeleteLinkHandlerService extends CoreContentLinksHandle
 | 
			
		||||
     */
 | 
			
		||||
    getActions(siteIds: string[], url: string, params: Record<string, string>, courseId?: number): CoreContentLinksAction[] {
 | 
			
		||||
        return [{
 | 
			
		||||
            action: async (siteId): Promise<void> => {
 | 
			
		||||
                const dataId = parseInt(params.d, 10);
 | 
			
		||||
                const entryId = parseInt(params.delete, 10);
 | 
			
		||||
 | 
			
		||||
                await AddonModDataHelper.showDeleteEntryModal(dataId, entryId, courseId, siteId);
 | 
			
		||||
            },
 | 
			
		||||
            action: (siteId) => this.handleAction(siteId, params, courseId),
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     * Handle link action.
 | 
			
		||||
     *
 | 
			
		||||
     * @param siteId Site id.
 | 
			
		||||
     * @param params Params.
 | 
			
		||||
     * @param courseId Course id.
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined || params.delete === undefined) {
 | 
			
		||||
            // Required fields not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>, courseId?: number): Promise<void> {
 | 
			
		||||
        // Stub to override.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataDeleteLinkHandler = makeSingleton(AddonModDataDeleteLinkHandlerService);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get delete link handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Link handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getDeleteLinkHandlerInstance(): CoreContentLinksHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModDataDeleteLinkHandlerLazyService,
 | 
			
		||||
        AddonModDataDeleteLinkHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModDataDeleteLinkHandler } = await import('./delete-link-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModDataDeleteLinkHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModDataDeleteLinkHandlerService());
 | 
			
		||||
    lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										77
									
								
								src/addons/mod/data/services/handlers/edit-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/addons/mod/data/services/handlers/edit-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { Params } from '@angular/router';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME, ADDON_MOD_DATA_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { AddonModDataEditLinkHandlerService } from '@addons/mod/data/services/handlers/edit-link';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database add or edit entry.
 | 
			
		||||
 * Match mod/data/edit.php?d=6&rid=6 with a valid data and optional record id.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataEditLinkHandlerLazyService extends AddonModDataEditLinkHandlerService {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModDataEditLinkHandler';
 | 
			
		||||
    featureName = ADDON_MOD_DATA_FEATURE_NAME;
 | 
			
		||||
    pattern = /\/mod\/data\/edit\.php.*([?&](d|rid)=\d+)/;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
        const dataId = parseInt(params.d, 10);
 | 
			
		||||
        const rId = params.rid || '';
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                dataId,
 | 
			
		||||
                'data',
 | 
			
		||||
                { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
            );
 | 
			
		||||
            const pageParams: Params = {
 | 
			
		||||
                title: module.name,
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/edit/${rId}`,
 | 
			
		||||
                { siteId, params: pageParams },
 | 
			
		||||
            );
 | 
			
		||||
        } finally {
 | 
			
		||||
            // Just in case. In fact we need to dismiss the modal before showing a toast or error message.
 | 
			
		||||
            modal.dismiss();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined) {
 | 
			
		||||
            // Id not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataEditLinkHandler = makeSingleton(AddonModDataEditLinkHandlerLazyService);
 | 
			
		||||
@ -12,22 +12,12 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { Params } from '@angular/router';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants';
 | 
			
		||||
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
 | 
			
		||||
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME, ADDON_MOD_DATA_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import type { AddonModDataEditLinkHandlerLazyService } from '@addons/mod/data/services/handlers/edit-link-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database add or edit entry.
 | 
			
		||||
 * Match mod/data/edit.php?d=6&rid=6 with a valid data and optional record id.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataEditLinkHandlerService extends CoreContentLinksHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModDataEditLinkHandler';
 | 
			
		||||
@ -39,44 +29,40 @@ export class AddonModDataEditLinkHandlerService extends CoreContentLinksHandlerB
 | 
			
		||||
     */
 | 
			
		||||
    getActions(siteIds: string[], url: string, params: Record<string, string>): CoreContentLinksAction[] {
 | 
			
		||||
        return [{
 | 
			
		||||
            action: async (siteId): Promise<void> => {
 | 
			
		||||
                const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
                const dataId = parseInt(params.d, 10);
 | 
			
		||||
                const rId = params.rid || '';
 | 
			
		||||
 | 
			
		||||
                try {
 | 
			
		||||
                    const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                        dataId,
 | 
			
		||||
                        'data',
 | 
			
		||||
                        { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
                    );
 | 
			
		||||
                    const pageParams: Params = {
 | 
			
		||||
                        title: module.name,
 | 
			
		||||
                    };
 | 
			
		||||
 | 
			
		||||
                    await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                        `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/edit/${rId}`,
 | 
			
		||||
                        { siteId, params: pageParams },
 | 
			
		||||
                    );
 | 
			
		||||
                } finally {
 | 
			
		||||
                    // Just in case. In fact we need to dismiss the modal before showing a toast or error message.
 | 
			
		||||
                    modal.dismiss();
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            action: (siteId) => this.handleAction(siteId, params),
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     * Handle link action.
 | 
			
		||||
     *
 | 
			
		||||
     * @param siteId Site id.
 | 
			
		||||
     * @param params Params.
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined) {
 | 
			
		||||
            // Id not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        // Stub to override.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataEditLinkHandler = makeSingleton(AddonModDataEditLinkHandlerService);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get edit link handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Link handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getEditLinkHandlerInstance(): CoreContentLinksHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModDataEditLinkHandlerLazyService,
 | 
			
		||||
        AddonModDataEditLinkHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModDataEditLinkHandler } = await import('./edit-link-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModDataEditLinkHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModDataEditLinkHandlerService());
 | 
			
		||||
    lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										291
									
								
								src/addons/mod/data/services/handlers/prefetch-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										291
									
								
								src/addons/mod/data/services/handlers/prefetch-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,291 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { CoreComments } from '@features/comments/services/comments';
 | 
			
		||||
import { CoreCourseCommonModWSOptions, CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course';
 | 
			
		||||
import { CoreCourses } from '@features/courses/services/courses';
 | 
			
		||||
import { CoreFilepool } from '@services/filepool';
 | 
			
		||||
import { CoreGroup, CoreGroups } from '@services/groups';
 | 
			
		||||
import { CoreSitesCommonWSOptions, CoreSites, CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreTimeUtils } from '@services/utils/time';
 | 
			
		||||
import { CoreUtils } from '@services/utils/utils';
 | 
			
		||||
import { CoreWSFile } from '@services/ws';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataEntry, AddonModData, AddonModDataData } from '../data';
 | 
			
		||||
import { AddonModDataSync, AddonModDataSyncResult } from '../data-sync';
 | 
			
		||||
import { ContextLevel } from '@/core/constants';
 | 
			
		||||
import { AddonModDataPrefetchHandlerService } from '@addons/mod/data/services/handlers/prefetch';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to prefetch databases.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataPrefetchHandlerLazyService extends AddonModDataPrefetchHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Retrieves all the entries for all the groups and then returns only unique entries.
 | 
			
		||||
     *
 | 
			
		||||
     * @param dataId Database Id.
 | 
			
		||||
     * @param groups Array of groups in the activity.
 | 
			
		||||
     * @param options Other options.
 | 
			
		||||
     * @returns All unique entries.
 | 
			
		||||
     */
 | 
			
		||||
    protected async getAllUniqueEntries(
 | 
			
		||||
        dataId: number,
 | 
			
		||||
        groups: CoreGroup[],
 | 
			
		||||
        options: CoreSitesCommonWSOptions = {},
 | 
			
		||||
    ): Promise<AddonModDataEntry[]> {
 | 
			
		||||
 | 
			
		||||
        const promises = groups.map((group) => AddonModData.fetchAllEntries(dataId, {
 | 
			
		||||
            groupId: group.id,
 | 
			
		||||
            ...options, // Include all options.
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
        const responses = await Promise.all(promises);
 | 
			
		||||
 | 
			
		||||
        const uniqueEntries: Record<number, AddonModDataEntry> = {};
 | 
			
		||||
        responses.forEach((groupEntries) => {
 | 
			
		||||
            groupEntries.forEach((entry) => {
 | 
			
		||||
                uniqueEntries[entry.id] = entry;
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return CoreUtils.objectToArray(uniqueEntries);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Helper function to get all database info just once.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module to get the files.
 | 
			
		||||
     * @param courseId Course ID the module belongs to.
 | 
			
		||||
     * @param omitFail True to always return even if fails. Default false.
 | 
			
		||||
     * @param options Other options.
 | 
			
		||||
     * @returns Promise resolved with the info fetched.
 | 
			
		||||
     */
 | 
			
		||||
    protected async getDatabaseInfoHelper(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        omitFail: boolean,
 | 
			
		||||
        options: CoreCourseCommonModWSOptions = {},
 | 
			
		||||
    ): Promise<{ database: AddonModDataData; groups: CoreGroup[]; entries: AddonModDataEntry[]; files: CoreWSFile[]}> {
 | 
			
		||||
        let groups: CoreGroup[] = [];
 | 
			
		||||
        let entries: AddonModDataEntry[] = [];
 | 
			
		||||
        let files: CoreWSFile[] = [];
 | 
			
		||||
 | 
			
		||||
        options.cmId = options.cmId || module.id;
 | 
			
		||||
        options.siteId = options.siteId || CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        const database = await AddonModData.getDatabase(courseId, module.id, options);
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            files = this.getIntroFilesFromInstance(module, database);
 | 
			
		||||
 | 
			
		||||
            const groupInfo = await CoreGroups.getActivityGroupInfo(module.id, false, undefined, options.siteId);
 | 
			
		||||
            if (!groupInfo.groups || groupInfo.groups.length == 0) {
 | 
			
		||||
                groupInfo.groups = [{ id: 0, name: '' }];
 | 
			
		||||
            }
 | 
			
		||||
            groups = groupInfo.groups || [];
 | 
			
		||||
 | 
			
		||||
            entries = await this.getAllUniqueEntries(database.id, groups, options);
 | 
			
		||||
            files = files.concat(this.getEntriesFiles(entries));
 | 
			
		||||
 | 
			
		||||
            return {
 | 
			
		||||
                database,
 | 
			
		||||
                groups,
 | 
			
		||||
                entries,
 | 
			
		||||
                files,
 | 
			
		||||
            };
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            if (omitFail) {
 | 
			
		||||
                // Any error, return the info we have.
 | 
			
		||||
                return {
 | 
			
		||||
                    database,
 | 
			
		||||
                    groups,
 | 
			
		||||
                    entries,
 | 
			
		||||
                    files,
 | 
			
		||||
                };
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            throw error;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the file contained in the entries.
 | 
			
		||||
     *
 | 
			
		||||
     * @param entries List of entries to get files from.
 | 
			
		||||
     * @returns List of files.
 | 
			
		||||
     */
 | 
			
		||||
    protected getEntriesFiles(entries: AddonModDataEntry[]): CoreWSFile[] {
 | 
			
		||||
        let files: CoreWSFile[] = [];
 | 
			
		||||
 | 
			
		||||
        entries.forEach((entry) => {
 | 
			
		||||
            CoreUtils.objectToArray(entry.contents).forEach((content) => {
 | 
			
		||||
                files = files.concat(<CoreWSFile[]>content.files);
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return files;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
 | 
			
		||||
        return this.getDatabaseInfoHelper(module, courseId, true).then((info) => info.files);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
 | 
			
		||||
        const data = await CoreUtils.ignoreErrors(AddonModData.getDatabase(courseId, module.id));
 | 
			
		||||
 | 
			
		||||
        return this.getIntroFilesFromInstance(module, data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async invalidateContent(moduleId: number, courseId: number): Promise<void> {
 | 
			
		||||
        await AddonModData.invalidateContent(moduleId, courseId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
 | 
			
		||||
        const promises: Promise<void>[] = [];
 | 
			
		||||
        promises.push(AddonModData.invalidateDatabaseData(courseId));
 | 
			
		||||
        promises.push(AddonModData.invalidateDatabaseAccessInformationData(module.instance));
 | 
			
		||||
 | 
			
		||||
        await Promise.all(promises);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async isDownloadable(module: CoreCourseAnyModuleData, courseId: number): Promise<boolean> {
 | 
			
		||||
        const database = await AddonModData.getDatabase(courseId, module.id, {
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE,
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        const accessData = await AddonModData.getDatabaseAccessInformation(database.id, { cmId: module.id });
 | 
			
		||||
        // Check if database is restricted by time.
 | 
			
		||||
        if (!accessData.timeavailable) {
 | 
			
		||||
            const time = CoreTimeUtils.timestamp();
 | 
			
		||||
 | 
			
		||||
            // It is restricted, checking times.
 | 
			
		||||
            if (database.timeavailablefrom && time < database.timeavailablefrom) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            if (database.timeavailableto && time > database.timeavailableto) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
 | 
			
		||||
        return this.prefetchPackage(module, courseId, (siteId) => this.prefetchDatabase(module, courseId, siteId));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Prefetch a database.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module.
 | 
			
		||||
     * @param courseId Course ID the module belongs to.
 | 
			
		||||
     * @param siteId Site ID.
 | 
			
		||||
     * @returns Promise resolved when done.
 | 
			
		||||
     */
 | 
			
		||||
    protected async prefetchDatabase(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise<void> {
 | 
			
		||||
        const options = {
 | 
			
		||||
            cmId: module.id,
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
 | 
			
		||||
            siteId,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        const info = await this.getDatabaseInfoHelper(module, courseId, false, options);
 | 
			
		||||
 | 
			
		||||
        // Prefetch the database data.
 | 
			
		||||
        const database = info.database;
 | 
			
		||||
 | 
			
		||||
        const commentsEnabled = CoreComments.areCommentsEnabledInSite();
 | 
			
		||||
 | 
			
		||||
        const promises: Promise<unknown>[] = [];
 | 
			
		||||
 | 
			
		||||
        promises.push(AddonModData.getFields(database.id, options));
 | 
			
		||||
        promises.push(CoreFilepool.addFilesToQueue(siteId, info.files, this.component, module.id));
 | 
			
		||||
 | 
			
		||||
        info.groups.forEach((group) => {
 | 
			
		||||
            promises.push(AddonModData.getDatabaseAccessInformation(database.id, {
 | 
			
		||||
                groupId: group.id,
 | 
			
		||||
                ...options, // Include all options.
 | 
			
		||||
            }));
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        info.entries.forEach((entry) => {
 | 
			
		||||
            promises.push(AddonModData.getEntry(database.id, entry.id, options));
 | 
			
		||||
 | 
			
		||||
            if (commentsEnabled && database.comments) {
 | 
			
		||||
                promises.push(CoreComments.getComments(
 | 
			
		||||
                    ContextLevel.MODULE,
 | 
			
		||||
                    database.coursemodule,
 | 
			
		||||
                    'mod_data',
 | 
			
		||||
                    entry.id,
 | 
			
		||||
                    'database_entry',
 | 
			
		||||
                    0,
 | 
			
		||||
                    siteId,
 | 
			
		||||
                ));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Add Basic Info to manage links.
 | 
			
		||||
        promises.push(CoreCourse.getModuleBasicInfoByInstance(database.id, 'data', { siteId }));
 | 
			
		||||
 | 
			
		||||
        // Get course data, needed to determine upload max size if it's configured to be course limit.
 | 
			
		||||
        promises.push(CoreUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
 | 
			
		||||
 | 
			
		||||
        await Promise.all(promises);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sync a module.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module.
 | 
			
		||||
     * @param courseId Course ID the module belongs to
 | 
			
		||||
     * @param siteId Site ID. If not defined, current site.
 | 
			
		||||
     * @returns Promise resolved when done.
 | 
			
		||||
     */
 | 
			
		||||
    async sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModDataSyncResult> {
 | 
			
		||||
        const promises = [
 | 
			
		||||
            AddonModDataSync.syncDatabase(module.instance, siteId),
 | 
			
		||||
            AddonModDataSync.syncRatings(module.id, true, siteId),
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        const results = await Promise.all(promises);
 | 
			
		||||
 | 
			
		||||
        return results.reduce((a, b) => ({
 | 
			
		||||
            updated: a.updated || b.updated,
 | 
			
		||||
            warnings: (a.warnings || []).concat(b.warnings || []),
 | 
			
		||||
        }), { updated: false , warnings: [] });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataPrefetchHandler = makeSingleton(AddonModDataPrefetchHandlerLazyService);
 | 
			
		||||
@ -12,286 +12,50 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { CoreComments } from '@features/comments/services/comments';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import {
 | 
			
		||||
    ADDON_MOD_DATA_PREFETCH_COMPONENT,
 | 
			
		||||
    ADDON_MOD_DATA_PREFETCH_MODNAME,
 | 
			
		||||
    ADDON_MOD_DATA_PREFETCH_NAME,
 | 
			
		||||
} from '@addons/mod/data/constants';
 | 
			
		||||
import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler';
 | 
			
		||||
import { CoreCourseCommonModWSOptions, CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course';
 | 
			
		||||
import { CoreCourses } from '@features/courses/services/courses';
 | 
			
		||||
import { CoreFilepool } from '@services/filepool';
 | 
			
		||||
import { CoreGroup, CoreGroups } from '@services/groups';
 | 
			
		||||
import { CoreSitesCommonWSOptions, CoreSites, CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreTimeUtils } from '@services/utils/time';
 | 
			
		||||
import { CoreUtils } from '@services/utils/utils';
 | 
			
		||||
import { CoreWSFile } from '@services/ws';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataEntry, AddonModData, AddonModDataData } from '../data';
 | 
			
		||||
import { AddonModDataSync, AddonModDataSyncResult } from '../data-sync';
 | 
			
		||||
import { ContextLevel } from '@/core/constants';
 | 
			
		||||
import { ADDON_MOD_DATA_COMPONENT } from '../../constants';
 | 
			
		||||
import { CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate';
 | 
			
		||||
import type { AddonModDataPrefetchHandlerLazyService } from '@addons/mod/data/services/handlers/prefetch-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to prefetch databases.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModData';
 | 
			
		||||
    modName = 'data';
 | 
			
		||||
    component = ADDON_MOD_DATA_COMPONENT;
 | 
			
		||||
    name = ADDON_MOD_DATA_PREFETCH_NAME;
 | 
			
		||||
    modName = ADDON_MOD_DATA_PREFETCH_MODNAME;
 | 
			
		||||
    component = ADDON_MOD_DATA_PREFETCH_COMPONENT;
 | 
			
		||||
    updatesNames = /^configuration$|^.*files$|^entries$|^gradeitems$|^outcomes$|^comments$|^ratings/;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Retrieves all the entries for all the groups and then returns only unique entries.
 | 
			
		||||
     *
 | 
			
		||||
     * @param dataId Database Id.
 | 
			
		||||
     * @param groups Array of groups in the activity.
 | 
			
		||||
     * @param options Other options.
 | 
			
		||||
     * @returns All unique entries.
 | 
			
		||||
     */
 | 
			
		||||
    protected async getAllUniqueEntries(
 | 
			
		||||
        dataId: number,
 | 
			
		||||
        groups: CoreGroup[],
 | 
			
		||||
        options: CoreSitesCommonWSOptions = {},
 | 
			
		||||
    ): Promise<AddonModDataEntry[]> {
 | 
			
		||||
 | 
			
		||||
        const promises = groups.map((group) => AddonModData.fetchAllEntries(dataId, {
 | 
			
		||||
            groupId: group.id,
 | 
			
		||||
            ...options, // Include all options.
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
        const responses = await Promise.all(promises);
 | 
			
		||||
 | 
			
		||||
        const uniqueEntries: Record<number, AddonModDataEntry> = {};
 | 
			
		||||
        responses.forEach((groupEntries) => {
 | 
			
		||||
            groupEntries.forEach((entry) => {
 | 
			
		||||
                uniqueEntries[entry.id] = entry;
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return CoreUtils.objectToArray(uniqueEntries);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Helper function to get all database info just once.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module to get the files.
 | 
			
		||||
     * @param courseId Course ID the module belongs to.
 | 
			
		||||
     * @param omitFail True to always return even if fails. Default false.
 | 
			
		||||
     * @param options Other options.
 | 
			
		||||
     * @returns Promise resolved with the info fetched.
 | 
			
		||||
     */
 | 
			
		||||
    protected async getDatabaseInfoHelper(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        omitFail: boolean,
 | 
			
		||||
        options: CoreCourseCommonModWSOptions = {},
 | 
			
		||||
    ): Promise<{ database: AddonModDataData; groups: CoreGroup[]; entries: AddonModDataEntry[]; files: CoreWSFile[]}> {
 | 
			
		||||
        let groups: CoreGroup[] = [];
 | 
			
		||||
        let entries: AddonModDataEntry[] = [];
 | 
			
		||||
        let files: CoreWSFile[] = [];
 | 
			
		||||
 | 
			
		||||
        options.cmId = options.cmId || module.id;
 | 
			
		||||
        options.siteId = options.siteId || CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        const database = await AddonModData.getDatabase(courseId, module.id, options);
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            files = this.getIntroFilesFromInstance(module, database);
 | 
			
		||||
 | 
			
		||||
            const groupInfo = await CoreGroups.getActivityGroupInfo(module.id, false, undefined, options.siteId);
 | 
			
		||||
            if (!groupInfo.groups || groupInfo.groups.length == 0) {
 | 
			
		||||
                groupInfo.groups = [{ id: 0, name: '' }];
 | 
			
		||||
            }
 | 
			
		||||
            groups = groupInfo.groups || [];
 | 
			
		||||
 | 
			
		||||
            entries = await this.getAllUniqueEntries(database.id, groups, options);
 | 
			
		||||
            files = files.concat(this.getEntriesFiles(entries));
 | 
			
		||||
 | 
			
		||||
            return {
 | 
			
		||||
                database,
 | 
			
		||||
                groups,
 | 
			
		||||
                entries,
 | 
			
		||||
                files,
 | 
			
		||||
            };
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            if (omitFail) {
 | 
			
		||||
                // Any error, return the info we have.
 | 
			
		||||
                return {
 | 
			
		||||
                    database,
 | 
			
		||||
                    groups,
 | 
			
		||||
                    entries,
 | 
			
		||||
                    files,
 | 
			
		||||
                };
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            throw error;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns the file contained in the entries.
 | 
			
		||||
     *
 | 
			
		||||
     * @param entries List of entries to get files from.
 | 
			
		||||
     * @returns List of files.
 | 
			
		||||
     */
 | 
			
		||||
    protected getEntriesFiles(entries: AddonModDataEntry[]): CoreWSFile[] {
 | 
			
		||||
        let files: CoreWSFile[] = [];
 | 
			
		||||
 | 
			
		||||
        entries.forEach((entry) => {
 | 
			
		||||
            CoreUtils.objectToArray(entry.contents).forEach((content) => {
 | 
			
		||||
                files = files.concat(<CoreWSFile[]>content.files);
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return files;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
 | 
			
		||||
        return this.getDatabaseInfoHelper(module, courseId, true).then((info) => info.files);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getIntroFiles(module: CoreCourseAnyModuleData, courseId: number): Promise<CoreWSFile[]> {
 | 
			
		||||
        const data = await CoreUtils.ignoreErrors(AddonModData.getDatabase(courseId, module.id));
 | 
			
		||||
 | 
			
		||||
        return this.getIntroFilesFromInstance(module, data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async invalidateContent(moduleId: number, courseId: number): Promise<void> {
 | 
			
		||||
        await AddonModData.invalidateContent(moduleId, courseId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async invalidateModule(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
 | 
			
		||||
        const promises: Promise<void>[] = [];
 | 
			
		||||
        promises.push(AddonModData.invalidateDatabaseData(courseId));
 | 
			
		||||
        promises.push(AddonModData.invalidateDatabaseAccessInformationData(module.instance));
 | 
			
		||||
 | 
			
		||||
        await Promise.all(promises);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async isDownloadable(module: CoreCourseAnyModuleData, courseId: number): Promise<boolean> {
 | 
			
		||||
        const database = await AddonModData.getDatabase(courseId, module.id, {
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE,
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        const accessData = await AddonModData.getDatabaseAccessInformation(database.id, { cmId: module.id });
 | 
			
		||||
        // Check if database is restricted by time.
 | 
			
		||||
        if (!accessData.timeavailable) {
 | 
			
		||||
            const time = CoreTimeUtils.timestamp();
 | 
			
		||||
 | 
			
		||||
            // It is restricted, checking times.
 | 
			
		||||
            if (database.timeavailablefrom && time < database.timeavailablefrom) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            if (database.timeavailableto && time > database.timeavailableto) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    prefetch(module: CoreCourseAnyModuleData, courseId: number): Promise<void> {
 | 
			
		||||
        return this.prefetchPackage(module, courseId, (siteId) => this.prefetchDatabase(module, courseId, siteId));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Prefetch a database.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module.
 | 
			
		||||
     * @param courseId Course ID the module belongs to.
 | 
			
		||||
     * @param siteId Site ID.
 | 
			
		||||
     * @returns Promise resolved when done.
 | 
			
		||||
     */
 | 
			
		||||
    protected async prefetchDatabase(module: CoreCourseAnyModuleData, courseId: number, siteId: string): Promise<void> {
 | 
			
		||||
        const options = {
 | 
			
		||||
            cmId: module.id,
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
 | 
			
		||||
            siteId,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        const info = await this.getDatabaseInfoHelper(module, courseId, false, options);
 | 
			
		||||
 | 
			
		||||
        // Prefetch the database data.
 | 
			
		||||
        const database = info.database;
 | 
			
		||||
 | 
			
		||||
        const commentsEnabled = CoreComments.areCommentsEnabledInSite();
 | 
			
		||||
 | 
			
		||||
        const promises: Promise<unknown>[] = [];
 | 
			
		||||
 | 
			
		||||
        promises.push(AddonModData.getFields(database.id, options));
 | 
			
		||||
        promises.push(CoreFilepool.addFilesToQueue(siteId, info.files, this.component, module.id));
 | 
			
		||||
 | 
			
		||||
        info.groups.forEach((group) => {
 | 
			
		||||
            promises.push(AddonModData.getDatabaseAccessInformation(database.id, {
 | 
			
		||||
                groupId: group.id,
 | 
			
		||||
                ...options, // Include all options.
 | 
			
		||||
            }));
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        info.entries.forEach((entry) => {
 | 
			
		||||
            promises.push(AddonModData.getEntry(database.id, entry.id, options));
 | 
			
		||||
 | 
			
		||||
            if (commentsEnabled && database.comments) {
 | 
			
		||||
                promises.push(CoreComments.getComments(
 | 
			
		||||
                    ContextLevel.MODULE,
 | 
			
		||||
                    database.coursemodule,
 | 
			
		||||
                    'mod_data',
 | 
			
		||||
                    entry.id,
 | 
			
		||||
                    'database_entry',
 | 
			
		||||
                    0,
 | 
			
		||||
                    siteId,
 | 
			
		||||
                ));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Add Basic Info to manage links.
 | 
			
		||||
        promises.push(CoreCourse.getModuleBasicInfoByInstance(database.id, 'data', { siteId }));
 | 
			
		||||
 | 
			
		||||
        // Get course data, needed to determine upload max size if it's configured to be course limit.
 | 
			
		||||
        promises.push(CoreUtils.ignoreErrors(CoreCourses.getCourseByField('id', courseId, siteId)));
 | 
			
		||||
 | 
			
		||||
        await Promise.all(promises);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sync a module.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module.
 | 
			
		||||
     * @param courseId Course ID the module belongs to
 | 
			
		||||
     * @param siteId Site ID. If not defined, current site.
 | 
			
		||||
     * @returns Promise resolved when done.
 | 
			
		||||
     */
 | 
			
		||||
    async sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModDataSyncResult> {
 | 
			
		||||
        const promises = [
 | 
			
		||||
            AddonModDataSync.syncDatabase(module.instance, siteId),
 | 
			
		||||
            AddonModDataSync.syncRatings(module.id, true, siteId),
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        const results = await Promise.all(promises);
 | 
			
		||||
 | 
			
		||||
        return results.reduce((a, b) => ({
 | 
			
		||||
            updated: a.updated || b.updated,
 | 
			
		||||
            warnings: (a.warnings || []).concat(b.warnings || []),
 | 
			
		||||
        }), { updated: false , warnings: [] });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataPrefetchHandler = makeSingleton(AddonModDataPrefetchHandlerService);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get prefetch handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Prefetch handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModDataPrefetchHandlerLazyService,
 | 
			
		||||
        AddonModDataPrefetchHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModDataPrefetchHandler } = await import('./prefetch-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModDataPrefetchHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModDataPrefetchHandlerService());
 | 
			
		||||
    lazyHandler.setLazyMethods(['sync']);
 | 
			
		||||
    lazyHandler.setLazyOverrides([
 | 
			
		||||
        'getFiles',
 | 
			
		||||
        'getIntroFiles',
 | 
			
		||||
        'invalidateContent',
 | 
			
		||||
        'invalidateModule',
 | 
			
		||||
        'isDownloadable',
 | 
			
		||||
        'prefetch',
 | 
			
		||||
    ]);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										87
									
								
								src/addons/mod/data/services/handlers/show-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								src/addons/mod/data/services/handlers/show-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,87 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { Params } from '@angular/router';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { ADDON_MOD_DATA_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { AddonModDataShowLinkHandlerService } from '@addons/mod/data/services/handlers/show-link';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database show entry.
 | 
			
		||||
 * Match mod/data/view.php?d=6&rid=5 with a valid data id and entryid.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataShowLinkHandlerLazyService extends AddonModDataShowLinkHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
        const dataId = parseInt(params.d, 10);
 | 
			
		||||
        const rId = params.rid || '';
 | 
			
		||||
        const group = parseInt(params.group, 10) || false;
 | 
			
		||||
        const page = parseInt(params.page, 10) || false;
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                dataId,
 | 
			
		||||
                'data',
 | 
			
		||||
                { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
            );
 | 
			
		||||
            const pageParams: Params = {
 | 
			
		||||
                title: module.name,
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            if (group) {
 | 
			
		||||
                pageParams.group = group;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (params.mode && params.mode == 'single') {
 | 
			
		||||
                pageParams.offset = page || 0;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/${rId}`,
 | 
			
		||||
                { siteId, params: pageParams },
 | 
			
		||||
            );
 | 
			
		||||
        } finally {
 | 
			
		||||
            // Just in case. In fact we need to dismiss the modal before showing a toast or error message.
 | 
			
		||||
            modal.dismiss();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined) {
 | 
			
		||||
            // Id not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ((!params.mode || params.mode != 'single') && params.rid === undefined) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataShowLinkHandler = makeSingleton(AddonModDataShowLinkHandlerLazyService);
 | 
			
		||||
@ -12,22 +12,12 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { Params } from '@angular/router';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME } from '@addons/mod/data/constants';
 | 
			
		||||
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
 | 
			
		||||
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { ADDON_MOD_DATA_FEATURE_NAME, ADDON_MOD_DATA_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import type { AddonModDataShowLinkHandlerLazyService } from '@addons/mod/data/services/handlers/show-link-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Content links handler for database show entry.
 | 
			
		||||
 * Match mod/data/view.php?d=6&rid=5 with a valid data id and entryid.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataShowLinkHandlerService extends CoreContentLinksHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModDataShowLinkHandler';
 | 
			
		||||
@ -40,58 +30,40 @@ export class AddonModDataShowLinkHandlerService extends CoreContentLinksHandlerB
 | 
			
		||||
     */
 | 
			
		||||
    getActions(siteIds: string[], url: string, params: Record<string, string>): CoreContentLinksAction[] {
 | 
			
		||||
        return [{
 | 
			
		||||
            action: async (siteId): Promise<void> => {
 | 
			
		||||
                const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
                const dataId = parseInt(params.d, 10);
 | 
			
		||||
                const rId = params.rid || '';
 | 
			
		||||
                const group = parseInt(params.group, 10) || false;
 | 
			
		||||
                const page = parseInt(params.page, 10) || false;
 | 
			
		||||
 | 
			
		||||
                try {
 | 
			
		||||
                    const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                        dataId,
 | 
			
		||||
                        'data',
 | 
			
		||||
                        { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
                    );
 | 
			
		||||
                    const pageParams: Params = {
 | 
			
		||||
                        title: module.name,
 | 
			
		||||
                    };
 | 
			
		||||
 | 
			
		||||
                    if (group) {
 | 
			
		||||
                        pageParams.group = group;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if (params.mode && params.mode == 'single') {
 | 
			
		||||
                        pageParams.offset = page || 0;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                        `${ADDON_MOD_DATA_PAGE_NAME}/${module.course}/${module.id}/${rId}`,
 | 
			
		||||
                        { siteId, params: pageParams },
 | 
			
		||||
                    );
 | 
			
		||||
                } finally {
 | 
			
		||||
                    // Just in case. In fact we need to dismiss the modal before showing a toast or error message.
 | 
			
		||||
                    modal.dismiss();
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            action: (siteId) => this.handleAction(siteId, params),
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     * Handle link action.
 | 
			
		||||
     *
 | 
			
		||||
     * @param siteId Site id.
 | 
			
		||||
     * @param params Params.
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        if (params.d === undefined) {
 | 
			
		||||
            // Id not defined. Cannot treat the URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ((!params.mode || params.mode != 'single') && params.rid === undefined) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        // Stub to override.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataShowLinkHandler = makeSingleton(AddonModDataShowLinkHandlerService);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get show link handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Link handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getShowLinkHandlerInstance(): CoreContentLinksHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModDataShowLinkHandlerLazyService,
 | 
			
		||||
        AddonModDataShowLinkHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModDataShowLinkHandler } = await import('./show-link-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModDataShowLinkHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModDataShowLinkHandlerService());
 | 
			
		||||
    lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								src/addons/mod/data/services/handlers/sync-cron-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/addons/mod/data/services/handlers/sync-cron-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { CoreCronHandler } from '@services/cron';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataSync } from '../data-sync';
 | 
			
		||||
import { AddonModDataSyncCronHandlerService } from '@addons/mod/data/services/handlers/sync-cron';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Synchronization cron handler.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataSyncCronHandlerLazyService extends AddonModDataSyncCronHandlerService implements CoreCronHandler {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    execute(siteId?: string, force?: boolean): Promise<void> {
 | 
			
		||||
        return AddonModDataSync.syncAllDatabases(siteId, force);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    getInterval(): number {
 | 
			
		||||
        return AddonModDataSync.syncInterval;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModDataSyncCronHandler = makeSingleton(AddonModDataSyncCronHandlerLazyService);
 | 
			
		||||
@ -12,32 +12,33 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { CoreCronHandler } from '@services/cron';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModDataSync } from '../data-sync';
 | 
			
		||||
import type { AddonModDataSyncCronHandlerLazyService } from '@addons/mod/data/services/handlers/sync-cron-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Synchronization cron handler.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModDataSyncCronHandlerService implements CoreCronHandler {
 | 
			
		||||
export class AddonModDataSyncCronHandlerService {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModDataSyncCronHandler';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    execute(siteId?: string, force?: boolean): Promise<void> {
 | 
			
		||||
        return AddonModDataSync.syncAllDatabases(siteId, force);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    getInterval(): number {
 | 
			
		||||
        return AddonModDataSync.syncInterval;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
export const AddonModDataSyncCronHandler = makeSingleton(AddonModDataSyncCronHandlerService);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get cron handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Cron handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getCronHandlerInstance(): CoreCronHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModDataSyncCronHandlerLazyService,
 | 
			
		||||
        AddonModDataSyncCronHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModDataSyncCronHandler } = await import('./sync-cron-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModDataSyncCronHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModDataSyncCronHandlerService());
 | 
			
		||||
    lazyHandler.setLazyMethods(['execute', 'getInterval']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -50,8 +50,7 @@ import {
 | 
			
		||||
    AddonModWikiSyncWikiResult,
 | 
			
		||||
    AddonModWikiSyncWikiSubwiki,
 | 
			
		||||
} from '../../services/wiki-sync';
 | 
			
		||||
import { AddonModWikiMapModalComponent, AddonModWikiMapModalReturn } from '../map/map';
 | 
			
		||||
import { AddonModWikiSubwikiPickerComponent } from '../subwiki-picker/subwiki-picker';
 | 
			
		||||
import { AddonModWikiMapModalReturn } from '../map/map';
 | 
			
		||||
import {
 | 
			
		||||
    ADDON_MOD_WIKI_AUTO_SYNCED,
 | 
			
		||||
    ADDON_MOD_WIKI_COMPONENT,
 | 
			
		||||
@ -665,6 +664,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
 | 
			
		||||
     */
 | 
			
		||||
    async openMap(): Promise<void> {
 | 
			
		||||
        // Create the toc modal.
 | 
			
		||||
        const { AddonModWikiMapModalComponent } = await import('../map/map');
 | 
			
		||||
        const modalData = await CoreDomUtils.openSideModal<AddonModWikiMapModalReturn>({
 | 
			
		||||
            component: AddonModWikiMapModalComponent,
 | 
			
		||||
            componentProps: {
 | 
			
		||||
@ -883,6 +883,7 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp
 | 
			
		||||
     * @param event Event.
 | 
			
		||||
     */
 | 
			
		||||
    async showSubwikiPicker(event: MouseEvent): Promise<void> {
 | 
			
		||||
        const { AddonModWikiSubwikiPickerComponent } = await import('../subwiki-picker/subwiki-picker');
 | 
			
		||||
        const subwiki = await CoreDomUtils.openPopover<AddonModWikiSubwiki>({
 | 
			
		||||
            component: AddonModWikiSubwikiPickerComponent,
 | 
			
		||||
            componentProps: {
 | 
			
		||||
 | 
			
		||||
@ -21,3 +21,9 @@ export const ADDON_MOD_WIKI_MANUAL_SYNCED = 'addon_mod_wiki_manual_synced';
 | 
			
		||||
 | 
			
		||||
export const ADDON_MOD_WIKI_PAGE_CREATED_EVENT = 'addon_mod_wiki_page_created';
 | 
			
		||||
export const ADDON_MOD_WIKI_RENEW_LOCK_TIME = 30000; // Milliseconds.
 | 
			
		||||
 | 
			
		||||
export const ADDON_MOD_WIKI_FEATURE_NAME = 'CoreCourseModuleDelegate_AddonModWiki';
 | 
			
		||||
 | 
			
		||||
export const ADDON_MOD_WIKI_PREFETCH_NAME = 'AddonModWiki';
 | 
			
		||||
export const ADDON_MOD_WIKI_PREFETCH_MODNAME = 'wiki';
 | 
			
		||||
export const ADDON_MOD_WIKI_PREFETCH_COMPONENT = ADDON_MOD_WIKI_COMPONENT;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										142
									
								
								src/addons/mod/wiki/services/handlers/create-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										142
									
								
								src/addons/mod/wiki/services/handlers/create-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,142 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { ActivatedRoute } from '@angular/router';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWiki } from '../wiki';
 | 
			
		||||
import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { AddonModWikiCreateLinkHandlerService } from '@addons/mod/wiki/services/handlers/create-link';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to treat links to create a wiki page.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiCreateLinkHandlerLazyService extends AddonModWikiCreateLinkHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check if the current view is a wiki page of the same wiki.
 | 
			
		||||
     *
 | 
			
		||||
     * @param route Activated route if current route is wiki index page, null otherwise.
 | 
			
		||||
     * @param subwikiId Subwiki ID to check.
 | 
			
		||||
     * @param siteId Site ID.
 | 
			
		||||
     * @returns Promise resolved with boolean: whether current view belongs to the same wiki.
 | 
			
		||||
     */
 | 
			
		||||
    protected async currentStateIsSameWiki(route: ActivatedRoute | null, subwikiId: number, siteId: string): Promise<boolean> {
 | 
			
		||||
        if (!route) {
 | 
			
		||||
            // Current view isn't wiki index.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const params = CoreNavigator.getRouteParams(route);
 | 
			
		||||
        const queryParams = CoreNavigator.getRouteQueryParams(route);
 | 
			
		||||
 | 
			
		||||
        if (queryParams.subwikiId == subwikiId) {
 | 
			
		||||
            // Same subwiki, so it's same wiki.
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const options = {
 | 
			
		||||
            cmId: params.cmId,
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE,
 | 
			
		||||
            siteId,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        if (queryParams.pageId) {
 | 
			
		||||
            // Get the page contents to check the subwiki.
 | 
			
		||||
            try {
 | 
			
		||||
                const page = await AddonModWiki.getPageContents(queryParams.pageId, options);
 | 
			
		||||
 | 
			
		||||
                return page.subwikiid == subwikiId;
 | 
			
		||||
            } catch {
 | 
			
		||||
                // Not found, check next case.
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            // Get the wiki.
 | 
			
		||||
            const wiki = await AddonModWiki.getWiki(params.courseId, params.cmId, options);
 | 
			
		||||
 | 
			
		||||
            // Check if the subwiki belongs to this wiki.
 | 
			
		||||
            return await AddonModWiki.wikiHasSubwiki(wiki.id, subwikiId, options);
 | 
			
		||||
        } catch {
 | 
			
		||||
            // Not found, return false.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async handleAction(siteId: string, courseId: number, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
        const { AddonModWikiIndexPage } = await import('../../pages/index');
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const route = CoreNavigator.getCurrentRoute({ pageComponent: AddonModWikiIndexPage });
 | 
			
		||||
            if (!route) {
 | 
			
		||||
                // Current view isn't wiki index.
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            const subwikiId = parseInt(params.swid, 10);
 | 
			
		||||
            const wikiId = parseInt(params.wid, 10);
 | 
			
		||||
            let path = ADDON_MOD_WIKI_PAGE_NAME;
 | 
			
		||||
 | 
			
		||||
            // Check if the link is inside the same wiki.
 | 
			
		||||
            const isSameWiki = await this.currentStateIsSameWiki(route, subwikiId, siteId);
 | 
			
		||||
 | 
			
		||||
            if (isSameWiki) {
 | 
			
		||||
                // User is seeing the wiki, we can get the module from the wiki params.
 | 
			
		||||
                const routeParams = CoreNavigator.getRouteParams(route);
 | 
			
		||||
 | 
			
		||||
                path = path + `/${routeParams.courseId}/${routeParams.cmId}/edit`;
 | 
			
		||||
            } else if (wikiId) {
 | 
			
		||||
                // The URL specifies which wiki it belongs to. Get the module.
 | 
			
		||||
                const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                    wikiId,
 | 
			
		||||
                    'wiki',
 | 
			
		||||
                    { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
                );
 | 
			
		||||
 | 
			
		||||
                path = path + `/${module.course}/${module.id}/edit`;
 | 
			
		||||
            } else {
 | 
			
		||||
                // Cannot get module ID.
 | 
			
		||||
                path = path + `/${courseId || 0}/0/edit`;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Open the page.
 | 
			
		||||
            CoreNavigator.navigateToSitePath(
 | 
			
		||||
                path,
 | 
			
		||||
                {
 | 
			
		||||
                    params: {
 | 
			
		||||
                        pageTitle: params.title,
 | 
			
		||||
                        subwikiId: subwikiId,
 | 
			
		||||
                    },
 | 
			
		||||
                    siteId,
 | 
			
		||||
                },
 | 
			
		||||
            );
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true);
 | 
			
		||||
        } finally {
 | 
			
		||||
            modal.dismiss();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiCreateLinkHandler = makeSingleton(AddonModWikiCreateLinkHandlerLazyService);
 | 
			
		||||
@ -12,80 +12,18 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { ActivatedRoute } from '@angular/router';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { ADDON_MOD_WIKI_FEATURE_NAME } from '@addons/mod/wiki/constants';
 | 
			
		||||
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
 | 
			
		||||
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWikiIndexPage } from '../../pages/index';
 | 
			
		||||
import { AddonModWiki } from '../wiki';
 | 
			
		||||
import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import type { AddonModWikiCreateLinkHandlerLazyService } from '@addons/mod/wiki/services/handlers/create-link-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to treat links to create a wiki page.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiCreateLinkHandlerService extends CoreContentLinksHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModWikiCreateLinkHandler';
 | 
			
		||||
    featureName = 'CoreCourseModuleDelegate_AddonModWiki';
 | 
			
		||||
    featureName = ADDON_MOD_WIKI_FEATURE_NAME;
 | 
			
		||||
    pattern = /\/mod\/wiki\/create\.php.*([&?]swid=\d+)/;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Check if the current view is a wiki page of the same wiki.
 | 
			
		||||
     *
 | 
			
		||||
     * @param route Activated route if current route is wiki index page, null otherwise.
 | 
			
		||||
     * @param subwikiId Subwiki ID to check.
 | 
			
		||||
     * @param siteId Site ID.
 | 
			
		||||
     * @returns Promise resolved with boolean: whether current view belongs to the same wiki.
 | 
			
		||||
     */
 | 
			
		||||
    protected async currentStateIsSameWiki(route: ActivatedRoute | null, subwikiId: number, siteId: string): Promise<boolean> {
 | 
			
		||||
        if (!route) {
 | 
			
		||||
            // Current view isn't wiki index.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const params = CoreNavigator.getRouteParams(route);
 | 
			
		||||
        const queryParams = CoreNavigator.getRouteQueryParams(route);
 | 
			
		||||
 | 
			
		||||
        if (queryParams.subwikiId == subwikiId) {
 | 
			
		||||
            // Same subwiki, so it's same wiki.
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const options = {
 | 
			
		||||
            cmId: params.cmId,
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE,
 | 
			
		||||
            siteId,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        if (queryParams.pageId) {
 | 
			
		||||
            // Get the page contents to check the subwiki.
 | 
			
		||||
            try {
 | 
			
		||||
                const page = await AddonModWiki.getPageContents(queryParams.pageId, options);
 | 
			
		||||
 | 
			
		||||
                return page.subwikiid == subwikiId;
 | 
			
		||||
            } catch {
 | 
			
		||||
                // Not found, check next case.
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            // Get the wiki.
 | 
			
		||||
            const wiki = await AddonModWiki.getWiki(params.courseId, params.cmId, options);
 | 
			
		||||
 | 
			
		||||
            // Check if the subwiki belongs to this wiki.
 | 
			
		||||
            return await AddonModWiki.wikiHasSubwiki(wiki.id, subwikiId, options);
 | 
			
		||||
        } catch {
 | 
			
		||||
            // Not found, return false.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
@ -94,65 +32,45 @@ export class AddonModWikiCreateLinkHandlerService extends CoreContentLinksHandle
 | 
			
		||||
        url: string,
 | 
			
		||||
        params: Record<string, string>,
 | 
			
		||||
        courseId?: number,
 | 
			
		||||
    ): CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
 | 
			
		||||
    ): CoreContentLinksAction[] {
 | 
			
		||||
        courseId = Number(courseId || params.courseid || params.cid);
 | 
			
		||||
 | 
			
		||||
        return [{
 | 
			
		||||
            action: async (siteId: string) => {
 | 
			
		||||
                const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
 | 
			
		||||
                try {
 | 
			
		||||
                    const route = CoreNavigator.getCurrentRoute({ pageComponent: AddonModWikiIndexPage });
 | 
			
		||||
                    if (!route) {
 | 
			
		||||
                        // Current view isn't wiki index.
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    const subwikiId = parseInt(params.swid, 10);
 | 
			
		||||
                    const wikiId = parseInt(params.wid, 10);
 | 
			
		||||
                    let path = ADDON_MOD_WIKI_PAGE_NAME;
 | 
			
		||||
 | 
			
		||||
                    // Check if the link is inside the same wiki.
 | 
			
		||||
                    const isSameWiki = await this.currentStateIsSameWiki(route, subwikiId, siteId);
 | 
			
		||||
 | 
			
		||||
                    if (isSameWiki) {
 | 
			
		||||
                        // User is seeing the wiki, we can get the module from the wiki params.
 | 
			
		||||
                        const routeParams = CoreNavigator.getRouteParams(route);
 | 
			
		||||
 | 
			
		||||
                        path = path + `/${routeParams.courseId}/${routeParams.cmId}/edit`;
 | 
			
		||||
                    } else if (wikiId) {
 | 
			
		||||
                        // The URL specifies which wiki it belongs to. Get the module.
 | 
			
		||||
                        const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                            wikiId,
 | 
			
		||||
                            'wiki',
 | 
			
		||||
                            { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
                        );
 | 
			
		||||
 | 
			
		||||
                        path = path + `/${module.course}/${module.id}/edit`;
 | 
			
		||||
                    } else {
 | 
			
		||||
                        // Cannot get module ID.
 | 
			
		||||
                        path = path + `/${courseId || 0}/0/edit`;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Open the page.
 | 
			
		||||
                    CoreNavigator.navigateToSitePath(
 | 
			
		||||
                        path,
 | 
			
		||||
                        {
 | 
			
		||||
                            params: {
 | 
			
		||||
                                pageTitle: params.title,
 | 
			
		||||
                                subwikiId: subwikiId,
 | 
			
		||||
                            },
 | 
			
		||||
                            siteId,
 | 
			
		||||
                        },
 | 
			
		||||
                    );
 | 
			
		||||
                } catch (error) {
 | 
			
		||||
                    CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true);
 | 
			
		||||
                } finally {
 | 
			
		||||
                    modal.dismiss();
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            action: (siteId) => this.handleAction(siteId, courseId, params),
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handle link action.
 | 
			
		||||
     *
 | 
			
		||||
     * @param siteId Site id.
 | 
			
		||||
     * @param courseId Course id.
 | 
			
		||||
     * @param params Params.
 | 
			
		||||
     */
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
    async handleAction(siteId: string, courseId: number | undefined, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        // Stub to override.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiCreateLinkHandler = makeSingleton(AddonModWikiCreateLinkHandlerService);
 | 
			
		||||
/**
 | 
			
		||||
 * Get create link handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Link handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getCreateLinkHandlerInstance(): CoreContentLinksHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModWikiCreateLinkHandlerLazyService,
 | 
			
		||||
        AddonModWikiCreateLinkHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModWikiCreateLinkHandler } = await import('./create-link-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModWikiCreateLinkHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModWikiCreateLinkHandlerService());
 | 
			
		||||
    lazyHandler.setLazyOverrides(['handleAction']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										72
									
								
								src/addons/mod/wiki/services/handlers/edit-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								src/addons/mod/wiki/services/handlers/edit-link-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,72 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWiki } from '../wiki';
 | 
			
		||||
import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { AddonModWikiEditLinkHandlerService } from '@addons/mod/wiki/services/handlers/edit-link';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to treat links to edit a wiki page.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiEditLinkHandlerLazyService extends AddonModWikiEditLinkHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const pageId = Number(params.pageid);
 | 
			
		||||
 | 
			
		||||
            const pageContents = await AddonModWiki.getPageContents(pageId, { siteId });
 | 
			
		||||
 | 
			
		||||
            const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                pageContents.wikiid,
 | 
			
		||||
                'wiki',
 | 
			
		||||
                { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            let section = '';
 | 
			
		||||
            if (params.section !== undefined) {
 | 
			
		||||
                section = params.section.replace(/\+/g, ' ');
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/edit`,
 | 
			
		||||
                {
 | 
			
		||||
                    params: {
 | 
			
		||||
                        section: section,
 | 
			
		||||
                        pageId: pageId,
 | 
			
		||||
                    },
 | 
			
		||||
                    siteId,
 | 
			
		||||
                },
 | 
			
		||||
            );
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true);
 | 
			
		||||
        } finally {
 | 
			
		||||
            modal.dismiss();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiEditLinkHandler = makeSingleton(AddonModWikiEditLinkHandlerLazyService);
 | 
			
		||||
@ -12,75 +12,57 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { ADDON_MOD_WIKI_FEATURE_NAME } from '@addons/mod/wiki/constants';
 | 
			
		||||
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
 | 
			
		||||
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWiki } from '../wiki';
 | 
			
		||||
import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import type { AddonModWikiEditLinkHandlerLazyService } from '@addons/mod/wiki/services/handlers/edit-link-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to treat links to edit a wiki page.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiEditLinkHandlerService extends CoreContentLinksHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModWikiEditLinkHandler';
 | 
			
		||||
    featureName = 'CoreCourseModuleDelegate_AddonModWiki';
 | 
			
		||||
    featureName = ADDON_MOD_WIKI_FEATURE_NAME;
 | 
			
		||||
    pattern = /\/mod\/wiki\/edit\.php.*([&?]pageid=\d+)/;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    getActions(
 | 
			
		||||
        siteIds: string[],
 | 
			
		||||
        url: string,
 | 
			
		||||
        params: Record<string, string>,
 | 
			
		||||
    ): CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
 | 
			
		||||
 | 
			
		||||
    getActions(siteIds: string[], url: string, params: Record<string, string>): CoreContentLinksAction[] {
 | 
			
		||||
        return [{
 | 
			
		||||
            action: async (siteId: string) => {
 | 
			
		||||
                const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
 | 
			
		||||
                try {
 | 
			
		||||
                    const pageId = Number(params.pageid);
 | 
			
		||||
 | 
			
		||||
                    const pageContents = await AddonModWiki.getPageContents(pageId, { siteId });
 | 
			
		||||
 | 
			
		||||
                    const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                        pageContents.wikiid,
 | 
			
		||||
                        'wiki',
 | 
			
		||||
                        { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
                    );
 | 
			
		||||
 | 
			
		||||
                    let section = '';
 | 
			
		||||
                    if (params.section !== undefined) {
 | 
			
		||||
                        section = params.section.replace(/\+/g, ' ');
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                        `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/edit`,
 | 
			
		||||
                        {
 | 
			
		||||
                            params: {
 | 
			
		||||
                                section: section,
 | 
			
		||||
                                pageId: pageId,
 | 
			
		||||
                            },
 | 
			
		||||
                            siteId,
 | 
			
		||||
                        },
 | 
			
		||||
                    );
 | 
			
		||||
                } catch (error) {
 | 
			
		||||
                    CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true);
 | 
			
		||||
                } finally {
 | 
			
		||||
                    modal.dismiss();
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            action: (siteId) => this.handleAction(siteId, params),
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handle link action.
 | 
			
		||||
     *
 | 
			
		||||
     * @param siteId Site id.
 | 
			
		||||
     * @param params Params.
 | 
			
		||||
     */
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
    async handleAction(siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        // Stub to override.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiEditLinkHandler = makeSingleton(AddonModWikiEditLinkHandlerService);
 | 
			
		||||
/**
 | 
			
		||||
 * Get edit link handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Link handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getEditLinkHandlerInstance(): CoreContentLinksHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModWikiEditLinkHandlerLazyService,
 | 
			
		||||
        AddonModWikiEditLinkHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModWikiEditLinkHandler } = await import('./edit-link-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModWikiEditLinkHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModWikiEditLinkHandlerService());
 | 
			
		||||
    lazyHandler.setLazyOverrides(['handleAction']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,97 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { Md5 } from 'ts-md5';
 | 
			
		||||
import { AddonModWiki } from '../wiki';
 | 
			
		||||
import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { AddonModWikiPageOrMapLinkHandlerService } from '@addons/mod/wiki/services/handlers/page-or-map-link';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to treat links to a wiki page or the wiki map.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiPageOrMapLinkHandlerLazyService extends AddonModWikiPageOrMapLinkHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async handleAction(url: string, siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
        const pageId = parseInt(params.pageid, 10);
 | 
			
		||||
        const action = url.indexOf('mod/wiki/map.php') != -1 ? 'map' : 'page';
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            // Get the page data to obtain wikiId, subwikiId, etc.
 | 
			
		||||
            const page = await AddonModWiki.getPageContents(pageId, { siteId });
 | 
			
		||||
 | 
			
		||||
            const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                page.wikiid,
 | 
			
		||||
                'wiki',
 | 
			
		||||
                { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            const hash = Md5.hashAsciiStr(JSON.stringify({
 | 
			
		||||
                pageId: page.id,
 | 
			
		||||
                pageTitle: page.title,
 | 
			
		||||
                subwikiId: page.subwikiid,
 | 
			
		||||
                action: action,
 | 
			
		||||
                timestamp: Date.now(),
 | 
			
		||||
            }));
 | 
			
		||||
 | 
			
		||||
            await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/page/${hash}`,
 | 
			
		||||
                {
 | 
			
		||||
                    params: {
 | 
			
		||||
                        module,
 | 
			
		||||
                        pageId: page.id,
 | 
			
		||||
                        pageTitle: page.title,
 | 
			
		||||
                        subwikiId: page.subwikiid,
 | 
			
		||||
                        action: action,
 | 
			
		||||
                    },
 | 
			
		||||
                    siteId,
 | 
			
		||||
                },
 | 
			
		||||
            );
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true);
 | 
			
		||||
        } finally {
 | 
			
		||||
            modal.dismiss();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        const isMap = url.indexOf('mod/wiki/map.php') != -1;
 | 
			
		||||
 | 
			
		||||
        if (params.id && !isMap) {
 | 
			
		||||
            // ID param is more prioritary than pageid in index page, it's a index URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        } else if (isMap && params.option !== undefined && params.option != '5') {
 | 
			
		||||
            // Map link but the option isn't "Page list", not supported.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiPageOrMapLinkHandler = makeSingleton(AddonModWikiPageOrMapLinkHandlerLazyService);
 | 
			
		||||
@ -12,100 +12,58 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { ADDON_MOD_WIKI_FEATURE_NAME } from '@addons/mod/wiki/constants';
 | 
			
		||||
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
 | 
			
		||||
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import { CoreCourse } from '@features/course/services/course';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
import { CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreDomUtils } from '@services/utils/dom';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { Md5 } from 'ts-md5';
 | 
			
		||||
import { AddonModWiki } from '../wiki';
 | 
			
		||||
import { ADDON_MOD_WIKI_PAGE_NAME } from '../../constants';
 | 
			
		||||
import { CoreContentLinksAction, CoreContentLinksHandler } from '@features/contentlinks/services/contentlinks-delegate';
 | 
			
		||||
import type { AddonModWikiPageOrMapLinkHandlerLazyService } from '@addons/mod/wiki/services/handlers/page-or-map-link-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to treat links to a wiki page or the wiki map.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiPageOrMapLinkHandlerService extends CoreContentLinksHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModWikiPageOrMapLinkHandler';
 | 
			
		||||
    featureName = 'CoreCourseModuleDelegate_AddonModWiki';
 | 
			
		||||
    featureName = ADDON_MOD_WIKI_FEATURE_NAME;
 | 
			
		||||
    pattern = /\/mod\/wiki\/(view|map)\.php.*([&?]pageid=\d+)/;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    getActions(
 | 
			
		||||
        siteIds: string[],
 | 
			
		||||
        url: string,
 | 
			
		||||
        params: Record<string, string>,
 | 
			
		||||
    ): CoreContentLinksAction[] | Promise<CoreContentLinksAction[]> {
 | 
			
		||||
 | 
			
		||||
    getActions(siteIds: string[], url: string, params: Record<string, string>): CoreContentLinksAction[] {
 | 
			
		||||
        return [{
 | 
			
		||||
            action: async (siteId: string) => {
 | 
			
		||||
                const modal = await CoreDomUtils.showModalLoading();
 | 
			
		||||
                const pageId = parseInt(params.pageid, 10);
 | 
			
		||||
                const action = url.indexOf('mod/wiki/map.php') != -1 ? 'map' : 'page';
 | 
			
		||||
 | 
			
		||||
                try {
 | 
			
		||||
                    // Get the page data to obtain wikiId, subwikiId, etc.
 | 
			
		||||
                    const page = await AddonModWiki.getPageContents(pageId, { siteId });
 | 
			
		||||
 | 
			
		||||
                    const module = await CoreCourse.getModuleBasicInfoByInstance(
 | 
			
		||||
                        page.wikiid,
 | 
			
		||||
                        'wiki',
 | 
			
		||||
                        { siteId, readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE },
 | 
			
		||||
                    );
 | 
			
		||||
 | 
			
		||||
                    const hash = Md5.hashAsciiStr(JSON.stringify({
 | 
			
		||||
                        pageId: page.id,
 | 
			
		||||
                        pageTitle: page.title,
 | 
			
		||||
                        subwikiId: page.subwikiid,
 | 
			
		||||
                        action: action,
 | 
			
		||||
                        timestamp: Date.now(),
 | 
			
		||||
                    }));
 | 
			
		||||
 | 
			
		||||
                    await CoreNavigator.navigateToSitePath(
 | 
			
		||||
                        `${ADDON_MOD_WIKI_PAGE_NAME}/${module.course}/${module.id}/page/${hash}`,
 | 
			
		||||
                        {
 | 
			
		||||
                            params: {
 | 
			
		||||
                                module,
 | 
			
		||||
                                pageId: page.id,
 | 
			
		||||
                                pageTitle: page.title,
 | 
			
		||||
                                subwikiId: page.subwikiid,
 | 
			
		||||
                                action: action,
 | 
			
		||||
                            },
 | 
			
		||||
                            siteId,
 | 
			
		||||
                        },
 | 
			
		||||
                    );
 | 
			
		||||
                } catch (error) {
 | 
			
		||||
                    CoreDomUtils.showErrorModalDefault(error, 'addon.mod_wiki.errorloadingpage', true);
 | 
			
		||||
                } finally {
 | 
			
		||||
                    modal.dismiss();
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            action: (siteId) => this.handleAction(url, siteId, params),
 | 
			
		||||
        }];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     * Handle link action.
 | 
			
		||||
     *
 | 
			
		||||
     * @param url Url.
 | 
			
		||||
     * @param siteId Site id.
 | 
			
		||||
     * @param params Params.
 | 
			
		||||
     */
 | 
			
		||||
    async isEnabled(siteId: string, url: string, params: Record<string, string>): Promise<boolean> {
 | 
			
		||||
        const isMap = url.indexOf('mod/wiki/map.php') != -1;
 | 
			
		||||
 | 
			
		||||
        if (params.id && !isMap) {
 | 
			
		||||
            // ID param is more prioritary than pageid in index page, it's a index URL.
 | 
			
		||||
            return false;
 | 
			
		||||
        } else if (isMap && params.option !== undefined && params.option != '5') {
 | 
			
		||||
            // Map link but the option isn't "Page list", not supported.
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
 | 
			
		||||
    async handleAction(url: string, siteId: string, params: Record<string, string>): Promise<void> {
 | 
			
		||||
        // Stub to override.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiPageOrMapLinkHandler = makeSingleton(AddonModWikiPageOrMapLinkHandlerService);
 | 
			
		||||
/**
 | 
			
		||||
 * Get page or map link handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Link handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getPageOrMapLinkHandlerInstance(): CoreContentLinksHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModWikiPageOrMapLinkHandlerLazyService,
 | 
			
		||||
        AddonModWikiPageOrMapLinkHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModWikiPageOrMapLinkHandler } = await import('./page-or-map-link-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModWikiPageOrMapLinkHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModWikiPageOrMapLinkHandlerService());
 | 
			
		||||
    lazyHandler.setLazyOverrides(['isEnabled', 'handleAction']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										205
									
								
								src/addons/mod/wiki/services/handlers/prefetch-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										205
									
								
								src/addons/mod/wiki/services/handlers/prefetch-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,205 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course';
 | 
			
		||||
import { CoreFilepool } from '@services/filepool';
 | 
			
		||||
import { CoreGroups } from '@services/groups';
 | 
			
		||||
import { CoreFileSizeSum, CorePluginFileDelegate } from '@services/plugin-file-delegate';
 | 
			
		||||
import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreUtils } from '@services/utils/utils';
 | 
			
		||||
import { CoreWSFile } from '@services/ws';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWiki, AddonModWikiSubwikiPage } from '../wiki';
 | 
			
		||||
import { AddonModWikiSync, AddonModWikiSyncWikiResult } from '../wiki-sync';
 | 
			
		||||
import { AddonModWikiPrefetchHandlerService } from '@addons/mod/wiki/services/handlers/prefetch';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to prefetch wikis.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiPrefetchHandlerLazyService extends AddonModWikiPrefetchHandlerService {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns a list of pages that can be downloaded.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module The module object returned by WS.
 | 
			
		||||
     * @param courseId The course ID.
 | 
			
		||||
     * @param options Other options.
 | 
			
		||||
     * @returns List of pages.
 | 
			
		||||
     */
 | 
			
		||||
    protected async getAllPages(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        options: CoreSitesCommonWSOptions = {},
 | 
			
		||||
    ): Promise<AddonModWikiSubwikiPage[]> {
 | 
			
		||||
        options.siteId = options.siteId || CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const wiki = await AddonModWiki.getWiki(courseId, module.id, options);
 | 
			
		||||
 | 
			
		||||
            return await AddonModWiki.getWikiPageList(wiki, options);
 | 
			
		||||
        } catch {
 | 
			
		||||
            // Wiki not found, return empty list.
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getDownloadSize(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<CoreFileSizeSum> {
 | 
			
		||||
        const promises: Promise<CoreFileSizeSum>[] = [];
 | 
			
		||||
        const siteId = CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        promises.push(this.getFiles(module, courseId, single, siteId).then((files) =>
 | 
			
		||||
            CorePluginFileDelegate.getFilesDownloadSize(files)));
 | 
			
		||||
 | 
			
		||||
        promises.push(this.getAllPages(module, courseId, {
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
 | 
			
		||||
            siteId,
 | 
			
		||||
        }).then((pages) => {
 | 
			
		||||
            let size = 0;
 | 
			
		||||
 | 
			
		||||
            pages.forEach((page) => {
 | 
			
		||||
                if (page.contentsize) {
 | 
			
		||||
                    size = size + page.contentsize;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            return { size: size, total: true };
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
        const sizes = await Promise.all(promises);
 | 
			
		||||
 | 
			
		||||
        return {
 | 
			
		||||
            size: sizes[0].size + sizes[1].size,
 | 
			
		||||
            total: sizes[0].total && sizes[1].total,
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getFiles(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        single?: boolean,
 | 
			
		||||
        siteId?: string,
 | 
			
		||||
    ): Promise<CoreWSFile[]> {
 | 
			
		||||
        siteId = siteId || CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const wiki = await AddonModWiki.getWiki(courseId, module.id, { siteId });
 | 
			
		||||
 | 
			
		||||
            const introFiles = this.getIntroFilesFromInstance(module, wiki);
 | 
			
		||||
 | 
			
		||||
            const files = await AddonModWiki.getWikiFileList(wiki, { siteId });
 | 
			
		||||
 | 
			
		||||
            return introFiles.concat(files);
 | 
			
		||||
        } catch {
 | 
			
		||||
            // Wiki not found, return empty list.
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    invalidateContent(moduleId: number, courseId: number): Promise<void> {
 | 
			
		||||
        return AddonModWiki.invalidateContent(moduleId, courseId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async prefetch(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<void> {
 | 
			
		||||
        // Get the download time of the package before starting the download (otherwise we'd always get current time).
 | 
			
		||||
        const siteId = CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        const data = await CoreUtils.ignoreErrors(CoreFilepool.getPackageData(siteId, this.component, module.id));
 | 
			
		||||
 | 
			
		||||
        const downloadTime = data?.downloadTime || 0;
 | 
			
		||||
 | 
			
		||||
        return this.prefetchPackage(
 | 
			
		||||
            module,
 | 
			
		||||
            courseId,
 | 
			
		||||
            (siteId) => this.prefetchWiki(module, courseId, !!single, downloadTime, siteId),
 | 
			
		||||
            siteId,
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Prefetch a wiki.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module.
 | 
			
		||||
     * @param courseId Course ID the module belongs to.
 | 
			
		||||
     * @param single True if we're downloading a single module, false if we're downloading a whole section.
 | 
			
		||||
     * @param downloadTime The previous download time, 0 if no previous download.
 | 
			
		||||
     * @param siteId Site ID.
 | 
			
		||||
     * @returns Promise resolved when done.
 | 
			
		||||
     */
 | 
			
		||||
    protected async prefetchWiki(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        single: boolean,
 | 
			
		||||
        downloadTime: number,
 | 
			
		||||
        siteId: string,
 | 
			
		||||
    ): Promise<void> {
 | 
			
		||||
        const userId = CoreSites.getCurrentSiteUserId();
 | 
			
		||||
 | 
			
		||||
        const commonOptions = {
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
 | 
			
		||||
            siteId,
 | 
			
		||||
        };
 | 
			
		||||
        const modOptions = {
 | 
			
		||||
            cmId: module.id,
 | 
			
		||||
            ...commonOptions, // Include all common options.
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        // Get the list of pages.
 | 
			
		||||
        const pages = await this.getAllPages(module, courseId, commonOptions);
 | 
			
		||||
        const promises: Promise<unknown>[] = [];
 | 
			
		||||
 | 
			
		||||
        pages.forEach((page) => {
 | 
			
		||||
            // Fetch page contents if it needs to be fetched.
 | 
			
		||||
            if (page.timemodified > downloadTime) {
 | 
			
		||||
                promises.push(AddonModWiki.getPageContents(page.id, modOptions));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Fetch group data.
 | 
			
		||||
        promises.push(CoreGroups.getActivityGroupInfo(module.id, false, userId, siteId));
 | 
			
		||||
 | 
			
		||||
        // Fetch info to provide wiki links.
 | 
			
		||||
        promises.push(AddonModWiki.getWiki(courseId, module.id, { siteId }).then((wiki) =>
 | 
			
		||||
            CoreCourse.getModuleBasicInfoByInstance(wiki.id, 'wiki', { siteId })));
 | 
			
		||||
 | 
			
		||||
        // Get related page files and fetch them.
 | 
			
		||||
        promises.push(this.getFiles(module, courseId, single, siteId).then((files) =>
 | 
			
		||||
            CoreFilepool.addFilesToQueue(siteId, files, this.component, module.id)));
 | 
			
		||||
 | 
			
		||||
        await Promise.all(promises);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModWikiSyncWikiResult> {
 | 
			
		||||
        return AddonModWikiSync.syncWiki(module.instance, module.course, module.id, siteId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiPrefetchHandler = makeSingleton(AddonModWikiPrefetchHandlerLazyService);
 | 
			
		||||
@ -12,200 +12,48 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import {
 | 
			
		||||
    ADDON_MOD_WIKI_PREFETCH_COMPONENT,
 | 
			
		||||
    ADDON_MOD_WIKI_PREFETCH_MODNAME,
 | 
			
		||||
    ADDON_MOD_WIKI_PREFETCH_NAME,
 | 
			
		||||
} from '@addons/mod/wiki/constants';
 | 
			
		||||
import { CoreCourseActivityPrefetchHandlerBase } from '@features/course/classes/activity-prefetch-handler';
 | 
			
		||||
import { CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course';
 | 
			
		||||
import { CoreFilepool } from '@services/filepool';
 | 
			
		||||
import { CoreGroups } from '@services/groups';
 | 
			
		||||
import { CoreFileSizeSum, CorePluginFileDelegate } from '@services/plugin-file-delegate';
 | 
			
		||||
import { CoreSites, CoreSitesCommonWSOptions, CoreSitesReadingStrategy } from '@services/sites';
 | 
			
		||||
import { CoreUtils } from '@services/utils/utils';
 | 
			
		||||
import { CoreWSFile } from '@services/ws';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWiki, AddonModWikiSubwikiPage } from '../wiki';
 | 
			
		||||
import { AddonModWikiSync, AddonModWikiSyncWikiResult } from '../wiki-sync';
 | 
			
		||||
import { ADDON_MOD_WIKI_COMPONENT } from '../../constants';
 | 
			
		||||
import { CoreCourseModulePrefetchHandler } from '@features/course/services/module-prefetch-delegate';
 | 
			
		||||
import type { AddonModWikiPrefetchHandlerLazyService } from '@addons/mod/wiki/services/handlers/prefetch-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Handler to prefetch wikis.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiPrefetchHandlerService extends CoreCourseActivityPrefetchHandlerBase {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModWiki';
 | 
			
		||||
    modName = 'wiki';
 | 
			
		||||
    component = ADDON_MOD_WIKI_COMPONENT;
 | 
			
		||||
    name = ADDON_MOD_WIKI_PREFETCH_NAME;
 | 
			
		||||
    modName = ADDON_MOD_WIKI_PREFETCH_MODNAME;
 | 
			
		||||
    component = ADDON_MOD_WIKI_PREFETCH_COMPONENT;
 | 
			
		||||
    updatesNames = /^.*files$|^pages$/;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Returns a list of pages that can be downloaded.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module The module object returned by WS.
 | 
			
		||||
     * @param courseId The course ID.
 | 
			
		||||
     * @param options Other options.
 | 
			
		||||
     * @returns List of pages.
 | 
			
		||||
     */
 | 
			
		||||
    protected async getAllPages(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        options: CoreSitesCommonWSOptions = {},
 | 
			
		||||
    ): Promise<AddonModWikiSubwikiPage[]> {
 | 
			
		||||
        options.siteId = options.siteId || CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const wiki = await AddonModWiki.getWiki(courseId, module.id, options);
 | 
			
		||||
 | 
			
		||||
            return await AddonModWiki.getWikiPageList(wiki, options);
 | 
			
		||||
        } catch {
 | 
			
		||||
            // Wiki not found, return empty list.
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getDownloadSize(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<CoreFileSizeSum> {
 | 
			
		||||
        const promises: Promise<CoreFileSizeSum>[] = [];
 | 
			
		||||
        const siteId = CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        promises.push(this.getFiles(module, courseId, single, siteId).then((files) =>
 | 
			
		||||
            CorePluginFileDelegate.getFilesDownloadSize(files)));
 | 
			
		||||
 | 
			
		||||
        promises.push(this.getAllPages(module, courseId, {
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
 | 
			
		||||
            siteId,
 | 
			
		||||
        }).then((pages) => {
 | 
			
		||||
            let size = 0;
 | 
			
		||||
 | 
			
		||||
            pages.forEach((page) => {
 | 
			
		||||
                if (page.contentsize) {
 | 
			
		||||
                    size = size + page.contentsize;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            return { size: size, total: true };
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
        const sizes = await Promise.all(promises);
 | 
			
		||||
 | 
			
		||||
        return {
 | 
			
		||||
            size: sizes[0].size + sizes[1].size,
 | 
			
		||||
            total: sizes[0].total && sizes[1].total,
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async getFiles(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        single?: boolean,
 | 
			
		||||
        siteId?: string,
 | 
			
		||||
    ): Promise<CoreWSFile[]> {
 | 
			
		||||
        siteId = siteId || CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            const wiki = await AddonModWiki.getWiki(courseId, module.id, { siteId });
 | 
			
		||||
 | 
			
		||||
            const introFiles = this.getIntroFilesFromInstance(module, wiki);
 | 
			
		||||
 | 
			
		||||
            const files = await AddonModWiki.getWikiFileList(wiki, { siteId });
 | 
			
		||||
 | 
			
		||||
            return introFiles.concat(files);
 | 
			
		||||
        } catch {
 | 
			
		||||
            // Wiki not found, return empty list.
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    invalidateContent(moduleId: number, courseId: number): Promise<void> {
 | 
			
		||||
        return AddonModWiki.invalidateContent(moduleId, courseId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    async prefetch(module: CoreCourseAnyModuleData, courseId: number, single?: boolean): Promise<void> {
 | 
			
		||||
        // Get the download time of the package before starting the download (otherwise we'd always get current time).
 | 
			
		||||
        const siteId = CoreSites.getCurrentSiteId();
 | 
			
		||||
 | 
			
		||||
        const data = await CoreUtils.ignoreErrors(CoreFilepool.getPackageData(siteId, this.component, module.id));
 | 
			
		||||
 | 
			
		||||
        const downloadTime = data?.downloadTime || 0;
 | 
			
		||||
 | 
			
		||||
        return this.prefetchPackage(
 | 
			
		||||
            module,
 | 
			
		||||
            courseId,
 | 
			
		||||
            (siteId) => this.prefetchWiki(module, courseId, !!single, downloadTime, siteId),
 | 
			
		||||
            siteId,
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Prefetch a wiki.
 | 
			
		||||
     *
 | 
			
		||||
     * @param module Module.
 | 
			
		||||
     * @param courseId Course ID the module belongs to.
 | 
			
		||||
     * @param single True if we're downloading a single module, false if we're downloading a whole section.
 | 
			
		||||
     * @param downloadTime The previous download time, 0 if no previous download.
 | 
			
		||||
     * @param siteId Site ID.
 | 
			
		||||
     * @returns Promise resolved when done.
 | 
			
		||||
     */
 | 
			
		||||
    protected async prefetchWiki(
 | 
			
		||||
        module: CoreCourseAnyModuleData,
 | 
			
		||||
        courseId: number,
 | 
			
		||||
        single: boolean,
 | 
			
		||||
        downloadTime: number,
 | 
			
		||||
        siteId: string,
 | 
			
		||||
    ): Promise<void> {
 | 
			
		||||
        const userId = CoreSites.getCurrentSiteUserId();
 | 
			
		||||
 | 
			
		||||
        const commonOptions = {
 | 
			
		||||
            readingStrategy: CoreSitesReadingStrategy.ONLY_NETWORK,
 | 
			
		||||
            siteId,
 | 
			
		||||
        };
 | 
			
		||||
        const modOptions = {
 | 
			
		||||
            cmId: module.id,
 | 
			
		||||
            ...commonOptions, // Include all common options.
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        // Get the list of pages.
 | 
			
		||||
        const pages = await this.getAllPages(module, courseId, commonOptions);
 | 
			
		||||
        const promises: Promise<unknown>[] = [];
 | 
			
		||||
 | 
			
		||||
        pages.forEach((page) => {
 | 
			
		||||
            // Fetch page contents if it needs to be fetched.
 | 
			
		||||
            if (page.timemodified > downloadTime) {
 | 
			
		||||
                promises.push(AddonModWiki.getPageContents(page.id, modOptions));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        // Fetch group data.
 | 
			
		||||
        promises.push(CoreGroups.getActivityGroupInfo(module.id, false, userId, siteId));
 | 
			
		||||
 | 
			
		||||
        // Fetch info to provide wiki links.
 | 
			
		||||
        promises.push(AddonModWiki.getWiki(courseId, module.id, { siteId }).then((wiki) =>
 | 
			
		||||
            CoreCourse.getModuleBasicInfoByInstance(wiki.id, 'wiki', { siteId })));
 | 
			
		||||
 | 
			
		||||
        // Get related page files and fetch them.
 | 
			
		||||
        promises.push(this.getFiles(module, courseId, single, siteId).then((files) =>
 | 
			
		||||
            CoreFilepool.addFilesToQueue(siteId, files, this.component, module.id)));
 | 
			
		||||
 | 
			
		||||
        await Promise.all(promises);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    sync(module: CoreCourseAnyModuleData, courseId: number, siteId?: string): Promise<AddonModWikiSyncWikiResult> {
 | 
			
		||||
        return AddonModWikiSync.syncWiki(module.instance, module.course, module.id, siteId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiPrefetchHandler = makeSingleton(AddonModWikiPrefetchHandlerService);
 | 
			
		||||
/**
 | 
			
		||||
 * Get prefetch handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Prefetch handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getPrefetchHandlerInstance(): CoreCourseModulePrefetchHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModWikiPrefetchHandlerLazyService,
 | 
			
		||||
        AddonModWikiPrefetchHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModWikiPrefetchHandler } = await import('./prefetch-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModWikiPrefetchHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModWikiPrefetchHandlerService());
 | 
			
		||||
    lazyHandler.setLazyMethods(['sync']);
 | 
			
		||||
    lazyHandler.setLazyOverrides([
 | 
			
		||||
        'getFiles',
 | 
			
		||||
        'getDownloadSize',
 | 
			
		||||
        'invalidateContent',
 | 
			
		||||
        'prefetch',
 | 
			
		||||
    ]);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								src/addons/mod/wiki/services/handlers/sync-cron-lazy.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/addons/mod/wiki/services/handlers/sync-cron-lazy.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
// (C) Copyright 2015 Moodle Pty Ltd.
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { CoreCronHandler } from '@services/cron';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWikiSync } from '../wiki-sync';
 | 
			
		||||
import { AddonModWikiSyncCronHandlerService } from '@addons/mod/wiki/services/handlers/sync-cron';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Synchronization cron handler.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiSyncCronHandlerLazyService extends AddonModWikiSyncCronHandlerService implements CoreCronHandler {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    execute(siteId?: string, force?: boolean): Promise<void> {
 | 
			
		||||
        return AddonModWikiSync.syncAllWikis(siteId, force);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    getInterval(): number {
 | 
			
		||||
        return AddonModWikiSync.syncInterval;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiSyncCronHandler = makeSingleton(AddonModWikiSyncCronHandlerLazyService);
 | 
			
		||||
@ -12,33 +12,33 @@
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import { asyncInstance } from '@/core/utils/async-instance';
 | 
			
		||||
import { CoreCronHandler } from '@services/cron';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { AddonModWikiSync } from '../wiki-sync';
 | 
			
		||||
import type { AddonModWikiSyncCronHandlerLazyService } from '@addons/mod/wiki/services/handlers/sync-cron-lazy';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Synchronization cron handler.
 | 
			
		||||
 */
 | 
			
		||||
@Injectable({ providedIn: 'root' })
 | 
			
		||||
export class AddonModWikiSyncCronHandlerService implements CoreCronHandler {
 | 
			
		||||
export class AddonModWikiSyncCronHandlerService {
 | 
			
		||||
 | 
			
		||||
    name = 'AddonModWikiSyncCronHandler';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    execute(siteId?: string, force?: boolean): Promise<void> {
 | 
			
		||||
        return AddonModWikiSync.syncAllWikis(siteId, force);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @inheritdoc
 | 
			
		||||
     */
 | 
			
		||||
    getInterval(): number {
 | 
			
		||||
        return AddonModWikiSync.syncInterval;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const AddonModWikiSyncCronHandler = makeSingleton(AddonModWikiSyncCronHandlerService);
 | 
			
		||||
/**
 | 
			
		||||
 * Get cron handler instance.
 | 
			
		||||
 *
 | 
			
		||||
 * @returns Cron handler.
 | 
			
		||||
 */
 | 
			
		||||
export function getCronHandlerInstance(): CoreCronHandler {
 | 
			
		||||
    const lazyHandler = asyncInstance<
 | 
			
		||||
        AddonModWikiSyncCronHandlerLazyService,
 | 
			
		||||
        AddonModWikiSyncCronHandlerService
 | 
			
		||||
    >(async () => {
 | 
			
		||||
        const { AddonModWikiSyncCronHandler } = await import('./sync-cron-lazy');
 | 
			
		||||
 | 
			
		||||
        return AddonModWikiSyncCronHandler.instance;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    lazyHandler.setEagerInstance(new AddonModWikiSyncCronHandlerService());
 | 
			
		||||
    lazyHandler.setLazyMethods(['execute', 'getInterval']);
 | 
			
		||||
 | 
			
		||||
    return lazyHandler;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -23,14 +23,14 @@ import { CoreTagAreaDelegate } from '@features/tag/services/tag-area-delegate';
 | 
			
		||||
import { CoreCronDelegate } from '@services/cron';
 | 
			
		||||
import { CORE_SITE_SCHEMAS } from '@services/sites';
 | 
			
		||||
import { OFFLINE_SITE_SCHEMA } from './services/database/wiki';
 | 
			
		||||
import { AddonModWikiCreateLinkHandler } from './services/handlers/create-link';
 | 
			
		||||
import { AddonModWikiEditLinkHandler } from './services/handlers/edit-link';
 | 
			
		||||
import { getCreateLinkHandlerInstance } from './services/handlers/create-link';
 | 
			
		||||
import { getEditLinkHandlerInstance } from './services/handlers/edit-link';
 | 
			
		||||
import { AddonModWikiIndexLinkHandler } from './services/handlers/index-link';
 | 
			
		||||
import { AddonModWikiListLinkHandler } from './services/handlers/list-link';
 | 
			
		||||
import { AddonModWikiModuleHandler } from './services/handlers/module';
 | 
			
		||||
import { AddonModWikiPageOrMapLinkHandler } from './services/handlers/page-or-map-link';
 | 
			
		||||
import { AddonModWikiPrefetchHandler } from './services/handlers/prefetch';
 | 
			
		||||
import { AddonModWikiSyncCronHandler } from './services/handlers/sync-cron';
 | 
			
		||||
import { getPageOrMapLinkHandlerInstance } from './services/handlers/page-or-map-link';
 | 
			
		||||
import { getPrefetchHandlerInstance } from './services/handlers/prefetch';
 | 
			
		||||
import { getCronHandlerInstance } from './services/handlers/sync-cron';
 | 
			
		||||
import { AddonModWikiTagAreaHandler } from './services/handlers/tag-area';
 | 
			
		||||
import { ADDON_MOD_WIKI_COMPONENT, ADDON_MOD_WIKI_PAGE_NAME } from './constants';
 | 
			
		||||
 | 
			
		||||
@ -55,14 +55,15 @@ const routes: Routes = [
 | 
			
		||||
            provide: APP_INITIALIZER,
 | 
			
		||||
            multi: true,
 | 
			
		||||
            useValue: () => {
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(getCreateLinkHandlerInstance());
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(getEditLinkHandlerInstance());
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(getPageOrMapLinkHandlerInstance());
 | 
			
		||||
                CoreCourseModulePrefetchDelegate.registerHandler(getPrefetchHandlerInstance());
 | 
			
		||||
                CoreCronDelegate.register(getCronHandlerInstance());
 | 
			
		||||
 | 
			
		||||
                CoreCourseModuleDelegate.registerHandler(AddonModWikiModuleHandler.instance);
 | 
			
		||||
                CoreCourseModulePrefetchDelegate.registerHandler(AddonModWikiPrefetchHandler.instance);
 | 
			
		||||
                CoreCronDelegate.register(AddonModWikiSyncCronHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModWikiIndexLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModWikiListLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModWikiCreateLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModWikiEditLinkHandler.instance);
 | 
			
		||||
                CoreContentLinksDelegate.registerHandler(AddonModWikiPageOrMapLinkHandler.instance);
 | 
			
		||||
                CoreTagAreaDelegate.registerHandler(AddonModWikiTagAreaHandler.instance);
 | 
			
		||||
 | 
			
		||||
                CoreCourseHelper.registerModuleReminderClick(ADDON_MOD_WIKI_COMPONENT);
 | 
			
		||||
 | 
			
		||||
@ -239,7 +239,7 @@ export class CoreDbProvider {
 | 
			
		||||
            await CorePlatform.ready();
 | 
			
		||||
 | 
			
		||||
            return SQLite.create({ name, location: 'default' });
 | 
			
		||||
        });
 | 
			
		||||
        }, {});
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -151,15 +151,32 @@ export type AsyncMethod<T> =
 | 
			
		||||
            : (...args: Params) => Promise<Return>
 | 
			
		||||
        : never;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get instance methods that don't return a promise.
 | 
			
		||||
 */
 | 
			
		||||
export type GetEagerMethods<TEagerInstance extends AsyncObject> = {
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
 | 
			
		||||
    [k in keyof TEagerInstance]: TEagerInstance[k] extends (...args: any[]) => infer TReturn
 | 
			
		||||
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
 | 
			
		||||
        ? (TReturn extends Promise<any> ? never : k)
 | 
			
		||||
        : never
 | 
			
		||||
}[keyof TEagerInstance];
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Asynchronous instance.
 | 
			
		||||
 *
 | 
			
		||||
 * All methods are converted to their asynchronous version, and properties are available asynchronously using
 | 
			
		||||
 * the getProperty method.
 | 
			
		||||
 */
 | 
			
		||||
export type AsyncInstance<TLazyInstance extends TEagerInstance, TEagerInstance extends AsyncObject = Partial<TLazyInstance>> =
 | 
			
		||||
    AsyncInstanceWrapper<TLazyInstance, TEagerInstance> & {
 | 
			
		||||
export type AsyncInstance<
 | 
			
		||||
    TLazyInstance extends TEagerInstance,
 | 
			
		||||
    TEagerInstance extends AsyncObject = Partial<TLazyInstance>,
 | 
			
		||||
    TEagerMethods extends keyof TEagerInstance = GetEagerMethods<TEagerInstance>
 | 
			
		||||
> =
 | 
			
		||||
    AsyncInstanceWrapper<TLazyInstance, TEagerInstance> & Omit<{
 | 
			
		||||
        [k in keyof TLazyInstance]: AsyncMethod<TLazyInstance[k]>;
 | 
			
		||||
    }, TEagerMethods> & {
 | 
			
		||||
        [k in TEagerMethods]: TEagerInstance[k];
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@ -177,9 +194,14 @@ export type LazyMethodsGuard<TMethods extends Array<string | number | symbol>, T
 | 
			
		||||
 */
 | 
			
		||||
export function asyncInstance<TLazyInstance extends TEagerInstance, TEagerInstance extends AsyncObject = Partial<TLazyInstance>>(
 | 
			
		||||
    lazyConstructor?: () => TLazyInstance | Promise<TLazyInstance>,
 | 
			
		||||
    eagerInstance?: TEagerInstance,
 | 
			
		||||
): AsyncInstance<TLazyInstance, TEagerInstance> {
 | 
			
		||||
    const wrapper = createAsyncInstanceWrapper<TLazyInstance, TEagerInstance>(lazyConstructor);
 | 
			
		||||
 | 
			
		||||
    if (eagerInstance) {
 | 
			
		||||
        wrapper.setEagerInstance(eagerInstance);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return new Proxy(wrapper, {
 | 
			
		||||
        get: (target, p, receiver) => {
 | 
			
		||||
            const property = p as keyof TEagerInstance;
 | 
			
		||||
 | 
			
		||||
@ -113,6 +113,20 @@ describe('AsyncInstance', () => {
 | 
			
		||||
        expectSameTypes<AsyncInstance<LazyService>['goodbye'], () => Promise<string>>(true);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('keeps eager methods synchronous', () => {
 | 
			
		||||
        // Arrange.
 | 
			
		||||
        const asyncService = asyncInstance<LazyService, EagerService>(() => new LazyService());
 | 
			
		||||
 | 
			
		||||
        asyncService.setEagerInstance(new EagerService());
 | 
			
		||||
 | 
			
		||||
        // Act.
 | 
			
		||||
        const message = asyncService.eagerHello();
 | 
			
		||||
 | 
			
		||||
        // Assert.
 | 
			
		||||
        expect(message).toEqual('hello');
 | 
			
		||||
        expectSameTypes<typeof message, string>(true);
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
class EagerService {
 | 
			
		||||
@ -121,6 +135,10 @@ class EagerService {
 | 
			
		||||
 | 
			
		||||
    notImplemented?(): void;
 | 
			
		||||
 | 
			
		||||
    eagerHello(): string {
 | 
			
		||||
        return 'hello';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async isEager(): Promise<boolean> {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user