Skip to content

Commit

Permalink
Issue #SB-11001 merge: Merge pull request #61 from project-sunbird/qu…
Browse files Browse the repository at this point in the history
…estion_asset_fix

Question plugin missing assets fix
  • Loading branch information
vinukumar-vs authored Mar 18, 2019
2 parents ab3df17 + ab02816 commit 942b49f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ <h5 ng-bind="message.message"></h5>
<script src="scripts/contenteditor/migration/scribblemigration-task.js"></script>
<script src="scripts/contenteditor/migration/readalongmigration-task.js"></script>
<script src="scripts/contenteditor/migration/assessmentmigration-task.js"></script>
<script src="scripts/contenteditor/migration/questionsetfix1-task.js"></script>
<script src="scripts/contenteditor/migration/eventsmigration-task.js"></script>
<script src="scripts/contenteditor/migration/settagmigration-task.js"></script>
<script src="scripts/contenteditor/migration/textmigration-task.js"></script>
Expand Down
4 changes: 3 additions & 1 deletion app/scripts/contenteditor/manager/stage-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ org.ekstep.contenteditor.stageManager = new (Class.extend({
content.theme['migration-media'].media = _.values(org.ekstep.contenteditor.mediaManager.migratedMediaMap)
}
content.theme.manifest.media = _.uniqBy(_.concat(content.theme.manifest.media, _.values(mediaMap)), 'id')

if (!_.isEmpty(org.ekstep.contenteditor.migration.patch)) {
content.theme['patch'] = org.ekstep.contenteditor.migration.patch.toString()
}
return _.cloneDeep(content)
},
manifestGenerator: function (content) {
Expand Down
54 changes: 54 additions & 0 deletions app/scripts/contenteditor/migration/1_migration-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,58 @@
org.ekstep.contenteditor.migration = new (Class.extend({
migrationFlag: false,
contentBackup: undefined,
patch: [],
init: function () {
org.ekstep.contenteditor.api.addEventListener('content:migration:start', this.execute, this)
},
_startTime: undefined,
tasks: ['mediamigration_task', 'basestage_task', 'orderstage_task', 'scribblemigration_task', 'imagemigration_task', 'readalongmigration_task', 'assessmentmigration_task', 'eventsmigration_task', 'settagmigration_task'],
questionPatchs: ['patch_1'],
migrationErrors: [],
execute: function (event, data) {
var instance = this
var contentbody = data.body; var stageIcons = data.stageIcons
this.contentBackup = _.cloneDeep(contentbody)

if (!_.has(contentbody, 'theme.stage')) org.ekstep.services.telemetryService.error({ 'env': 'content', 'stage': '', 'action': 'migration', 'objectId': '', objectType: '', 'err': 'migration has errors', 'type': 'PORTAL', 'data': '', 'severity': 'error' })
if (contentbody.theme && contentbody.theme.patch) {
this.patch = contentbody.theme.patch.split(',')
}
if (this.isOldContent(contentbody)) {
this.initLoadScreenMsg()
this._startTime = (new Date()).getTime()
_.forEach(this.tasks, function (task) {
org.ekstep.contenteditor.migration[task].migrate(contentbody)
})
this.postMigration(contentbody, stageIcons)
} else if (this.isAssessmentContent(contentbody)) {
this._startTime = (new Date()).getTime()
console.info('Question media migration started!')
_.forEach(this.questionPatchs, function (task) {
if (_.indexOf(instance.patch, task) === -1) {
org.ekstep.contenteditor.migration[task].migrate(contentbody)
}
})
this.postQuestionMediaMigration(contentbody, stageIcons)
} else {
console.info('no need for migration')
org.ekstep.contenteditor.stageManager.fromECML(contentbody, stageIcons)
}
},
postQuestionMediaMigration: function (content, stageIcons) {
var instance = this
console.info('Question media migration completed!')
org.ekstep.contenteditor.stageManager.fromECML(content, stageIcons)
org.ekstep.services.telemetryService.log({
type: 'process',
level: 'TRACE',
message: 'duration for question media migration',
params: [{
// eslint-disable-next-line
duration: (new Date()).getTime() - instance._startTime
}]
})
},
postMigration: function (content, stageIcons) {
var instance = this
org.ekstep.services.telemetryService.log({
Expand Down Expand Up @@ -82,5 +111,30 @@ org.ekstep.contenteditor.migration = new (Class.extend({
},
getBackupContent: function () {
return this.contentBackup
},
isAssessmentContent: function (contentbody) {
var instance = this
var assessmentContent = false
if (_.isEqual(_.sortBy(instance.patch), _.sortBy(instance.questionPatchs))) {
return false
}
_.forEach(contentbody.theme.stage, function (stage) {
_.forEach(stage['org.ekstep.questionset'], function (qSet, index) {
if (_.isArray(qSet['org.ekstep.question'])) {
_.forEach(qSet['org.ekstep.question'], function (question) {
if (question.pluginId === 'org.ekstep.questionset.quiz') {
return
}
assessmentContent = true
})
} else {
if (qSet.pluginId === 'org.ekstep.questionset.quiz') {
return
}
assessmentContent = true
}
})
})
return assessmentContent
}
}))()
150 changes: 150 additions & 0 deletions app/scripts/contenteditor/migration/questionsetfix1-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use strict'

org.ekstep.contenteditor.migration.patch_1 = new (Class.extend({
init: function () {
},
id: 'patch_1',
_startTime: undefined,
contentbody: undefined,
assetHostPaths: [
'https://ntpproductionall.blob.core.windows.net/ntp-content-production/',
'https://ntpstagingall.blob.core.windows.net/ntp-content-staging/',
'https://preprodall.blob.core.windows.net/ntp-content-preprod/',
'https://sunbirddev.blob.core.windows.net/sunbird-content-dev/',
'https://s3.ap-south-1.amazonaws.com/ekstep-public-dev/',
'https://ekstep-public-dev.s3-ap-south-1.amazonaws.com/',
'https://s3.ap-south-1.amazonaws.com/ekstep-public-qa/',
'https://ekstep-public-qa.s3-ap-south-1.amazonaws.com/',
'https://s3.ap-south-1.amazonaws.com/ekstep-public-preprod/',
'https://ekstep-public-preprod.s3-ap-south-1.amazonaws.com/',
'https://s3.ap-south-1.amazonaws.com/ekstep-public-prod/',
'https://ekstep-public-prod.s3-ap-south-1.amazonaws.com/'
],
migrate: function (contentbody) {
var instance = this
this._startTime = (new Date()).getTime()
this.contentbody = contentbody
_.forEach(contentbody.theme.stage, function (stage) {
if (stage['org.ekstep.questionset']) {
if (_.isArray(stage['org.ekstep.questionset'])) {
_.forEach(stage['org.ekstep.questionset'], function (qSet, index) {
_.forEach(qSet['org.ekstep.question'], function (question, qindex) {
if (question.pluginId === 'org.ekstep.questionset.quiz') {
return
}
var fixedQuestion = instance.updateQuestionMedia(question)
qSet['org.ekstep.question'][qindex] = fixedQuestion
instance.contentbody.theme.manifest.media = instance.updateContentManifestMedia(instance.contentbody.theme.manifest.media, fixedQuestion)
})
})
} else if (_.isObject(stage['org.ekstep.questionset'])) {
var question = stage['org.ekstep.questionset']['org.ekstep.question']
if (question.pluginId === 'org.ekstep.questionset.quiz') {
return
}
var fixedQuestion = instance.updateQuestionMedia(question)
stage['org.ekstep.questionset']['org.ekstep.question'] = fixedQuestion
instance.contentbody.theme.manifest.media = instance.updateContentManifestMedia(instance.contentbody.theme.manifest.media, fixedQuestion)
}
}
})
org.ekstep.contenteditor.migration.patch.push(instance.id)
org.ekstep.services.telemetryService.log({
type: 'process',
level: 'TRACE',
message: 'questionsetfix_1 migration complted',
params: [{
// eslint-disable-next-line
duration: (new Date()).getTime() - instance._startTime
}]
})
},
updateContentManifestMedia: function (contentMedia, question) {
var questionMedia = JSON.parse(question.data.__cdata).media
_.each(questionMedia, function (media) {
if (_.isUndefined(_.find(contentMedia, { 'src': media.src }))) {
contentMedia.push(media)
org.ekstep.contenteditor.mediaManager.addToMigratedMedia(media)
}
})
return contentMedia
},
updateQuestionMedia: function (question) {
var instance = this
var questionData = JSON.parse(question.data.__cdata)
var quesAssets = []

// For Question Title
if (questionData.question) {
instance.addAssets(questionData.question, quesAssets)
}

// For Question Options
if (questionData.options) {
// For MCQ
_.each(questionData.options, function (o) {
instance.addAssets(o, quesAssets)
})
}

if (questionData.option) {
// For MTF
if (questionData.option.optionsLHS) {
_.each(questionData.option.optionsLHS, function (o) {
instance.addAssets(o, quesAssets)
})
}
if (questionData.option.optionsRHS) {
_.each(questionData.option.optionsRHS, function (o) {
instance.addAssets(o, quesAssets)
})
}
}

_.each(questionData.media, function (media) {
media.src = instance.getRelativeURL(media.src)
})
_.each(quesAssets, function (quesAsset) {
// quesAsset.url = getRelativeURL(quesAsset.url);
var mediaExist = _.find(questionData.media, function (m) {
return m.src === quesAsset.src
})
if (!mediaExist) {
questionData.media.push(instance.getMediaObj(quesAsset.src, quesAsset.type))
}
})
question.data['__cdata'] = JSON.stringify(questionData)
return question
},
addAssets: function (obj, targetArray) {
var instance = this
if (obj.image) {
obj.image = instance.getRelativeURL(obj.image)
targetArray.push({ 'type': 'image', 'src': obj.image })
}
if (obj.audio) {
obj.audio = instance.getRelativeURL(obj.audio)
targetArray.push({ 'type': 'audio', 'src': obj.audio })
}
},
getRelativeURL: function (src) {
var instance = this
var relativeURLPrefix = '/assets/public/'
_.forEach(instance.assetHostPaths, function (url) {
if (src.indexOf(url) !== -1) {
src = src.replace(url, relativeURLPrefix)
}
})
return src
},
getMediaObj: function (src, type) {
var mediaId = Math.floor(Math.random() * 1000000000)
return {
'id': mediaId + '-1',
'src': src,
'assetId': mediaId,
'type': type,
'preload': false
}
}
}))()
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var contentEditorApp = [
"app/scripts/contenteditor/migration/eventsmigration-task.js",
"app/scripts/contenteditor/migration/settagmigration-task.js",
"app/scripts/contenteditor/migration/textmigration-task.js",
"app/scripts/contenteditor/migration/questionsetfix1-task.js",
"app/scripts/contenteditor/manager/stage-manager.js"
];

Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ var EDITOR_APP = [
"./app/scripts/contenteditor/migration/eventsmigration-task.js",
"./app/scripts/contenteditor/migration/settagmigration-task.js",
"./app/scripts/contenteditor/migration/textmigration-task.js",
"./app/scripts/contenteditor/migration/questionsetfix1-task.js",
"./app/scripts/contenteditor/manager/stage-manager.js"
];
const APP_STYLE = [
Expand Down

0 comments on commit 942b49f

Please sign in to comment.