@@ -277,6 +277,43 @@ function getPluginFrameworkVersionCategory(serverFrameworkVersion, pluginMetaDat
277277 } ) ;
278278} ;
279279
280+ function validateCourse ( data , cb ) {
281+ let errors = '' ;
282+ let contentObjects = data . contentobject ;
283+ let articles = data . article ;
284+ let blocks = data . block ;
285+ let components = data . component ;
286+
287+ if ( typeof contentObjects === 'undefined' ) {
288+ let courseString = app . polyglot . t ( 'app.course' ) . charAt ( 0 ) . toUpperCase ( ) + app . polyglot . t ( 'app.course' ) . slice ( 1 ) ;
289+ errors += courseString + ' "' + data . course [ 0 ] . title + '" ' + app . polyglot . t ( 'app.doesnotcontain' ) + ' ' + app . polyglot . t ( 'app.page' ) + 's\n' ;
290+ return cb ( errors , false ) ;
291+ }
292+
293+ errors += iterateThroughChildren ( contentObjects , articles ) ;
294+ errors += iterateThroughChildren ( articles , blocks ) ;
295+ errors += iterateThroughChildren ( blocks , components ) ;
296+
297+ if ( errors . length !== 0 ) return cb ( errors , false ) ;
298+
299+ return cb ( null , true ) ;
300+ }
301+
302+ function iterateThroughChildren ( parents , children ) {
303+ let errors = '' ;
304+ parents . forEach ( parent => {
305+ let parentType = app . polyglot . t ( 'app.' + parent . _type ) . charAt ( 0 ) . toUpperCase ( ) + app . polyglot . t ( 'app.' + parent . _type ) . slice ( 1 ) ;
306+ let childType = app . polyglot . t ( 'app.children' ) ;
307+ if ( children [ 0 ] && children [ 0 ] . _type ) childType = app . polyglot . t ( 'app.' + children [ 0 ] . _type ) + 's' ;
308+ let found = children . find ( child => JSON . stringify ( child . _parentId ) === JSON . stringify ( parent . _id ) ) ;
309+
310+ if ( typeof found === 'undefined' ) {
311+ errors += parentType + ' "' + parent . title + '" ' + app . polyglot . t ( 'app.doesnotcontain' ) + ' ' + childType + '\n' ;
312+ }
313+ } ) ;
314+ return errors ;
315+ }
316+
280317function ImportError ( message , httpStatus ) {
281318 this . message = message || "Course import failed" ;
282319 this . httpStatus = httpStatus || 500 ;
@@ -322,5 +359,6 @@ exports = module.exports = {
322359 ImportError : ImportError ,
323360 PartialImportError : PartialImportError ,
324361 sortContentObjects : sortContentObjects ,
325- cleanUpImport : cleanUpImport
362+ cleanUpImport : cleanUpImport ,
363+ validateCourse : validateCourse
326364} ;
0 commit comments