Mangled path fax send (#7515)
[openemr.git] / interface / patient_file / summary / stats.php
blob48be4bddeed5cc0b2617ea732ca36be2de2cf94c
1 <?php
3 /**
4 * stats.php
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 require_once("../../globals.php");
14 require_once("$srcdir/lists.inc.php");
15 require_once("$srcdir/options.inc.php");
17 use OpenEMR\Common\Acl\AclMain;
18 use OpenEMR\Common\Csrf\CsrfUtils;
19 use OpenEMR\Common\Twig\TwigContainer;
21 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
22 CsrfUtils::csrfNotVerified();
25 $twigContainer = new TwigContainer(null, $kernel);
26 $t = $twigContainer->getTwig();
28 /**
29 * Return an array of list data for a given issue type and patient
31 * @var $pid string Patient ID
32 * @var $type string Issue Type
33 * @return
35 function getListData($pid, $type)
37 if ($type == "medication") {
38 $sqlArr = [
39 "SELECT lists.*, medications.list_id, medications.drug_dosage_instructions FROM lists",
40 "LEFT JOIN ( SELECT id AS lists_medication_id, list_id, drug_dosage_instructions FROM lists_medication )",
41 "medications ON medications.list_id = id",
42 "WHERE pid = ? AND type = ? AND",
43 dateEmptySql('enddate')
45 } else {
46 $sqlArr = [
47 "SELECT * FROM lists WHERE pid = ? AND type = ? AND",
48 dateEmptySql('enddate')
53 if ($GLOBALS['erx_enable'] && $GLOBALS['erx_medication_display'] && $type == 'medication') {
54 $sqlArr[] = "and erx_uploaded != '1'";
57 if ($GLOBALS['erx_enable'] && $GLOBALS['erx_allergy_display'] && $type == 'allergy') {
58 $sqlArr[] = "and erx_uploaded != '1'";
61 $sqlArr[] = "ORDER BY begdate";
63 $sql = implode(" ", $sqlArr);
64 $res = sqlStatement($sql, [$pid, $type]);
65 $list = [];
67 while ($row = sqlFetchArray($res)) {
68 if (!$row['enddate'] && !$row['returndate']) {
69 $rowclass = "noend_noreturn";
70 } elseif (!$row['enddate'] && $row['returndate']) {
71 $rowclass = "noend";
72 } elseif ($row['enddate'] && !$row['returndate']) {
73 $rowclass = "noreturn";
76 if ($type == "allergy") {
77 $reaction = "";
78 if (!empty($row['reaction'])) {
79 $reaction = getListItemTitle("reaction", $row['reaction']);
80 $row['reactionTitle'] = $reaction;
82 if (!empty($row['severity_al'])) {
83 $severity = getListItemTitle("severity_ccda", $row['severity_al']);
84 // Collapse the SNOMED-CT 272141005 List to 3 groups
85 // Not great to hard code this here, this should be abstracted
86 // to a better place to handle more comprehensive, centralized class
87 // @todo Find a better home for this
88 if (in_array($row['severity_al'], ['severe', 'life_threatening_severity', 'fatal'])) {
89 $row['critical'] = true;
91 $row['severity'] = $severity;
95 $list[] = $row;
98 return $list;
101 function getPrescriptions($pid)
103 $sql = "SELECT * FROM prescriptions WHERE patient_id = ? AND active = '1'";
104 $res = sqlStatement($sql, [$pid]);
105 $rx = [];
106 while ($row = sqlFetchArray($res)) {
107 $row['unit'] = generate_display_field(['data_type' => '1', 'list_id' => 'drug_units'], $row['unit']);
108 $row['form'] = generate_display_field(['data_type' => '1', 'list_id' => 'drug_form'], $row['form']);
109 $row['route'] = generate_display_field(['data_type' => '1', 'list_id' => 'drug_route'], $row['route']);
110 $row['interval'] = generate_display_field(['data_type' => '1', 'list_id' => 'drug_interval'], $row['interval']);
112 $rx[] = $row;
114 return $rx;
118 <script>
119 if(typeof load_location === 'undefined') {
120 function load_location(location) {
121 top.restoreSession();
122 document.location = location;
125 </script>
127 <?php
128 $erx_upload_complete = 0;
129 $old_key = "";
130 $display_current_medications_below = 1;
132 // Process Medical Problems, Allergies, and Medications
133 $reducedIssueTypes = $ISSUE_TYPES;
134 $refactoredIssues = ['allergy', 'medication', 'medical_problem'];
136 foreach ($refactoredIssues as $i) {
137 if (array_key_exists($i, $reducedIssueTypes)) {
138 unset($ISSUE_TYPES[$i]);
142 foreach ($ISSUE_TYPES as $key => $arr) {
143 // Skip if user has no access to this issue type.
144 if (!AclMain::aclCheckIssue($key)) {
145 continue;
148 if ($old_key == "medication" && $GLOBALS['erx_enable'] && $erx_upload_complete == 1) {
149 $display_current_medications_below = 0;
151 if ($GLOBALS['erx_enable']) {
152 $res = sqlStatement("SELECT * FROM prescriptions WHERE patient_id=? AND active='1'", [$pid]);
153 $list = [];
154 $rxArr = [];
155 while ($row = sqlFetchArray($res)) {
156 $row['unit'] = generate_display_field(array('data_type' => '1', 'list_id' => 'drug_units'), $row['unit']);
157 $row['form'] = generate_display_field(array('data_type' => '1', 'list_id' => 'drug_form'), $row['form']);
158 $row['route'] = generate_display_field(array('data_type' => '1', 'list_id' => 'drug_route'), $row['route']);
159 $row['interval'] = generate_display_field(array('data_type' => '1', 'list_id' => 'drug_interval'), $row['interval']);
160 $unit = ($row['size'] > 0) ? text($row['size']) . " " . $row['unit'] : "";
161 $row['unit'] = $unit;
162 $rxArr[] = $row;
165 $id = "current_prescriptions";
166 $viewArgs = [
167 'title' => xl('Current Medications'),
168 'id' => $id,
169 'initiallyCollapsed' => (getUserSetting($id) == 0) ? true : false,
170 'auth' => false,
171 'rxList' => $rxArr,
174 echo $t->render('patient/card/erx.html.twig', $viewArgs);
176 $old_key = '';
180 $issues = getListData($pid, $key);
183 if (count($issues) > 0 || $arr[4] == 1) {
184 $old_key = $key;
185 if ($GLOBALS['erx_enable'] && $key = "medication") {
186 $sqlUploadedArr = [
187 "SELECT * FROM lists WHERE pid = ? AND type = 'medication' AND",
188 dateEmptySql('enddate'),
189 "AND erx_uploaded != '1' ORDER BY begdate",
191 $sqlUploaded = implode(" ", $sqlUploadedArr);
192 $resUploaded = sqlStatement($sqlUploaded, [$pid]);
193 if (sqlNumRows($resUploaded) == 0) {
194 $erx_upload_complete = 1;
195 continue;
199 $listData = getListData($pid, $key);
200 $id = $key . "_ps_expand";
201 $viewArgs = [
202 'title' => xl($arr[0]),
203 'id' => $id,
204 'initiallyCollapsed' => (getUserSetting($id) == 0) ? true : false,
205 'linkMethod' => "javascript",
206 'list' => $listData,
207 'auth' => AclMain::aclCheckIssue($key, '', ['write', 'addonly'])
210 $btnLinkBase = "return load_location('{$GLOBALS['webroot']}/interface/__page__')";
211 if (in_array($key, ["allergy", "medication"]) && $GLOBALS["erx_enable"]) {
212 $viewArgs['btnLabel'] = "Add";
213 $btnLinkPage = "eRx.php?page=medentry";
214 } else {
215 $viewArgs['btnLabel'] = "Edit";
216 $btnLinkPage = "patient_file/summary/stats_full.php?active=all&category=" . attr_url($key);
218 $viewArgs['btnLink'] = str_replace("__page__", $btnLinkPage, $btnLinkBase);
220 if (count($listData) == 0) {
221 $viewArgs['listTouched'] = (getListTouch($pid, $key)) ? true : false;
224 if ($id == "medication_ps_expand") {
225 echo $t->render('patient/card/medication.html.twig', $viewArgs);
226 } else {
227 echo $t->render('patient/card/medical_problems.html.twig', $viewArgs);
232 // Render Cards for 2 specific forms
233 foreach (['treatment_protocols', 'injury_log'] as $formname) {
234 if (sqlNumRows(sqlStatement("SHOW TABLES LIKE 'form_{$formname}'")) > 0) {
235 $sql = "SELECT tp.id, tp.value
236 FROM forms, form_{$formname} AS tp
237 WHERE forms.pid = ?
238 AND forms.formdir = ?
239 AND tp.id = forms.form_id
240 AND tp.rownbr = -1
241 AND tp.colnbr = -1
242 AND tp.value LIKE '0%'
243 ORDER BY tp.value DESC";
244 $dres = sqlStatement($sql, [$pid, $formname]);
245 if (sqlNumRows($dres) > 0 && $need_head) {
246 $formRows = [];
247 while ($row = sqlFetchArray($dres)) {
248 list($completed, $start_date, $template_name) = explode('|', $row['value'], 3);
249 $formRows['startDate'] = $start_date;
250 $formRws['templateName'] = $template_name;
251 $formRows['id'] = $row['id'];
254 $id = "injury_log";
255 echo $t->render('patient/card/tp_il.html.twig', [
256 'title' => xl("Injury Log"),
257 'id' => $id,
258 'initiallyCollapsed' => (getUserSetting($id) == 0) ? true : false,
259 'formName' => $formname,
260 'formRows' => $formRows,
266 // Render the Immunizations card if turned on
267 if (!$GLOBALS['disable_immunizations'] && !$GLOBALS['weight_loss_clinic']) :
268 $sql = "SELECT i1.id AS id, i1.immunization_id AS immunization_id, i1.cvx_code AS cvx_code, c.code_text_short AS cvx_text,
269 IF(i1.administered_date, concat(i1.administered_date,' - ',c.code_text_short),
270 IF(i1.note,substring(i1.note,1,20),c.code_text_short)) AS immunization_data
271 FROM immunizations i1
272 LEFT JOIN code_types ct ON ct.ct_key = 'CVX'
273 LEFT JOIN codes c ON c.code_type = ct.ct_id AND i1.cvx_code = c.code
274 WHERE i1.patient_id = ?
275 AND i1.added_erroneously = 0
276 ORDER BY i1.administered_date DESC";
277 $result = sqlStatement($sql, [$pid]);
279 $imxList = [];
280 while ($row = sqlFetchArray($result)) {
281 $row['immunization_data'] = text($row['immunization_data']);
283 // Figure out which name to use (ie. from cvx list or from the custom list)
284 if ($GLOBALS['use_custom_immun_list']) {
285 $row['field'] = generate_display_field(array('data_type' => '1', 'list_id' => 'immunizations'), $row['immunization_id']);
286 } else {
287 if (!(empty($row['cvx_text']))) {
288 $row['field'] = htmlspecialchars(xl($row['cvx_text']), ENT_NOQUOTES);
289 } else {
290 $row['field'] = generate_display_field(array('data_type' => '1', 'list_id' => 'immunizations'), $row['immunization_id']);
294 $row['url'] = attr_js("immunizations.php?mode=edit&id=" . urlencode($row['id']) . "&csrf_token_form=" . urlencode(CsrfUtils::collectCsrfToken()));
295 $imxList[] = $row;
297 $id = "immunizations_ps_expand";
298 echo $t->render('patient/card/immunizations.html.twig', [
299 'title' => xl('Immunizations'),
300 'id' => $id,
301 'initiallyCollapsed' => (getUserSetting($id) == 0) ? true : false,
302 'btnLabel' => 'Edit',
303 'btnLink' => 'immunizations.php',
304 'linkMethod' => 'html',
305 'auth' => true,
306 'imx' => $imxList,
308 endif; // End immunizations
310 // Render Old Medications card
311 if ($erx_upload_complete == 1) {
312 $sql = [
313 "SELECT * FROM lists WHERE pid = ? AND type = 'medication' AND",
314 dateEmptySql('enddate'),
315 "ORDER BY begdate"
317 $res = sqlStatement(implode(" ", $sql), [$pid]);
319 $rxList = [];
320 while ($row = sqlFetchArray($res)) {
321 $rxList[] = $row;
324 $id = "old_medication_ps_expand";
325 $viewArgs = [
326 'title' => xl('Old Medication'),
327 'label' => $id,
328 'initiallyCollapsed' => (getUserSetting($id) == 0) ? true : false,
329 'btnLabel' => 'Edit',
330 'btnLink' => "return load_location(\"{$GLOBALS['webroot']}/interface/patient_file/summary/stats_full.php?active=all&category=medication\")",
331 'linkMethod' => 'javascript',
332 'auth' => true,
333 'list' => $rxList,
336 echo $t->render('patient/card/medical_problems.html.twig', $viewArgs);