fix for attendance form
[openemr.git] / myportal / soap_service / server_med_rec.php
blobc04a56f4f805b196df7fdd913550f474347af69d
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2011 Z&H Consultancy Services Private Limited <sam@zhservices.com>
4 //
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Jacob T Paul <jacob@zhservices.com>
26 // +------------------------------------------------------------------------------+
28 //STOP FAKE REGISTER GLOBALS
29 $fake_register_globals=false;
32 require_once("server_audit.php");
33 class Userforms extends UserAudit{
37 public function issue_type($data){
38 if(UserService::valid($data[0])=='existingpatient'){
39 global $ISSUE_TYPES;
40 require_once("../../library/lists.inc");
41 return $ISSUE_TYPES;
43 else{
44 throw new SoapFault("Server", "credentials failed");
50 public function print_report($data){
51 global $pid;
52 if(UserService::valid($data[0])=='existingpatient'){
53 $repArr = $data[1];
54 $type = $data[3];
55 global $ISSUE_TYPES;
56 require_once("../../library/forms.inc");
57 require_once("../../library/billing.inc");
58 require_once("../../library/pnotes.inc");
59 require_once("../../library/patient.inc");
60 require_once("../../library/options.inc.php");
61 require_once("../../library/acl.inc");
62 require_once("../../library/lists.inc");
63 require_once("../../library/report.inc");
64 require_once("../../custom/code_types.inc.php");
65 foreach($repArr as $value){
66 ob_start();
67 if($type=="profile"){
68 $this->getIncudes($value);
69 $out .= ob_get_clean();
71 else{
72 if($type=='issue')
73 $this->getIid($value);
74 if($type=='forms')
75 $this->getforms($value);
76 $out .= ob_get_clean();
80 return $out;
82 else{
83 throw new SoapFault("Server", "credentials failed");
90 public function print_ccr_report($data){
91 if(UserService::valid($data[0])=='existingpatient'){
92 $ccraction = $data[1];
93 $raw = $data[2];
94 require_once("../../ccr/createCCR.php");
95 ob_start();
96 createCCR($ccraction,$raw);
97 $html = ob_get_clean();
98 if($ccraction=='viewccd')
101 $html = preg_replace('/<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD HTML 4.01\/\/EN" "http:\/\/www.w3.org\/TR\/html4\/strict.dtd">/','',$html);
102 $pos1 = strpos($html,'body {');
103 $pos2 = strpos($html,'.h1center');
104 $tes = substr("$html",$pos1,($pos2-$pos1));
105 $html = str_replace($tes,'',$html);
106 $html = str_replace('h3>','h2>',$html);
107 $html = base64_encode($html);
109 else{
110 $pos1 = strpos($html,'*{');
111 $pos2 = strpos($html,'h1');
112 $tes = substr("$html",$pos1,($pos2-$pos1));
113 $html = str_replace($tes,'',$html);
115 return $html;
117 else{
118 throw new SoapFault("Server", "credentials failed");
122 //Return the forms requested from Portal.
124 private function getforms($fId){
125 global $pid;
126 $GLOBALS['pid'] = $pid;
127 $inclookupres = sqlStatement("SELECT DISTINCT formdir FROM forms WHERE pid = ? AND deleted=0",array($pid));
128 while($result = sqlFetchArray($inclookupres)) {
129 $formdir = $result['formdir'];
130 if (substr($formdir,0,3) == 'LBF')
131 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
132 else
133 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
135 $N = 6;
136 $inclookupres = sqlStatement("SELECT encounter,form_id,formdir,id FROM forms WHERE pid = ? AND deleted=0
137 AND id =? ",array($pid,$fId));
138 while($result = sqlFetchArray($inclookupres)) {
139 $form_encounter=$result['encounter'];
140 $form_id=$result['form_id'];
141 $formdir = $result['formdir'];
142 $id=$result['id'];
143 ob_start();
144 if (substr($formdir,0,3) == 'LBF')
145 call_user_func("lbf_report", $pid, $form_encounter, $N, $form_id, $formdir);
146 else
147 call_user_func($formdir . "_report", $pid, $form_encounter, $N, $form_id);
148 $out=ob_get_clean();
149 ?> <table>
150 <tr class=text>
151 <th><?php echo htmlspecialchars($formdir,ENT_QUOTES);?></th>
152 </tr>
153 </table>
154 <?php echo $out;?>
155 <?php
161 private function getIid($val){
162 global $pid;
163 global $ISSUE_TYPES;
164 $inclookupres = sqlStatement("SELECT DISTINCT formdir FROM forms WHERE pid = ? AND deleted=?",array($pid,0));
165 while($result = sqlFetchArray($inclookupres)) {
166 $formdir = $result['formdir'];
167 if (substr($formdir,0,3) == 'LBF')
168 include_once($GLOBALS['incdir'] . "/forms/LBF/report.php");
169 else
170 include_once($GLOBALS['incdir'] . "/forms/$formdir/report.php");
173 <tr class=text>
174 <td></td>
175 <td>
176 <?php
177 $irow = sqlQuery("SELECT type, title, comments, diagnosis FROM lists WHERE id =? ",array($val));
178 $diagnosis = $irow['diagnosis'];
180 if ($prevIssueType != $irow['type'])
182 $disptype = $ISSUE_TYPES[$irow['type']][0];
184 <div class='issue_type' style='font-weight: bold;'><?php echo htmlspecialchars($disptype,ENT_QUOTES);?>:</div>
185 <?php
186 $prevIssueType = $irow['type'];
189 <div class='text issue'>
190 <span class='issue_title'><?php echo htmlspecialchars($irow['title'],ENT_QUOTES);?>:</span>
191 <span class='issue_comments'><?php echo htmlspecialchars($irow['comments'],ENT_QUOTES);?></span>
192 <?php
193 if ($diagnosis)
196 <div class='text issue_diag'>
197 <span class='bold'>[<?php echo htmlspecialchars(xl('Diagnosis'),ENT_QUOTES);?>]</span><br>
198 <?php
199 $dcodes = explode(";", $diagnosis);
200 foreach ($dcodes as $dcode)
203 <span class='italic'><?php echo htmlspecialchars($dcode,ENT_QUOTES);?></span>:
204 <?php
205 echo htmlspecialchars(lookup_code_descriptions($dcode),ENT_QUOTES);
207 <br>
208 <?php
211 </div>
212 <?php
214 if ($irow['type'] == 'ippf_gcac')
217 <table>
218 <?php
219 display_layout_rows('GCA', sqlQuery("SELECT * FROM lists_ippf_gcac WHERE id = ?",array($rowid)));
222 </table>
223 <?php
225 else if ($irow['type'] == 'contraceptive')
228 <table>
229 <?php
230 display_layout_rows('CON', sqlQuery("SELECT * FROM lists_ippf_con WHERE id = ?",array($rowid)));
232 </table>
233 <?php
236 </div>
237 <?php
239 </td>
240 <?php
246 private function getIncudes($val){
247 global $pid;
248 if ($val == "demographics")
251 <hr />
252 <div class='text demographics' id='DEM'>
253 <?php
254 // printRecDataOne($patient_data_array, getRecPatientData ($pid), $N);
255 $result1 = getPatientData($pid);
256 $result2 = getEmployerData($pid);
258 <table>
259 <tr><td><h6><?php echo htmlspecialchars(xl('Patient Data').":",ENT_QUOTES);?></h6></td></tr>
260 <?php
261 display_layout_rows('DEM', $result1, $result2);
263 </table>
264 </div>
265 <?php
267 elseif ($val == "history")
270 <hr />
271 <div class='text history' id='HIS'>
272 <?php
273 $result1 = getHistoryData($pid);
275 <table>
276 <tr><td><h6><?php echo htmlspecialchars(xl('History Data').":",ENT_QUOTES);?></h6></td></tr>
277 <?php
278 display_layout_rows('HIS', $result1);
280 </table>
281 </div>
282 <?php
284 elseif ($val == "insurance")
287 <hr />
288 <div class='text insurance'>";
289 <h6><?php echo htmlspecialchars(xl('Insurance Data').":",ENT_QUOTES);?></h6>
290 <br><span class=bold><?php echo htmlspecialchars(xl('Primary Insurance Data').":",ENT_QUOTES);?></span><br>
291 <?php
292 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"primary"), $N);
294 <span class=bold><?php echo htmlspecialchars(xl('Secondary Insurance Data').":",ENT_QUOTES);?></span><br>
295 <?php
296 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"secondary"), $N);
298 <span class=bold><?php echo htmlspecialchars(xl('Tertiary Insurance Data').":",ENT_QUOTES);?></span><br>
299 <?php
300 printRecDataOne($insurance_data_array, getRecInsuranceData ($pid,"tertiary"), $N);
302 </div>
303 <?php
305 elseif ($val == "billing")
308 <hr />
309 <div class='text billing'>
310 <h6><?php echo htmlspecialchars(xl('Billing Information').":",ENT_QUOTES);?></h6>
311 <?php
312 if (count($ar['newpatient']) > 0) {
313 $billings = array();
315 <table>
316 <tr><td width='400' class='bold'><?php echo htmlspecialchars(xl('Code'),ENT_QUOTES);?></td><td class='bold'><?php echo htmlspecialchars(xl('Fee'),ENT_QUOTES);?></td></tr>
317 <?php
318 $total = 0.00;
319 $copays = 0.00;
320 foreach ($ar['newpatient'] as $be) {
321 $ta = split(":",$be);
322 $billing = getPatientBillingEncounter($pid,$ta[1]);
323 $billings[] = $billing;
324 foreach ($billing as $b) {
326 <tr>
327 <td class=text>
328 <?php
329 echo htmlspecialchars($b['code_type'],ENT_QUOTES) . ":\t" .htmlspecialchars( $b['code'],ENT_QUOTES) . "&nbsp;". htmlspecialchars($b['modifier'],ENT_QUOTES) . "&nbsp;&nbsp;&nbsp;" . htmlspecialchars($b['code_text'],ENT_QUOTES) . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
331 </td>
332 <td class=text>
333 <?php
334 echo htmlspecialchars(oeFormatMoney($b['fee']),ENT_QUOTES);
336 </td>
337 </tr>
338 <?php
339 $total += $b['fee'];
340 if ($b['code_type'] == "COPAY") {
341 $copays += $b['fee'];
345 echo "<tr><td>&nbsp;</td></tr>";
346 echo "<tr><td class=bold>".htmlspecialchars(xl('Sub-Total'),ENT_QUOTES)."</td><td class=text>" . htmlspecialchars(oeFormatMoney($total + abs($copays)),ENT_QUOTES) . "</td></tr>";
347 echo "<tr><td class=bold>".htmlspecialchars(xl('Paid'),ENT_QUOTES)."</td><td class=text>" . htmlspecialchars(oeFormatMoney(abs($copays)),ENT_QUOTES) . "</td></tr>";
348 echo "<tr><td class=bold>".htmlspecialchars(xl('Total'),ENT_QUOTES)."</td><td class=text>" .htmlspecialchars(oeFormatMoney($total),ENT_QUOTES) . "</td></tr>";
349 echo "</table>";
350 echo "<pre>";
351 //print_r($billings);
352 echo "</pre>";
353 } else {
354 printPatientBilling($pid);
356 echo "</div>\n"; // end of billing DIV
358 elseif ($val == "immunizations")
362 <hr />
363 <div class='text immunizations'>
364 <h6><?php echo htmlspecialchars(xl('Patient Immunization').":",ENT_QUOTES);?></h6>
365 <?php
366 $sql = "select i1.immunization_id as immunization_id, if(i1.administered_date,concat(i1.administered_date,' - ') ,substring(i1.note,1,20) ) as immunization_data from immunizations i1 where i1.patient_id = ? order by administered_date desc";
367 $result = sqlStatement($sql,array($pid));
368 while ($row=sqlFetchArray($result)) {
369 echo htmlspecialchars($row{'immunization_data'},ENT_QUOTES);
370 echo generate_display_field(array('data_type'=>'1','list_id'=>'immunizations'), $row['immunization_id']);
372 <br>
373 <?php
376 </div>
377 <?php
380 elseif ($val == "batchcom")
383 <hr />
384 <div class='text transactions'>
385 <h6><?php htmlspecialchars(xl('Patient Communication sent').":",ENT_QUOTES);?></h6>
386 <?php
387 $sql="SELECT concat( 'Messsage Type: ', batchcom.msg_type, ', Message Subject: ', batchcom.msg_subject, ', Sent on:', batchcom.msg_date_sent ) AS batchcom_data, batchcom.msg_text, concat( users.fname, users.lname ) AS user_name FROM `batchcom` JOIN `users` ON users.id = batchcom.sent_by WHERE batchcom.patient_id=?";
388 $result = sqlStatement($sql,array($pid));
389 while ($row=sqlFetchArray($result)) {
390 echo htmlspecialchars($row{'batchcom_data'}.", ".xl('By').": ".$row{'user_name'},ENT_QUOTES);
392 <br><?php echo htmlspecialchars(xl('Text'),ENT_QUOTES);?>:<br><?php echo htmlspecialchars($row{'msg_txt'},ENT_QUOTES);?><br>
393 <?php
396 </div>
397 <?php
399 elseif ($val == "notes")
402 <hr />
403 <div class='text notes'>
404 <h6><?php echo htmlspecialchars(xl('Patient Notes').":",ENT_QUOTES);?></h6>
405 <?php
406 printPatientNotes($pid);
408 </div>
409 <?php
411 elseif ($val == "transactions")
414 <hr />
415 <div class='text transactions'>
416 <h6><?php echo htmlspecialchars(xl('Patient Transactions').":",ENT_QUOTES);?></h6>
417 <?php
418 printPatientTransactions($pid);
420 </div>
421 <?php
426 * Method to fetch CCDA
427 * @param type $data
428 * @return type
430 public function ccdaFetching($data)
432 global $pid;
433 global $server_url;
435 if (UserService::valid($data[0])=='existingpatient') {
436 if ($this->checkModuleInstalled($moduleName = 'Carecoordination')) {
437 $site_id = $data[0][0];
438 try {
439 $ch = curl_init();
440 $url = $server_url . "/interface/modules/zend_modules/public/encounterccdadispatch/index?cron=1&pid=$pid&site=$site_id";
442 curl_setopt($ch, CURLOPT_URL, $url);
443 curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
444 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
445 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
446 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
447 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
448 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
449 $result = curl_exec($ch) or die(curl_error($ch));
450 curl_close($ch);
452 catch (Exception $e) {
456 try {
457 $event = isset ($data['event']) ? $data['event'] : 'patient-record';
458 $menu_item = isset($data['menu_item']) ? $data['menu_item'] : 'Dashboard';
459 newEvent($event, 1, '', 1, '', $pid,$log_from = 'patient-portal', $menu_item );
460 }catch (Exception $e) {
463 return $result;
465 else {
466 return '<?xml version="1.0" encoding="UTF-8"?>
467 <note>
468 <heading>WARNING!</heading>
469 <body>Unable to fetch CCDA Carecoordination module not installed!</body>
470 </note>';
473 else {
474 return '<?xml version="1.0" encoding="UTF-8"?>
475 <note>
476 <heading>WARNING!</heading>
477 <body>Existing patient checking failed!</body>
478 </note>';
480 return '<?xml version="1.0" encoding="UTF-8"?>
481 <note>
482 <heading>WARNING!</heading>
483 <body>Un known error occured</body>
484 </note>';
487 public function checkModuleInstalled($moduleName = 'Carecoordination')
489 $sql = "SELECT mod_id FROM modules WHERE mod_name = ? AND mod_active = '1'";
490 $res = sqlStatement($sql, array($moduleName));
491 $row = sqlFetchArray($res);
492 return !empty($row);
496 * @param mysql_resource - $inputArray - mysql query result
497 * @param string - $rootElementName - root element name
498 * @param string - $childElementName - child element name
500 public function arrayToXml($inputArray, $rootElementName = 'root', $childElementName = 'RowItem')
502 $xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
503 $xmlData .= "<" . $rootElementName . ">";
504 foreach ($inputArray as $rowItem) {
505 $xmlData .= "<" . $childElementName . ">";
506 foreach($rowItem as $fieldName => $fieldValue) {
507 $xmlData .= "<" . $fieldName . ">";
508 $xmlData .= !empty($fieldValue) ? $fieldValue : "null";
509 $xmlData .= "</" . $fieldName . ">";
511 $xmlData .= "</" . $childElementName . ">";
513 $xmlData .= "</" . $rootElementName . ">";
515 return $xmlData;
520 * @param type $data
521 * @return type
523 public function getEventLog($data)
525 global $pid;
526 if (UserService::valid($data[0])=='existingpatient') {
527 $date1 = $data['start_date'];
528 $date2 = $data['end_date'];
529 $keyword = $data['keyword'];
530 $arrBinds = array();
531 $cols = "DISTINCT log.date, event, user, groupname, patient_id, success, comments,checksum,crt_user";
532 $sql = "SELECT $cols, CONCAT(fname, ' ', lname) as patient_ful_name, patient_portal_menu.`menu_name`,
533 patient_portal_menu_group.`menu_group_name`, ccda_doc_id FROM log
534 JOIN patient_data ON log.patient_id = patient_data.pid
535 JOIN patient_access_offsite ON log.patient_id = patient_access_offsite.pid
536 JOIN patient_portal_menu ON patient_portal_menu.`patient_portal_menu_id` = log.menu_item_id
537 JOIN patient_portal_menu_group ON patient_portal_menu_group.`patient_portal_menu_group_id` = patient_portal_menu.`patient_portal_menu_group_id`
538 WHERE log.date >= ? AND log.date <= ?";
540 $sql .= " AND log_from = 'patient-portal'";
541 $sql .= " AND patient_id = ?";
542 $arrBinds = array($date1 . ' 00:00:00', $date2 . ' 23:59:59', $pid);
543 if(!empty($keyword)) {
544 $sql .= " AND (log.date LIKE ?
545 OR LOWER(event) LIKE ?
546 OR LOWER(user) LIKE ?
547 OR LOWER(CONCAT(fname, ' ', lname)) LIKE ?
548 OR LOWER(groupname) LIKE ?
549 OR LOWER(comments) LIKE ?
550 OR LOWER(user) LIKE ?
551 ) ";
552 $arrBinds[] = '%' . $keyword . '%' ;
553 $arrBinds[] = '%' . strtolower($keyword) . '%';
554 $arrBinds[] = '%' . strtolower($keyword) . '%';
555 $arrBinds[] = '%' . strtolower($keyword) . '%';
556 $arrBinds[] = '%' . strtolower($keyword) . '%';
557 $arrBinds[] = '%' . strtolower($keyword) . '%';
558 $arrBinds[] = '%' . strtolower($keyword) . '%';
560 $sql .= " ORDER BY date DESC LIMIT 5000";
562 $res = sqlStatement($sql, $arrBinds);
563 $all = array();
564 for($iter=0; $row=sqlFetchArray($res); $iter++) {
565 $all[$iter] = $row;
568 $responseString = $this->arrayToXml($all );
570 return $responseString;
575 * Connect to a phiMail Direct Messaging server and transmit
576 * a CCD document to the specified recipient. If the message is accepted by the
577 * server, the script will return "SUCCESS", otherwise it will return an error msg.
578 * @param DOMDocument ccd the xml data to transmit, a CCDA document is assumed
579 * @param string recipient the Direct Address of the recipient
580 * @param string requested_by user | patient
581 * @return string result of operation
583 function transmitCCD($data = array()) {
584 $ccd = $data['ccd'];
585 $recipient = $data['recipient'];
586 $requested_by = $data['requested_by'];
587 $xml_type = $data['xml_type'];
589 if (UserService::valid($data[0])=='existingpatient') {
591 try {
592 $_SESSION['authProvider'] = 1;
593 global $pid;
594 //get patient name in Last_First format (used for CCDA filename) and
595 //First Last for the message text.
596 $patientData = getPatientPID(array("pid"=>$pid));
597 if (empty($patientData[0]['lname'])) {
598 $att_filename = "";
599 $patientName2 = "";
600 } else {
601 //spaces are the argument delimiter for the phiMail API calls and must be removed
602 $extension = $xml_type == 'CCDA' ? 'xml' : strtolower($xml_type);
603 $att_filename = " " .
604 str_replace(" ", "_", $xml_type . "_" . $patientData[0]['lname']
605 . "_" . $patientData[0]['fname']) . "." . $extension;
606 $patientName2 = $patientData[0]['fname'] . " " . $patientData[0]['lname'];
609 $config_err = xl("Direct messaging is currently unavailable.")." EC:";
610 if ($GLOBALS['phimail_enable']==false) return("$config_err 1");
612 $fp = phimail_connect($err);
613 if ($fp===false) return("$config_err $err");
615 $phimail_username = $GLOBALS['phimail_username'];
616 $phimail_password = $GLOBALS['phimail_password'];
617 $ret = phimail_write_expect_OK($fp,"AUTH $phimail_username $phimail_password\n");
618 if($ret!==TRUE) return("$config_err 4");
620 $ret = phimail_write_expect_OK($fp,"TO $recipient\n");
621 if($ret!==TRUE) return( xl("Delivery is not allowed to the specified Direct Address.") );
623 $ret=fgets($fp,1024); //ignore extra server data
625 if($requested_by=="patient")
626 $text_out = xl("Delivery of the attached clinical document was requested by the patient") .
627 ($patientName2=="" ? "." : ", " . $patientName2 . ".");
628 else
629 $text_out = xl("A clinical document is attached") .
630 ($patientName2=="" ? "." : " " . xl("for patient") . " " . $patientName2 . ".");
632 $text_len=strlen($text_out);
633 phimail_write($fp,"TEXT $text_len\n");
634 $ret=@fgets($fp,256);
636 if($ret!="BEGIN\n") {
637 phimail_close($fp);
638 return("$config_err 5");
640 $ret=phimail_write_expect_OK($fp,$text_out);
641 if($ret!==TRUE) return("$config_err 6");
643 if(in_array($xml_type, array('CCR', 'CCDA', 'CDA')))
645 $ccd = simplexml_load_string($ccd);
646 $ccd_out = $ccd->saveXml();
647 $ccd_len = strlen($ccd_out);
648 phimail_write($fp,"ADD " . ($xml_type=="CCR" ? $xml_type . ' ' : "CDA ") . $ccd_len . $att_filename . "\n");
649 //phimail_write($fp,"ADD " . (isset($xml_type) ? $xml_type . ' ' : "CDA ") . $ccd_len . $att_filename . "\n");
650 } else if(strtolower($xml_type) == 'html' || strtolower($xml_type) == 'pdf') {
651 $ccd_out = base64_decode($ccd);
652 $message_length = strlen($ccd_out);
653 $add_type = (strtolower($xml_type) == 'html') ? 'TEXT' : 'RAW';
654 phimail_write($fp, "ADD " . $add_type . " " . $message_length . "" . $att_filename . "\n");
658 $ret=fgets($fp,256);
660 if($ret!="BEGIN\n") {
661 phimail_close($fp);
662 return("$config_err 7");
664 $ret=phimail_write_expect_OK($fp,$ccd_out);
666 if($ret!==TRUE) return("$config_err 8");
669 phimail_write($fp,"SEND\n");
670 $ret=fgets($fp,256);
671 phimail_close($fp);
673 if($requested_by=="patient") {
674 $reqBy="portal-user";
675 $sql = "SELECT id FROM users WHERE username='portal-user'";
677 if (($r = sqlStatement($sql)) === FALSE ||
678 ($u = sqlFetchArray($r)) === FALSE) {
679 $reqID = 1; //default if we don't have a service user
680 } else {
681 $reqID = $u['id'];
684 } else {
685 $reqBy=$_SESSION['authUser'];
686 $reqID=$_SESSION['authUserID'];
689 if(substr($ret,5)=="ERROR") {
690 //log the failure
691 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
692 return( xl("The message could not be sent at this time."));
696 * If we get here, the message was successfully sent and the return
697 * value $ret is of the form "QUEUED recipient message-id" which
698 * is suitable for logging.
700 $msg_id=explode(" ",trim($ret),4);
701 if($msg_id[0]!="QUEUED" || !isset($msg_id[2])) { //unexpected response
702 $ret = "UNEXPECTED RESPONSE: " . $ret;
703 newEvent("transmit-ccd",$reqBy,$_SESSION['authProvider'],0,$ret,$pid);
704 return( xl("There was a problem sending the message."));
706 newEvent("transmit-".$xml_type,$reqBy,$_SESSION['authProvider'],1,$ret,$pid);
707 $adodb=$GLOBALS['adodb']['db'];
709 // $sql="INSERT INTO direct_message_log (msg_type,msg_id,sender,recipient,status,status_ts,patient_id,user_id) " .
710 // "VALUES ('S', ?, ?, ?, 'S', NOW(), ?, ?)";
711 // $res=@sqlStatement($sql,array($msg_id[2],$phimail_username,$recipient,$pid,$reqID));
713 return("SUCCESS");
714 }catch (Exception $e) {
715 return 'Error: ' . $e->getMessage();