{
  "resourceType": "Library",
  "id": "Cccbase",
  "text": {
    "status": "extensions",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n<div>\n    <table class=\"grid dict\">\n        \n        \n        <tr>\n            <th scope=\"row\"><b>Title: </b></th>\n            <td style=\"padding-left: 4px;\">CccBase</td>\n        </tr>\n        \n\n        \n        \n        <tr>\n            <th scope=\"row\"><b>Id: </b></th>\n            <td style=\"padding-left: 4px;\">Cccbase</td>\n        </tr>\n        \n\n        \n        \n        <tr>\n            <th scope=\"row\"><b>Version: </b></th>\n            <td style=\"padding-left: 4px;\">0.1.0</td>\n        </tr>\n        \n\n        \n        <tr>\n            <th scope=\"row\"><b>Url: </b></th>\n            <td style=\"padding-left: 4px;\"><a href=\"Library-Cccbase.html\">CccBase</a></td>\n        </tr>\n        \n\n        \n\n        \n\n        \n\n        \n\n        \n        <tr>\n            <th scope=\"row\"><b>Type: </b></th>\n            <td style=\"padding-left: 4px;\">\n                \n                    \n                        \n                        <p style=\"margin-bottom: 5px;\">\n                            <b>system: </b> <span><a href=\"http://hl7.org/fhir/R4/valueset-library-type.html\">http://hl7.org/fhir/ValueSet/library-type</a></span>\n                        </p>\n                        \n                        \n                        <p style=\"margin-bottom: 5px;\">\n                            <b>code: </b> <span>logic-library</span>\n                        </p>\n                        \n                        \n                    \n                \n                \n            </td>\n        </tr>\n        \n\n        \n\n        \n        <tr>\n            <th scope=\"row\"><b>Date: </b></th>\n            <td style=\"padding-left: 4px;\">2026-04-07 13:45:53+0000</td>\n        </tr>\n        \n\n        \n        <tr>\n            <th scope=\"row\"><b>Publisher: </b></th>\n            <td style=\"padding-left: 4px;\">World Health Organization (WHO)</td>\n        </tr>\n        \n\n        \n\n        \n\n        \n\n        \n\n        \n        <tr>\n            <th scope=\"row\"><b>Jurisdiction: </b></th>\n            <td style=\"padding-left: 4px;\">001</td>\n        </tr>\n        \n\n        \n\n        \n\n        \n\n        \n\n        \n\n        \n\n        \n        <tr>\n            <th scope=\"row\"><b>Related Artifacts: </b></th>\n            <td style=\"padding-left: 4px;\">\n                \n                \n                \n                <p><b>Dependencies</b></p>\n                <ul>\n                  \n                    <li><code>http://fhir.org/guides/who/anc-cds/Library/FHIRHelpers</code></li>\n                  \n                </ul>\n                \n                \n                \n                \n                \n            </td>\n        </tr>\n        \n\n        \n        <tr>\n          <th scope=\"row\"><b>Parameters: </b></th>\n          <td style=\"padding-left: 4px;\">\n            <table class=\"grid-dict\">\n              <tr><th><b>Name</b></th><th><b>Type</b></th><th><b>Min</b></th><th><b>Max</b></th><th><b>In/Out</b></th></tr>\n              \n                <tr><th>encounterid</th><th>string</th><th/><th/><th>In</th></tr>\n              \n            </table>\n          </td>\n        </tr>\n        \n\n        \n        <tr>\n          <th scope=\"row\"><b>Data Requirements:</b></th>\n          <td style=\"padding-left: 4px;\">\n            <table class=\"grid-dict\">\n              <tr><th><b>Type</b></th><th><b>Profile</b></th><th><b>MS</b></th><th><b>Code Filter</b></th></tr>\n              \n                <tr>\n                  <th>Patient</th>\n                  <th>http://hl7.org/fhir/StructureDefinition/Patient</th>\n                  <th/>\n                  <th>\n                    \n                  </th>\n                </tr>\n              \n                <tr>\n                  <th>Encounter</th>\n                  <th>http://hl7.org/fhir/StructureDefinition/Encounter</th>\n                  <th/>\n                  <th>\n                    \n                  </th>\n                </tr>\n              \n            </table>\n          </td>\n        </tr>\n        \n\n        \n        \n        <tr>\n          <td colspan=\"2\">\n            <table>\n              <tr><th><a id=\"cql-content\"><b>Content: </b></a> text/cql</th></tr>\n              <tr><td><pre><code class=\"language-cql\">/*nter&quot;\n@author: Patrick Delcroix\n@description: This library is part of the project EmCare Project, its allows standardisation for the observation processing\n*/\n\nlibrary emcarebase version '1.0.1.rc12.build.156'\n\nusing FHIR version '4.0.1'\n\ninclude FHIRHelpers version '4.0.1'\n\nparameter &quot;encounterid&quot; String\n//parameter &quot;Encounter&quot; Encounter$\ncodesystem &quot;RoleCodeSystem&quot; : 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes'\ncode &quot;Mother&quot;: 'MTH' from &quot;RoleCodeSystem&quot;\n\ncontext Patient\n\n//define encounterid:\n//  &quot;Encounter&quot;.id\n\n\n\n\ndefine &quot;Yes&quot;:\n  true\n\ndefine &quot;No&quot;:\n  false \n\n\ndefine function IsInEncounter(ref FHIR.Reference):\n    case\n    when encounterid = ref.id then true\n    when encounterid = Last(Split(ref.reference, '/')) then true\n   // when ref.identifier is not null and id = ref.identifier.id then true\n    else false\nend\n\ndefine getEncounters:\n    [Encounter] E \n      where E.id = &quot;encounterid&quot;\n        return E\n\ndefine getEncounter:\n    singleton from getEncounters\n\ndefine function EmCareExtensions(element Patient, id String):\n  element.extension E\n      where E.url = ('https://fhir.dk.swisstph-mis.ch/matchbox/fhir//StructureDefinition/' + id)\n      return E\n\ndefine function EmCareExtension(element Patient, id String):\n  singleton from EmCareExtensions(element, id)\n\ndefine getPrimaryCareGiverReference:\n  EmCareExtension(Patient, 'primarycaregiver').value\n\ndefine getPrimaryCareGiver:\n  [RelatedPerson] RP where RP.id = Last(Split(getPrimaryCareGiverReference.reference, '/'))\n    return RP\n\ndefine isMotherPrimaryCareGiver:\n  getPrimaryCareGiver is not null and Exists(getPrimaryCareGiver.relationship r where r ~ &quot;Mother&quot;)\n\ndefine &quot;Biological Mother Vital Status&quot;:\n  EmCareExtension(Patient, 'motherVitalStatus').value\n  \ndefine &quot;Biological Father Vital Status&quot;:\n  EmCareExtension(Patient, 'fatherVitalStatus').value\n// person accompanying the child is a participant with a null type\ndefine getRelatedPersonReferenceAccompanyingTheChild:\n  Coalesce(First(getEncounter.participant P where not exists(P.type) ).individual, getPrimaryCareGiverReference)\n\ndefine getRelatedPersonsAccompanyingTheChild:\n  [RelatedPerson] RP where RP.id = Last(Split(getRelatedPersonReferenceAccompanyingTheChild.reference, '/'))\n    return RP\n\n\ndefine getRelatedPersonAccompanyingTheChild:\n  singleton from getRelatedPersonsAccompanyingTheChild\n\n\ndefine &quot;Person accompanying child today's Relationship to Client&quot;:\n  getRelatedPersonAccompanyingTheChild.relationship\n\n\n\n\ndefine function getValue(O Observation):\n  case  \n      when  O is null then null\n      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))\n      else O.value\n  end\n\n\ndefine function assesObservation(O Observation):\n  case  \n      when  O is null then null\n      when  O.status in  {'cancelled'} then false\n      when O.value is null then  true\n      else O.value\n  end\n/*      \ndefine function GetObservationDuringEncounter(idlist List&lt;System.Code&gt;):\n  if GetObservationDuringEncounterDone(idlist ) is null then \n      GetObservationDuringEncounterDone(idlist )\n  else\n    GetObservationDuringEncounterNotFound(idlist )\n*/\n\ndefine function GetObservationDuringEncounter(id System.Code):\n  Coalesce(GetObservationDuringEncounterDone(id ), GetObservationDuringEncounterNotFound(id ))\n/*\ndefine function GetObservationDuringEncounterDone(idlist List&lt;System.Code&gt;):\n      Last([Observation: idlist ] O\n        where   IsInEncounter(O.encounter) \n        and O.status in { 'final', 'amended', 'corrected'})\n*/\ndefine function GetObservationDuringEncounterDone(id System.Code):\n      First([Observation: id ] O\n        where   IsInEncounter(O.encounter) \n        and O.status in { 'final', 'amended', 'corrected'} \n        sort by issued.value desc) \n\n\ndefine function HasObservationCodeDuringEncounterDone(id System.Code, valueCode System.Code):\n    \n    Exists([Observation: id ] O\n        where   IsInEncounter(O.encounter) \n        and O.status in { 'final', 'amended', 'corrected'}\n        and O.value is FHIR.CodeableConcept\n        and O.value ~ valueCode)\n\n//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'}) )&quot;\n\n/*\ndefine function GetObservationDuringEncounterNotFound(idlist List&lt;System.Code&gt;):\n      Last([Observation: idlist] O\n       where  IsInEncounter(O.encounter) \n          and O.status in {'cancelled'})\n          */\ndefine function GetObservationDuringEncounterNotFound(id System.Code):\n      First([Observation: id] O\n       where  IsInEncounter(O.encounter) \n          and O.status in {'cancelled'}\n          sort by issued.value desc)\n/*\n\ndefine function GetNumberObservationDuringEncounter(idlist List&lt;System.Code&gt;):\n  Count(\n    [Observation: idlist] O\n        where  IsInEncounter( O.encounter) \n        and O.status in { 'final', 'amended', 'corrected'})\n*/\ndefine function GetNumberObservationDuringEncounter(id System.Code):\n  Count(\n    [Observation: id] O\n        where  IsInEncounter( O.encounter) \n        and O.status in { 'final', 'amended', 'corrected'})\n\n// ************************   GetObsValue ************************/\n/*\ndefine function GetObsValue(idlist List&lt;System.Code&gt;):\n    getValue(GetObservationDuringEncounter(idlist))\n\ndefine function GetObsValue(id System.Code):\n    getValue(GetObservationDuringEncounter({id}))\n*/\n\ndefine function GetObsValue(id System.Code):\n    getValue(GetObservationDuringEncounter(id))\n\ndefine function GetObsValue(id System.String, codesystem System.String):\n    getValue(GetObservationDuringEncounter(coding(id, codesystem)))\n\ndefine function GetObsValue(id System.String):\n    getValue(GetObservationDuringEncounter(coding(id, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes')))\n\ndefine function HasObsValueCode(id System.String, value System.Code):\n   HasObservationCodeDuringEncounterDone(coding(id, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes') , value )\n  \ndefine function HasObsValueCode(id System.String, value System.String):\n   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') )\n\ndefine function HasObsValueCode(id System.Code, value System.String):\n   HasObservationCodeDuringEncounterDone(id , coding(value, 'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes') )\n\n// ************************   HasObs ************************/\n/*\ndefine function HasObs(idlist List&lt;System.Code&gt;):\n    assesObservation(GetObservationDuringEncounter(idlist))\n\ndefine function HasObs(id System.Code):\n    assesObservation(GetObservationDuringEncounter({id}))\n*/\ndefine function HasObs(id System.Code):\n    assesObservation(GetObservationDuringEncounter(id))\n\ndefine function HasObs(id System.String, codesystem System.String):\n    assesObservation(GetObservationDuringEncounter(coding(id, codesystem)))\n\ndefine function HasObs(id System.String):\n    assesObservation(GetObservationDuringEncounter(coding(id)))\n\ndefine function HasObs(id FHIR.code, codesystem System.String):\n    assesObservation(GetObservationDuringEncounter(coding(id, codesystem)))\n/*\ndefine function GetHistoricObservation(idlist List&lt;System.Code&gt;, nbdays Integer):\n  if GetHistoricObservationNotfound(idlist , codesystem , nbdays ) is null then \n      GetHistoricObservationNotfound(idlist , codesystem , nbdays )\n  else\n    GetHistoricObservationNotfound(idlist , codesystem , nbdays )\n*/\n\ndefine function GetHistoricObservation(id System.Code, nbdays Integer):\n  Coalesce(GetHistoricObservationDone(id ,  nbdays ),  GetHistoricObservationNotfound(id  , nbdays ))\n/*\ndefine function GetHistoricObservationDone(idlist List&lt;System.Code&gt;, nbdays Integer):\n      Last([Observation: idlist ] O\n        where  O.status in { 'final', 'amended', 'corrected'}\n        and (difference in days between O.issued and Today()) &lt; nbdays)\n*/\ndefine function GetHistoricObservationDone(id System.Code, nbdays Integer):\n      First([Observation: id ] O\n        where  O.status in { 'final', 'amended', 'corrected'}\n        and (difference in days between O.issued.value and Today()) &lt; nbdays \n        sort by issued.value desc )\n\n/*\ndefine function GetHistoricObservationNotfound(idlist List&lt;System.Code&gt;, nbdays Integer):\n      Last([Observation: idlist] O\n        where  O.status in {'cancelled'}\n*/\ndefine function GetHistoricObservationNotfound(id System.Code, nbdays Integer):\n      First([Observation: id] O\n        where  O.status in {'cancelled'}\n          and (difference in days between O.issued.value and Today()) &lt; nbdays \n        sort by issued.value desc)\n// ************************   HasObsHistory ************************/\n\n/*\ndefine function HasObsHistory(idlist List&lt;System.Code&gt;,  nbdays Integer):\n  assesObservation(GetHistoricObservation(idlist, nbdays))\n\n\n define function HasObsHistory(id System.Code, cnbdays Integer):\n  assesObservation(GetHistoricObservation({id}, nbdays))\n*/\ndefine function HasObsHistory(id System.Code, nbdays Integer):\n  assesObservation(GetHistoricObservation(id, nbdays))\n\ndefine function HasObsHistory(id System.String, codesystem System.String, nbdays Integer):\n  assesObservation(GetHistoricObservation(coding(id, codesystem), nbdays))\n\n\ndefine function HasObsHistory(id System.String, nbdays Integer):\n  assesObservation(GetHistoricObservation(coding(id), nbdays))\n\ndefine function c(s System.String):\n  FHIR.code { value: s }\n\n\n\ndefine function coding(s System.String, url System.String):\n  System.Code {\n                code:  s ,\n                system:  url \n            }\n\ndefine function coding(s FHIR.code, url System.String):\n  System.Code {\n                code:  s.value ,\n                system:  url \n            }\n\ndefine function coding(s System.String):\n  System.Code {\n                code:  s ,\n                system:  'https://fhir.dk.swisstph-mis.ch/matchbox/fhir/CodeSystem/emcare-custom-codes'\n            }\n\n\ndefine function ToUrl(s System.String):\n  url {value:s}\n\n\n\n// ************************   GetCond ************************/\n/*\ndefine function GetCoO.encounterndition(idlist List&lt;System.Code&gt;):\n      Last([Condition: idlist] O\n        where  IsInEncounter( O.encounter) \n        and exists(O.verificationStatus.coding C where C.code in {'unconfirmed', 'provisional', 'differential', 'confirmed'} \n        and C.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status'))\n*/\ndefine function GetCondition(id System.Code):\n      First([Condition: id] O\n        where  IsInEncounter(O.encounter) \n        and exists(O.verificationStatus.coding C where C.code in {'unconfirmed', 'provisional', 'differential', 'confirmed'} \n        and C.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status') \n        sort by recordedDate desc)\n\n\ndefine function assesCondition(O Condition):\n  if O is null then \n    false\n  else\n    true\n/*\ndefine function GetCond(idlist List&lt;System.Code&gt;):\n  GetCondition(idlist)\n\ndefine function GetCond(id System.Code):\n  GetCondition({id})\n*/\ndefine function GetCond(id System.Code):\n  (GetCondition(id))\n\ndefine function GetCond(id System.String, codesystem System.String):\n  (GetCondition(coding(id, codesystem)))\n\ndefine function GetCond(id System.String):\n  (GetCondition(coding(id)))\n\n// ************************   HasCond ************************/\n/*\ndefine function HasCond(idlist List&lt;System.Code&gt;):\n  assesCondition(GetCondition(idlist))\n\n\n3518GetCondition(id))\n*/\ndefine function HasCond(id System.String, codesystem System.String):\n  assesCondition(GetCondition(coding(id, codesystem)))\n\ndefine function HasCond(id System.String):\n  assesCondition(GetCondition(coding(id)))\n\ndefine function HasCond(id System.Code):\n  assesCondition(GetCondition(id))\n\n// ************************   PatientHasHistoryOfCondition ************************/\n/*\ndefine function GetHistoricCondition(idlist List&lt;System.Code&gt;,  nbdays Integer):\n      Last([Condition: idlist] O\n        where  (difference in days between O.recordedDate and Today()) &lt; nbdays\n        and exists(O.verificationStatus.coding C where C.code in {'unconfirmed', 'provisional', 'differential', 'confirmed'} \n        and C.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status'))\n*/\n\ndefine function GetHistoricCondition(id System.Code,  nbdays Integer):\n      First([Condition: id] O\n        where  (difference in days between O.recordedDate and Today()) &lt; nbdays\n        and exists(O.verificationStatus.coding C where C.code in {'unconfirmed', 'provisional', 'differential', 'confirmed'} \n        and C.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status') \n        sort by recordedDate desc)\n\n\n\n\n/*\ndefine function HasCondHistory(idlist List&lt;System.Code&gt;,  nbdays Integer):\n  assesCondition(GetHistoricCondition(idlist,  nbdays))\n\n\ndefine function HasCondHistory(id System.Code,  nbdays Integer):\n  assesCondition(GetHistoricCondition({id}, nbdays))\n*/\ndefine function HasCondHistory(id System.Code, nbdays Integer):\n  assesCondition(GetHistoricCondition(id,  nbdays))\n\ndefine function HasCondHistory(id System.String, codesystem System.String, nbdays Integer):\n  assesCondition(GetHistoricCondition(coding(id,codesystem),nbdays))\n\n\ndefine function HasCondHistory(id System.String, nbdays Integer):\n  assesCondition(GetHistoricCondition(coding(id),  nbdays))\n\n\n\n//define function PatientHasConditionDuringEncounter(id String, codesystem url, duration Integer)):\n\n//define function PatientHasConditionFromValueSetDuringEncounter(id String, codesystem url, duration Integer)):\n\n//define function PatientHasHsistoryCondition(id String,codesystem url , duration Integer):\n//define function PatientHasHsistoryConditionFromValueSet(id String,codesystem url , duration Integer):\n\n\n// PatientHasSymptom(&quot;symptom name&quot;): symptom in the encounter\n\n\n// PatientLastMeasureOf(&quot;measure name&quot;): measure in the encounter\n\n// PatientLastHistoricMeasureOf(&quot;measure name&quot;, duration): measure in the patient history during duration\n\n// PatientHasDiagnostic(&quot;Diagnostic name&quot; ): Diagnostic in the encounter\n\n// PatientHasHistoryOfDiagnostic(&quot;Diagnostic name&quot;, duration ): Diagnostic in the patient history during duration\n\n// PatientHasProposedorConfirmedDianostic(&quot;Diagnostic name&quot; ): Diagnostic in the encounter\n\n// PatientHasHistoryOfProposedorConfirmedDianostic(&quot;Diagnostic name&quot;, duration ): Diagnostic in the patient history during duration\n\n// PatientZScoreWfA(wieght, age)\n\n// PatientWeightFromWfA(age, Muac)\n</code></pre></td></tr>\n            </table>\n          </td>\n        </tr>\n        \n        \n        \n    </table>\n</div>\n</div>"
  },
  "url": "https://smart.who.int/ccc/Library/Cccbase",
  "version": "0.1.0",
  "name": "Cccbase",
  "title": "CccBase",
  "status": "active",
  "type": {
    "coding": [
      {
        "system": "http://hl7.org/fhir/ValueSet/library-type",
        "code": "logic-library"
      }
    ]
  },
  "date": "2026-04-07T13:45:53+00:00",
  "publisher": "World Health Organization (WHO)",
  "contact": [
    {
      "name": "World Health Organization (WHO)",
      "telecom": [
        {
          "system": "url",
          "value": "https://www.who.int"
        }
      ]
    },
    {
      "telecom": [
        {
          "system": "url",
          "value": "https://www.who.int"
        }
      ]
    }
  ],
  "jurisdiction": [
    {
      "coding": [
        {
          "system": "http://unstats.un.org/unsd/methods/m49/m49.htm",
          "code": "001"
        }
      ]
    }
  ],
  "relatedArtifact": [
    {
      "type": "depends-on",
      "resource": "http://fhir.org/guides/who/anc-cds/Library/FHIRHelpers"
    }
  ],
  "parameter": [
    {
      "name": "encounterid",
      "use": "in",
      "type": "string"
    }
  ],
  "dataRequirement": [
    {
      "type": "Patient",
      "profile": [
        "http://hl7.org/fhir/StructureDefinition/Patient"
      ]
    },
    {
      "type": "Encounter",
      "profile": [
        "http://hl7.org/fhir/StructureDefinition/Encounter"
      ]
    }
  ],
  "content": [
    {
      "id": "ig-loader-Cccbase.cql",
      "contentType": "text/cql",
      "url": "Library-Cccbase.cql"
    }
  ]
}
