From 1906a62080297dc7d91702d3c043142c94f185cb Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 12 Nov 2021 12:52:45 +0100 Subject: [PATCH] MOBILE-3918 h5p: Fix undefined title for some packages --- src/core/features/h5p/classes/framework.ts | 19 +++++++++++++++++-- src/core/features/h5p/classes/storage.ts | 6 ++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/core/features/h5p/classes/framework.ts b/src/core/features/h5p/classes/framework.ts index b54c08901..326d7dc05 100644 --- a/src/core/features/h5p/classes/framework.ts +++ b/src/core/features/h5p/classes/framework.ts @@ -483,6 +483,10 @@ export class CoreH5PFramework { if (!params.metadata) { params.metadata = {}; } + // Add title to metadata. + if (typeof params.title === 'string' && !params.metadata.title) { + params.metadata.title = params.title; + } content.metadata = params.metadata; content.params = JSON.stringify(typeof params.params != 'undefined' && params.params != null ? params.params : params); @@ -834,6 +838,14 @@ export class CoreH5PFramework { content.library.libraryId = mainLibrary.id; } + // Add title to 'params' to be able to add it to metadata later. + if (typeof content.title === 'string') { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const params = CoreTextUtils.parseJSON(content.params || '{}'); + params.title = content.title; + content.params = JSON.stringify(params); + } + const data: Partial = { id: undefined, jsoncontent: content.params, @@ -844,23 +856,26 @@ export class CoreH5PFramework { fileurl: fileUrl, timecreated: undefined, }; + let contentId: number | undefined; if (typeof content.id != 'undefined') { data.id = content.id; + contentId = content.id; } else { data.timecreated = data.timemodified; } await db.insertRecord(CONTENT_TABLE_NAME, data); - if (!data.id) { + if (!contentId) { // New content. Get its ID. const entry = await db.getRecord(CONTENT_TABLE_NAME, data); content.id = entry.id; + contentId = content.id; } - return content.id!; + return contentId; } /** diff --git a/src/core/features/h5p/classes/storage.ts b/src/core/features/h5p/classes/storage.ts index 395eee442..4fc3bcdb1 100644 --- a/src/core/features/h5p/classes/storage.ts +++ b/src/core/features/h5p/classes/storage.ts @@ -173,6 +173,11 @@ export class CoreH5PStorage { const content: CoreH5PContentBeingSaved = {}; + // Add the 'title' if exists from 'h5p.json' data to be able to add it to metadata later. + if (typeof data.mainJsonData.title === 'string') { + content.title = data.mainJsonData.title; + } + if (!skipContent) { // Find main library version. if (data.mainJsonData.preloadedDependencies) { @@ -226,6 +231,7 @@ export type CoreH5PContentBeingSaved = { id?: number; params?: string; library?: CoreH5PContentLibrary; + title?: string; }; export type CoreH5PContentLibrary = CoreH5PLibraryBasicData & {