-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added upgradePci to upgrade TAO PCI's after qti3 stylesheet conversion
- Loading branch information
1 parent
683955f
commit fdd8830
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/lib/qti-transformer/transformers/upgrade-pci/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import { upgradePci } from '.'; | ||
import { qtiTransform } from '../../qti-transform'; | ||
import { areXmlEqual } from '../utils-node-only'; | ||
|
||
const xml = String.raw; | ||
|
||
const transformObjectTags = (xmlContent: string) => { | ||
const modifiedContent = qtiTransform(xmlContent).fnCh(upgradePci).xml(); | ||
return modifiedContent; | ||
}; | ||
|
||
test('upgrade tao exported pci', async () => { | ||
const input = xml`<?xml version="1.0" encoding="UTF-8"?> | ||
<qti-custom-interaction response-identifier="RESPONSE" | ||
data-base-ref="https://europe-west4-qti-convert.cloudfunctions.net/api/application/convert-online/package/b11f2f15259a9289eab9ff1c8bb6b94bd503914b62da22668547d27855b5df5a" | ||
data-base-item="https://europe-west4-qti-convert.cloudfunctions.net/api/application/convert-online/package/b11f2f15259a9289eab9ff1c8bb6b94bd503914b62da22668547d27855b5df5a"> | ||
<qti-portable-custom-interaction | ||
custom-interaction-type-identifier="colorProportions" data-version="1.0.1" | ||
data-base-url="https://europe-west4-qti-convert.cloudfunctions.net/api/application/convert-online/package/b11f2f15259a9289eab9ff1c8bb6b94bd503914b62da22668547d27855b5df5a"> | ||
<properties> | ||
<property key="colors">red, blue, yellow</property> | ||
<property key="width">400</property> | ||
<property key="height">400</property> | ||
</properties> | ||
<modules> | ||
<module id="colorProportions/interaction/runtime/js/index" | ||
primary-path="http://localhost:3333/application/convert-online/package/b11f2f15259a9289eab9ff1c8bb6b94bd503914b62da22668547d27855b5df5a/interaction/runtime/js/index.js" /> | ||
</modules> | ||
<markup> | ||
<div class="pciInteraction"> | ||
<style>.pciInteraction ul.pci{ | ||
list-style-type: none; | ||
margin: 0 5px; | ||
padding: 0; | ||
display: inline-block; | ||
position: relative; | ||
top: 7px; | ||
} | ||
.pciInteraction ul.pci li{ | ||
float: left; | ||
text-align: left; | ||
list-style-type: none; | ||
}</style> | ||
<div class="prompt" /> | ||
<ul class="pci" /> | ||
</div> | ||
</markup> | ||
</qti-portable-custom-interaction> | ||
</qti-custom-interaction> | ||
`; | ||
const expectedOutput = xml`<?xml version="1.0" encoding="UTF-8"?> | ||
<qti-portable-custom-interaction | ||
custom-interaction-type-identifier="colorProportions" | ||
data-version="1.0.1" | ||
data-colors="red, blue, yellow" | ||
data-width="400" | ||
data-height="400" | ||
module="colorProportions" | ||
response-identifier="RESPONSE" | ||
> | ||
<qti-interaction-modules> | ||
<qti-interaction-module | ||
id="colorProportions" | ||
primary-path="http://localhost:3333/application/convert-online/package/b11f2f15259a9289eab9ff1c8bb6b94bd503914b62da22668547d27855b5df5a/interaction/runtime/js/index.js" | ||
></qti-interaction-module> | ||
</qti-interaction-modules> | ||
<qti-interaction-markup> | ||
<div class="pciInteraction"> | ||
<div class="prompt" /> | ||
<ul class="pci" /> | ||
</div> | ||
</qti-interaction-markup> | ||
</qti-portable-custom-interaction>`; | ||
const result = await transformObjectTags(input); | ||
const areEqual = await areXmlEqual(result, expectedOutput); | ||
expect(areEqual).toEqual(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as cheerio from 'cheerio'; | ||
|
||
export function upgradePci($: cheerio.CheerioAPI) { | ||
const customInteraction = $('qti-custom-interaction'); | ||
const portableCustomInteraction = customInteraction.find('qti-portable-custom-interaction'); | ||
|
||
if (portableCustomInteraction.length === 0) { | ||
return $; | ||
} | ||
|
||
// 1. Remove the <style> tag | ||
portableCustomInteraction.find('style').remove(); | ||
|
||
// 2. Move properties to data-attributes on qti-portable-custom-interaction | ||
const properties = portableCustomInteraction.find('properties property'); | ||
properties.each((_, property) => { | ||
const key = $(property).attr('key'); | ||
const value = $(property).text(); | ||
if (key && value) { | ||
portableCustomInteraction.attr(`data-${key}`, value); | ||
} | ||
}); | ||
portableCustomInteraction.find('properties').remove(); | ||
|
||
// 3. Fix tagnames and structure | ||
// Change <modules> to <qti-interaction-modules> and <module> to <qti-interaction-module> | ||
const modules = portableCustomInteraction.find('modules'); | ||
if (modules.length > 0) { | ||
const newModules = $('<qti-interaction-modules></qti-interaction-modules>'); | ||
modules.find('module').each((_, module) => { | ||
const newModule = $('<qti-interaction-module></qti-interaction-module>'); | ||
const id = $(module).attr('id')?.split('/')[0]; // Get the id up to the first slash | ||
newModule.attr('id', id || ''); | ||
newModule.attr('primary-path', $(module).attr('primary-path')); | ||
newModules.append(newModule); | ||
}); | ||
modules.replaceWith(newModules); | ||
} | ||
|
||
// 4. Remove data-base-* attributes | ||
portableCustomInteraction.removeAttr('data-base-ref'); | ||
portableCustomInteraction.removeAttr('data-base-item'); | ||
portableCustomInteraction.removeAttr('data-base-url'); | ||
|
||
// 5. Fix the id of the qti-interaction-module to be the value of custom-interaction-type-identifier | ||
const customInteractionTypeIdentifier = portableCustomInteraction.attr('custom-interaction-type-identifier'); | ||
if (customInteractionTypeIdentifier) { | ||
portableCustomInteraction.find('qti-interaction-module').attr('id', customInteractionTypeIdentifier); | ||
|
||
// Add the module attribute with the same value as custom-interaction-type-identifier | ||
portableCustomInteraction.attr('module', customInteractionTypeIdentifier); | ||
} | ||
|
||
// 6. Copy response-identifier from qti-custom-interaction to qti-portable-custom-interaction | ||
const responseIdentifier = customInteraction.attr('response-identifier'); | ||
if (responseIdentifier) { | ||
portableCustomInteraction.attr('response-identifier', responseIdentifier); | ||
} | ||
|
||
// 7. Rename <markup> to <qti-interaction-markup> | ||
const markup = portableCustomInteraction.find('markup'); | ||
if (markup.length > 0) { | ||
markup.replaceWith('<qti-interaction-markup>' + markup.html() + '</qti-interaction-markup>'); | ||
} | ||
|
||
// 8. Remove the qti-custom-interaction wrapper | ||
const newPortableCustomInteraction = portableCustomInteraction.clone(); | ||
customInteraction.replaceWith(newPortableCustomInteraction); | ||
|
||
return $; | ||
} |