Skip to content

Commit

Permalink
Merge pull request #87 from gouravmore/mandatory-pdf
Browse files Browse the repository at this point in the history
Issue #KN-63 feat: Mandatory & optional configuration
  • Loading branch information
HarishGangula authored Oct 21, 2022
2 parents 1f82c98 + e69d8e2 commit 714c037
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 157 deletions.
99 changes: 72 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Follow below-mentioned steps to use it in plain javascript project:
- Copy the assets files from web component folder
[assets](https://github.com/project-sunbird/sunbird-pdf-player/tree/release-5.2.0/web-component/assets) to assets folder
- Create sunbird folder inside assets folder, and copy [sunbird-pdf-player.js](https://github.com/project-sunbird/sunbird-pdf-player/blob/release-5.2.0/web-component/sunbird-pdf-player.js) and [styles.css](https://github.com/project-sunbird/sunbird-pdf-player/blob/release-5.2.0/web-component/styles.css). and Add/import these enteries in angular json file inside scripts and styles respectively.
- Create sunbird folder inside assets folder, and copy [sunbird-pdf-player.js](https://github.com/project-sunbird/sunbird-pdf-player/blob/release-5.2.0/web-component/sunbird-pdf-player.js) and [styles.css](https://github.com/project-sunbird/sunbird-pdf-player/blob/release-5.2.0/web-component/styles.css). and Add/import these entries in angular json file inside scripts and styles respectively.
- Add the reflect-metadata script to index.html file
```javascript
Expand All @@ -87,7 +87,7 @@ Follow below-mentioned steps to use it in plain javascript project:
(telemetryEvent)="playerTelemetryEvents($event)"></sunbird-pdf-player>
```
- Send input to render PDF player
- Provide input to render PDF player
Use the mock config in your component to send input to PDF player
Click to see the mock - [playerConfig](https://github.com/project-sunbird/sunbird-pdf-player/blob/release-5.2.0/src/app/data.ts)
Expand Down Expand Up @@ -221,34 +221,79 @@ var playerConfig = {
}

```

## Metadata Mandatory property description
Metadata gives complete information about the content.

Sample metadata object interface:

```js
"metadata": {
identifier: string;
name: string;
artifactUrl: string;
streamingUrl?: string;
compatibilityLevel?: number;
pkgVersion?: number;
isAvailableLocally?: boolean;
basePath?: string;
baseDir?: string;
}
```

In metadata, the following properties are mandatory to play the content.

|Property Name| Description| Mandatory/Optional| Without field | Comment
|--|----------------------|--| --| --|
| `identifier` | It is `string` of unique content id | Mandatory | Unable to load the content error | Its a unique content id so Its a required to log the telemetry and other data against content|
| `name` | It is `string` to represent the name of the content or pdf | Mandatory | Unable to load the content error | Its a required to show the name of the pdf while loading the pdf|
| `artifactUrl` | It is `string` url to load the pdf from artifact url | Mandatory | Unable to load the content error | It is required to load the pdf file|
| `streamingUrl` | It is `string` url to load the pdf from streaming url | Optional | Unable to load the content error | This is required if you want to load the streaming pdf URL|
| `isAvailableLocally` | It is a `boolean` value which indicate the content is locally available | Optional | Content will not load offline | It is required to know - the content is downloaded and can be played offline|
| `basePath` | It is `string` to represent the base path of the pdf file | Optional | Content will not load offline | It is required to load the pdf file in offline use case|
| `baseDir` | It is `string` to represent the base path of the pdf file | Optional | Content will not load offline | It is required to load the pdf file in offline use case |
| `compatibilityLevel` | It is `number` to represent the compatibility level | Optional | Default compatibilityLevel 4 will be set | It's an optional field
| `pkgVersion` | It is `number` to represent the version of the current packages | Optional | Default compatibilityLevel `1.0` will be set | it's an optional field

Sample config for mandatory fields
```js
var playerConfig = {
"metadata": {
identifier: 'do_31291455031832576019477',
name: 'NAME_OF_THE_CONTENT',
artifactUrl: 'https://ntpproductionall.blob.core.windows.net/ntp-content-production/content/assets/do_31291458881611366418883/b331332333_std_5_mathssciencesocial_tm_term-1_opt.pdf'
}
}
```

## Telemetry property description
|Property Name| Description| Default Value
|--|----------------------|--|
| `context` | It is an `object` it contains the `uid`,`did`,`sid`,`mode` etc., these will be logged inside the telemetry | ```{}``` |
| `mode` | It is `string` to identify preview used by the user to play/edit/preview | ```play```|
| `authToken` | It is `string` and Auth key to make api calls | ```''```|
| `sid` | It is `string` and User sessionid on portal or mobile | ```''```|
| `did` | It is `string` and Unique id to identify the device or browser| ```''```|
| `uid` | It is `string` and Current logged in user id| ```''```|
| `channel` | It is `string` which defines channel identifier to know which channel is currently using.| `in.sunbird` |
| `pdata` | It is an `object` which defines the producer information it should have identifier and version and canvas will log in the telemetry| ```{'id':'in.sunbird', 'ver':'1.0'}```|
| `contextRollup` | It is an `object` which defines content roll up data | ```{}```|
| `tags` | It is an `array` which defines the tag data | ```[]```|
| `objectRollup` | It is an `object` which defines object rollup data | ```{}```|
| `host` | It is `string` which defines the from which domain content should be load|```window.location.origin``` |
| `userData` | It is an `object` which defines user data | ```{}```|
| `cdata` | It is an `array` which defines the correlation data | ```[]```|
|Property Name| Description| Default Value | Mandatory/Optional|
|--|----------------------|--|--|
| `channel` | It is `string` which defines a channel identifier to know which channel is currently being used.| `in.sunbird` |Mandatory|
| `env` | It is an string containing Unique environment where the event has occurred | ```"contentplayer"```|Optional|
| `pdata` | It is an `object` which defines the producer information. it should have an identifier and version and canvas will log in the telemetry| ```{'id':'in.sunbird', 'ver':'1.0'}```|Mandatory|
| `mode` | It is a `string` to identify preview used by the user to play/edit/preview | ```play```|Optional|
| `sid` | It is a `string` containing user session id. | ```sid = uid ```|Optional|
| `did` | It is a `string` containing unique device id.| ```fingerPrintjs2```|Optional|
| `uid` |It is a `string` containing the user id.| ```actor.id = did ? did : "anonymous" ```|Optional|
| `authToken` | It is a `string` to send telemetry to given endpoint (API uses for authentication) | ```''```|Optional|
| `contextRollup` | It is an `object` which defines content roll up data | ```{}```|Optional|
| `objectRollup` | It is an `object` which defines object rollup data | ```{}```|Optional|
| `tags` | It is an `array`. It can be used to tag devices so that summaries or metrics can be derived via specific tags. Helpful during analysis | ```[]```|Optional|
| `cdata` | It is an `array` Correlation data. Can be used to correlate multiple events. Generally used to track user flow | ```[]```|Optional|
| `host` | It is a `string` which defines the from which domain content should be load|```window.location.origin``` |Optional|
| `userData` | It is an `object` which defines user data | ```Anonymous```|Optional|


## Config property description
|Property Name| Description| Default Value
|--|----------------------|--|
| `config` | It is an `object` it contains the `sideMenu`, these will be used to configure the canvas | ```{ sideMenu: {"showShare": true, "showDownload": true, "showReplay": true, "showExit": false,"showPrint": true}}``` |
| `config.sideMenu.showShare` | It is `boolean` to show/hide share button in side menu| ```true```|
| `config.sideMenu.showDownload` | It is `boolean` to show/hide download button in side menu| ```true```|
| `config.sideMenu.showReplay` | It is `boolean` to show/hide replay button in side menu| ```true```|
| `config.sideMenu.showExit` | It is `boolean` to show/hide exit button in side menu| ```false```|
| `config.sideMenu.showPrint` | It is `boolean` to show/hide print button in side menu| ```true```|
| `metadata` | It is an `object` which defines content metadata json object (from API response take -> response.result.content) | ```{}```|
|Property Name| Description| Default Value | Mandatory/Optional
|--|----------------------|--| --|
| `config` | It is an `object` it contains the `sideMenu`. These will be used to configure the canvas | ```{ sideMenu: {"showShare": true, "showDownload": true, "showReplay": true, "showExit": false,"showPrint": true}}``` | Optional |
| `config.sideMenu.showShare` | It is `boolean` to show/hide share button in side menu| ```true```| Optional |
| `config.sideMenu.showDownload` | It is `boolean` to show/hide download button in side menu| ```true```| Optional |
| `config.sideMenu.showReplay` | It is `boolean` to show/hide replay button in side menu| ```true```| Optional |
| `config.sideMenu.showExit` | It is `boolean` to show/hide exit button in side menu| ```false```| Optional |
| `config.sideMenu.showPrint` | It is `boolean` to show/hide print button in side menu| ```true```| Optional |

## Available components
|Feature| Notes| Selector|Code|Input|Output
Expand Down
8 changes: 4 additions & 4 deletions projects/sunbird-pdf-player/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/sunbird-pdf-player/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@project-sunbird/sunbird-pdf-player-v9",
"version": "5.2.0",
"version": "5.2.1",
"peerDependencies": {
"@angular/common": ">= 14.2.5",
"@angular/core": ">= 14.2.5",
Expand Down
35 changes: 23 additions & 12 deletions projects/sunbird-pdf-player/src/lib/playerInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

export interface Pdata {
id: string;
pid: string;
ver: string;
pid?: string;
ver?: string;
}

export interface ContextRollup {
Expand All @@ -25,15 +25,15 @@ export interface ObjectRollup {
}

export interface Context {
mode: string;
mode?: string;
authToken?: string;
sid: string;
did: string;
uid: string;
sid?: string;
did?: string;
uid?: string;
channel: string;
pdata: Pdata;
contextRollup: ContextRollup;
tags: string[];
contextRollup?: ContextRollup;
tags?: string[];
cdata?: Cdata[];
timeDiff?: number;
objectRollup?: ObjectRollup;
Expand All @@ -60,13 +60,24 @@ sideMenu?: {
showDownload?: boolean;
showReplay?: boolean;
showExit?: boolean;
showPrint?: boolean;
};
[propName: string]: any;
}

export interface Metadata {
identifier: string;
name: string;
artifactUrl: string;
streamingUrl?: string;
compatibilityLevel?: number;
pkgVersion?: number;
isAvailableLocally?: boolean;
basePath?: string;
baseDir?: string;
}
export interface PlayerConfig {
context: Context;
config: Config;
metadata: any; // content
data?: any; // body
context?: Context;
config?: Config;
metadata: Metadata;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export const mockData = {
playerConfig: {
// tslint:disable-next-line:max-line-length
metadata: {copyright: 'Tamilnadu', keywords: ['B515EPDF', 'B516EPDF', 'B517EPDF', 'EPDF', '3EPDFT3'], subject: ['Mathematics', 'Science', 'Social Science'], channel: '01235953109336064029450', language: ['English'], mimeType: 'application/pdf', gradeLevel: ['Class 3'], appIcon: 'https://ntpproductionall.blob.core.windows.net/ntp-content-production/content/do_31290118719492096012070/artifact/3_1535000262441.thumb.png', artifactUrl: 'https://ntpstagingall.blob.core.windows.net/ntp-content-staging/content/assets/do_2129420726199091201242/owncloud-manual.pdf', contentType: 'Resource', identifier: 'do_31290118719492096012070', audience: ['Learner'], visibility: 'Default', mediaType: 'content', osId: 'org.ekstep.quiz.app', languageCode: ['en'], license: 'CC BY 4.0', name: 'B515,B516,B517_STD_3_MATHS,SCIENCE,SOCIAL_TM_TERM 3_IOPT', status: 'Live', code: '51c5b565-4d3c-45d2-99cd-48a1a330a7c1', description: '3T3PDF', streamingUrl: 'https://ntpproductionall.blob.core.windows.net/ntp-content-production/content/assets/do_31290118719492096012070/b515b516b517_std_3_mathssciencesocial_tm_term-3_iopt.pdf', medium: ['Tamil'], createdOn: '2019-11-27T10:52:32.504+0000', copyrightYear: 2019, lastUpdatedOn: '2019-12-16T11:50:37.503+0000', creator: 'SCERT 2 ECONTENTS', pkgVersion: 2, versionKey: '1576497038092', framework: 'tn_k-12_5', createdBy: 'f4f80b17-8609-44b9-b781-b79df5cf7e8d', board: 'State (Tamil Nadu)', resourceType: 'Read', orgDetails: {email: '[email protected]', orgName: 'Tamilnadu'}, licenseDetails: {name: 'CC BY 4.0', url: 'https://creativecommons.org/licenses/by/4.0/legalcode', description: 'For details see below:'}, body: {}}
},
optionalPlayerConfig: {
context: {
mode: 'play',
authToken: '',
Expand All @@ -26,6 +30,6 @@ export const mockData = {
},
// eslint-disable-next-line max-len
metadata: {copyright: 'Tamilnadu', keywords: ['B515EPDF', 'B516EPDF', 'B517EPDF', 'EPDF', '3EPDFT3'], subject: ['Mathematics', 'Science', 'Social Science'], channel: '01235953109336064029450', language: ['English'], mimeType: 'application/pdf', gradeLevel: ['Class 3'], appIcon: 'https://ntpproductionall.blob.core.windows.net/ntp-content-production/content/do_31290118719492096012070/artifact/3_1535000262441.thumb.png', artifactUrl: 'https://ntpstagingall.blob.core.windows.net/ntp-content-staging/content/assets/do_2129420726199091201242/owncloud-manual.pdf', contentType: 'Resource', identifier: 'do_31290118719492096012070', audience: ['Learner'], visibility: 'Default', mediaType: 'content', osId: 'org.ekstep.quiz.app', languageCode: ['en'], license: 'CC BY 4.0', name: 'B515,B516,B517_STD_3_MATHS,SCIENCE,SOCIAL_TM_TERM 3_IOPT', status: 'Live', code: '51c5b565-4d3c-45d2-99cd-48a1a330a7c1', description: '3T3PDF', streamingUrl: 'https://ntpproductionall.blob.core.windows.net/ntp-content-production/content/assets/do_31290118719492096012070/b515b516b517_std_3_mathssciencesocial_tm_term-3_iopt.pdf', medium: ['Tamil'], createdOn: '2019-11-27T10:52:32.504+0000', copyrightYear: 2019, lastUpdatedOn: '2019-12-16T11:50:37.503+0000', creator: 'SCERT 2 ECONTENTS', pkgVersion: 2, versionKey: '1576497038092', framework: 'tn_k-12_5', createdBy: 'f4f80b17-8609-44b9-b781-b79df5cf7e8d', board: 'State (Tamil Nadu)', resourceType: 'Read', orgDetails: {email: '[email protected]', orgName: 'Tamilnadu'}, licenseDetails: {name: 'CC BY 4.0', url: 'https://creativecommons.org/licenses/by/4.0/legalcode', description: 'For details see below:'}, body: {}},
data: {}
}
};

Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,15 @@ describe('ViewerService', () => {
expect(sunbirdPdfPlayerService.error).toHaveBeenCalled();
});

it('if context or context.userdata is not available default Anonymous should log as a username', () => {
const service = TestBed.inject(ViewerService);
service.initialize(mockData.playerConfig);
expect(service.userName).toEqual("Anonymous");
});

it('if context or context.userdata is available user firstname and last name should log as a username', () => {
const service = TestBed.inject(ViewerService);
service.initialize(mockData.optionalPlayerConfig);
expect(service.userName).toEqual("Harish Kumar Gangula");
});
});
12 changes: 7 additions & 5 deletions projects/sunbird-pdf-player/src/lib/services/viewer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export class ViewerService {
private utilService: UtilService) { }

initialize({ context, config, metadata }: PlayerConfig) {
this.zoom = _.last(config.zoom) || 'auto';
this.rotation = _.last(config.rotation) || 0;
this.zoom = _.last(config?.zoom) || 'auto';
this.rotation = _.last(config?.rotation) || 0;
this.pdfPlayerStartTime = this.pdfLastPageTime = new Date().getTime();
this.totalNumberOfPages = 0;
this.currentPagePointer = _.last(config.pagesVisited) || 1;
this.currentPagePointer = _.last(config?.pagesVisited) || 1;
this.contentName = metadata.name;
this.isAvailableLocally = metadata.isAvailableLocally;
if (this.isAvailableLocally) {
Expand All @@ -46,9 +46,11 @@ export class ViewerService {
} else {
this.src = metadata.streamingUrl || metadata.artifactUrl;
}
if (context.userData) {
if (context?.userData) {
const { userData: { firstName, lastName } } = context;
this.userName = firstName === lastName ? firstName : `${firstName} ${lastName}`;
} else {
this.userName = 'Anonymous';
}
this.metaData = {
pagesVisited: [],
Expand All @@ -62,7 +64,7 @@ export class ViewerService {
this.endPageSeen = false;

this.defaultConfig = {
startFromPage: _.last(config.pagesVisited) || 1,
startFromPage: _.last(config?.pagesVisited) || 1,
zoom: this.zoom,
rotation: this.rotation
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,47 @@ describe('SunbirdPdfPlayerComponent', () => {
component.resetValidPage();
expect(component.validPage).toBeTruthy();
});

it('player config should have metadata object', () => {
const sunbirdPdfPlayerService = TestBed.inject(SunbirdPdfPlayerService);
const viewerService = TestBed.inject(ViewerService);
spyOn(sunbirdPdfPlayerService, 'initialize');
spyOn(viewerService, 'initialize');
component.ngOnInit();
expect(Object.keys(component.playerConfig)).toContain('metadata');
expect(sunbirdPdfPlayerService.initialize).toHaveBeenCalled();
expect(viewerService.initialize).toHaveBeenCalled();
});

it('config and context should optional property for player and metadata should be mandatory field', () => {
component.playerConfig = {
metadata: {identifier: 'do_testId', name: 'test_name', artifactUrl: 'testArtifact url'}
};
const sunbirdPdfPlayerService = TestBed.inject(SunbirdPdfPlayerService);
const viewerService = TestBed.inject(ViewerService);
spyOn(sunbirdPdfPlayerService, 'initialize');
spyOn(viewerService, 'initialize');
component.ngOnInit();
expect(Object.keys(component.playerConfig)).not.toContain('context');
expect(Object.keys(component.playerConfig)).not.toContain('config');
expect(Object.keys(component.playerConfig)).toContain('metadata');
expect(sunbirdPdfPlayerService.initialize).toHaveBeenCalled();
expect(viewerService.initialize).toHaveBeenCalled();
});

it('player config metadata should have the identifer, name, artifactUrl', () => {
component.playerConfig = {
metadata: {identifier: 'do_testId', name: 'test_name', artifactUrl: 'testArtifact url'}
};
const sunbirdPdfPlayerService = TestBed.inject(SunbirdPdfPlayerService);
const viewerService = TestBed.inject(ViewerService);
spyOn(sunbirdPdfPlayerService, 'initialize');
spyOn(viewerService, 'initialize');
component.ngOnInit();
expect(Object.keys(component.playerConfig.metadata)).toContain('identifier');
expect(Object.keys(component.playerConfig.metadata)).toContain('name');
expect(Object.keys(component.playerConfig.metadata)).toContain('artifactUrl');
expect(sunbirdPdfPlayerService.initialize).toHaveBeenCalled();
expect(viewerService.initialize).toHaveBeenCalled();
});
});
Loading

0 comments on commit 714c037

Please sign in to comment.