Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 9x 9x 9x 9x 9x 9x 9x 9x 9x 8x 16x 16x 8x 6x 6x 6x 5x 5x 10x 10x 10x 10x 10x 10x 10x 10x 5x 5x 5x 5x 5x 5x 9x 9x 8x 8x 40x 40x 40x 40x 8x 33x 33x 33x 33x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 12x 20x 20x 20x 20x 32x 32x 12x 12x 20x 95x 19x 19x 19x 6x 6x 6x 6x 12x 6x 6x 6x 6x 6x 12x 12x 6x 6x 1x 6x 11x 10x 50x 40x 10x 10x 40x 40x 10x 5x 5x 5x 5x 5x 4x 10x 48x 320x 199x 199x 2985x 199x 199x 9x 9x 9x 9x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 56x 16x 16x 16x 16x 56x 24x 24x 24x 24x 24x 24x 56x 16x 16x 16x 16x 16x 16x 24x 16x 16x 24x 24x 24x 24x 24x 24x 80x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 16x 16x 16x 16x | /* eslint-disable no-console */ import { Injectable } from '@angular/core'; import { BehaviorSubject, from, Observable, of, Subject, Subscription } from 'rxjs'; import { concatMap, distinctUntilChanged, last, map, shareReplay, switchMap, tap } from 'rxjs/operators'; import { CustomtextService, BookletConfig, TestMode } from '../../shared/shared.module'; import { isLoadingFileLoaded, isNavigationLeaveRestrictionValue, LoadedFile, LoadingProgress, StateReportEntry, LoadingQueueEntry, TestControllerState, TestData, TestLogEntryKey, TestStateKey, UnitData, UnitNavigationTarget, UnitStateKey, MaxTimeLeaveValue, maxTimeLeaveValues } from '../interfaces/test-controller.interfaces'; import { EnvironmentData, NavigationLeaveRestrictions, Testlet } from '../classes/test-controller.classes'; import { TestControllerService } from './test-controller.service'; import { BackendService } from './backend.service'; import { AppError } from '../../app.interfaces'; @Injectable({ providedIn: 'root' }) export class TestLoaderService { private loadStartTimeStamp = 0; private unitContentLoadSubscription: Subscription | null = null; private environment: EnvironmentData; // TODO (possible refactoring) outsource to a service or what private lastUnitSequenceId = 0; private unitContentLoadingQueue: LoadingQueueEntry[] = []; private totalLoadingProgressParts: { [loadingId: string]: number } = {}; constructor( public tcs: TestControllerService, private bs: BackendService, private cts: CustomtextService ) { this.environment = new EnvironmentData(); } async loadTest(): Promise<void> { this.reset(); const ts = Date.now(); this.tcs.testStatus$.next(TestControllerState.LOADING); const testData = await this.bs.getTestData(this.tcs.testId).toPromise(); Iif (!testData) { return; // error is already thrown } this.tcs.workspaceId = testData.workspaceId; this.tcs.testMode = new TestMode(testData.mode); this.restoreRestrictions(testData.laststate); this.tcs.rootTestlet = this.getBookletFromXml(testData.xml); this.tcs.timerWarningPoints = this.tcs.bookletConfig.unit_time_left_warnings .split(',') .map(x => parseInt(x, 10)) .filter(x => !Number.isNaN(x)); await this.loadUnits(testData); this.prepareUnitContentLoadingQueueOrder(testData.laststate.CURRENT_UNIT_ID || '1'); this.tcs.rootTestlet.lockUnitsIfTimeLeftNull(); // eslint-disable-next-line consistent-return return this.loadUnitContents(testData) .then(() => { console.log({ loaded: (Date.now() - ts) }); this.resumeTest(testData.laststate); }); } reset(): void { this.unsubscribeTestSubscriptions(); // Reset TestMode to be Demo, before the correct one comes with getTestData // TODO maybe it would be better to retrieve the testmode from the login this.tcs.testMode = new TestMode(); this.tcs.resetDataStore(); this.tcs.totalLoadingProgress = 0; this.totalLoadingProgressParts = {}; this.environment = new EnvironmentData(); this.loadStartTimeStamp = Date.now(); this.unitContentLoadingQueue = []; } private resumeTest(lastState: { [k in TestStateKey]?: string }): void { Iif (!this.tcs.rootTestlet) { throw new AppError({ description: '', label: 'Booklet not loaded yet.', type: 'script' }); } const currentUnitId = lastState[TestStateKey.CURRENT_UNIT_ID]; this.tcs.resumeTargetUnitSequenceId = currentUnitId ? this.tcs.rootTestlet.getSequenceIdByUnitAlias(currentUnitId) : 1; Iif ( (lastState[TestStateKey.CONTROLLER] === TestControllerState.TERMINATED_PAUSED) || (lastState[TestStateKey.CONTROLLER] === TestControllerState.PAUSED) ) { this.tcs.testStatus$.next(TestControllerState.PAUSED); this.tcs.setUnitNavigationRequest(UnitNavigationTarget.PAUSE); return; } this.tcs.testStatus$.next(TestControllerState.RUNNING); this.tcs.setUnitNavigationRequest(this.tcs.resumeTargetUnitSequenceId.toString()); } private restoreRestrictions(lastState: { [k in TestStateKey]?: string }): void { Iif (lastState[TestStateKey.TESTLETS_TIMELEFT]) { this.tcs.maxTimeTimers = JSON.parse(lastState[TestStateKey.TESTLETS_TIMELEFT]); } Iif (lastState[TestStateKey.TESTLETS_CLEARED_CODE]) { this.tcs.clearCodeTestlets = JSON.parse(lastState[TestStateKey.TESTLETS_CLEARED_CODE]); } } private loadUnits(testData: TestData): Promise<number | undefined> { const sequence = []; for (let i = 1; i < this.lastUnitSequenceId; i++) { this.totalLoadingProgressParts[`unit-${i}`] = 0; this.totalLoadingProgressParts[`player-${i}`] = 0; this.totalLoadingProgressParts[`content-${i}`] = 0; sequence.push(i); } return from(sequence) .pipe( concatMap((sequenceId: number) => this.loadUnit(sequenceId, testData)) ) .toPromise(); } private loadUnit(sequenceId: number, testData: TestData): Observable<number> { const unitDef = this.tcs.getUnitWithContext(sequenceId).unitDef; const resources = testData.resources[unitDef.id.toUpperCase()]; if (!resources) { throw new Error(`No resources for unitId: \`${unitDef.id}\`.`); } Iif (!(resources.usesPlayer && resources.usesPlayer.length)) { throw new Error(`Unit has no player: \`${unitDef.id}\`)`); } unitDef.playerFileName = resources.usesPlayer[0]; const definitionFile = (resources.isDefinedBy && resources.isDefinedBy.length) ? resources.isDefinedBy[0] : null; Iif (testData.firstStart && definitionFile) { // we don't need to call `[GET] /test/{testID}/unit` when this is the first test and no inline definition this.incrementTotalProgress({ progress: 100 }, `unit-${sequenceId}`); this.unitContentLoadingQueue.push({ sequenceId, definitionFile }); return this.getPlayer(testData, sequenceId, unitDef.playerFileName); } return this.bs.getUnitData(this.tcs.testId, unitDef.id, unitDef.alias) .pipe( switchMap((unit: UnitData) => { Iif (!unit) { throw new Error(`Unit is empty ${this.tcs.testId}/${unitDef.id}.`); } this.incrementTotalProgress({ progress: 100 }, `unit-${sequenceId}`); this.tcs.setUnitPresentationProgress(sequenceId, unit.state[UnitStateKey.PRESENTATION_PROGRESS]); this.tcs.setUnitResponseProgress(sequenceId, unit.state[UnitStateKey.RESPONSE_PROGRESS]); this.tcs.setUnitStateCurrentPage(sequenceId, unit.state[UnitStateKey.CURRENT_PAGE_ID]); this.tcs.setUnitStateDataParts(sequenceId, unit.dataParts); this.tcs.setUnitResponseType(sequenceId, unit.unitResponseType); if (definitionFile) { this.unitContentLoadingQueue.push({ sequenceId, definitionFile }); } else { // inline unit definition this.tcs.setUnitPlayerFilename(sequenceId, unitDef.playerFileName); this.tcs.setUnitDefinition(sequenceId, unit.definition); this.tcs.setUnitLoadProgress$(sequenceId, of({ progress: 100 })); this.incrementTotalProgress({ progress: 100 }, `content-${sequenceId}`); } return this.getPlayer(testData, sequenceId, unitDef.playerFileName); }) ); } private getPlayer(testData: TestData, sequenceId: number, playerFileName: string) { if (this.tcs.hasPlayer(playerFileName)) { this.incrementTotalProgress({ progress: 100 }, `player-${sequenceId}`); return of(sequenceId); } return this.bs.getResource(testData.workspaceId, playerFileName) .pipe( tap((progress: LoadedFile | LoadingProgress) => { this.incrementTotalProgress( isLoadingFileLoaded(progress) ? { progress: 100 } : progress, `player-${sequenceId}` ); }), last(), map((player: LoadedFile | LoadingProgress) => { Iif (!isLoadingFileLoaded(player)) { throw new Error('File Loading Error'); } this.tcs.addPlayer(playerFileName, player.content); return sequenceId; }) ); } private prepareUnitContentLoadingQueueOrder(currentUnitId: string = '1'): void { Iif (!this.tcs.rootTestlet) { throw new AppError({ description: '', label: 'Testheft noch nicht verfügbar', type: 'script' }); } const currentUnitSequenceId = this.tcs.rootTestlet.getSequenceIdByUnitAlias(currentUnitId); const queue = this.unitContentLoadingQueue; let firstToLoadQueuePosition; for (firstToLoadQueuePosition = 0; firstToLoadQueuePosition < queue.length; firstToLoadQueuePosition++) { if (Number(queue[firstToLoadQueuePosition % queue.length].sequenceId) >= currentUnitSequenceId) { break; } } const offset = ((firstToLoadQueuePosition % queue.length) + queue.length) % queue.length; this.unitContentLoadingQueue = queue.slice(offset).concat(queue.slice(0, offset)); } private loadUnitContents(testData: TestData): Promise<void> { // we don't load files in parallel since it made problems, when a whole class tried it at once const unitContentLoadingProgresses$: { [unitSequenceID: number] : Subject<LoadingProgress> } = {}; this.unitContentLoadingQueue .forEach(unitToLoad => { unitContentLoadingProgresses$[Number(unitToLoad.sequenceId)] = new BehaviorSubject<LoadingProgress>({ progress: 'PENDING' }); this.tcs.setUnitLoadProgress$( Number(unitToLoad.sequenceId), unitContentLoadingProgresses$[Number(unitToLoad.sequenceId)].asObservable() ); }); return new Promise<void>(resolve => { if (this.tcs.bookletConfig.loading_mode === 'LAZY') { resolve(); } this.unitContentLoadSubscription = from(this.unitContentLoadingQueue) .pipe( concatMap(queueEntry => { const unitContentLoading$ = this.bs.getResource(testData.workspaceId, queueEntry.definitionFile) .pipe(shareReplay()); unitContentLoading$ .pipe( map(loadingFile => { if (!isLoadingFileLoaded(loadingFile)) { return loadingFile; } this.tcs.setUnitDefinition(queueEntry.sequenceId, loadingFile.content); return { progress: 100 }; }), distinctUntilChanged((v1, v2) => v1.progress === v2.progress), tap(progress => this.incrementTotalProgress(progress, `content-${queueEntry.sequenceId}`)) ) .subscribe(unitContentLoadingProgresses$[queueEntry.sequenceId]); return unitContentLoading$; }) ) .subscribe({ complete: () => { if (this.tcs.testMode.saveResponses) { this.environment.loadTime = Date.now() - this.loadStartTimeStamp; this.bs.addTestLog(this.tcs.testId, [<StateReportEntry>{ key: TestLogEntryKey.LOADCOMPLETE, timeStamp: Date.now(), content: JSON.stringify(this.environment) }]); } this.tcs.totalLoadingProgress = 100; if (this.tcs.bookletConfig.loading_mode === 'EAGER') { resolve(); } } }); }); } private unsubscribeTestSubscriptions(): void { Iif (this.unitContentLoadSubscription !== null) { this.unitContentLoadSubscription.unsubscribe(); this.unitContentLoadSubscription = null; } } private static getChildElements(element: Element): Element[] { return Array.prototype.slice.call(element.childNodes) .filter(e => e.nodeType === 1); } private incrementTotalProgress(progress: LoadingProgress, file: string): void { Iif (typeof progress.progress !== 'number') { return; } this.totalLoadingProgressParts[file] = progress.progress; const sumOfProgresses = Object.values(this.totalLoadingProgressParts).reduce((i, a) => i + a, 0); const maxProgresses = Object.values(this.totalLoadingProgressParts).length * 100; this.tcs.totalLoadingProgress = (sumOfProgresses / maxProgresses) * 100; } private getBookletFromXml(xmlString: string): Testlet { const oParser = new DOMParser(); const xmlStringWithOutBom = xmlString.replace(/^\uFEFF/gm, ''); const oDOM = oParser.parseFromString(xmlStringWithOutBom, 'text/xml'); if (oDOM.documentElement.nodeName !== 'Booklet') { throw Error('Root element fo Booklet should be <Booklet>'); } const metadataElements = oDOM.documentElement.getElementsByTagName('Metadata'); Iif (metadataElements.length === 0) { throw Error('<Metadata>-Element missing'); } const metadataElement = metadataElements[0]; const IdElement = metadataElement.getElementsByTagName('Id')[0]; const LabelElement = metadataElement.getElementsByTagName('Label')[0]; const rootTestlet = new Testlet(0, IdElement.textContent || '', LabelElement.textContent || ''); const unitsElements = oDOM.documentElement.getElementsByTagName('Units'); if (unitsElements.length > 0) { const customTextsElements = oDOM.documentElement.getElementsByTagName('CustomTexts'); Iif (customTextsElements.length > 0) { const customTexts = TestLoaderService.getChildElements(customTextsElements[0]); const customTextsForBooklet: { [key: string] : string } = {}; for (let childIndex = 0; childIndex < customTexts.length; childIndex++) { Iif (customTexts[childIndex].nodeName === 'CustomText') { const customTextKey = customTexts[childIndex].getAttribute('key'); Iif (customTextKey) { customTextsForBooklet[customTextKey] = customTexts[childIndex].textContent || ''; } } } this.cts.addCustomTexts(customTextsForBooklet); } const bookletConfigElements = oDOM.documentElement.getElementsByTagName('BookletConfig'); this.tcs.bookletConfig = new BookletConfig(); if (bookletConfigElements.length > 0) { this.tcs.bookletConfig.setFromXml(bookletConfigElements[0]); } // recursive call through all testlets this.lastUnitSequenceId = 1; this.tcs.allUnitIds = []; this.addTestletContentFromBookletXml( rootTestlet, unitsElements[0], new NavigationLeaveRestrictions( this.tcs.bookletConfig.force_presentation_complete, this.tcs.bookletConfig.force_response_complete ) ); } return rootTestlet; } private addTestletContentFromBookletXml( targetTestlet: Testlet, node: Element, navigationLeaveRestrictions: NavigationLeaveRestrictions ) { const childElements = TestLoaderService.getChildElements(node); if (childElements.length > 0) { let codeToEnter = ''; let codePrompt = ''; let maxTime = -1; let maxTimeLeave: MaxTimeLeaveValue = 'confirm'; let forcePresentationComplete = navigationLeaveRestrictions.presentationComplete; let forceResponseComplete = navigationLeaveRestrictions.responseComplete; let restrictionElement: Element | null = null; for (let childIndex = 0; childIndex < childElements.length; childIndex++) { if (childElements[childIndex].nodeName === 'Restrictions') { restrictionElement = childElements[childIndex]; break; } } if (restrictionElement !== null) { const restrictionElements = TestLoaderService.getChildElements(restrictionElement); for (let childIndex = 0; childIndex < restrictionElements.length; childIndex++) { if (restrictionElements[childIndex].nodeName === 'CodeToEnter') { const restrictionParameter = restrictionElements[childIndex].getAttribute('code'); if ((typeof restrictionParameter !== 'undefined') && (restrictionParameter !== null)) { codeToEnter = restrictionParameter.toUpperCase(); codePrompt = restrictionElements[childIndex].textContent || ''; } } if (restrictionElements[childIndex].nodeName === 'TimeMax') { const restrictionParameter = restrictionElements[childIndex].getAttribute('minutes'); if ((typeof restrictionParameter !== 'undefined') && (restrictionParameter !== null)) { maxTime = Number(restrictionParameter); Iif (Number.isNaN(maxTime)) { maxTime = -1; } } const leaveParameter = restrictionElements[childIndex].getAttribute('leave'); Iif (leaveParameter && maxTimeLeaveValues.includes(leaveParameter)) { maxTimeLeave = leaveParameter; } } if (restrictionElements[childIndex].nodeName === 'DenyNavigationOnIncomplete') { const presentationComplete = restrictionElements[childIndex].getAttribute('presentation'); if (presentationComplete && isNavigationLeaveRestrictionValue(presentationComplete)) { forcePresentationComplete = presentationComplete; } const responseComplete = restrictionElements[childIndex].getAttribute('response'); if (responseComplete && isNavigationLeaveRestrictionValue(responseComplete)) { forceResponseComplete = responseComplete; } } } } if (codeToEnter.length > 0) { targetTestlet.codeToEnter = codeToEnter; targetTestlet.codePrompt = codePrompt; } targetTestlet.maxTimeLeft = maxTime; targetTestlet.maxTimeLeave = maxTimeLeave; if (this.tcs.maxTimeTimers) { Iif (targetTestlet.id in this.tcs.maxTimeTimers) { targetTestlet.maxTimeLeft = this.tcs.maxTimeTimers[targetTestlet.id]; } } const newNavigationLeaveRestrictions = new NavigationLeaveRestrictions(forcePresentationComplete, forceResponseComplete); for (let childIndex = 0; childIndex < childElements.length; childIndex++) { if (childElements[childIndex].nodeName === 'Unit') { const unitId = childElements[childIndex].getAttribute('id'); Iif (!unitId) { throw new AppError({ description: '', label: `Unit-Id Fehlt in unit nr ${childIndex} von ${targetTestlet.id}`, type: 'xml' }); } let unitAlias = childElements[childIndex].getAttribute('alias'); if (!unitAlias) { unitAlias = unitId; } let unitAliasClear = unitAlias; let unitIdSuffix = 1; while (this.tcs.allUnitIds.indexOf(unitAliasClear) > -1) { unitAliasClear = `${unitAlias}-${unitIdSuffix.toString()}`; unitIdSuffix += 1; } this.tcs.allUnitIds.push(unitAliasClear); targetTestlet.addUnit( this.lastUnitSequenceId, unitId, childElements[childIndex].getAttribute('label') ?? '', unitAliasClear, childElements[childIndex].getAttribute('labelshort') ?? '', newNavigationLeaveRestrictions ); this.lastUnitSequenceId += 1; } else if (childElements[childIndex].nodeName === 'Testlet') { const testletId = childElements[childIndex].getAttribute('id'); Iif (!testletId) { throw new AppError({ description: '', label: `Testlet-Id fehlt in unit nr ${childIndex} von ${targetTestlet.id}`, type: 'xml' }); } const testletLabel: string = childElements[childIndex].getAttribute('label')?.trim() ?? ''; this.addTestletContentFromBookletXml( targetTestlet.addTestlet(testletId, testletLabel), childElements[childIndex], newNavigationLeaveRestrictions ); } } } } } |