Skip to content
Snippets Groups Projects
Commit 3656a310 authored by paf's avatar paf
Browse files

get rid of warning during build an linting

parent 5bebcd24
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@ import {Injectable} from '@angular/core';
import {MainDataService} from '../maindata.service';
import {BackendService} from './backend.service';
import {Observable, of} from 'rxjs';
import {isDefined} from '@angular/compiler/src/util';
import {map, shareReplay} from 'rxjs/operators';
import {Booklet, BookletError, BookletMetadata, Restrictions, Testlet, Unit} from './group-monitor.interfaces';
import {BookletConfig} from '../config/booklet-config';
......@@ -11,49 +10,14 @@ import {BookletConfig} from '../config/booklet-config';
@Injectable()
export class BookletService {
public booklets: Observable<Booklet|BookletError>[] = [];
constructor(
private bs: BackendService
) { }
public getBooklet(bookletName: string): Observable<Booklet|BookletError> {
if (isDefined(this.booklets[bookletName])) {
// console.log('FORWARDING booklet for ' + bookletName + '');
return this.booklets[bookletName];
}
if (bookletName == "") {
// console.log("EMPTY bookletID");
this.booklets[bookletName] = of<Booklet|BookletError>({error: 'missing-id'});
} else {
// console.log('LOADING testletOrUnit data for ' + bookletName + ' not available. loading');
this.booklets[bookletName] = this.bs.getBooklet(bookletName)
.pipe(
map((response: string|BookletError) => {
return (typeof response === 'string') ? BookletService.parseBookletXml(response) : response;
}),
shareReplay(1)
);
}
return this.booklets[bookletName];
}
private static parseBookletXml(xmlString: string): Booklet|BookletError {
try {
const domParser = new DOMParser();
const bookletElement = domParser.parseFromString(xmlString, 'text/xml').documentElement;
......@@ -67,17 +31,13 @@ export class BookletService {
metadata: BookletService.parseMetadata(bookletElement),
config: BookletService.parseBookletConfig(bookletElement)
};
} catch (error) {
console.warn('Error reading booklet XML:', error);
return {error: 'xml'};
}
}
private static parseBookletConfig(bookletElement: Element): BookletConfig {
const bookletConfigElements = BookletService.xmlGetChildIfExists(bookletElement, 'BookletConfig', true);
const bookletConfig = new BookletConfig();
bookletConfig.setFromKeyValuePairs(MainDataService.getTestConfig());
......@@ -87,23 +47,17 @@ export class BookletService {
return bookletConfig;
}
private static parseMetadata(bookletElement: Element): BookletMetadata {
const metadataElement = BookletService.xmlGetChildIfExists(bookletElement, 'Metadata');
return {
id: BookletService.xmlGetChildTextIfExists(metadataElement, "Id"),
label: BookletService.xmlGetChildTextIfExists(metadataElement, "Label"),
description: BookletService.xmlGetChildTextIfExists(metadataElement, "Description", true),
}
id: BookletService.xmlGetChildTextIfExists(metadataElement, 'Id'),
label: BookletService.xmlGetChildTextIfExists(metadataElement, 'Label'),
description: BookletService.xmlGetChildTextIfExists(metadataElement, 'Description', true),
};
}
private static parseTestlet(testletElement: Element): Testlet {
// TODO id will be mandatory (https://github.com/iqb-berlin/testcenter-iqb-php/issues/116), the remove fallback to ''
return {
id: testletElement.getAttribute('id') || '',
label: testletElement.getAttribute('label') || '',
......@@ -114,55 +68,40 @@ export class BookletService {
};
}
private static parseUnitOrTestlet(unitOrTestletElement: Element): (Unit|Testlet) {
if (unitOrTestletElement.tagName == 'Unit') {
if (unitOrTestletElement.tagName === 'Unit') {
return {
id: unitOrTestletElement.getAttribute('alias') || unitOrTestletElement.getAttribute('id'),
label: unitOrTestletElement.getAttribute('label'),
labelShort: unitOrTestletElement.getAttribute('labelshort')
}
};
}
return BookletService.parseTestlet(unitOrTestletElement);
}
private static parseRestrictions(testletElement: Element): Restrictions {
const restrictions: Restrictions = {};
const restrictionsElement = BookletService.xmlGetChildIfExists(testletElement, 'Restrictions', true);
if (!restrictionsElement) {
return restrictions;
}
const codeToEnterElement = restrictionsElement.querySelector('CodeToEnter');
if (codeToEnterElement) {
restrictions.codeToEnter = {
code: codeToEnterElement.getAttribute('code'),
message: codeToEnterElement.textContent
}
};
}
const timeMaxElement = restrictionsElement.querySelector('TimeMax');
if (timeMaxElement) {
restrictions.timeMax = {
minutes: parseFloat(timeMaxElement.getAttribute('minutes')),
};
}
return restrictions;
}
private static xmlGetChildIfExists(element: Element, childName: string, isOptional: boolean = false): Element {
const elements = BookletService.xmlGetDirectChildrenByTagName(element, [childName]);
if (!elements.length && !isOptional) {
throw new Error(`Missing field: '${childName}'`);
......@@ -170,26 +109,42 @@ export class BookletService {
return elements.length ? elements[0] : null;
}
private static xmlGetChildTextIfExists(element: Element, childName: string, isOptional: boolean = false) : string {
private static xmlGetChildTextIfExists(element: Element, childName: string, isOptional: boolean = false): string {
const childElement = BookletService.xmlGetChildIfExists(element, childName, isOptional);
return childElement ? childElement.textContent : "";
return childElement ? childElement.textContent : '';
}
private static xmlGetDirectChildrenByTagName(element: Element, tagNames: string[]): Element[] {
return [].slice.call(element.childNodes)
.filter((element: Element) => (element.nodeType === 1))
.filter((element: Element) => (tagNames.indexOf(element.tagName) > -1));
.filter((elem: Element) => (elem.nodeType === 1))
.filter((elem: Element) => (tagNames.indexOf(elem.tagName) > -1));
}
private static xmlCountChildrenOfTagNames(element: Element, tagNames: string[]): number {
return element.querySelectorAll(tagNames.join(', ')).length;
}
public getBooklet(bookletName: string): Observable<Booklet|BookletError> {
if (typeof this.booklets[bookletName] !== 'undefined') {
// console.log('FORWARDING booklet for ' + bookletName + '');
return this.booklets[bookletName];
}
if (bookletName === '') {
// console.log("EMPTY bookletID");
this.booklets[bookletName] = of<Booklet|BookletError>({error: 'missing-id'});
} else {
// console.log('LOADING testletOrUnit data for ' + bookletName + ' not available. loading');
this.booklets[bookletName] = this.bs.getBooklet(bookletName)
.pipe(
map((response: string|BookletError) => {
return (typeof response === 'string') ? BookletService.parseBookletXml(response) : response;
}),
shareReplay(1)
);
}
return this.booklets[bookletName];
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment