MOBILE-3666 h5p: Store library metadata in database

main
Dani Palou 2020-12-17 08:44:28 +01:00
parent 6e98c43651
commit 06c5153e0e
4 changed files with 42 additions and 5 deletions

View File

@ -40,7 +40,8 @@ import {
import { CoreError } from '@classes/errors/error';
import { CoreH5PSemantics } from './content-validator';
import { CoreH5PContentBeingSaved, CoreH5PLibraryBeingSaved } from './storage';
import { CoreH5PLibraryAddTo } from './validator';
import { CoreH5PLibraryAddTo, CoreH5PLibraryMetadataSettings } from './validator';
import { CoreH5PMetadata } from './metadata';
/**
* Equivalent to Moodle's implementation of H5PFrameworkInterface.
@ -625,6 +626,7 @@ export class CoreH5PFramework {
return Object.assign(library, {
semantics: library.semantics ? CoreTextUtils.instance.parseJSON(library.semantics, null) : null,
addto: library.addto ? CoreTextUtils.instance.parseJSON(library.addto, null) : null,
metadatasettings: library.metadatasettings ? CoreTextUtils.instance.parseJSON(library.metadatasettings, null) : null,
});
}
@ -712,6 +714,8 @@ export class CoreH5PFramework {
droplibrarycss: dropLibraryCSS,
semantics: typeof libraryData.semantics != 'undefined' ? JSON.stringify(libraryData.semantics) : null,
addto: typeof libraryData.addTo != 'undefined' ? JSON.stringify(libraryData.addTo) : null,
metadatasettings: typeof libraryData.metadataSettings != 'undefined' ?
CoreH5PMetadata.boolifyAndEncodeSettings(libraryData.metadataSettings) : null,
};
if (libraryData.libraryId) {
@ -898,9 +902,10 @@ export type CoreH5PFrameworkContentData = {
metadata: unknown; // Content metadata.
};
export type CoreH5PLibraryParsedDBRecord = Omit<CoreH5PLibraryDBRecord, 'semantics'|'addto'> & {
export type CoreH5PLibraryParsedDBRecord = Omit<CoreH5PLibraryDBRecord, 'semantics'|'addto'|'metadatasettings'> & {
semantics: CoreH5PSemantics[] | null;
addto: CoreH5PLibraryAddTo | null;
metadatasettings: CoreH5PLibraryMetadataSettings | null;
};
type LibraryDependency = {

View File

@ -26,7 +26,11 @@ export class CoreH5PMetadata {
* @param metadataSettings Settings.
* @return Stringified settings.
*/
static boolifyAndEncodeSettings(metadataSettings: CoreH5PLibraryMetadataSettings): string {
static boolifyAndEncodeSettings(metadataSettings: CoreH5PLibraryMetadataSettings | string): string {
if (typeof metadataSettings == 'string') {
return metadataSettings;
}
// Convert metadataSettings values to boolean.
if (typeof metadataSettings.disable != 'undefined') {
metadataSettings.disable = metadataSettings.disable === 1;

View File

@ -77,7 +77,7 @@ export class CoreH5PStorage {
// Convert metadataSettings values to boolean and json_encode it before saving.
libraryData.metadataSettings = libraryData.metadataSettings ?
CoreH5PMetadata.boolifyAndEncodeSettings(<CoreH5PLibraryMetadataSettings> libraryData.metadataSettings) : undefined;
CoreH5PMetadata.boolifyAndEncodeSettings(libraryData.metadataSettings) : undefined;
// Save the library data in DB.
await this.h5pFramework.saveLibraryData(libraryData, siteId);

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { SQLiteDB } from '@classes/sqlitedb';
import { CoreSiteSchema } from '@services/sites';
/**
@ -19,7 +20,7 @@ import { CoreSiteSchema } from '@services/sites';
*/
// DB table names.
export const CONTENT_TABLE_NAME = 'h5p_content'; // H5P content.
export const LIBRARIES_TABLE_NAME = 'h5p_libraries'; // Installed libraries.
export const LIBRARIES_TABLE_NAME = 'h5p_libraries_2'; // Installed libraries.
export const LIBRARY_DEPENDENCIES_TABLE_NAME = 'h5p_library_dependencies'; // Library dependencies.
export const CONTENTS_LIBRARIES_TABLE_NAME = 'h5p_contents_libraries'; // Which library is used in which content.
export const LIBRARIES_CACHEDASSETS_TABLE_NAME = 'h5p_libraries_cachedassets'; // H5P cached library assets.
@ -148,6 +149,10 @@ export const SITE_SCHEMA: CoreSiteSchema = {
name: 'addto',
type: 'TEXT',
},
{
name: 'metadatasettings',
type: 'TEXT',
},
],
},
{
@ -239,6 +244,28 @@ export const SITE_SCHEMA: CoreSiteSchema = {
],
},
],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async migrate(db: SQLiteDB, oldVersion: number, siteId: string): Promise<void> {
if (oldVersion >= 2) {
return;
}
const newTable = LIBRARIES_TABLE_NAME;
const oldTable = 'h5p_libraries';
try {
await db.tableExists(oldTable);
// Move the records from the old table.
const entries = await db.getAllRecords<CoreH5PLibraryDBRecord>(oldTable);
await Promise.all(entries.map((entry) => db.insertRecord(newTable, entry)));
await db.dropTable(oldTable);
} catch {
// Old table does not exist, ignore.
}
},
};
/**
@ -273,6 +300,7 @@ export type CoreH5PLibraryDBRecord = {
droplibrarycss?: string | null; // Libraries that should not have CSS included if this lib is used. Comma separated list.
semantics?: string | null; // The semantics definition.
addto?: string | null; // Plugin configuration data.
metadatasettings?: string | null; // Metadata settings.
};
/**