Highway to PSR2
[openemr.git] / interface / cmsportal / portal.inc.php
blob824a32bede3c2dab6bfbcb170e99a1779fe5534a
1 <?php
2 /**
3 * Remote access to a WordPress Patient Portal.
5 * Copyright (C) 2014 Rod Roark <rod@sunsetsystems.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Rod Roark <rod@sunsetsystems.com>
22 // Note: In Ubuntu this requires the php5-curl package.
23 // http://www.php.net/manual/en/function.curl-setopt.php has many comments and examples.
25 if (!$GLOBALS['gbl_portal_cms_enable']) {
26 die(xlt('CMS Portal not enabled!'));
29 function cms_portal_call($args)
31 $portal_url = $GLOBALS['gbl_portal_cms_address'] . "/wp-content/plugins/sunset-patient-portal/webserve.php";
32 $args['login' ] = $GLOBALS['gbl_portal_cms_username'];
33 $args['password'] = $GLOBALS['gbl_portal_cms_password'];
35 if (($phandle = curl_init($portal_url)) === false) {
36 die(text(xl('Unable to access URL') . " '$portal_url'"));
39 curl_setopt($phandle, CURLOPT_POST, true);
40 curl_setopt($phandle, CURLOPT_RETURNTRANSFER, true);
41 curl_setopt($phandle, CURLOPT_POSTFIELDS, $args);
42 if (($presult = curl_exec($phandle)) === false) {
43 die(text('curl_exec ' . xl('failed') . ': ' . curl_error($phandle)));
46 curl_close($phandle);
47 // With JSON-over-HTTP we would use json_decode($presult,TRUE) here.
48 return unserialize($presult);
51 // Look up the OpenEMR patient matching this request. More or less than 1 is an error.
52 function lookup_openemr_patient($wp_login)
54 if (empty($wp_login)) {
55 die(xlt('The patient was not logged in when submitting this form'));
58 $ptres = sqlStatement("SELECT pid FROM patient_data WHERE cmsportal_login = ?", array($wp_login));
59 if (sqlNumRows($ptres) < 1) {
60 die(xlt('There is no patient with portal login') . " '$wp_login'");
63 if (sqlNumRows($ptres) > 1) {
64 die(xlt('There are multiple patients with portal login') . " '$wp_login'");
67 $ptrow = sqlFetchArray($ptres);
68 return $ptrow['pid'];
71 // This constructs a LBF field value string from form data provided by the portal.
73 function cms_field_to_lbf($data_type, $field_id, &$fldarr)
75 $newvalue = '';
76 if ($data_type == '23') {
77 // Type Exam Results is special, pieced together from multiple CMS fields.
78 // For example layout field "exams" might find CMS fields "exams:brs" = 1
79 // and "exams:cec" = 2 and aggregate them into the value "brs:1|cec:2".
80 foreach ($fldarr as $key => $value) {
81 if (preg_match('/^' . $field_id . ':(\w+)/', $key, $matches)) {
82 if ($newvalue !== '') {
83 $newvalue .= '|';
86 $newvalue .= $matches[1] . ":$value:";
89 } else {
90 if (isset($fldarr[$field_id])) {
91 $newvalue = $fldarr[$field_id];
94 if ($newvalue !== '') {
95 // Lifestyle Status.
96 if ($data_type == '28') {
97 $newvalue = "|$newvalue$field_id|";
98 } // Smoking Status.
99 else if ($data_type == '32') {
100 // See the smoking_status list for these array values:
101 $ssarr = array('current' => 1, 'quit' => 3, 'never' => 4, 'not_applicable' => 9);
102 $ssindex = isset($ssarr[$newvalue]) ? $ssarr[$newvalue] : 0;
103 $newvalue = "|$newvalue$field_id||$ssindex";
104 } // Checkbox list.
105 else if (is_array($newvalue)) {
106 $tmp = '';
107 foreach ($newvalue as $value) {
108 if ($tmp !== '') {
109 $tmp .= '|';
112 $tmp .= $value;
115 $newvalue = $tmp;
120 return $newvalue;