Clearing the Active Patient

tmccormi wrote on Tuesday, October 30, 2012:

Several people have asked for this.  The code looks simple.   Check me if I’m missing something here.  Javascript is not my forte’…

If there is consensus I’ll update the 4.1.2-dev with it.

What I propose that hitting the “home” link in the upper right title bar clears the active patient and encounter

From - left_nav.php

function genFindBlock() {
 function goHome() {
     top.frames['RTop'].location='messages/messages.php?form_active=1';
     top.frames['RBot'].location='finder/dynamic_finder.php';
+    top.frames['Title'].location.reload();
+       document.location.reload();
 }

sunsetsystems wrote on Tuesday, October 30, 2012:

You’re reloading left_nav?  That doesn’t look right… it’s destructive to other state information in there, and you also need to clear pid and encounter from the session data.  Suggest you look at the code involved in selecting a new patient, and work from that.  And definitely post up for review as this seems non-trivial.

Rod
www.sunsetsystems.com

tmccormi wrote on Tuesday, October 30, 2012:

Thanks for the feedback, that’s why I didn’t do anything else, it  seemed too simple…  :slight_smile:

edit_globals.php seems to do something different and equally simple the clears the active patient/encounter.  Is that model OK or is ti doing bad things too?

<?php if ($_GET['mode'] == "user") { ?>
  <form method='post' name='theform' id='theform' action='edit_globals.php?mode=user' onsubmit='return top.restoreSession()'>
<?php } else { ?>
  <form method='post' name='theform' id='theform' action='edit_globals.php' onsubmit='return top.restoreSession()'>
<?php } ?>

-Tony

sunsetsystems wrote on Tuesday, October 30, 2012:

Not sure what you mean about edit_globals.php.  Suggest you look at interface/patient_file/summary/demographics.php.  Search for all occurrences of the string “set_pid” and look at the surrounding code.  There is some PHP code as well as some javaScript that must be run.  But instead of the JS setPatient() function I think you’ll want to call clearPatient().  This will need to be done with some understanding of what is going on.

Rod
www.sunsetsystems.com

tmccormi wrote on Tuesday, October 30, 2012:

Thanks,
  Is there documentation on this whole interaction somewhere.  As I have said I’m not a javascript guy.  and adding the complex interaction of the titlebar, left_nav and main frames just makes it virtually impossible for me to figure out.  I know you and yehster get it.

Please share your knowledge on the architecture, in the form of some formal documentation on the wiki.

-Tony

tmccormi wrote on Tuesday, October 30, 2012:

Re: edit_globals.php - Just this, when you make changes to the globals settings and click save.  The active patient appears to be cleared.  Yet there are no obvious way (to me) that that happens, there is not a call to clearPatient() or anything like that.    So I’m worried now that using that menu item without some kind of logoff/on may cause some issues such as you describe in reloading left_nav.

-Tony

yehster wrote on Tuesday, October 30, 2012:

My functionality is temporarily reduced due to Sandy, but there is also a server side component you need to deal with. Since there is at least patID saved as a session variable, if you don’t clear that, some weirdness could ensue.

What it looks like you are doing so far is only just trying to clear the client side info via JavaScript.

If you dont’t clear the sever side state too, it’s possible that future entries could go to the wrong patient.

yehster wrote on Tuesday, October 30, 2012:

You also need to clear the active encounter session variable.
On the client side, resetting to “no patient” needs to deactivate more menus than switching patients. For example, new encounter is still enabled when switching patients, but needs to be disabled if you are clearing. I think you can probably set the patient ID tracked in JavaScript to zero, but I’d need to take a closer look at the code.

bradymiller wrote on Wednesday, October 31, 2012:

Hi,
In the edit_globals.php doing reload on all the frames seems to do what Tony is looking for:

  echo "<script type='text/javascript'>";
  echo "parent.left_nav.location.reload();";
  echo "parent.Title.location.reload();";
  echo "if(self.name=='RTop'){";
  echo "parent.RBot.location.reload();";
  echo "}else{";
  echo "parent.RTop.location.reload();";
  echo "}";
  echo "self.location.href='edit_globals.php?mode=user&unique=yes';";
  echo "</script>";

(above is from edit_globals; obviously would need to be adapted if click is taken from the Title frame)

-brady
OpenEMR

sunsetsystems wrote on Wednesday, October 31, 2012:

Brady I don’t see where that’s going to clear the (server-side) session pid/encounter.  Am I missing something?

Rod
www.sunsetsystems.com

tmccormi wrote on Wednesday, October 31, 2012:

This is what we ended up with.  Thoguhts?
-Tony

diff -git a/interface/main/left_nav.php b/interface/main/left_nav.php

index 73d035b..7caed8f 100644
--- a/interface/main/left_nav.php
+++ b/interface/main/left_nav.php
@@ -548,12 +548,18 @@ function genFindBlock() {
  }
 
 function goHome() {
-       top.frames['RTop'].location='<?php echo $GLOBALS['default_top_pane']?>';
-    top.frames['RBot'].location='messages/messages.php?form_active=1';
-    top.frames['Title'].location.reload();
-       document.location.reload();
-       //window.top.reload();
+       $.ajax({
+         type: "POST",
+         url: "<?php echo $GLOBALS['webroot'] ?>/library/ajax/unset_session_ajax.php",
+         data: { func: "unset_pid"},
+         success:function( msg ) {
+               clearPatient();
+               top.frames['RTop'].location='<?php echo $GLOBALS['default_top_pane']?>';
+               top.frames['RBot'].location='messages/messages.php?form_active=1';
+         }
+       });
     
+
 }
 
  // Reference to the search.php window.
diff --git a/library/ajax/unset_session_ajax.php b/library/ajax/unset_session_ajax.php
new file mode 100644
index 0000000..4bef59c
--- /dev/null
+++ b/library/ajax/unset_session_ajax.php
@@ -0,0 +1,8 @@
+<?php
+require_once("../../interface/globals.php");
+if(($_POST['func']=="unset_pid"))
+{
+       if(isset($_SESSION['pid'])){unset($_SESSION['pid']);}
+       if(isset($_SESSION['encounter'])){unset($_SESSION['encounter']);}
+}
+?>

tmccormi wrote on Wednesday, October 31, 2012:

So it looks like edit_globals.php has the same problem that my original solution proposed and should also be fixed.
-Tony

yehster wrote on Wednesday, October 31, 2012:

My first thought is it’s easier to review code on github than in the forum.

That said, calling document reload right before your Ajax call is likely to result in a race condition. It’s probably better to do the reloads in the Ajax callback after the session stuff gets cleared server side rather than before.

I don’t see where clearPatient is defined. Does that already exist in left_nav.php?

tmccormi wrote on Wednesday, October 31, 2012:

Yes, agreed, but I didn’t have to pull it over into a formal branch this morning and it’s a really short set of code.
clearPatient() is a function in left_nav
-Tony

tmccormi wrote on Wednesday, October 31, 2012:

that should have said “time to pull it over” - I am not the coder here, Visolve is…  I’m just the guy coordinating the change.
-Tony

sunsetsystems wrote on Wednesday, October 31, 2012:

I suggest NOT reloading left_nav.  In theory clearPatient() (yes that does exist) should do what’s necessary on the client side, and if it doesn’t then that is the place to fix it.  You could move the ajax call into there too, that makes it easier for other places that want to do this.

Alternatively, skip the ajax call and add the PHP code to one of the reloaded scripts.  Either way, suggest you include pid.inc and call set_pid(0) to clear the session pid and encounter… it does logging, and it’s always better to use a common function when it applies.

You probably don’t need to reload any frames at all.  I think clearPatient() resets what matters (or used to, anyway) in the title frame.  If it doesn’t, it should.  Note also it calls reloadPatient() which attempts to reload the top and bottom frames only when necessary; not sure how perfectly that works, but in theory it should.  I’d like to see that approach preserved because other things depend on it, and reloading the same frame twice is pretty silly.

And yeah, using github would be nice.  :slight_smile:

Rod
www.sunsetsystems.com

yehster wrote on Wednesday, October 31, 2012:

Definitely agree that you should use setPid(0) instead of directly unsetting the session variables, that’s a much cleaner abstraction.

Picking nits here, but if the implemented function is truly meant to be “Go HOME” then it should explicitly reset the Top and Bottom frames, to their initial states at login.  However, if it is meant to be just “clear patient” then it should leave any non patient specific frames alone per Rod’s description of the functionality:

attempts to reload the top and bottom frames only when necessary;

e.g. If you “clear patient” while the “Billing” screen is up, it should leave billing alone.

tmccormi wrote on Wednesday, October 31, 2012:

All good feed back.   I’m going to get out of the middle and let the Visolve developer get this sorted out.

However:  In light of the Go Home vs Clear Patient,  I felt that “Home” was the right place to do that, clear the patient and return to the main base screen config, seems natural.   I can see an argument for a separate Clear Patient button that does not “go Home” however and would be OK with that or some global that options either.

Should it, in fact, in either case, give an alert about “Unsaved Data” before clearing and/or going home?

It’s these seemingly little things that always have complications :slight_smile:

Tony
www.mi-squared.com / @tonymi2
oemr.org / @OEMR_org

bradymiller wrote on Thursday, November 01, 2012:

Hi,

The Go Home idea will be much easier (since then don’t need to ensure the currently loaded screens are “safe” after resetting the patient/encounter ids). And Rod, not sure how the code in globals is working, but it is basically reloading everything, which on testing appears to reset the patient/encounter ids.

-brady
OpenEMR

tmccormi wrote on Friday, November 02, 2012:

Whatever we do it should be consistent … :slight_smile:
-Tony