src/services/canvas-player.service.ts
Methods |
constructor(_http: HttpClient, events: Events, preferences: SharedPreferences, localCourseService: LocalCourseService, commonUtilService: CommonUtilService, file: File)
|
|||||||||||||||||||||
Defined in src/services/canvas-player.service.ts:18
|
|||||||||||||||||||||
Parameters :
|
handleAction |
handleAction()
|
Defined in src/services/canvas-player.service.ts:31
|
This is the globally available method used by player to communicate with mobile
Returns :
void
|
readJSON | ||||||||
readJSON(path: string)
|
||||||||
Defined in src/services/canvas-player.service.ts:137
|
||||||||
This will read JSON file
Parameters :
Returns :
Promise<any>
Returns JSON object |
xmlToJSon | ||||||||||||
xmlToJSon(path: string, file)
|
||||||||||||
Defined in src/services/canvas-player.service.ts:114
|
||||||||||||
This will convert xml to JSON
Parameters :
Returns :
Promise<any>
|
import { Inject, Injectable } from '@angular/core';
import { ContentStateResponse, GetContentStateRequest, SunbirdSdk, SharedPreferences } from 'sunbird-sdk';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as X2JS from 'x2js';
import {MaxAttempt, PreferenceKey, ProfileConstants} from '@app/app/app.constant';
import { Events } from '@app/util/events';
import { LocalCourseService } from './local-course.service';
import { CommonUtilService } from './common-util.service';
import { File } from '@ionic-native/file/ngx';
declare global {
interface Window {
handleAction: (methodName, params) => void;
}
}
@Injectable()
export class CanvasPlayerService {
constructor(
private _http: HttpClient,
private events: Events,
@Inject('SHARED_PREFERENCES') private preferences: SharedPreferences,
private localCourseService: LocalCourseService,
private commonUtilService: CommonUtilService,
private file: File,
) { }
/**
* This is the globally available method used by player to communicate with mobile
*/
handleAction() {
window.handleAction = (methodName: string, params = []) => {
switch (methodName) {
case 'getCurrentUser':
return SunbirdSdk.instance.profileService.getActiveSessionProfile({
requiredFields: ProfileConstants.REQUIRED_FIELDS
}).toPromise();
case 'getAllUserProfile':
return SunbirdSdk.instance.profileService.getAllProfiles(params[0]).toPromise();
case 'setUser':
return SunbirdSdk.instance.profileService.setActiveSessionForProfile(params[0]).toPromise();
case 'getContent':
return SunbirdSdk.instance.contentService.getContents(params[0]).toPromise();
case 'getRelevantContent':
const request = JSON.parse(params[0]);
request['shouldConvertBasePath'] = true;
return SunbirdSdk.instance.contentService.getRelevantContent(request).toPromise();
case 'getRelatedContent':
console.log('getRelatedContent to be defined');
break;
case 'getContentList':
return SunbirdSdk.instance.contentService.getContents(params[0]).toPromise();
case 'sendFeedback':
return SunbirdSdk.instance.contentFeedbackService.sendFeedback(params[0]).toPromise();
case 'languageSearch':
console.log('languageSearch to be defined');
break;
case 'endGenieCanvas':
this.events.publish('endGenieCanvas', { showConfirmBox: false });
break;
case 'showExitConfirmPopup':
this.events.publish('endGenieCanvas', { showConfirmBox: true });
break;
case 'endContent':
console.log('endContent to be defined');
break;
case 'launchContent':
console.log('launchContent to be defined');
break;
case 'send':
return SunbirdSdk.instance.telemetryService.saveTelemetry(params[0]).subscribe();
case 'checkMaxLimit':
const content = params[0];
return this.preferences.getString(PreferenceKey.CONTENT_CONTEXT).toPromise()
.then(async (context: string) => {
if (!context) {
return {
isLastAttempt: false,
limitExceeded: false,
isCloseButtonClicked: false
};
}
const courseContext = JSON.parse(context);
let maxAttempt: MaxAttempt;
if (courseContext.courseId && courseContext.batchId && courseContext.leafNodeIds) {
const getContentStateRequest: GetContentStateRequest = {
userId: courseContext.userId,
courseId: courseContext.courseId,
contentIds: courseContext.leafNodeIds,
returnRefreshedContentStates: true,
batchId: courseContext.batchId,
fields: ['progress', 'score']
};
const contentStateResponse: ContentStateResponse = await SunbirdSdk.instance.courseService
.getContentState(getContentStateRequest).toPromise();
const assessmentStatus = this.localCourseService.fetchAssessmentStatus(contentStateResponse, content);
maxAttempt = await this.commonUtilService.handleAssessmentStatus(assessmentStatus);
}
return maxAttempt;
});
default:
console.log('Please use valid method');
}
};
}
/**
* This will convert xml to JSON
* @param {string} path Path to the xml file
*/
xmlToJSon(path: string, file): Promise<any> {
if (path.length) {
const _headers = new HttpHeaders();
const headers = _headers.set('Content-Type', 'text/xml');
return new Promise((resolve, reject) => {
try {
this.file.readAsText(path, file).then((response) => {
const x2js = new X2JS();
const json = x2js.xml2js(response);
resolve(json);
});
} catch (error) {
reject('Unable to convert');
}
});
}
}
/**
* This will read JSON file
* @param {string} path Path to the JSON file
* @returns {Promise} Returns JSON object
*/
readJSON(path: string): Promise<any> {
if (path.length) {
const _headers = new HttpHeaders();
const headers = _headers.set('Content-Type', 'text/javascript');
return new Promise((resolve, reject) => {
try {
this._http.get(path, { headers: _headers, responseType: 'json' }).subscribe((data) => {
resolve(data);
});
} catch (error) {
reject('Unable to read JSON');
}
});
}
}
}