Skip to content

Commit

Permalink
Merge pull request I-TECH#163 from patryllus/screening-forms
Browse files Browse the repository at this point in the history
Screening forms
  • Loading branch information
Ojwang' Antony authored Feb 7, 2019
2 parents a93df3f + 5f37aad commit 6e8271a
Show file tree
Hide file tree
Showing 37 changed files with 2,929 additions and 959 deletions.
3 changes: 3 additions & 0 deletions api/src/main/distro/metadata/concepts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,7 @@
<ref key="IPT_PROGRAM" uuid="164128AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"/>
<ref key="FRIEND" uuid="5618AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"/>
<ref key="CO_WORKER" uuid="160237AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />
<ref key= "ARV_TREATMENT_PLAN_EVENT_CONCEPT" uuid ="1255AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />
<ref key= "START_DRUGS" uuid="1256AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />
<ref key= "TB_TREATMENT_PLAN_CONCEPT" uuid="1268AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />
</refs>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.joda.time.Days;
import org.joda.time.Minutes;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Program;
import org.openmrs.api.context.Context;
Expand All @@ -27,13 +28,15 @@
import org.openmrs.module.kenyaemr.Dictionary;
import org.openmrs.module.kenyaemr.calculation.BaseEmrCalculation;
import org.openmrs.module.kenyaemr.calculation.EmrCalculationUtils;
import org.openmrs.module.kenyaemr.metadata.HivMetadata;
import org.openmrs.module.kenyaemr.metadata.TbMetadata;
import org.openmrs.module.kenyaemr.util.EncounterBasedRegimenUtils;
import org.openmrs.module.metadatadeploy.MetadataUtils;
import org.openmrs.ui.framework.SimpleObject;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
Expand All @@ -47,13 +50,17 @@
public class GreenCardVelocityCalculation extends BaseEmrCalculation {

protected static final Log log = LogFactory.getLog(GreenCardVelocityCalculation.class);

static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy");
@Override
public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) {

Set<Integer> alive = Filters.alive(cohort, context);
//Check whether in tb program
Program tbProgram = MetadataUtils.existing(Program.class, TbMetadata._Program.TB);
Program hivProgram = MetadataUtils.existing(Program.class, HivMetadata._Program.HIV);

Set<Integer> inTbProgram = Filters.inProgram(tbProgram, alive, context);
Set<Integer> inHivProgram = Filters.inProgram(hivProgram, alive, context);
//Check whether in tb greencard
Concept OnAntiTbQuestion = Context.getConceptService().getConcept(164948);
Concept StartAntiTbQuestion = Context.getConceptService().getConcept(162309);
Expand All @@ -65,11 +72,16 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
Concept IptCurrentQuestion = Context.getConceptService().getConcept(164949);
Concept IptStartQuestion = Context.getConceptService().getConcept(1265);
Concept IptStopQuestion = Context.getConceptService().getConcept(160433);
//Viral load
Concept latestVL = Dictionary.getConcept(Dictionary.HIV_VIRAL_LOAD);
Concept LDLQuestion = Context.getConceptService().getConcept(1305);
Concept LDLAnswer = Context.getConceptService().getConcept(1302);

CalculationResultMap iptCurrent = Calculations.lastObs(IptCurrentQuestion, cohort, context);
CalculationResultMap iptStarted = Calculations.lastObs(IptStartQuestion, cohort, context);
CalculationResultMap iptStopped = Calculations.lastObs(IptStopQuestion, cohort, context);

CalculationResultMap lastVLObs = Calculations.lastObs(latestVL, inHivProgram, context);
CalculationResultMap lastLDLObs = Calculations.lastObs(LDLQuestion, inHivProgram, context);

// Get active ART regimen of each patient
Concept arvs = Dictionary.getConcept(Dictionary.ANTIRETROVIRAL_DRUGS);
Expand All @@ -90,15 +102,25 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
//IPT Calculation
Date iptStartObsDate = null;
Date iptStopObsDate = null;
Date iptStartDate = null;
Date tbStartObsDate = null;
Date tbStopObsDate = null;
Date currentDate =new Date();
boolean inIptProgram = false;
boolean currentInIPT = false;
boolean patientInIPT6Months = false;
Integer iptStartStopDiff = 0;
Integer iptCompletionDays = 0;
//ART calculations
String artStartObsDate = null;
Date artStartDate = null;
Integer tbStartStopDiff = 0;
Integer artStartCurrDiff = 0;

String regimenName = null;
Obs lastVLObsResult = null;
String ldlResult = null;
Double vlResult = 0.0;
//Patient with current on anti tb drugs and/or anti tb start dates
Obs tbCurrentObs = EmrCalculationUtils.obsResultForPatient(tbCurrent, ptId);
Obs tbStartObs = EmrCalculationUtils.obsResultForPatient(tbStarted, ptId);
Expand All @@ -108,6 +130,40 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
Obs iptStartObs = EmrCalculationUtils.obsResultForPatient(iptStarted, ptId);
Obs iptStopObs = EmrCalculationUtils.obsResultForPatient(iptStopped, ptId);

//Viral Load
Double vl = EmrCalculationUtils.numericObsResultForPatient(lastVLObs, ptId);
Concept ldl = EmrCalculationUtils.codedObsResultForPatient(lastLDLObs, ptId);

// get latest of ldl or vl
if (ldl != null && vl != null) {
Obs vlObs = EmrCalculationUtils.obsResultForPatient(lastVLObs, ptId);
Obs ldlObs = EmrCalculationUtils.obsResultForPatient(lastLDLObs, ptId);
lastVLObsResult = EmrCalculationUtils.findLastOnOrBefore(Arrays.asList(vlObs, ldlObs), context.getNow());
if (lastVLObsResult != null && lastVLObsResult.getConcept() == latestVL){
vlResult = lastVLObsResult.getValueNumeric();
ldlResult = null;
}
else if (lastVLObsResult != null && (lastVLObsResult.getConcept() == LDLQuestion && lastVLObsResult.getValueCoded() == LDLAnswer)){
ldlResult = "LDL";
vlResult = 0.0;
}
} else if (ldl != null && vl == null) {
lastVLObsResult = EmrCalculationUtils.obsResultForPatient(lastLDLObs, ptId);
if(lastVLObsResult != null && (lastVLObsResult.getConcept() == LDLQuestion && lastVLObsResult.getValueCoded() == LDLAnswer)) {
ldlResult = "LDL";
vlResult = 0.0;
}
} else if (ldl == null && vl != null) {
lastVLObsResult = EmrCalculationUtils.obsResultForPatient(lastVLObs, ptId);
if(lastVLObsResult != null && lastVLObsResult.getConcept() == latestVL) {
vlResult = lastVLObsResult.getValueNumeric();
ldlResult = null;
}
}else if (ldl == null && vl == null) {
vlResult = 0.0;
ldlResult = null;
}

//Enrolled in tb program
if (inTbProgram.contains(ptId)) {
patientInTBProgram = true;
Expand Down Expand Up @@ -136,23 +192,57 @@ public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Obj
inIptProgram = true;
}
}
ListResult patientDrugOrders = (ListResult) currentARVDrugOrders.get(ptId);
if (patientDrugOrders != null) {
patientOnART = true;
//have completed 6 months IPT
Obs iptObs = EmrCalculationUtils.obsResultForPatient(iptStarted, ptId);
if (iptObs != null) {

iptStartDate = iptObs.getObsDatetime();
iptCompletionDays = daysBetween(currentDate, iptStartDate);
}

Date orderDate = EmrCalculationUtils.datetimeResultForPatient(earliestOrderDates, ptId);
if (orderDate != null) {
artStartCurrDiff = daysBetween(currentDate,orderDate);
if (artStartCurrDiff > 3) {
hasBeenOnART = true;
if (iptObs != null && iptObs.getValueCoded().getConceptId().equals(1065) && iptCompletionDays >= 182) {
patientInIPT6Months = true;
}

//On ART -- find if client has active ART
Encounter lastDrugRegimenEditorEncounter = EncounterBasedRegimenUtils.getLastEncounterForCategory(Context.getPatientService().getPatient(ptId), "ARV"); //last DRUG_REGIMEN_EDITOR encounter
if (lastDrugRegimenEditorEncounter != null) {
SimpleObject o = EncounterBasedRegimenUtils.buildRegimenChangeObject(lastDrugRegimenEditorEncounter.getAllObs(), lastDrugRegimenEditorEncounter);
regimenName = o.get("regimenShortDisplay").toString();
if (regimenName != null) {
patientOnART = true;
}
}

Encounter firstDrugRegimenEditorEncounter = EncounterBasedRegimenUtils.getFirstEncounterForCategory(Context.getPatientService().getPatient(ptId), "ARV"); //first DRUG_REGIMEN_EDITOR encounter
if (firstDrugRegimenEditorEncounter != null) {
SimpleObject o = EncounterBasedRegimenUtils.buildRegimenChangeObject(firstDrugRegimenEditorEncounter.getAllObs(), firstDrugRegimenEditorEncounter);
artStartObsDate =o.get("startDate").toString();
if (artStartObsDate != null) {
try {
artStartDate = DATE_FORMAT.parse(artStartObsDate);
artStartCurrDiff = daysBetween(currentDate,artStartDate);
if (artStartCurrDiff > 3) {
hasBeenOnART = true;
}
} catch (ParseException e) {
e.printStackTrace();
}

}
}

sb.append("inIPT:").append(inIptProgram).append(",");
sb.append("inTB:").append(patientInTBProgram).append(",");
sb.append("dueTB:").append(patientDueForTBEnrollment).append(",");
sb.append("onART:").append(patientOnART).append(",");
sb.append("hasBeenOnART:").append(hasBeenOnART);
sb.append("hasBeenOnART:").append(hasBeenOnART).append(",");
sb.append("regimenName:").append(regimenName).append(",");
sb.append("duration:").append(artStartCurrDiff).append(",");
sb.append("vlResult:").append(vlResult).append(",");
sb.append("ldlResult:").append(ldlResult).append(",");
sb.append("iptCompleted:").append(patientInIPT6Months);
// sb.append("artStartDate:").append(artStartDate).append(",");

ret.put(ptId, new SimpleResult(sb.toString(), this, context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,45 @@
*/
package org.openmrs.module.kenyaemr.calculation.library.hiv.art;

import org.openmrs.Concept;
import org.openmrs.DrugOrder;
import org.openmrs.Encounter;
import org.openmrs.api.context.Context;
import org.openmrs.calculation.patient.PatientCalculationContext;
import org.openmrs.calculation.result.CalculationResultMap;
import org.openmrs.calculation.result.ListResult;
import org.openmrs.calculation.result.SimpleResult;
import org.openmrs.module.kenyacore.calculation.CalculationUtils;
import org.openmrs.module.kenyaemr.Dictionary;
import org.openmrs.module.kenyaemr.calculation.BaseEmrCalculation;
import org.openmrs.module.kenyaemr.regimen.RegimenOrder;
import org.openmrs.module.kenyaemr.util.EncounterBasedRegimenUtils;
import org.openmrs.ui.framework.SimpleObject;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

/**
* Calculates the current ART regimen of each patient as a list of drug orders. Returns empty list if patient is not on ART
*/
public class CurrentArtRegimenCalculation extends BaseEmrCalculation {

/**
* @see org.openmrs.calculation.patient.PatientCalculation#evaluate(java.util.Collection,
* java.util.Map, org.openmrs.calculation.patient.PatientCalculationContext)
* java.util.Map, org.openmrs.calculation.patient.PatientCalculationContext)
*/
@Override
public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues,
PatientCalculationContext context) {
Concept arvs = Dictionary.getConcept(Dictionary.ANTIRETROVIRAL_DRUGS);
CalculationResultMap currentARVDrugOrders = activeDrugOrders(arvs, cohort, context);
PatientCalculationContext context) {

String regimenName = null;
CalculationResultMap ret = new CalculationResultMap();
for (Integer ptId : cohort) {
ListResult patientDrugOrders = (ListResult) currentARVDrugOrders.get(ptId);
Encounter lastDrugRegimenEditorEncounter = EncounterBasedRegimenUtils.getLastEncounterForCategory(Context.getPatientService().getPatient(ptId), "ARV"); //last DRUG_REGIMEN_EDITOR encounter

if (patientDrugOrders != null) {
RegimenOrder regimen = new RegimenOrder(new HashSet<DrugOrder>(CalculationUtils.<DrugOrder>extractResultValues(patientDrugOrders)));
ret.put(ptId, new SimpleResult(regimen, this, context));
}
else {
ret.put(ptId, null);
if (lastDrugRegimenEditorEncounter != null) {
SimpleObject o = EncounterBasedRegimenUtils.buildRegimenChangeObject(lastDrugRegimenEditorEncounter.getAllObs(), lastDrugRegimenEditorEncounter);
regimenName = o.get("regimenShortDisplay").toString();

if (regimenName != null) {
ret.put(ptId, new SimpleResult(regimenName, this, context));
} else {
ret.put(ptId, null);
}
}
}
return ret;
Expand Down
Loading

0 comments on commit 6e8271a

Please sign in to comment.