Skip to content

Commit 724e0cc

Browse files
committed
MOBILE-3406 h5p: Refactor code into different classes
1 parent 4f9adba commit 724e0cc

File tree

18 files changed

+3743
-3336
lines changed

18 files changed

+3743
-3336
lines changed

src/core/h5p/classes/content-validator.ts

Lines changed: 189 additions & 229 deletions
Large diffs are not rendered by default.

src/core/h5p/classes/core.ts

Lines changed: 989 additions & 0 deletions
Large diffs are not rendered by default.

src/core/h5p/classes/file-storage.ts

Lines changed: 457 additions & 0 deletions
Large diffs are not rendered by default.

src/core/h5p/classes/framework.ts

Lines changed: 902 additions & 0 deletions
Large diffs are not rendered by default.

src/core/h5p/classes/helper.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import { CoreFile, CoreFileProvider } from '@providers/file';
16+
import { CoreSites } from '@providers/sites';
17+
import { CoreMimetypeUtils } from '@providers/utils/mimetype';
18+
import { CoreTextUtils } from '@providers/utils/text';
19+
import { CoreH5P } from '../providers/h5p';
20+
import { CoreH5PCore } from './core';
21+
import { FileEntry } from '@ionic-native/file';
22+
23+
/**
24+
* Equivalent to Moodle's H5P helper class.
25+
*/
26+
export class CoreH5PHelper {
27+
28+
/**
29+
* Get the core H5P assets, including all core H5P JavaScript and CSS.
30+
*
31+
* @return Array core H5P assets.
32+
*/
33+
static async getCoreAssets(siteId?: string): Promise<{settings: any, cssRequires: string[], jsRequires: string[]}> {
34+
35+
// Get core settings.
36+
const settings = await CoreH5PHelper.getCoreSettings(siteId);
37+
38+
settings.core = {
39+
styles: [],
40+
scripts: []
41+
};
42+
settings.loadedJs = [];
43+
settings.loadedCss = [];
44+
45+
const libUrl = CoreH5P.instance.h5pCore.h5pFS.getCoreH5PPath();
46+
const cssRequires: string[] = [];
47+
const jsRequires: string[] = [];
48+
49+
// Add core stylesheets.
50+
CoreH5PCore.STYLES.forEach((style) => {
51+
settings.core.styles.push(libUrl + style);
52+
cssRequires.push(libUrl + style);
53+
});
54+
55+
// Add core JavaScript.
56+
CoreH5PCore.getScripts().forEach((script) => {
57+
settings.core.scripts.push(script);
58+
jsRequires.push(script);
59+
});
60+
61+
return {settings: settings, cssRequires: cssRequires, jsRequires: jsRequires};
62+
}
63+
64+
/**
65+
* Get the settings needed by the H5P library.
66+
*
67+
* @param siteId The site ID. If not defined, current site.
68+
* @return Promise resolved with the settings.
69+
*/
70+
static async getCoreSettings(siteId?: string): Promise<any> {
71+
72+
const site = await CoreSites.instance.getSite(siteId);
73+
74+
const basePath = CoreFile.instance.getBasePathInstant();
75+
const ajaxPaths = {
76+
xAPIResult: '',
77+
contentUserData: '',
78+
};
79+
80+
return {
81+
baseUrl: CoreFile.instance.getWWWPath(),
82+
url: CoreFile.instance.convertFileSrc(CoreTextUtils.instance.concatenatePaths(
83+
basePath, CoreH5P.instance.h5pCore.h5pFS.getExternalH5PFolderPath(site.getId()))),
84+
urlLibraries: CoreFile.instance.convertFileSrc(CoreTextUtils.instance.concatenatePaths(
85+
basePath, CoreH5P.instance.h5pCore.h5pFS.getLibrariesFolderPath(site.getId()))),
86+
postUserStatistics: false,
87+
ajax: ajaxPaths,
88+
saveFreq: false,
89+
siteUrl: site.getURL(),
90+
l10n: {
91+
H5P: CoreH5P.instance.h5pCore.getLocalization(),
92+
},
93+
user: [],
94+
hubIsEnabled: false,
95+
reportingIsEnabled: false,
96+
crossorigin: null,
97+
libraryConfig: null,
98+
pluginCacheBuster: '',
99+
libraryUrl: CoreTextUtils.instance.concatenatePaths(CoreH5P.instance.h5pCore.h5pFS.getCoreH5PPath(), 'js'),
100+
};
101+
}
102+
103+
/**
104+
* Extract and store an H5P file.
105+
* This function won't validate most things because it should've been done by the server already.
106+
*
107+
* @param fileUrl The file URL used to download the file.
108+
* @param file The file entry of the downloaded file.
109+
* @param siteId Site ID. If not defined, current site.
110+
* @return Promise resolved when done.
111+
*/
112+
static async saveH5P(fileUrl: string, file: FileEntry, siteId?: string): Promise<void> {
113+
siteId = siteId || CoreSites.instance.getCurrentSiteId();
114+
115+
// Unzip the file.
116+
const folderName = CoreMimetypeUtils.instance.removeExtension(file.name);
117+
const destFolder = CoreTextUtils.instance.concatenatePaths(CoreFileProvider.TMPFOLDER, 'h5p/' + folderName);
118+
119+
// Unzip the file.
120+
await CoreFile.instance.unzipFile(file.toURL(), destFolder);
121+
122+
try {
123+
// Read the contents of the unzipped dir, process them and store them.
124+
const contents = await CoreFile.instance.getDirectoryContents(destFolder);
125+
126+
const filesData = await CoreH5P.instance.h5pValidator.processH5PFiles(destFolder, contents);
127+
128+
const content = await CoreH5P.instance.h5pStorage.savePackage(filesData, folderName, fileUrl, false, siteId);
129+
130+
// Create the content player.
131+
const contentData = await CoreH5P.instance.h5pCore.loadContent(content.id, undefined, siteId);
132+
133+
const embedType = CoreH5PCore.determineEmbedType(contentData.embedType, contentData.library.embedTypes);
134+
135+
await CoreH5P.instance.h5pPlayer.createContentIndex(content.id, fileUrl, contentData, embedType, siteId);
136+
} finally {
137+
// Remove tmp folder.
138+
try {
139+
await CoreFile.instance.removeDir(destFolder);
140+
} catch (error) {
141+
// Ignore errors, it will be deleted eventually.
142+
}
143+
}
144+
}
145+
}

src/core/h5p/classes/metadata.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// (C) Copyright 2015 Moodle Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Equivalent to H5P's H5PMetadata class.
17+
*/
18+
export class CoreH5PMetadata {
19+
20+
/**
21+
* The metadataSettings field in libraryJson uses 1 for true and 0 for false.
22+
* Here we are converting these to booleans, and also doing JSON encoding.
23+
*
24+
* @param metadataSettings Settings.
25+
* @return Stringified settings.
26+
*/
27+
static boolifyAndEncodeSettings(metadataSettings: any): string {
28+
// Convert metadataSettings values to boolean.
29+
if (typeof metadataSettings.disable != 'undefined') {
30+
metadataSettings.disable = metadataSettings.disable === 1;
31+
}
32+
if (typeof metadataSettings.disableExtraTitleField != 'undefined') {
33+
metadataSettings.disableExtraTitleField = metadataSettings.disableExtraTitleField === 1;
34+
}
35+
36+
return JSON.stringify(metadataSettings);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)