2 /** @package Patient Portal::Controller */
6 * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
8 * LICENSE: This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * @author Jerry Padgett <sjpadgett@gmail.com>
23 * @link http://www.open-emr.org
27 * import supporting libraries
29 require_once ( "AppBaseController.php" );
30 require_once ( "Model/Patient.php" );
33 * PatientController is the controller class for the Patient object.
35 * controller is responsible for processing input from the user, reading/updating
36 * the model as necessary and displaying the appropriate view.
38 * @package Patient Portal::Controller
39 * @author ClassBuilder
42 class PatientController
extends AppBaseController
{
45 * Override here for any controller-specific functionality
49 protected function Init(){
51 // require_once ( '../lib/appsql.class.php' );
53 // $this->RequirePermission(SecureApp::$PERMISSION_USER,'SecureApp.LoginForm');
57 * Displays a list view of Patient objects
59 public function ListView(){
60 $rid = $pid = $user = $encounter = 0;
61 if( isset( $_GET['id'] ) ) $rid = ( int ) $_GET['id'];
62 if( isset( $_GET['pid'] ) ) $pid = ( int ) $_GET['pid'];
63 if( isset( $_GET['user'] ) ) $user = $_GET['user'];
64 if( isset( $_GET['enc'] ) ) $encounter = $_GET['enc'];
65 $this->Assign( 'recid', $rid );
66 $this->Assign( 'cpid', $pid );
67 $this->Assign( 'cuser', $user );
68 $this->Assign( 'encounter', $encounter );
69 $ptdata = $this->startupQuery($pid);
70 foreach( $ptdata[0] as $key => $v){
71 $trow[lcfirst($key)] = $v;
73 $this->Assign( 'trow', $trow);
77 * API Method queries for startup Patient records and return as php
79 public function startupQuery($pid){
81 $criteria = new PatientCriteria();
82 $recnum = ( int ) $pid;
83 $criteria->Pid_Equals
= $recnum;
85 $output = new stdClass();
87 $patientdata = $this->Phreezer
->Query( 'PatientReporter', $criteria );
88 $output->rows
= $patientdata->ToObjectArray( false, $this->SimpleObjectParams() );
89 $output->totalResults
= count( $output->rows
);
91 } catch( Exception
$ex ){
92 $this->RenderExceptionJSON( $ex );
96 * API Method queries for Patient records and render as JSON
98 public function Query(){
100 $criteria = new PatientCriteria();
101 $pid = RequestUtil
::Get( 'patientId' );
102 $criteria->Pid_Equals
= $pid;
104 $output = new stdClass();
106 // if a sort order was specified then specify in the criteria
107 $output->orderBy
= RequestUtil
::Get( 'orderBy' );
108 $output->orderDesc
= RequestUtil
::Get( 'orderDesc' ) != '';
109 if( $output->orderBy
) $criteria->SetOrder( $output->orderBy
, $output->orderDesc
);
111 $page = RequestUtil
::Get( 'page' );
113 // return all results
114 $patientdata = $this->Phreezer
->Query( 'PatientReporter', $criteria );
115 $output->rows
= $patientdata->ToObjectArray( true, $this->SimpleObjectParams() );
116 $output->totalResults
= count( $output->rows
);
117 $output->totalPages
= 1;
118 $output->pageSize
= $output->totalResults
;
119 $output->currentPage
= 1;
121 $this->RenderJSON( $output, $this->JSONPCallback() );
122 } catch( Exception
$ex ){
123 $this->RenderExceptionJSON( $ex );
128 * API Method retrieves a single Patient record and render as JSON
130 public function Read(){
132 $pk = $this->GetRouter()->GetUrlParam( 'id' );
133 $patient = $this->Phreezer
->Get( 'Patient', $pk );
134 $this->RenderJSON( $patient, $this->JSONPCallback(), true, $this->SimpleObjectParams() );
135 } catch( Exception
$ex ){
136 $this->RenderExceptionJSON( $ex );
141 * API Method inserts a new Patient record and render response as JSON
143 public function Create(){
146 $json = json_decode( RequestUtil
::GetBody() );
148 if( ! $json ){throw new Exception( 'The request body does not contain valid JSON' );}
150 $patient = new Patient( $this->Phreezer
);
152 // this is an auto-increment. uncomment if updating is allowed
153 // $patient->Id = $this->SafeGetVal($json, 'id');
155 $patient->Title
= $this->SafeGetVal( $json, 'title', $patient->Title
);
156 $patient->Language
= $this->SafeGetVal( $json, 'language', $patient->Language
);
157 $patient->Financial
= $this->SafeGetVal( $json, 'financial', $patient->Financial
);
158 $patient->Fname
= $this->SafeGetVal( $json, 'fname', $patient->Fname
);
159 $patient->Lname
= $this->SafeGetVal( $json, 'lname', $patient->Lname
);
160 $patient->Mname
= $this->SafeGetVal( $json, 'mname', $patient->Mname
);
161 $patient->Dob
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'dob', $patient->Dob
) ) );
162 $patient->Street
= $this->SafeGetVal( $json, 'street', $patient->Street
);
163 $patient->PostalCode
= $this->SafeGetVal( $json, 'postalCode', $patient->PostalCode
);
164 $patient->City
= $this->SafeGetVal( $json, 'city', $patient->City
);
165 $patient->State
= $this->SafeGetVal( $json, 'state', $patient->State
);
166 $patient->CountryCode
= $this->SafeGetVal( $json, 'countryCode', $patient->CountryCode
);
167 $patient->DriversLicense
= $this->SafeGetVal( $json, 'driversLicense', $patient->DriversLicense
);
168 $patient->Ss
= $this->SafeGetVal( $json, 'ss', $patient->Ss
);
169 $patient->Occupation
= $this->SafeGetVal( $json, 'occupation', $patient->Occupation
);
170 $patient->PhoneHome
= $this->SafeGetVal( $json, 'phoneHome', $patient->PhoneHome
);
171 $patient->PhoneBiz
= $this->SafeGetVal( $json, 'phoneBiz', $patient->PhoneBiz
);
172 $patient->PhoneContact
= $this->SafeGetVal( $json, 'phoneContact', $patient->PhoneContact
);
173 $patient->PhoneCell
= $this->SafeGetVal( $json, 'phoneCell', $patient->PhoneCell
);
174 $patient->PharmacyId
= $this->SafeGetVal( $json, 'pharmacyId', $patient->PharmacyId
);
175 $patient->Status
= $this->SafeGetVal( $json, 'status', $patient->Status
);
176 $patient->ContactRelationship
= $this->SafeGetVal( $json, 'contactRelationship', $patient->ContactRelationship
);
177 $patient->Date
= date( 'Y-m-d H:i:s', strtotime( $this->SafeGetVal( $json, 'date', $patient->Date
) ) );
178 $patient->Sex
= $this->SafeGetVal( $json, 'sex', $patient->Sex
);
179 $patient->Referrer
= $this->SafeGetVal( $json, 'referrer', $patient->Referrer
);
180 $patient->Referrerid
= $this->SafeGetVal( $json, 'referrerid', $patient->Referrerid
);
181 $patient->Providerid
= $this->SafeGetVal( $json, 'providerid', $patient->Providerid
);
182 $patient->RefProviderid
= $this->SafeGetVal( $json, 'refProviderid', $patient->RefProviderid
);
183 $patient->Email
= $this->SafeGetVal( $json, 'email', $patient->Email
);
184 $patient->EmailDirect
= $this->SafeGetVal( $json, 'emailDirect', $patient->EmailDirect
);
185 $patient->Ethnoracial
= $this->SafeGetVal( $json, 'ethnoracial', $patient->Ethnoracial
);
186 $patient->Race
= $this->SafeGetVal( $json, 'race', $patient->Race
);
187 $patient->Ethnicity
= $this->SafeGetVal( $json, 'ethnicity', $patient->Ethnicity
);
188 $patient->Religion
= $this->SafeGetVal( $json, 'religion', $patient->Religion
);
189 $patient->Interpretter
= $this->SafeGetVal( $json, 'interpretter', $patient->Interpretter
);
190 $patient->Migrantseasonal
= $this->SafeGetVal( $json, 'migrantseasonal', $patient->Migrantseasonal
);
191 $patient->FamilySize
= $this->SafeGetVal( $json, 'familySize', $patient->FamilySize
);
192 $patient->MonthlyIncome
= $this->SafeGetVal( $json, 'monthlyIncome', $patient->MonthlyIncome
);
193 $patient->BillingNote
= $this->SafeGetVal( $json, 'billingNote', $patient->BillingNote
);
194 $patient->Homeless
= $this->SafeGetVal( $json, 'homeless', $patient->Homeless
);
195 $patient->FinancialReview
= date( 'Y-m-d H:i:s', strtotime( $this->SafeGetVal( $json, 'financialReview', $patient->FinancialReview
) ) );
196 $patient->Pubpid
= $this->SafeGetVal( $json, 'pubpid', $patient->Pubpid
);
197 $patient->Pid
= $this->SafeGetVal( $json, 'pid', $patient->Pid
);
198 $patient->Genericname1
= $this->SafeGetVal( $json, 'genericname1', $patient->Genericname1
);
199 $patient->Genericval1
= $this->SafeGetVal( $json, 'genericval1', $patient->Genericval1
);
200 $patient->Genericname2
= $this->SafeGetVal( $json, 'genericname2', $patient->Genericname2
);
201 $patient->Genericval2
= $this->SafeGetVal( $json, 'genericval2', $patient->Genericval2
);
202 $patient->HipaaMail
= $this->SafeGetVal( $json, 'hipaaMail', $patient->HipaaMail
);
203 $patient->HipaaVoice
= $this->SafeGetVal( $json, 'hipaaVoice', $patient->HipaaVoice
);
204 $patient->HipaaNotice
= $this->SafeGetVal( $json, 'hipaaNotice', $patient->HipaaNotice
);
205 $patient->HipaaMessage
= $this->SafeGetVal( $json, 'hipaaMessage', $patient->HipaaMessage
);
206 $patient->HipaaAllowsms
= $this->SafeGetVal( $json, 'hipaaAllowsms', $patient->HipaaAllowsms
);
207 $patient->HipaaAllowemail
= $this->SafeGetVal( $json, 'hipaaAllowemail', $patient->HipaaAllowemail
);
208 $patient->Squad
= $this->SafeGetVal( $json, 'squad', $patient->Squad
);
209 $patient->Fitness
= $this->SafeGetVal( $json, 'fitness', $patient->Fitness
);
210 $patient->ReferralSource
= $this->SafeGetVal( $json, 'referralSource', $patient->ReferralSource
);
211 $patient->Pricelevel
= $this->SafeGetVal( $json, 'pricelevel', $patient->Pricelevel
);
212 $patient->Regdate
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'regdate', $patient->Regdate
) ) );
213 $patient->Contrastart
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'contrastart', $patient->Contrastart
) ) );
214 $patient->CompletedAd
= $this->SafeGetVal( $json, 'completedAd', $patient->CompletedAd
);
215 $patient->AdReviewed
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'adReviewed', $patient->AdReviewed
) ) );
216 $patient->Vfc
= $this->SafeGetVal( $json, 'vfc', $patient->Vfc
);
217 $patient->Mothersname
= $this->SafeGetVal( $json, 'mothersname', $patient->Mothersname
);
218 $patient->Guardiansname
= $this->SafeGetVal( $json, 'guardiansname', $patient->Guardiansname
);
219 $patient->AllowImmRegUse
= $this->SafeGetVal( $json, 'allowImmRegUse', $patient->AllowImmRegUse
);
220 $patient->AllowImmInfoShare
= $this->SafeGetVal( $json, 'allowImmInfoShare', $patient->AllowImmInfoShare
);
221 $patient->AllowHealthInfoEx
= $this->SafeGetVal( $json, 'allowHealthInfoEx', $patient->AllowHealthInfoEx
);
222 $patient->AllowPatientPortal
= $this->SafeGetVal( $json, 'allowPatientPortal', $patient->AllowPatientPortal
);
223 $patient->DeceasedDate
= date( 'Y-m-d H:i:s', strtotime( $this->SafeGetVal( $json, 'deceasedDate', $patient->DeceasedDate
) ) );
224 $patient->DeceasedReason
= $this->SafeGetVal( $json, 'deceasedReason', $patient->DeceasedReason
);
225 $patient->SoapImportStatus
= $this->SafeGetVal( $json, 'soapImportStatus', $patient->SoapImportStatus
);
226 $patient->CmsportalLogin
= $this->SafeGetVal( $json, 'cmsportalLogin', $patient->CmsportalLogin
);
227 $patient->CareTeam
= $this->SafeGetVal( $json, 'careTeam', $patient->CareTeam
);
228 $patient->County
= $this->SafeGetVal( $json, 'county', $patient->County
);
229 $patient->Industry
= $this->SafeGetVal( $json, 'industry', $patient->Industry
);
231 $patient->Validate();
232 $errors = $patient->GetValidationErrors();
234 if( count( $errors ) > 0 ){
235 $this->RenderErrorJSON( 'Please check the form for errors', $errors );
238 $this->RenderJSON( $patient, $this->JSONPCallback(), true, $this->SimpleObjectParams() );
240 } catch( Exception
$ex ){
241 $this->RenderExceptionJSON( $ex );
246 * API Method updates an existing Patient record and render response as JSON
248 public function Update(){
251 $json = json_decode( RequestUtil
::GetBody() );
253 if( ! $json ){throw new Exception( 'The request body does not contain valid JSON' );}
255 $pk = $this->GetRouter()->GetUrlParam( 'id' );
256 $patient = $this->Phreezer
->Get( 'Patient', $pk );
257 // this is a primary key. uncomment if updating is allowed
258 // $patient->Id = $this->SafeGetVal($json, 'id', $patient->Id);
259 $patient->Title
= $this->SafeGetVal( $json, 'title', $patient->Title
);
260 $patient->Language
= $this->SafeGetVal( $json, 'language', $patient->Language
);
261 $patient->Financial
= $this->SafeGetVal( $json, 'financial', $patient->Financial
);
262 $patient->Fname
= $this->SafeGetVal( $json, 'fname', $patient->Fname
);
263 $patient->Lname
= $this->SafeGetVal( $json, 'lname', $patient->Lname
);
264 $patient->Mname
= $this->SafeGetVal( $json, 'mname', $patient->Mname
);
265 $patient->Dob
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'dob', $patient->Dob
) ) );
266 $patient->Street
= $this->SafeGetVal( $json, 'street', $patient->Street
);
267 $patient->PostalCode
= $this->SafeGetVal( $json, 'postalCode', $patient->PostalCode
);
268 $patient->City
= $this->SafeGetVal( $json, 'city', $patient->City
);
269 $patient->State
= $this->SafeGetVal( $json, 'state', $patient->State
);
270 $patient->CountryCode
= $this->SafeGetVal( $json, 'countryCode', $patient->CountryCode
);
271 $patient->DriversLicense
= $this->SafeGetVal( $json, 'driversLicense', $patient->DriversLicense
);
272 $patient->Ss
= $this->SafeGetVal( $json, 'ss', $patient->Ss
);
273 $patient->Occupation
= $this->SafeGetVal( $json, 'occupation', $patient->Occupation
);
274 $patient->PhoneHome
= $this->SafeGetVal( $json, 'phoneHome', $patient->PhoneHome
);
275 $patient->PhoneBiz
= $this->SafeGetVal( $json, 'phoneBiz', $patient->PhoneBiz
);
276 $patient->PhoneContact
= $this->SafeGetVal( $json, 'phoneContact', $patient->PhoneContact
);
277 $patient->PhoneCell
= $this->SafeGetVal( $json, 'phoneCell', $patient->PhoneCell
);
278 $patient->PharmacyId
= $this->SafeGetVal( $json, 'pharmacyId', $patient->PharmacyId
);
279 $patient->Status
= $this->SafeGetVal( $json, 'status', $patient->Status
);
280 $patient->ContactRelationship
= $this->SafeGetVal( $json, 'contactRelationship', $patient->ContactRelationship
);
281 $patient->Date
= date( 'Y-m-d H:i:s', strtotime( $this->SafeGetVal( $json, 'date', $patient->Date
) ) );
282 $patient->Sex
= $this->SafeGetVal( $json, 'sex', $patient->Sex
);
283 $patient->Referrer
= $this->SafeGetVal( $json, 'referrer', $patient->Referrer
);
284 $patient->Referrerid
= $this->SafeGetVal( $json, 'referrerid', $patient->Referrerid
);
285 $patient->Providerid
= $this->SafeGetVal( $json, 'providerid', $patient->Providerid
);
286 $patient->RefProviderid
= $this->SafeGetVal( $json, 'refProviderid', $patient->RefProviderid
);
287 $patient->Email
= $this->SafeGetVal( $json, 'email', $patient->Email
);
288 $patient->EmailDirect
= $this->SafeGetVal( $json, 'emailDirect', $patient->EmailDirect
);
289 $patient->Ethnoracial
= $this->SafeGetVal( $json, 'ethnoracial', $patient->Ethnoracial
);
290 $patient->Race
= $this->SafeGetVal( $json, 'race', $patient->Race
);
291 $patient->Ethnicity
= $this->SafeGetVal( $json, 'ethnicity', $patient->Ethnicity
);
292 $patient->Religion
= $this->SafeGetVal( $json, 'religion', $patient->Religion
);
293 $patient->Interpretter
= $this->SafeGetVal( $json, 'interpretter', $patient->Interpretter
);
294 $patient->Migrantseasonal
= $this->SafeGetVal( $json, 'migrantseasonal', $patient->Migrantseasonal
);
295 $patient->FamilySize
= $this->SafeGetVal( $json, 'familySize', $patient->FamilySize
);
296 $patient->MonthlyIncome
= $this->SafeGetVal( $json, 'monthlyIncome', $patient->MonthlyIncome
);
297 $patient->BillingNote
= $this->SafeGetVal( $json, 'billingNote', $patient->BillingNote
);
298 $patient->Homeless
= $this->SafeGetVal( $json, 'homeless', $patient->Homeless
);
299 $patient->FinancialReview
= date( 'Y-m-d H:i:s', strtotime( $this->SafeGetVal( $json, 'financialReview', $patient->FinancialReview
) ) );
300 $patient->Pubpid
= $this->SafeGetVal( $json, 'pubpid', $patient->Pubpid
);
301 $patient->Pid
= $this->SafeGetVal( $json, 'pid', $patient->Pid
);
302 $patient->HipaaMail
= $this->SafeGetVal( $json, 'hipaaMail', $patient->HipaaMail
);
303 $patient->HipaaVoice
= $this->SafeGetVal( $json, 'hipaaVoice', $patient->HipaaVoice
);
304 $patient->HipaaNotice
= $this->SafeGetVal( $json, 'hipaaNotice', $patient->HipaaNotice
);
305 $patient->HipaaMessage
= $this->SafeGetVal( $json, 'hipaaMessage', $patient->HipaaMessage
);
306 $patient->HipaaAllowsms
= $this->SafeGetVal( $json, 'hipaaAllowsms', $patient->HipaaAllowsms
);
307 $patient->HipaaAllowemail
= $this->SafeGetVal( $json, 'hipaaAllowemail', $patient->HipaaAllowemail
);
308 $patient->ReferralSource
= $this->SafeGetVal( $json, 'referralSource', $patient->ReferralSource
);
309 $patient->Pricelevel
= $this->SafeGetVal( $json, 'pricelevel', $patient->Pricelevel
);
310 $patient->Regdate
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'regdate', $patient->Regdate
) ) );
311 $patient->Contrastart
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'contrastart', $patient->Contrastart
) ) );
312 $patient->CompletedAd
= $this->SafeGetVal( $json, 'completedAd', $patient->CompletedAd
);
313 $patient->AdReviewed
= date( 'Y-m-d', strtotime( $this->SafeGetVal( $json, 'adReviewed', $patient->AdReviewed
) ) );
314 $patient->Vfc
= $this->SafeGetVal( $json, 'vfc', $patient->Vfc
);
315 $patient->Mothersname
= $this->SafeGetVal( $json, 'mothersname', $patient->Mothersname
);
316 $patient->Guardiansname
= $this->SafeGetVal( $json, 'guardiansname', $patient->Guardiansname
);
317 $patient->AllowImmRegUse
= $this->SafeGetVal( $json, 'allowImmRegUse', $patient->AllowImmRegUse
);
318 $patient->AllowImmInfoShare
= $this->SafeGetVal( $json, 'allowImmInfoShare', $patient->AllowImmInfoShare
);
319 $patient->AllowHealthInfoEx
= $this->SafeGetVal( $json, 'allowHealthInfoEx', $patient->AllowHealthInfoEx
);
320 $patient->AllowPatientPortal
= $this->SafeGetVal( $json, 'allowPatientPortal', $patient->AllowPatientPortal
);
321 $patient->CareTeam
= $this->SafeGetVal( $json, 'careTeam', $patient->CareTeam
);
322 $patient->County
= $this->SafeGetVal( $json, 'county', $patient->County
);
323 $patient->Industry
= $this->SafeGetVal( $json, 'industry', $patient->Industry
);
325 $patient->Validate();
326 $errors = $patient->GetValidationErrors();
328 if( count( $errors ) > 0 ){
329 $this->RenderErrorJSON( 'Please check the form for errors', $errors );
332 self
::CloseAudit( $patient );
333 $this->RenderJSON( $patient, $this->JSONPCallback(), true, $this->SimpleObjectParams() );
335 } catch( Exception
$ex ){
336 $this->RenderExceptionJSON( $ex );
339 public function CloseAudit( $p ){
340 $appsql = new ApplicationTable();
341 $ja = $p->GetArray();
344 // date("Y-m-d H:i:s");
345 $audit['patient_id'] = $ja['pid'];
346 $audit['activity'] = "profile";
347 $audit['require_audit'] = "1";
348 $audit['pending_action'] = "completed";
349 $audit['action_taken'] = "accept";
350 $audit['status'] = "closed";
351 $audit['narrative'] = "Changes reviewed and commited to demographics.";
352 $audit['table_action'] = "update";
353 $audit['table_args'] = $ja;
354 $audit['action_user'] = isset( $_SESSION['authUserID'] ) ?
$_SESSION['authUserID'] : "0";
355 $audit['action_taken_time'] = date( "Y-m-d H:i:s" );
356 $audit['checksum'] = "0";
358 $edata = $appsql->getPortalAudit( $ja['pid'], 'review' );
359 $audit['date'] = $edata['date'];
360 if( $edata['id'] > 0 ) $appsql->portalAudit( 'update', $edata['id'], $audit );
361 } catch( Exception
$ex ){
362 $this->RenderExceptionJSON( $ex );
366 * API Method deletes an existing Patient record and render response as JSON
368 public function Delete(){
370 // TODO: if a soft delete is prefered, change this to update the deleted flag instead of hard-deleting
372 $pk = $this->GetRouter()->GetUrlParam( 'id' );
373 $patient = $this->Phreezer
->Get( 'Patient', $pk );
377 $output = new stdClass();
379 $this->RenderJSON( $output, $this->JSONPCallback() );
380 } catch( Exception
$ex ){
381 $this->RenderExceptionJSON( $ex );