Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
testcenter-frontend
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IQB
discontinued
testcenter-frontend
Commits
3656a310
Commit
3656a310
authored
4 years ago
by
paf
Browse files
Options
Downloads
Patches
Plain Diff
get rid of warning during build an linting
parent
5bebcd24
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/app/group-monitor/booklet.service.ts
+33
-78
33 additions, 78 deletions
src/app/group-monitor/booklet.service.ts
with
33 additions
and
78 deletions
src/app/group-monitor/booklet.service.ts
+
33
−
78
View file @
3656a310
...
...
@@ -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
((
elem
ent
:
Element
)
=>
(
elem
ent
.
nodeType
===
1
))
.
filter
((
elem
ent
:
Element
)
=>
(
tagNames
.
indexOf
(
elem
ent
.
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
];
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment