Stored Functions

Content-Type: text/csv
URN: urn:openhie.org:openinfoman-rapidpro:get_csv_for_import

Description



 
    Creates selection menu of all the CSD organizations and faciltiies with a specified parent.  Designed as part of heierarchical facility selection menus in RapidPro webhooks.
  

Definition

Source:
import module namespace oi_csv =  "https://github.com/openhie/openinfoman/adapter/csv";
declare namespace csd = "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;

oi_csv:get_serialized(/,$careServicesRequest)
Content-Type: application/json
URN: urn:openhie.org:openinfoman-rapidpro:get_json_for_import

Description



 
    Get JSON representation of CSD for import to RapidPro via API
  

Definition

Source:
import module namespace csr_proc = "https://github.com/openhie/openinfoman/csr_proc";
import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";

declare namespace csd = "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;



let $careServicesSubRequest :=  
  <csd:careServicesRequest>
    <csd:function  urn="urn:ihe:iti:csd:2014:stored-function:provider-search" >
      <csd:requestParams/>
    </csd:function>
  </csd:careServicesRequest> 

let $providers := csr_proc:process_CSR_stored_results( /. , $careServicesSubRequest)


let $contacts :=  
  <json type='object'>
    <contacts type="array">
    {
      for $provider in  $providers/csd:providerDirectory/csd:provider
      let $uuid := lower-case(string($provider/@entityID))
      let $name := ($provider/csd:demographic/csd:name/csd:commonName)[1]/text()
      let $tels := $provider/csd:demographic/csd:contactPoint/csd:codedType[@code="BP" and  @codingScheme="urn:ihe:iti:csd:2013:contactPoint"]
      let $tel_1 := $tels[1]/text()
      let $tel_2 := $tels[2]/text()
      let $tel_3 := $tels[3]/text()
      return 
	if (true()) (:  ($uuid and $name)  :)
       then 
         <_  type="object">
	   <name>{$name}</name>
	   <urns type="array">
             { if ($tel_1) then   <_ type='string'>tel:{$tel_1}</_> else ()} 
             { if ($tel_2) then   <_ type='string'>tel:{$tel_2}</_> else ()} 
             { if ($tel_3) then   <_ type='string'>tel:{$tel_3}</_> else ()} 
	   </urns>
	   <fields type="object">
             <globalid>{$uuid}</globalid>
	   </fields>
         </_>
       else ()
    }
    </contacts>
  </json>


return json:serialize($contacts,map{"format":"direct"})  
Content-Type: text/html
URN: urn:openhie.org:openinfoman-rapidpro:phone_stats

Description



 
    Statistics on phone numbers
  

Definition

Source:
import module namespace functx = "http://www.functx.com";
declare namespace csd = "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;


let $country_code := '+231'
let $cellcom_code := '77'

let $get_clean_phones := function($provider) {
  let $all_phones := $provider/csd:demographic/csd:contactPoint/csd:codedType[@code = "BP" and @codingScheme="urn:ihe:iti:csd:2013:contactPoint"]
  let $clean_phones :=
    for $phone in $all_phones
    let $raw_phones := tokenize($phone/text(),'[/\s]+')
    let $normalized_phones := 
      for $raw_phone in $raw_phones
      let $clean_phone := replace($raw_phone,'[^\d\+]/', '')
      return 
	if (string-length($clean_phone) = 0)
	then ()
        else 
	  if (starts-with($clean_phone,'0'))
	  then concat($country_code, substring($clean_phone,2))
	  else $clean_phone
    return $normalized_phones
  return $clean_phones
}

let $clean_providers := 
  for $provider in /csd:CSD/csd:providerDirectory/csd:provider
  let $clean_phones := $get_clean_phones($provider)
  where count($clean_phones) > 0
  return $provider

let $cellcom_providers := 
  for $provider in $clean_providers 
  let $clean_phones := $get_clean_phones($provider)
  let $cellcom_phones := 
    for $clean_phone in $clean_phones
    return 
      if (starts-with($clean_phone,concat($country_code,$cellcom_code))) 
      then $clean_phone
      else ()
  where (count($cellcom_phones) > 0)
  return $provider

let $multi_providers :=
  for $provider in $clean_providers
  let $clean_phones := $get_clean_phones($provider)
  where (count($clean_phones) > 1)
  return $provider

return 
  <div>
    <ul>
      <li>Total number of health workers: {count(/csd:CSD/csd:providerDirectory/csd:provider)}</li>
      <li>Total number of health workers with phone: {count($clean_providers)}</li>
      <li>Total number of health workers with cellcom phone: {count($cellcom_providers)}</li>
      <li>Total number of health workers with multiple phones: {count($multi_providers)}</li>      
    </ul>
  </div>


Content-Type: application/json
URN: urn:openhie.org:openinfoman-rapidpro:select_facility

Description



 
    Creates selection menu of all the CSD organizations and faciltiies with a specified parent.  Designed as part of heierarchical facility selection menus in RapidPro webhooks.
  

Definition

Source:
import module namespace functx = "http://www.functx.com";
declare namespace csd = "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;

let $t_top_orgid  :=  $careServicesRequest/query[@name = 'orgid']/text()
let $top_org :=  (/csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $t_top_orgid])[1]



let $selected_text := $careServicesRequest/query[@name = 'input']/text()
(:
let $selections := $careServicesRequest/values/item[./pair[@name = 'label' and ./text() = 'facilityselection']]
let $selected_text := 
  if (count($selections) > 0)
  then
    let $max_time := max(for $time in $selections/pair[@name='time'] return xs:dateTime($time))
    return $selections[./pair[@name = 'time'] = $max_time]/pair[@name='text']/text()
   else ()
:)

let $selected_index := if ($selected_text castable as xs:integer) then xs:integer($selected_text) else -1


let $selected :=
  if ($selected_index = 0)
  then
    /csd:CSD/csd:organizationDirectory/csd:organization[
      @entityID = /csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $top_org/@entityID]/csd:parent/@entityID
    ]
  else if ($selected_index > 0)
  then
    let $select_orgs := 
      if (exists($top_org))
      then /csd:CSD/csd:organizationDirectory/csd:organization[./csd:parent/@entityID = $top_org/@entityID]
      else /csd:CSD/csd:organizationDirectory/csd:organization[count(./csd:parent[@entityID]) = 0]
	
    let $select_facs := 
      if (exists($top_org))
      then /csd:CSD/csd:facilityDirectory/csd:facility[./csd:organizations/csd:organization[@entityID = $top_org/@entityID]]
      else ()
    return ($select_orgs,$select_facs)[ $selected_index]
  else ()

    
let $orgs := 
  if (exists($selected) and  local-name($selected ) = 'organization')
  then /csd:CSD/csd:organizationDirectory/csd:organization[./csd:parent/@entityID = $selected/@entityID]
  else /csd:CSD/csd:organizationDirectory/csd:organization[count(./csd:parent[@entityID]) = 0]
	
let $facs := 
  if (exists($selected) and  local-name($selected ) = 'organization')
  then /csd:CSD/csd:facilityDirectory/csd:facility[./csd:organizations/csd:organization[@entityID = $selected/@entityID]]
  else ()



let $output := 
  if ( local-name($selected ) = 'facility')
  then
    <json type='object'>
      <menutext>You already selected a facility.  You shouldn't see this message</menutext>
      <facility>{string($selected/@entityID)}</facility>
      <facilityname>{$selected/csd:primaryName/text()}</facilityname>
    </json>
  else  if ( local-name($selected ) = 'organization'  or not(exists($selected)))
  then
    <json type='object'>
      <menutext >
 	{ if (exists($selected)) then concat("In ", $selected/csd:primaryName/text(), ". ")  else () }
	{
	  let $parent := (/csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $selected/csd:parent/@entityID])[1]/csd:primaryName/text()
	  return
	    if ($parent)
	    then concat("O) Up to ", $parent, " ") 
	    else ()
        }
	{ for $ent at $pos in ($orgs,$facs)  return concat( $pos, ") ", $ent/csd:primaryName/text())    }
      </menutext>
      <orgid>{if (exists($selected)) then string($selected/@entityID) else ()}</orgid>
      <facility/>
      <facilityname/>      
    </json>
   else <json type='object'/>

(:return $output :)
return json:serialize($output,map{'format':'direct'})