WHO Clinical Care in Crisis Implementation Guide for Children - Local Development build (v0.1.0). See the Directory of published versions
depends-on | http://fhir.org/guides/who/anc-cds/Library/FHIRHelpers |
Parameters
Data Requirements
Contents
text/cql
library chebase version '1.0.313+build.456'
using FHIR version '4.0.1'
include FHIRHelpers version '4.0.1'
parameter "encounterid" String
codesystem "RoleCodeSystem" : 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes'
code "Mother": 'MTH' from "RoleCodeSystem"
context Patient
define "Yes":
true
define "No":
false
define function IsInEncounter(ref FHIR.Reference):
case
when encounterid = ref.id then true
when encounterid = Last(Split(ref.reference, '/')) then true
else false
end
define getEncounters:
[Encounter] E
where E.id = "encounterid"
return E
define getEncounter:
singleton from getEncounters
define function CHEExtensions(element Patient, id String):
element.extension E
where E.url = ('https://fhir.dk.swisstph-mis.ch/matchbox/fhir/StructureDefinition/' + id)
return E
define function CHEExtension(element Patient, id String):
singleton from CHEExtensions(element, id)
define function CHEExtensions(element Encounter, id String):
element.extension E
where E.url = ('https://fhir.dk.swisstph-mis.ch/matchbox/fhir/StructureDefinition/' + id)
return E
define function postcordinationExtension(element Condition, postcordination List<String>):
element.extension E
where E.url = ('https://fhir.dk.swisstph-mis.ch/matchbox/fhir/StructureDefinition/postcordination')
and E.value in postcordination
define function CHEExtension(element Encounter, id String):
singleton from CHEExtensions(element, id)
define getPrimaryCareGiverReference:
CHEExtension(Patient, 'primarycaregiver').value
define getPrimaryCareGiver:
[RelatedPerson] RP where RP.id = Last(Split(getPrimaryCareGiverReference.reference, '/'))
return RP
define isMotherPrimaryCareGiver:
getPrimaryCareGiver is not null and Exists(getPrimaryCareGiver.relationship r where r ~ "Mother")
define "Biological Mother Vital Status":
CHEExtension(Patient, 'motherVitalStatus').value
define "Biological Father Vital Status":
CHEExtension(Patient, 'fatherVitalStatus').value
define getRelatedPersonReferenceAccompanyingTheChild:
Coalesce(First(getEncounter.participant P where not exists(P.type) ).individual, getPrimaryCareGiverReference)
define getRelatedPersonsAccompanyingTheChild:
[RelatedPerson] RP where RP.id = Last(Split(getRelatedPersonReferenceAccompanyingTheChild.reference, '/'))
return RP
define getRelatedPersonAccompanyingTheChild:
singleton from getRelatedPersonsAccompanyingTheChild
define "Person accompanying child today's Relationship to Client":
CHEExtension(getEncounter, 'motherpresent').value
define function getValue(O Observation):
case
when O is null then null
when O.value is CodeableConcept then Coalesce(First(O.value.coding C where C.system = 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/che-custom-codes'), First(O.value.coding))
else O.value
end
define function assesObservation(O Observation):
case
when O is null then null
when O.status in {'cancelled'} then false
when O.value is null then true
else O.value
end
define function GetObservationDuringEncounter(id System.Code):
First([Observation: id ] O
where IsInEncounter(O.encounter)
and O.status in { 'final', 'amended', 'corrected','cancelled'}
sort by issued.value desc)
define function GetObservationDuringEncounterDone(id System.Code):
First([Observation: id ] O
where IsInEncounter(O.encounter)
and O.status in { 'final', 'amended', 'corrected'}
sort by issued.value desc)
define function HasObservationCodeDuringEncounterDone(id System.Code, valueCode System.Code):
Exists([Observation: id ] O
where IsInEncounter(O.encounter)
and O.status in { 'final', 'amended', 'corrected'}
and O.value is FHIR.CodeableConcept
and O.value ~ valueCode)
define function GetObservationDuringEncounterNotFound(id System.Code):
First([Observation: id] O
where IsInEncounter(O.encounter)
and O.status in {'cancelled'}
sort by issued.value desc)
define function GetNumberObservationDuringEncounter(id System.Code):
Count(
[Observation: id] O
where IsInEncounter( O.encounter)
and O.status in { 'final', 'amended', 'corrected'})
define function GetObsValue(id System.Code):
getValue(GetObservationDuringEncounter(id))
define function GetObsValue(id System.String, codesystem System.String):
getValue(GetObservationDuringEncounter(coding(id, codesystem)))
define function GetObsValue(id System.String):
getValue(GetObservationDuringEncounter(coding(id, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/che-custom-codes')))
define function HasObsValueCode(id System.String, value System.Code):
HasObservationCodeDuringEncounterDone(coding(id, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/che-custom-codes') , value )
define function HasObsValueCode(id System.String, value System.String):
HasObservationCodeDuringEncounterDone(coding(id, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/che-custom-codes') , coding(value, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/che-custom-codes') )
define function HasObsValueCode(id System.Code, value System.String):
HasObservationCodeDuringEncounterDone(id , coding(value, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/che-custom-codes') )
define function HasObs(id System.Code):
assesObservation(GetObservationDuringEncounter(id))
define function HasObs(id System.String, codesystem System.String):
assesObservation(GetObservationDuringEncounter(coding(id, codesystem)))
define function HasObs(id System.String):
assesObservation(GetObservationDuringEncounter(coding(id)))
define function HasObs(id FHIR.code, codesystem System.String):
assesObservation(GetObservationDuringEncounter(coding(id, codesystem)))
define function GetHistoricObservation(id System.Code, nbdays Integer):
First([Observation: id ] O
where O.status in { 'final', 'amended', 'corrected','cancelled'}
and (difference in days between O.issued and Today()) < nbdays
sort by issued.value desc )
define function GetHistoricObservationDone(id System.Code, nbdays Integer):
First([Observation: id ] O
where O.status in { 'final', 'amended', 'corrected'}
and (difference in days between O.issued and Today()) < nbdays
sort by issued.value desc )
define function GetHistoricObservationNotfound(id System.Code, nbdays Integer):
First([Observation: id] O
where O.status in {'cancelled'}
and (difference in days between O.issued and Today()) < nbdays
sort by issued.value desc)
define function HasObsHistory(id System.Code, nbdays Integer):
assesObservation(GetHistoricObservation(id, nbdays))
define function HasObsHistory(id System.String, codesystem System.String, nbdays Integer):
assesObservation(GetHistoricObservation(coding(id, codesystem), nbdays))
define function HasObsHistory(id System.String, nbdays Integer):
assesObservation(GetHistoricObservation(coding(id), nbdays))
define function c(s System.String):
FHIR.code { value: s }
define function coding(s System.String, url System.String):
System.Code {
code: s ,
system: url
}
define function coding(s FHIR.code, url System.String):
System.Code {
code: s.value ,
system: url
}
define function coding(s System.String):
System.Code {
code: s ,
system: 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/che-custom-codes'
}
define function ToUrl(s System.String):
url {value:s}
define function ToIntC(b System.Boolean):
case
when b is null then 0
when b = true then 1
else 0
end
define function GetConditions(id System.Code):
[Condition: id] O
where IsInEncounter( O.encounter)
and exists(O.verificationStatus.coding C where C.code in {'unconfirmed', 'provisional', 'differential', 'confirmed'}
and C.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status')
sort by recordedDate desc
define function GetStemCode(id System.String):
coding(Split( id, '&')[0])
define function GetPostcoordinationCode(id System.String):
Tail(Split(id, '&'))
define function GetCondition(id System.Code):
First(GetConditions(id))
define function GetCondition(id System.Code, postcordination List<System.String>):
First(
[Condition: id] C
where IsInEncounter( C.encounter)
and exists(C.verificationStatus.coding cc where cc.code in {'unconfirmed', 'provisional', 'differential', 'confirmed'}
and cc.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status')
and postcordination is null or Count(postcordinationExtension(C,postcordination)) >= Count(postcordination)
sort by recordedDate desc
)
define function assesCondition(O Condition):
if O is null then
false
else
true
define function GetCond(id System.Code):
(GetCondition(id))
define function GetCond(id System.String, codesystem System.String):
(GetCondition(coding(id, codesystem)))
define function GetCond(id System.String):
(GetCondition(GetStemCode(id),GetPostcoordinationCode(id)))
define function HasCond(id System.String, codesystem System.String):
assesCondition(GetCondition(coding(id, codesystem)))
define function HasCond(id System.String):
assesCondition(GetCondition(coding(id)))
define function HasCond(id System.Code):
assesCondition(GetCondition(id))
define function GetHistoricCondition(id System.Code, nbdays Integer):
First([Condition: id] O
where (difference in days between O.recordedDate and Today()) < nbdays
and exists(O.verificationStatus.coding C where C.code in {'unconfirmed', 'provisional', 'differential', 'confirmed'}
and C.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status')
sort by recordedDate desc)
define function HasCondHistory(id System.Code, nbdays Integer):
assesCondition(GetHistoricCondition(id, nbdays))
define function HasCondHistory(id System.String, codesystem System.String, nbdays Integer):
assesCondition(GetHistoricCondition(coding(id,codesystem),nbdays))
define function HasCondHistory(id System.String, nbdays Integer):
assesCondition(GetHistoricCondition(coding(id), nbdays))