@@ -277,6 +277,63 @@ 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' ) ;
289+ errors += app . polyglot . t ( 'app.doesnotcontain' , {
290+ type : courseString [ 0 ] . toUpperCase ( ) + courseString . slice ( 1 ) ,
291+ title : data . course [ 0 ] . title ,
292+ childType : app . polyglot . t ( 'app.page' , 0 )
293+ } ) + '\n' ;
294+ return cb ( errors , false ) ;
295+ }
296+
297+ errors += iterateThroughChildren ( contentObjects , articles ) ;
298+ errors += iterateThroughChildren ( articles , blocks ) ;
299+ errors += iterateThroughChildren ( blocks , components ) ;
300+
301+ if ( errors . length !== 0 ) return cb ( errors , false ) ;
302+
303+ return cb ( null , true ) ;
304+ }
305+
306+ function iterateThroughChildren ( parents , children ) {
307+ let errors = '' ;
308+ if ( typeof parents === 'undefined' ) return errors ;
309+
310+ const appendError = ( parentType , parentTitle , childType ) => {
311+ errors += app . polyglot . t ( 'app.doesnotcontain' , {
312+ type : parentType [ 0 ] . toUpperCase ( ) + parentType . slice ( 1 ) ,
313+ title : parentTitle ,
314+ childType : childType
315+ } ) + '\n' ;
316+ } ;
317+
318+ parents . forEach ( parent => {
319+ let parentType = app . polyglot . t ( 'app.' + parent . _type , 1 ) ;
320+ let childType = app . polyglot . t ( 'app.children' ) ;
321+
322+ if ( typeof children === 'undefined' ) {
323+ appendError ( parentType , parent . title , childType ) ;
324+ return ;
325+ }
326+
327+ if ( children [ 0 ] && children [ 0 ] . _type ) childType = app . polyglot . t ( 'app.' + children [ 0 ] . _type , 0 ) ;
328+ let found = children . find ( child => JSON . stringify ( child . _parentId ) === JSON . stringify ( parent . _id ) ) ;
329+
330+ if ( typeof found === 'undefined' ) {
331+ appendError ( parentType , parent . title , childType ) ;
332+ }
333+ } ) ;
334+ return errors ;
335+ }
336+
280337function ImportError ( message , httpStatus ) {
281338 this . message = message || "Course import failed" ;
282339 this . httpStatus = httpStatus || 500 ;
@@ -322,5 +379,6 @@ exports = module.exports = {
322379 ImportError : ImportError ,
323380 PartialImportError : PartialImportError ,
324381 sortContentObjects : sortContentObjects ,
325- cleanUpImport : cleanUpImport
382+ cleanUpImport : cleanUpImport ,
383+ validateCourse : validateCourse
326384} ;
0 commit comments