commit
						8926a95db6
					
				| @ -40,6 +40,7 @@ import { CoreWSError } from '@classes/errors/wserror'; | |||||||
| import { CoreLogger } from '@singletons/logger'; | import { CoreLogger } from '@singletons/logger'; | ||||||
| import { Translate } from '@singletons'; | import { Translate } from '@singletons'; | ||||||
| import { CoreIonLoadingElement } from './ion-loading'; | import { CoreIonLoadingElement } from './ion-loading'; | ||||||
|  | import { CoreLang } from '@services/lang'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * QR Code type enumeration. |  * QR Code type enumeration. | ||||||
| @ -308,10 +309,7 @@ export class CoreSite { | |||||||
| 
 | 
 | ||||||
|         // Index function by name to speed up wsAvailable method.
 |         // Index function by name to speed up wsAvailable method.
 | ||||||
|         if (infos?.functions) { |         if (infos?.functions) { | ||||||
|             infos.functionsByName = {}; |             infos.functionsByName = CoreUtils.arrayToObject(infos.functions, 'name'); | ||||||
|             infos.functions.forEach((func) => { |  | ||||||
|                 infos.functionsByName![func.name] = func; |  | ||||||
|             }); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -574,9 +572,14 @@ export class CoreSite { | |||||||
| 
 | 
 | ||||||
|             // Call the WS.
 |             // Call the WS.
 | ||||||
|             try { |             try { | ||||||
|  |                 // Send the language to use. Do it after checking cache to prevent losing offline data when changing language.
 | ||||||
|  |                 data.moodlewssettinglang = preSets.lang ?? await CoreLang.getCurrentLanguage(); | ||||||
|  |                 data.moodlewssettinglang = data.moodlewssettinglang.replace('-', '_'); // Moodle uses underscore instead of dash.
 | ||||||
|  | 
 | ||||||
|                 const response = await this.callOrEnqueueRequest<T>(method, data, preSets, wsPreSets); |                 const response = await this.callOrEnqueueRequest<T>(method, data, preSets, wsPreSets); | ||||||
| 
 | 
 | ||||||
|                 if (preSets.saveToCache) { |                 if (preSets.saveToCache) { | ||||||
|  |                     delete data.moodlewssettinglang; | ||||||
|                     this.saveToCache(method, data, response, preSets); |                     this.saveToCache(method, data, response, preSets); | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
| @ -786,7 +789,8 @@ export class CoreSite { | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const requestsData = { |         let lang: string | undefined; | ||||||
|  |         const requestsData: Record<string, unknown> = { | ||||||
|             requests: requests.map((request) => { |             requests: requests.map((request) => { | ||||||
|                 const args = {}; |                 const args = {}; | ||||||
|                 const settings = {}; |                 const settings = {}; | ||||||
| @ -799,6 +803,11 @@ export class CoreSite { | |||||||
|                         if (match[1] == 'settingfilter' || match[1] == 'settingfileurl') { |                         if (match[1] == 'settingfilter' || match[1] == 'settingfileurl') { | ||||||
|                             // Undo special treatment of these settings in CoreWSProvider.convertValuesToString.
 |                             // Undo special treatment of these settings in CoreWSProvider.convertValuesToString.
 | ||||||
|                             value = (value == 'true' ? '1' : '0'); |                             value = (value == 'true' ? '1' : '0'); | ||||||
|  |                         } else if (match[1] == 'settinglang') { | ||||||
|  |                             // Use the lang globally to avoid exceptions with languages not installed.
 | ||||||
|  |                             lang = value; | ||||||
|  | 
 | ||||||
|  |                             return; | ||||||
|                         } |                         } | ||||||
|                         settings[match[1]] = value; |                         settings[match[1]] = value; | ||||||
|                     } else { |                     } else { | ||||||
| @ -813,6 +822,7 @@ export class CoreSite { | |||||||
|                 }; |                 }; | ||||||
|             }), |             }), | ||||||
|         }; |         }; | ||||||
|  |         requestsData.moodlewssettinglang = lang; | ||||||
| 
 | 
 | ||||||
|         const wsPresets: CoreWSPreSets = { |         const wsPresets: CoreWSPreSets = { | ||||||
|             siteUrl: this.siteUrl, |             siteUrl: this.siteUrl, | ||||||
| @ -894,7 +904,8 @@ export class CoreSite { | |||||||
|         preSets: CoreSiteWSPreSets, |         preSets: CoreSiteWSPreSets, | ||||||
|         emergency?: boolean, |         emergency?: boolean, | ||||||
|     ): Promise<T> { |     ): Promise<T> { | ||||||
|         if (!this.db || !preSets.getFromCache) { |         const db = this.db; | ||||||
|  |         if (!db || !preSets.getFromCache) { | ||||||
|             throw new CoreError('Get from cache is disabled.'); |             throw new CoreError('Get from cache is disabled.'); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -902,11 +913,11 @@ export class CoreSite { | |||||||
|         let entry: CoreSiteWSCacheRecord | undefined; |         let entry: CoreSiteWSCacheRecord | undefined; | ||||||
| 
 | 
 | ||||||
|         if (preSets.getCacheUsingCacheKey || (emergency && preSets.getEmergencyCacheUsingCacheKey)) { |         if (preSets.getCacheUsingCacheKey || (emergency && preSets.getEmergencyCacheUsingCacheKey)) { | ||||||
|             const entries = await this.db.getRecords<CoreSiteWSCacheRecord>(CoreSite.WS_CACHE_TABLE, { key: preSets.cacheKey }); |             const entries = await db.getRecords<CoreSiteWSCacheRecord>(CoreSite.WS_CACHE_TABLE, { key: preSets.cacheKey }); | ||||||
| 
 | 
 | ||||||
|             if (!entries.length) { |             if (!entries.length) { | ||||||
|                 // Cache key not found, get by params sent.
 |                 // Cache key not found, get by params sent.
 | ||||||
|                 entry = await this.db!.getRecord(CoreSite.WS_CACHE_TABLE, { id }); |                 entry = await db.getRecord(CoreSite.WS_CACHE_TABLE, { id }); | ||||||
|             } else { |             } else { | ||||||
|                 if (entries.length > 1) { |                 if (entries.length > 1) { | ||||||
|                     // More than one entry found. Search the one with same ID as this call.
 |                     // More than one entry found. Search the one with same ID as this call.
 | ||||||
| @ -918,7 +929,7 @@ export class CoreSite { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } else { |         } else { | ||||||
|             entry = await this.db!.getRecord(CoreSite.WS_CACHE_TABLE, { id }); |             entry = await db.getRecord(CoreSite.WS_CACHE_TABLE, { id }); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (typeof entry == 'undefined') { |         if (typeof entry == 'undefined') { | ||||||
| @ -933,7 +944,7 @@ export class CoreSite { | |||||||
|         if (!preSets.omitExpires) { |         if (!preSets.omitExpires) { | ||||||
|             expirationTime = entry.expirationTime + this.getExpirationDelay(preSets.updateFrequency); |             expirationTime = entry.expirationTime + this.getExpirationDelay(preSets.updateFrequency); | ||||||
| 
 | 
 | ||||||
|             if (now > expirationTime!) { |             if (now > expirationTime) { | ||||||
|                 this.logger.debug('Cached element found, but it is expired'); |                 this.logger.debug('Cached element found, but it is expired'); | ||||||
| 
 | 
 | ||||||
|                 throw new CoreError('Cache entry is expired.'); |                 throw new CoreError('Cache entry is expired.'); | ||||||
| @ -1734,7 +1745,12 @@ export class CoreSite { | |||||||
| 
 | 
 | ||||||
|         if (CoreSite.MOODLE_RELEASES[data.major] === undefined) { |         if (CoreSite.MOODLE_RELEASES[data.major] === undefined) { | ||||||
|             // Major version not found. Use the last one.
 |             // Major version not found. Use the last one.
 | ||||||
|             data.major = Object.keys(CoreSite.MOODLE_RELEASES).pop()!; |             const major = Object.keys(CoreSite.MOODLE_RELEASES).pop(); | ||||||
|  |             if (!major) { | ||||||
|  |                 return 0; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             data.major = major; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return CoreSite.MOODLE_RELEASES[data.major] + data.minor; |         return CoreSite.MOODLE_RELEASES[data.major] + data.minor; | ||||||
| @ -1942,6 +1958,11 @@ export type CoreSiteWSPreSets = { | |||||||
|      */ |      */ | ||||||
|     rewriteurls?: boolean; |     rewriteurls?: boolean; | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Language to send to the WebService (moodlewssettinglang). Defaults to app's language. | ||||||
|  |      */ | ||||||
|  |     lang?: string; | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Defaults to true. Set to false when the expected response is null. |      * Defaults to true. Set to false when the expected response is null. | ||||||
|      */ |      */ | ||||||
|  | |||||||
| @ -23,6 +23,8 @@ import { CoreSettingsHelper, CoreColorScheme, CoreZoomLevel } from '../../servic | |||||||
| import { CoreApp } from '@services/app'; | import { CoreApp } from '@services/app'; | ||||||
| import { CoreIframeUtils } from '@services/utils/iframe'; | import { CoreIframeUtils } from '@services/utils/iframe'; | ||||||
| import { Diagnostic } from '@singletons'; | import { Diagnostic } from '@singletons'; | ||||||
|  | import { CoreSites } from '@services/sites'; | ||||||
|  | import { CoreUtils } from '@services/utils/utils'; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * Page that displays the general settings. |  * Page that displays the general settings. | ||||||
| @ -109,7 +111,11 @@ export class CoreSettingsGeneralPage { | |||||||
|      * Called when a new language is selected. |      * Called when a new language is selected. | ||||||
|      */ |      */ | ||||||
|     languageChanged(): void { |     languageChanged(): void { | ||||||
|         CoreLang.changeCurrentLanguage(this.selectedLanguage).finally(() => { |         CoreLang.changeCurrentLanguage(this.selectedLanguage).finally(async () => { | ||||||
|  |             // Invalidate cache for all sites to get the content in the right language.
 | ||||||
|  |             const sites = await CoreSites.getSitesInstances(); | ||||||
|  |             await CoreUtils.ignoreErrors(Promise.all(sites.map((site) => site.invalidateWsCache()))); | ||||||
|  | 
 | ||||||
|             CoreEvents.trigger(CoreEvents.LANGUAGE_CHANGED, this.selectedLanguage); |             CoreEvents.trigger(CoreEvents.LANGUAGE_CHANGED, this.selectedLanguage); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -403,11 +403,11 @@ export class CoreLangProvider { | |||||||
| 
 | 
 | ||||||
|         this.customStringsRaw = strings; |         this.customStringsRaw = strings; | ||||||
| 
 | 
 | ||||||
|         if (currentLangChanged) { |         if (currentLangChanged && this.currentLanguage) { | ||||||
|             // Some lang strings have changed, emit an event to update the pipes.
 |             // Some lang strings have changed, emit an event to update the pipes.
 | ||||||
|             Translate.onLangChange.emit({ |             Translate.onLangChange.emit({ | ||||||
|                 lang: this.currentLanguage!, |                 lang: this.currentLanguage, | ||||||
|                 translations: Translate.translations[this.currentLanguage!], |                 translations: Translate.translations[this.currentLanguage], | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -1145,6 +1145,17 @@ export class CoreSitesProvider { | |||||||
|         return sites.map((site) => site.id); |         return sites.map((site) => site.id); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Get instances of all stored sites. | ||||||
|  |      * | ||||||
|  |      * @return Promise resolved when the sites are retrieved. | ||||||
|  |      */ | ||||||
|  |     async getSitesInstances(): Promise<CoreSite[]> { | ||||||
|  |         const siteIds = await this.getSitesIds(); | ||||||
|  | 
 | ||||||
|  |         return await Promise.all(siteIds.map(async (siteId) => await this.getSite(siteId))); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Login the user in a site. |      * Login the user in a site. | ||||||
|      * |      * | ||||||
|  | |||||||
| @ -336,8 +336,9 @@ export class CoreWSProvider { | |||||||
|      * @return Promise resolved with the mimetype or '' if failure. |      * @return Promise resolved with the mimetype or '' if failure. | ||||||
|      */ |      */ | ||||||
|     async getRemoteFileMimeType(url: string, ignoreCache?: boolean): Promise<string> { |     async getRemoteFileMimeType(url: string, ignoreCache?: boolean): Promise<string> { | ||||||
|         if (this.mimeTypeCache[url] && !ignoreCache) { |         const cachedMimeType = this.mimeTypeCache[url]; | ||||||
|             return this.mimeTypeCache[url]!; |         if (cachedMimeType && !ignoreCache) { | ||||||
|  |             return cachedMimeType; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
| @ -722,10 +723,12 @@ export class CoreWSProvider { | |||||||
|      */ |      */ | ||||||
|     protected processRetryQueue(): void { |     protected processRetryQueue(): void { | ||||||
|         if (this.retryCalls.length > 0 && this.retryTimeout == 0) { |         if (this.retryCalls.length > 0 && this.retryTimeout == 0) { | ||||||
|             const call = this.retryCalls.shift(); |             const call = this.retryCalls[0]; | ||||||
|  |             this.retryCalls.shift(); | ||||||
|  | 
 | ||||||
|             // Add a delay between calls.
 |             // Add a delay between calls.
 | ||||||
|             setTimeout(() => { |             setTimeout(() => { | ||||||
|                 call!.deferred.resolve(this.performPost(call!.method, call!.siteUrl, call!.data, call!.preSets)); |                 call.deferred.resolve(this.performPost(call.method, call.siteUrl, call.data, call.preSets)); | ||||||
|                 this.processRetryQueue(); |                 this.processRetryQueue(); | ||||||
|             }, 200); |             }, 200); | ||||||
|         } else { |         } else { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user