Skip to content

Commit

Permalink
implemented nested CC export.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Nov 23, 2023
1 parent fa8a048 commit 9c64a59
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AdmZip from 'adm-zip';
import { parseStringPromise } from 'xml2js';
import { CommonCartridgeResourceType, CommonCartridgeVersion } from './common-cartridge-enums';
import { CommonCartridgeFileBuilder, CommonCartridgeFileBuilderOptions } from './common-cartridge-file-builder';
import { ICommonCartridgeOrganizationProps } from './common-cartridge-organization-item-element';
import { CommonCartridgeOrganizationItemElementProps } from './common-cartridge-organization-item-element';
import { ICommonCartridgeResourceProps } from './common-cartridge-resource-item-element';

describe('CommonCartridgeFileBuilder', () => {
Expand All @@ -16,11 +16,12 @@ describe('CommonCartridgeFileBuilder', () => {
title: 'file-title',
version: CommonCartridgeVersion.V_1_1_0,
};
const organizationProps: ICommonCartridgeOrganizationProps = {
const organizationProps: CommonCartridgeOrganizationItemElementProps = {
version: CommonCartridgeVersion.V_1_1_0,
identifier: 'organization-identifier',
title: 'organization-title',
resources: [],
_tag: 'resourceCollection',
};
const ltiResourceProps: ICommonCartridgeResourceProps = {
version: CommonCartridgeVersion.V_1_1_0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { CommonCartridgeVersion } from './common-cartridge-enums';
import { CommonCartridgeManifestElement } from './common-cartridge-manifest-element';
import {
CommonCartridgeOrganizationItemElement,
ICommonCartridgeOrganizationProps,
CommonCartridgeOrganizationItemElementProps,
OrganizationItemCollection,
} from './common-cartridge-organization-item-element';
import {
CommonCartridgeResourceItemElement,
Expand All @@ -22,10 +23,11 @@ export type CommonCartridgeFileBuilderOptions = {

export interface ICommonCartridgeOrganizationBuilder {
addResourceToOrganization(props: ICommonCartridgeResourceProps): ICommonCartridgeOrganizationBuilder;
addSubOrganization(props: CommonCartridgeOrganizationItemElementProps): ICommonCartridgeOrganizationBuilder;
}

export interface ICommonCartridgeFileBuilder {
addOrganization(props: ICommonCartridgeOrganizationProps): ICommonCartridgeOrganizationBuilder;
addOrganization(props: CommonCartridgeOrganizationItemElementProps): ICommonCartridgeOrganizationBuilder;

addResourceToFile(props: ICommonCartridgeResourceProps): ICommonCartridgeFileBuilder;

Expand All @@ -34,29 +36,49 @@ export interface ICommonCartridgeFileBuilder {

class CommonCartridgeOrganizationBuilder implements ICommonCartridgeOrganizationBuilder {
constructor(
private readonly props: ICommonCartridgeOrganizationProps,
private readonly props: CommonCartridgeOrganizationItemElementProps,
private readonly xmlBuilder: Builder,
private readonly zipBuilder: AdmZip
) {}

private resourceProperties: ICommonCartridgeResourceProps[] = [];

private children: CommonCartridgeOrganizationBuilder[] = [];

get organization(): CommonCartridgeElement {
return new CommonCartridgeOrganizationItemElement(this.props);
return new CommonCartridgeOrganizationItemElement(this.orgProps);
}

get orgProps(): OrganizationItemCollection {
// TODO resources
return {
_tag: 'itemCollection',
title: this.props.title,
children: this.children.map((child) => child.orgProps),
};
}

get resources(): CommonCartridgeElement[] {
return this.props.resources.map(
(resourceProps) => new CommonCartridgeResourceItemElement(resourceProps, this.xmlBuilder)
);
return this.children
.flatMap((child) => child.resourceProperties)
.concat(this.resourceProperties)
.map((resourceProps) => new CommonCartridgeResourceItemElement(resourceProps, this.xmlBuilder));
}

addResourceToOrganization(props: ICommonCartridgeResourceProps): ICommonCartridgeOrganizationBuilder {
const newResource = new CommonCartridgeResourceItemElement(props, this.xmlBuilder);
this.props.resources.push(props);
this.resourceProperties.push(props);
if (!newResource.canInline()) {
this.zipBuilder.addFile(props.href, Buffer.from(newResource.content()));
}
return this;
}

addSubOrganization(props: CommonCartridgeOrganizationItemElementProps): ICommonCartridgeOrganizationBuilder {
const subOrgBuilder = new CommonCartridgeOrganizationBuilder(props, this.xmlBuilder, this.zipBuilder);
this.children.push(subOrgBuilder);
return subOrgBuilder;
}
}

export class CommonCartridgeFileBuilder implements ICommonCartridgeFileBuilder {
Expand All @@ -70,7 +92,7 @@ export class CommonCartridgeFileBuilder implements ICommonCartridgeFileBuilder {

constructor(private readonly options: CommonCartridgeFileBuilderOptions) {}

addOrganization(props: ICommonCartridgeOrganizationProps): ICommonCartridgeOrganizationBuilder {
addOrganization(props: CommonCartridgeOrganizationItemElementProps): ICommonCartridgeOrganizationBuilder {
const organizationBuilder = new CommonCartridgeOrganizationBuilder(props, this.xmlBuilder, this.zipBuilder);
this.organizations.push(organizationBuilder);
return organizationBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
ICommonCartridgeOrganizationProps,
CommonCartridgeOrganizationItemElementProps,
CommonCartridgeOrganizationItemElement,
} from './common-cartridge-organization-item-element';
import { CommonCartridgeVersion, CommonCartridgeResourceType } from './common-cartridge-enums';
Expand All @@ -16,11 +16,12 @@ describe('CommonCartridgeOrganizationItemElement', () => {
title: 'Web Link',
html: 'html tags for testing',
};
const props: ICommonCartridgeOrganizationProps = {
const props: CommonCartridgeOrganizationItemElementProps = {
identifier: 'identifier',
title: 'title of organization item element',
version: 'version of common cartridge',
resources: [webContentResourceProps],
_tag: 'resourceCollection',
};
const organizationItemElement = new CommonCartridgeOrganizationItemElement(props);
const transformed = organizationItemElement.transform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ import { CommonCartridgeElement } from './common-cartridge-element.interface';
import { ICommonCartridgeResourceProps } from './common-cartridge-resource-item-element';
import { createIdentifier } from './utils';

export type ICommonCartridgeOrganizationProps = {
identifier: string;
title: string;
version: string;
} & ({ children: ICommonCartridgeOrganizationProps[] } | { resources: ICommonCartridgeResourceProps[] });

type OrganizationItemCollection = {
export type OrganizationItemCollection = {
title: string;
children: OrganizationItemCollection[] | OrganizationResourceCollection;
_tag: 'itemCollection';
};

type OrganizationResourceCollection = {
export type OrganizationResourceCollection = {
identifier: string;
title: string;
version: string;
Expand Down Expand Up @@ -72,7 +66,7 @@ export class CommonCartridgeOrganizationItemElement implements CommonCartridgeEl
identifier: createIdentifier(),
},
title: this.props.title,
item: this.props.children.map((child) => new CommonCartridgeOrganizationItemElement(child).transform()), // TODO rekursiv weiter?
item: this.props.children.map((child) => new CommonCartridgeOrganizationItemElement(child).transform()),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CommonCartridgeIntendedUseType,
CommonCartridgeResourceType,
CommonCartridgeVersion,
ICommonCartridgeOrganizationBuilder,
ICommonCartridgeResourceProps,
ICommonCartridgeWebContentResourceProps,
} from '../common-cartridge';
Expand Down Expand Up @@ -51,10 +52,11 @@ export class CommonCartridgeExportService {
identifier: createIdentifier(lesson.id),
title: lesson.name,
resources: [],
_tag: 'resourceCollection',
});

lesson.contents.forEach((content) => {
const resourceProps = this.mapContentToResource(lesson.id, content, version);
const resourceProps = this.mapContentToResource(lesson.id, content, version, organizationBuilder);
if (resourceProps) {
organizationBuilder.addResourceToOrganization(resourceProps);
}
Expand All @@ -75,6 +77,7 @@ export class CommonCartridgeExportService {
// FIXME: change the title for tasks organization
title: '',
resources: [],
_tag: 'resourceCollection',
});

tasks.forEach((task) => {
Expand All @@ -85,7 +88,8 @@ export class CommonCartridgeExportService {
private mapContentToResource(
lessonId: string,
content: ComponentProperties,
version: CommonCartridgeVersion
version: CommonCartridgeVersion,
orgBuilder: ICommonCartridgeOrganizationBuilder
): ICommonCartridgeResourceProps | undefined {
const commonProps = (fileExt: 'html' | 'xml') => {
return {
Expand Down Expand Up @@ -131,9 +135,21 @@ export class CommonCartridgeExportService {
if (content.component === ComponentType.LERNSTORE && content.content) {
const { resources } = content.content;

return version === CommonCartridgeVersion.V_1_3_0
? { type: CommonCartridgeResourceType.WEB_LINK_V3, url: resources[0].url, ...commonProps('xml') }
: { type: CommonCartridgeResourceType.WEB_LINK_V1, url: resources[0].url, ...commonProps('xml') };
const resourceProps: ICommonCartridgeResourceProps[] = resources.map((resource) =>
version === CommonCartridgeVersion.V_1_3_0
? { type: CommonCartridgeResourceType.WEB_LINK_V3, url: resource.url, ...commonProps('xml') }
: { type: CommonCartridgeResourceType.WEB_LINK_V1, url: resource.url, ...commonProps('xml') }
);

orgBuilder.addSubOrganization({
version,
identifier: createIdentifier(content._id),
title: content.title,
resources: resourceProps,
_tag: 'resourceCollection',
});

return undefined;
}

return undefined;
Expand Down

0 comments on commit 9c64a59

Please sign in to comment.