<?xml version='1.0' encoding='UTF-8'?>
<Library xmlns="http://hl7.org/fhir" xmlns:html="http://www.w3.org/1999/xhtml">
  <id value="Cccbase" />
  <text>
    <status value="extensions" /><html:div>
<html:div>
    <html:table class="grid dict">
        
        
        <html:tr>
            <html:th scope="row"><html:b>Title: </html:b></html:th>
            <html:td style="padding-left: 4px;">CccBase</html:td>
        </html:tr>
        

        
        
        <html:tr>
            <html:th scope="row"><html:b>Id: </html:b></html:th>
            <html:td style="padding-left: 4px;">Cccbase</html:td>
        </html:tr>
        

        
        
        <html:tr>
            <html:th scope="row"><html:b>Version: </html:b></html:th>
            <html:td style="padding-left: 4px;">0.1.0</html:td>
        </html:tr>
        

        
        <html:tr>
            <html:th scope="row"><html:b>Url: </html:b></html:th>
            <html:td style="padding-left: 4px;"><html:a href="Library-Cccbase.html">CccBase</html:a></html:td>
        </html:tr>
        

        

        

        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Type: </html:b></html:th>
            <html:td style="padding-left: 4px;">
                
                    
                        
                        <html:p style="margin-bottom: 5px;">
                            <html:b>system: </html:b> <html:span><html:a href="http://hl7.org/fhir/R4/valueset-library-type.html">http://hl7.org/fhir/ValueSet/library-type</html:a></html:span>
                        </html:p>
                        
                        
                        <html:p style="margin-bottom: 5px;">
                            <html:b>code: </html:b> <html:span>logic-library</html:span>
                        </html:p>
                        
                        
                    
                
                
            </html:td>
        </html:tr>
        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Date: </html:b></html:th>
            <html:td style="padding-left: 4px;">2026-04-07 13:45:53+0000</html:td>
        </html:tr>
        

        
        <html:tr>
            <html:th scope="row"><html:b>Publisher: </html:b></html:th>
            <html:td style="padding-left: 4px;">World Health Organization (WHO)</html:td>
        </html:tr>
        

        

        

        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Jurisdiction: </html:b></html:th>
            <html:td style="padding-left: 4px;">001</html:td>
        </html:tr>
        

        

        

        

        

        

        

        
        <html:tr>
            <html:th scope="row"><html:b>Related Artifacts: </html:b></html:th>
            <html:td style="padding-left: 4px;">
                
                
                
                <html:p><html:b>Dependencies</html:b></html:p>
                <html:ul>
                  
                    <html:li><html:code>http://fhir.org/guides/who/anc-cds/Library/FHIRHelpers</html:code></html:li>
                  
                </html:ul>
                
                
                
                
                
            </html:td>
        </html:tr>
        

        
        <html:tr>
          <html:th scope="row"><html:b>Parameters: </html:b></html:th>
          <html:td style="padding-left: 4px;">
            <html:table class="grid-dict">
              <html:tr><html:th><html:b>Name</html:b></html:th><html:th><html:b>Type</html:b></html:th><html:th><html:b>Min</html:b></html:th><html:th><html:b>Max</html:b></html:th><html:th><html:b>In/Out</html:b></html:th></html:tr>
              
                <html:tr><html:th>encounterid</html:th><html:th>string</html:th><html:th /><html:th /><html:th>In</html:th></html:tr>
              
            </html:table>
          </html:td>
        </html:tr>
        

        
        <html:tr>
          <html:th scope="row"><html:b>Data Requirements:</html:b></html:th>
          <html:td style="padding-left: 4px;">
            <html:table class="grid-dict">
              <html:tr><html:th><html:b>Type</html:b></html:th><html:th><html:b>Profile</html:b></html:th><html:th><html:b>MS</html:b></html:th><html:th><html:b>Code Filter</html:b></html:th></html:tr>
              
                <html:tr>
                  <html:th>Patient</html:th>
                  <html:th>http://hl7.org/fhir/StructureDefinition/Patient</html:th>
                  <html:th />
                  <html:th>
                    
                  </html:th>
                </html:tr>
              
                <html:tr>
                  <html:th>Encounter</html:th>
                  <html:th>http://hl7.org/fhir/StructureDefinition/Encounter</html:th>
                  <html:th />
                  <html:th>
                    
                  </html:th>
                </html:tr>
              
            </html:table>
          </html:td>
        </html:tr>
        

        
        
        <html:tr>
          <html:td colspan="2">
            <html:table>
              <html:tr><html:th><html:a id="cql-content"><html:b>Content: </html:b></html:a> text/cql</html:th></html:tr>
              <html:tr><html:td><html:pre><html:code class="language-cql">/*nter"
@author: Patrick Delcroix
@description: This library is part of the project EmCare Project, its allows standardisation for the observation processing
*/

library emcarebase version '1.0.1.rc12.build.156'

using FHIR version '4.0.1'

include FHIRHelpers version '4.0.1'

parameter "encounterid" String
//parameter "Encounter" Encounter$
codesystem "RoleCodeSystem" : 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes'
code "Mother": 'MTH' from "RoleCodeSystem"

context Patient

//define encounterid:
//  "Encounter".id




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
   // when ref.identifier is not null and id = ref.identifier.id then true
    else false
end

define getEncounters:
    [Encounter] E 
      where E.id = "encounterid"
        return E

define getEncounter:
    singleton from getEncounters

define function EmCareExtensions(element Patient, id String):
  element.extension E
      where E.url = ('https://fhir.dk.swisstph-mis.ch/matchbox/fhir//StructureDefinition/' + id)
      return E

define function EmCareExtension(element Patient, id String):
  singleton from EmCareExtensions(element, id)

define getPrimaryCareGiverReference:
  EmCareExtension(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":
  EmCareExtension(Patient, 'motherVitalStatus').value
  
define "Biological Father Vital Status":
  EmCareExtension(Patient, 'fatherVitalStatus').value
// person accompanying the child is a participant with a null type
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":
  getRelatedPersonAccompanyingTheChild.relationship




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/emcare-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(idlist List&lt;System.Code&gt;):
  if GetObservationDuringEncounterDone(idlist ) is null then 
      GetObservationDuringEncounterDone(idlist )
  else
    GetObservationDuringEncounterNotFound(idlist )
*/

define function GetObservationDuringEncounter(id System.Code):
  Coalesce(GetObservationDuringEncounterDone(id ), GetObservationDuringEncounterNotFound(id ))
/*
define function GetObservationDuringEncounterDone(idlist List&lt;System.Code&gt;):
      Last([Observation: idlist ] O
        where   IsInEncounter(O.encounter) 
        and O.status in { 'final', 'amended', 'corrected'})
*/
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)

//Exists([Observation] O where O.status in { 'final', 'amended', 'corrected'} and O.value is FHIR.CodeableConcept   and Exists( O.value.coding C where C is FHIR.Coding and C.code = FHIR.code {value: 'EmCare.B20S2.DE04'}) )"

/*
define function GetObservationDuringEncounterNotFound(idlist List&lt;System.Code&gt;):
      Last([Observation: idlist] O
       where  IsInEncounter(O.encounter) 
          and O.status in {'cancelled'})
          */
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(idlist List&lt;System.Code&gt;):
  Count(
    [Observation: idlist] O
        where  IsInEncounter( O.encounter) 
        and O.status in { 'final', 'amended', 'corrected'})
*/
define function GetNumberObservationDuringEncounter(id System.Code):
  Count(
    [Observation: id] O
        where  IsInEncounter( O.encounter) 
        and O.status in { 'final', 'amended', 'corrected'})

// ************************   GetObsValue ************************/
/*
define function GetObsValue(idlist List&lt;System.Code&gt;):
    getValue(GetObservationDuringEncounter(idlist))

define function GetObsValue(id System.Code):
    getValue(GetObservationDuringEncounter({id}))
*/

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/emcare-custom-codes')))

define function HasObsValueCode(id System.String, value System.Code):
   HasObservationCodeDuringEncounterDone(coding(id, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes') , value )
  
define function HasObsValueCode(id System.String, value System.String):
   HasObservationCodeDuringEncounterDone(coding(id, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes') , coding(value, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes') )

define function HasObsValueCode(id System.Code, value System.String):
   HasObservationCodeDuringEncounterDone(id , coding(value, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes') )

// ************************   HasObs ************************/
/*
define function HasObs(idlist List&lt;System.Code&gt;):
    assesObservation(GetObservationDuringEncounter(idlist))

define function HasObs(id System.Code):
    assesObservation(GetObservationDuringEncounter({id}))
*/
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(idlist List&lt;System.Code&gt;, nbdays Integer):
  if GetHistoricObservationNotfound(idlist , codesystem , nbdays ) is null then 
      GetHistoricObservationNotfound(idlist , codesystem , nbdays )
  else
    GetHistoricObservationNotfound(idlist , codesystem , nbdays )
*/

define function GetHistoricObservation(id System.Code, nbdays Integer):
  Coalesce(GetHistoricObservationDone(id ,  nbdays ),  GetHistoricObservationNotfound(id  , nbdays ))
/*
define function GetHistoricObservationDone(idlist List&lt;System.Code&gt;, nbdays Integer):
      Last([Observation: idlist ] O
        where  O.status in { 'final', 'amended', 'corrected'}
        and (difference in days between O.issued and Today()) &lt; nbdays)
*/
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.value and Today()) &lt; nbdays 
        sort by issued.value desc )

/*
define function GetHistoricObservationNotfound(idlist List&lt;System.Code&gt;, nbdays Integer):
      Last([Observation: idlist] O
        where  O.status in {'cancelled'}
*/
define function GetHistoricObservationNotfound(id System.Code, nbdays Integer):
      First([Observation: id] O
        where  O.status in {'cancelled'}
          and (difference in days between O.issued.value and Today()) &lt; nbdays 
        sort by issued.value desc)
// ************************   HasObsHistory ************************/

/*
define function HasObsHistory(idlist List&lt;System.Code&gt;,  nbdays Integer):
  assesObservation(GetHistoricObservation(idlist, nbdays))


 define function HasObsHistory(id System.Code, cnbdays Integer):
  assesObservation(GetHistoricObservation({id}, nbdays))
*/
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/emcare-custom-codes'
            }


define function ToUrl(s System.String):
  url {value:s}



// ************************   GetCond ************************/
/*
define function GetCoO.encounterndition(idlist List&lt;System.Code&gt;):
      Last([Condition: idlist] 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'))
*/
define function GetCondition(id System.Code):
      First([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 assesCondition(O Condition):
  if O is null then 
    false
  else
    true
/*
define function GetCond(idlist List&lt;System.Code&gt;):
  GetCondition(idlist)

define function GetCond(id System.Code):
  GetCondition({id})
*/
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(coding(id)))

// ************************   HasCond ************************/
/*
define function HasCond(idlist List&lt;System.Code&gt;):
  assesCondition(GetCondition(idlist))


3518GetCondition(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))

// ************************   PatientHasHistoryOfCondition ************************/
/*
define function GetHistoricCondition(idlist List&lt;System.Code&gt;,  nbdays Integer):
      Last([Condition: idlist] O
        where  (difference in days between O.recordedDate and Today()) &lt; 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'))
*/

define function GetHistoricCondition(id System.Code,  nbdays Integer):
      First([Condition: id] O
        where  (difference in days between O.recordedDate and Today()) &lt; 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(idlist List&lt;System.Code&gt;,  nbdays Integer):
  assesCondition(GetHistoricCondition(idlist,  nbdays))


define function HasCondHistory(id System.Code,  nbdays Integer):
  assesCondition(GetHistoricCondition({id}, nbdays))
*/
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))



//define function PatientHasConditionDuringEncounter(id String, codesystem url, duration Integer)):

//define function PatientHasConditionFromValueSetDuringEncounter(id String, codesystem url, duration Integer)):

//define function PatientHasHsistoryCondition(id String,codesystem url , duration Integer):
//define function PatientHasHsistoryConditionFromValueSet(id String,codesystem url , duration Integer):


// PatientHasSymptom("symptom name"): symptom in the encounter


// PatientLastMeasureOf("measure name"): measure in the encounter

// PatientLastHistoricMeasureOf("measure name", duration): measure in the patient history during duration

// PatientHasDiagnostic("Diagnostic name" ): Diagnostic in the encounter

// PatientHasHistoryOfDiagnostic("Diagnostic name", duration ): Diagnostic in the patient history during duration

// PatientHasProposedorConfirmedDianostic("Diagnostic name" ): Diagnostic in the encounter

// PatientHasHistoryOfProposedorConfirmedDianostic("Diagnostic name", duration ): Diagnostic in the patient history during duration

// PatientZScoreWfA(wieght, age)

// PatientWeightFromWfA(age, Muac)
</html:code></html:pre></html:td></html:tr>
            </html:table>
          </html:td>
        </html:tr>
        
        
        
    </html:table>
</html:div>
</html:div>
  </text>
  <url value="https://smart.who.int/ccc/Library/Cccbase" />
  <version value="0.1.0" />
  <name value="Cccbase" />
  <title value="CccBase" />
  <status value="active" />
  <type>
    <coding>
      <system value="http://hl7.org/fhir/ValueSet/library-type" />
      <code value="logic-library" />
    </coding>
  </type>
  <date value="2026-04-07T13:45:53+00:00" />
  <publisher value="World Health Organization (WHO)" />
  <contact>
    <name value="World Health Organization (WHO)" />
    <telecom>
      <system value="url" />
      <value value="https://www.who.int" />
    </telecom>
  </contact>
  <contact>
    <telecom>
      <system value="url" />
      <value value="https://www.who.int" />
    </telecom>
  </contact>
  <jurisdiction>
    <coding>
      <system value="http://unstats.un.org/unsd/methods/m49/m49.htm" />
      <code value="001" />
    </coding>
  </jurisdiction>
  <relatedArtifact>
    <type value="depends-on" />
    <resource value="http://fhir.org/guides/who/anc-cds/Library/FHIRHelpers" />
  </relatedArtifact>
  <parameter>
    <name value="encounterid" />
    <use value="in" />
    <type value="string" />
  </parameter>
  <dataRequirement>
    <type value="Patient" />
    <profile value="http://hl7.org/fhir/StructureDefinition/Patient" />
  </dataRequirement>
  <dataRequirement>
    <type value="Encounter" />
    <profile value="http://hl7.org/fhir/StructureDefinition/Encounter" />
  </dataRequirement>
  <content id="ig-loader-Cccbase.cql">
    <contentType value="text/cql" />
    <url value="Library-Cccbase.cql" /></content>
</Library>