Skip to content

Commit c9d9bd3

Browse files
authored
HTML-881: add restrictToSessionVisitLocation parameter to restrict visit locations (#331)
1 parent 595bb27 commit c9d9bd3

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

api-tests/src/test/java/org/openmrs/module/htmlformentry/EncounterLocationTagTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,81 @@ public void shouldRestrictToVisitLocationAndChildrenIfRestrictToVisitLocationEna
241241
TestUtil.assertFuzzyContains("Jamaica Plain", htmlToDisplay);
242242
}
243243

244+
@Test
245+
public void shouldRestrictToSessionVisitLocationAndChildrenIfRestrictToSessionVisitLocationEnabledAndUserLoginLocationIsVisitLocation()
246+
throws Exception {
247+
executeVersionedDataSet("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml");
248+
249+
// "outpatient" (id=1002) is itself tagged as a Visit Location
250+
Location loginLocation = Context.getLocationService().getLocation(1002);
251+
Context.getUserContext().setLocation(loginLocation);
252+
253+
String htmlform = "<htmlform><encounterLocation restrictToSessionVisitLocation=\"true\"/></htmlform>";
254+
FormEntrySession session = new FormEntrySession(null, htmlform, null);
255+
256+
String htmlToDisplay = session.getHtmlToDisplay();
257+
258+
// "outpatient" and its descendants should be included
259+
TestUtil.assertFuzzyContains("out patient", htmlToDisplay);
260+
TestUtil.assertFuzzyContains("pharmacy", htmlToDisplay);
261+
TestUtil.assertFuzzyContains("dentist room", htmlToDisplay);
262+
TestUtil.assertFuzzyContains("nurse's room", htmlToDisplay);
263+
TestUtil.assertFuzzyContains("dispensary", htmlToDisplay);
264+
265+
// ancestors and locations outside the subtree should not appear
266+
TestUtil.assertFuzzyDoesNotContain("amani hospital", htmlToDisplay);
267+
TestUtil.assertFuzzyDoesNotContain("in patient", htmlToDisplay);
268+
TestUtil.assertFuzzyDoesNotContain("opd", htmlToDisplay);
269+
TestUtil.assertFuzzyDoesNotContain("casuality", htmlToDisplay);
270+
TestUtil.assertFuzzyDoesNotContain("intensive care", htmlToDisplay);
271+
}
272+
273+
@Test
274+
public void shouldRestrictToNearestVisitLocationAncestorIfRestrictToSessionVisitLocationEnabledAndUserLoginLocationIsNotVisitLocation()
275+
throws Exception {
276+
executeVersionedDataSet("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml");
277+
278+
// "intensive care" (id=1010) is NOT tagged as a Visit Location; its parent "in patient" (id=1003) is
279+
Location loginLocation = Context.getLocationService().getLocation(1010);
280+
Context.getUserContext().setLocation(loginLocation);
281+
282+
String htmlform = "<htmlform><encounterLocation restrictToSessionVisitLocation=\"true\"/></htmlform>";
283+
FormEntrySession session = new FormEntrySession(null, htmlform, null);
284+
285+
String htmlToDisplay = session.getHtmlToDisplay();
286+
287+
// the nearest visit-location ancestor ("in patient") and its descendants should be included
288+
TestUtil.assertFuzzyContains("in patient", htmlToDisplay);
289+
TestUtil.assertFuzzyContains("casuality", htmlToDisplay);
290+
TestUtil.assertFuzzyContains("intensive care", htmlToDisplay);
291+
292+
TestUtil.assertFuzzyDoesNotContain("amani hospital", htmlToDisplay);
293+
TestUtil.assertFuzzyDoesNotContain("out patient", htmlToDisplay);
294+
TestUtil.assertFuzzyDoesNotContain("opd", htmlToDisplay);
295+
TestUtil.assertFuzzyDoesNotContain("pharmacy", htmlToDisplay);
296+
TestUtil.assertFuzzyDoesNotContain("dentist room", htmlToDisplay);
297+
}
298+
299+
@Test
300+
public void shouldNotRestrictLocationsIfRestrictToSessionVisitLocationEnabledButUserHasNoLoginLocation()
301+
throws Exception {
302+
executeVersionedDataSet("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml");
303+
304+
Context.getUserContext().setLocation(null);
305+
306+
String htmlform = "<htmlform><encounterLocation restrictToSessionVisitLocation=\"true\"/></htmlform>";
307+
FormEntrySession session = new FormEntrySession(null, htmlform, null);
308+
309+
String htmlToDisplay = session.getHtmlToDisplay();
310+
311+
// no restriction applied - all non-retired locations should be present
312+
TestUtil.assertFuzzyContains("amani hospital", htmlToDisplay);
313+
TestUtil.assertFuzzyContains("out patient", htmlToDisplay);
314+
TestUtil.assertFuzzyContains("in patient", htmlToDisplay);
315+
TestUtil.assertFuzzyContains("opd", htmlToDisplay);
316+
TestUtil.assertFuzzyContains("intensive care", htmlToDisplay);
317+
}
318+
244319
@Test
245320
public void getAllVisitsAndChildLocations_shouldReturnAllVisitsAndTheirChildLocations() throws Exception {
246321
executeVersionedDataSet("org/openmrs/module/htmlformentry/data/encounterLocationTest.xml");

api/src/main/java/org/openmrs/module/htmlformentry/element/EncounterDetailSubmissionElement.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,20 @@ else if (parameters.get("order") != null) {
374374
locations = HtmlFormEntryUtil.removeLocationsNotEqualToOrDescendentOf(locations,
375375
((Visit) context.getVisit()).getLocation());
376376
}
377+
378+
// restrict to visit locations associated with the user login location
379+
if (parameters.get("restrictToSessionVisitLocation") != null
380+
&& "true".equalsIgnoreCase(parameters.get("restrictToSessionVisitLocation").toString())
381+
) {
382+
Location loginLocation = Context.getUserContext().getLocation();
383+
if (loginLocation != null) {
384+
//find the nearest Visit location
385+
Location visitLocation = HtmlFormEntryUtil.getFirstAncestorWithTag(loginLocation, HtmlFormEntryUtil.getLocationTag("Visit Location"));
386+
if (visitLocation != null) {
387+
locations = HtmlFormEntryUtil.removeLocationsNotEqualToOrDescendentOf(locations, visitLocation);
388+
}
389+
}
390+
}
377391

378392
// now create the actual location options
379393
for (Location location : locations) {

api/src/main/java/org/openmrs/module/htmlformentry/element/ObsSubmissionElement.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,17 @@ private void prepareWidgets(T context, Map<String, String> parameters) {
536536
HtmlFormEntryUtil.removeLocationsNotEqualToOrDescendentOf(locationList,
537537
((Visit) context.getVisit()).getLocation());
538538
}
539+
// if "restrictToSessionVisitLocation" parameter remove all locations that aren't children of the login Visit location
540+
if ("true".equalsIgnoreCase(parameters.get("restrictToSessionVisitLocation"))) {
541+
Location loginLocation = Context.getUserContext().getLocation();
542+
if (loginLocation != null) {
543+
//find the nearest Visit location
544+
Location visitLocation = HtmlFormEntryUtil.getFirstAncestorWithTag(loginLocation, HtmlFormEntryUtil.getLocationTag("Visit Location"));
545+
if (visitLocation != null) {
546+
HtmlFormEntryUtil.removeLocationsNotEqualToOrDescendentOf(locationList, visitLocation);
547+
}
548+
}
549+
}
539550

540551
List<Option> locationOptions = new ArrayList<>();
541552
for (Location location : locationList) {

0 commit comments

Comments
 (0)