Skip to content

Commit 91fe21f

Browse files
Merge pull request #2533 from adaptlearning/issue/adapt_framework#2841
Populate default globals on import
2 parents a3ef21a + ca53f36 commit 91fe21f

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

plugins/output/adapt/importsource.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const helpers = require('./outputHelpers');
1111
const logger = require("../../../lib/logger");
1212
const mime = require('mime');
1313
const path = require("path");
14+
const { promisify } = require('util');
1415

1516
function ImportSource(req, done) {
1617
var dbInstance;
@@ -340,6 +341,31 @@ function ImportSource(req, done) {
340341
}, cb2);
341342
}, cb);
342343
},
344+
async function populateGlobals() {
345+
const dbRetrieve = promisify(dbInstance.retrieve.bind(dbInstance));
346+
const dbUpdate = promisify(dbInstance.update.bind(dbInstance));
347+
const courseQuery = await dbRetrieve('course', { _id: courseId });
348+
const courseGlobals = courseQuery[0]._doc._globals || {};
349+
await Promise.all(plugindata.pluginIncludes.map(async ({ type, name }) => {
350+
const pluginQuery = await dbRetrieve(`${type}type`, { name });
351+
const plugin = pluginQuery[0]._doc;
352+
const schemaGlobals = plugin.globals;
353+
if (!schemaGlobals) return;
354+
const schemaDefaults = {};
355+
const typeKey = type === 'component' || type === 'extension' ?
356+
`_${type}s` :
357+
`_${type}`;
358+
const pluginKey = `_${plugin[type]}`;
359+
if (!courseGlobals[typeKey]) {
360+
courseGlobals[typeKey] = {};
361+
}
362+
Object.entries(schemaGlobals).forEach(([ key, value ]) => {
363+
schemaDefaults[key] = value.default;
364+
});
365+
courseGlobals[typeKey][pluginKey] = _.defaults(courseGlobals[typeKey][pluginKey], schemaDefaults);
366+
}));
367+
await dbUpdate('course', { _id: courseId }, { _globals: courseGlobals });
368+
},
343369
function checkDetachedContent(cb) {
344370
const detachedIds = Object.keys(detachedElementsMap);
345371
if (detachedIds.length === 0) return cb();

0 commit comments

Comments
 (0)