dygraphs changes for 5.0.1
[openemr.git] / interface / forms / track_anything / history.php
blobf51af622566e1797e7db2cbb3550a75ed2dc5770
1 <?php
2 /**
3 * Encounter form to track any clinical parameter.
5 * Copyright (C) 2014 Joe Slam <joe@produnis.de>
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 Joe Slam <joe@produnis.de>
20 * @link http://www.open-emr.org
23 // initial stuff
24 //---------------
27 require_once("../../globals.php");
28 include_once($GLOBALS["srcdir"] . "/api.inc");
30 $returnurl = 'encounter_top.php';
31 if(!$formid){
32 $formid = $_POST['formid']; // call from track_anything encounter
33 $fromencounter = 1;
34 if(!$formid){
35 $formid = $_GET['formid']; // call from demographic-widget "track_anything_fragement.php"
36 $fromencounter = 0;
40 if ($_POST['fromencounter'] != ''){
41 $fromencounter = $_POST['fromencounter'];
44 // get $_POSTed vars
45 //----------------------
46 $ASC_DESC = $_POST['ASC_DESC'];
48 if(!$ASC_DESC) {
49 $ASC_DESC = "DESC"; # order DESC by default
51 //---------- end $_POSTed vars
54 // set up some vars
55 //-------------------
56 $items_c = 0; # (count how many items are tracked)
57 $items_n = array(); # (save items names)
58 $row_gl = 0; # (global count of data_rows)
59 $row_lc = 0; # (local count of data_rows)
60 $hidden_loop = ''; # (collects all <input type='hidden'> entries )
61 $date_global = array(); # (collects items datetime for global rows)
62 $value_global = array(); # (collects items' values [global array])
63 $date_local = array(); # (collects items' datetime for local row)
64 $value_local = array(); # (collects item's values [local array])
65 $save_item_flag = 0; # flag to get item_names
66 $localplot = 0; # flag if local plot-button is shown
67 $localplot_c = array(); # dummy counter for localplot
68 $globalplot = 0; # flag if global plot-button is shown
69 $globalplot_c = array(); # flag if global plot-button is shown
70 $track_count = 0; # counts tracks and generates div-ids
71 //-----------end setup vars
75 echo "<html><head>";
76 // Javascript support and Javascript-functions
77 //******* **********************************
80 <?php require $GLOBALS['srcdir'] . '/js/xl/dygraphs.js.php'; ?>
82 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
83 <link rel="stylesheet" href="style.css" type="text/css">
84 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/modified/dygraphs-2-0-0/dygraph.css" type="text/css"></script>
85 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-3-2/index.js"></script>
86 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/modified/dygraphs-2-0-0/dygraph.js?v=<?php echo $v_js_includes; ?>"></script>
87 <script type="text/javascript">
88 //-------------- checkboxes checked checker --------------------
89 // Pass the checkbox name to the function
90 function getCheckedBoxes(chkboxName) {
91 var checkboxes = document.getElementsByName(chkboxName);
92 var checkedValue = [];
93 // loop over them all
94 for (var i=0; i<checkboxes.length; i++) {
95 // And stick the checked ones onto an array...
96 if (checkboxes[i].checked) {
97 checkedValue.push(checkboxes[i].value);
100 return checkedValue;
102 //---------------------------------------------------------------
105 // plot the current graph
106 //------------------------------------------------------
107 function plot_graph(checkedBoxes, theitems, thetrack, thedates, thevalues, trackCount){
108 top.restoreSession();
109 return $.ajax({ url: '<?php echo $web_root; ?>/library/ajax/graph_track_anything.php',
110 type: 'POST',
111 data: { dates: thedates,
112 values: thevalues,
113 items: theitems,
114 track: thetrack,
115 thecheckboxes: checkedBoxes
117 dataType: "json",
118 success: function(returnData){
119 g2 = new Dygraph(
120 document.getElementById("graph" + trackCount),
121 returnData.data_final,
123 title: returnData.title,
124 delimiter: '\t',
125 xRangePad: 20,
126 yRangePad: 20,
127 xlabel: xlabel_translate
131 error: function (XMLHttpRequest, textStatus, errorThrown) {
132 alert(XMLHttpRequest.responseText);
133 //alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
136 }); // end ajax query
138 //------------------------------------------------------
139 </script>
140 <?php
142 //#########################################################
143 // Here starts webpage-output
144 //-------------------------------
146 echo "</head><body class='body_top'>";
148 echo "<div id='track_anything'>";
149 // Choose output mode (order ASC vs order DESC)
150 //---------------------------------------------
151 echo "<form method='post' action='history.php' onsubmit='return top.restoreSession()'>";
152 echo "<table><tr>";
153 echo "<td class='menu'><input type='radio' name='ASC_DESC' ";
154 if($ASC_DESC == 'ASC'){ echo "checked='checked' "; }
155 echo " value='ASC'> " . xlt('ASC') . " &nbsp;";
156 echo "<input type='radio' name='ASC_DESC' ";
157 if($ASC_DESC != 'ASC'){ echo "checked='checked' ";}
158 echo " value='DESC'> " . xlt('DESC');
159 echo "</td>";
160 echo "<td class='menu'><input class='graph_button' type='submit' name='submit' value='" . xla('Order Tracks') . "' /></td>";
161 echo "<input type='hidden' name='formid' value='" . attr($formid) . "'>";
162 echo "<input type='hidden' name='fromencounter' value='" . attr($fromencounter) . "'>";
163 //--------/end Choose output ASC/DESC
165 // go to encounter or go to demographics
166 //---------------------------------------------
167 if($fromencounter == 1) {
168 echo "<td>&nbsp;&nbsp;&nbsp;<a class='css_button' href='".$GLOBALS['webroot'] . "/interface/patient_file/encounter/$returnurl' onclick='top.restoreSession()'><span>".xlt('Back to encounter')."</span></a></td>";
170 if($fromencounter == 0) {
171 echo "<td>&nbsp;&nbsp;&nbsp;<a href='../../patient_file/summary/demographics.php' ";
172 echo " class='css_button' onclick='top.restoreSession()'>";
173 echo "<span>" . xlt('Back to Patient') . "</span></a></td>";
175 //---------------------------------------------
177 echo "</tr></table>";
178 echo "</form>";
179 echo "<hr>";
182 // get name and id of selected track
183 $spell = "SELECT form_track_anything.procedure_type_id AS the_id, form_track_anything_type.name AS the_name ";
184 $spell .= "FROM form_track_anything ";
185 $spell .= "INNER JOIN form_track_anything_type ON form_track_anything.procedure_type_id = form_track_anything_type.track_anything_type_id ";
186 $spell .= "WHERE id = ? AND form_track_anything_type.active = 1";
187 //---
188 $myrow = sqlQuery($spell, array($formid));
189 $the_procedure = $myrow["the_id"];
190 $the_procedure_name = $myrow["the_name"];
193 //echo "<div>";
195 // print out track report
196 //###########################
197 echo "<h3>" . xlt('Track Report') . "</h3>";
198 echo "<table id='track_anything' border=0>";
199 echo "<tr><td> " . xlt('Track') . ": </td><td>" . text($the_procedure_name) . "</td></tr>";
200 echo "</table>";
201 echo "<hr>";
206 // get all track data, sort it by date $ASC_DESC, print it out grouped by encounter
207 $spell = "SELECT form_track_anything.id, forms.encounter, form_track_anything_results.track_timestamp AS sortdate ";
208 $spell .= "FROM form_track_anything ";
209 $spell .= "JOIN forms ON form_track_anything.id = forms.form_id ";
210 $spell .= "JOIN form_track_anything_results ON form_track_anything.id = form_track_anything_results.track_anything_id ";
211 $spell .= "WHERE form_track_anything.procedure_type_id = ? ";
212 $spell .= "AND forms.formdir = 'track_anything' AND forms.pid = ? ";
213 $spell .= "GROUP BY id ";
214 $spell .= "ORDER BY sortdate " . escape_sort_order($ASC_DESC);
215 //---
216 $query = sqlStatement($spell,array($the_procedure,$pid));
217 while($myrow = sqlFetchArray($query)){
218 $the_track = $myrow["id"];
219 $the_encounter = $myrow["encounter"];
220 $track_count++;
222 // reset local arrays;
223 $date_local = array(); # (collects items' datetime for local row)
224 $value_local = array(); # (collects item's values [local array])
225 $localplot_c = array(); // counter to decide if graph-button is shown
226 $shownameflag = 0; // show table-head ?
227 $localplot = 0; // show graph-button?
228 $col = 0; // how many Items per row
229 $row_lc = 0; // local row counter
230 //--- end reset local arrays
233 // get every single tracks
234 echo "<div id='graph" . attr($track_count) . "' class='chart-dygraphs'> </div><br>"; // here goes the graph
235 echo "<small>[" . xlt('Data from') . " ";
236 echo "<a href='../../patient_file/encounter/encounter_top.php?set_encounter=" . attr($the_encounter) . "' target='RBot'>" . xlt('encounter') . " #" . text($the_encounter) . "</a>]";
237 echo "</small>";
238 echo "<table border='1'>";
239 $spell2 = "SELECT DISTINCT track_timestamp ";
240 $spell2 .= "FROM form_track_anything_results ";
241 $spell2 .= "WHERE track_anything_id = ? ";
242 $spell2 .= "ORDER BY track_timestamp " . escape_sort_order($ASC_DESC);
243 $query2 = sqlStatement($spell2, array($the_track));
244 while($myrow2 = sqlFetchArray($query2)){
245 $thistime = $myrow2['track_timestamp'];
246 $shownameflag++;
248 // get data of this specific track
249 $spell3 = "SELECT form_track_anything_results.itemid, form_track_anything_results.result, form_track_anything_type.name AS the_name ";
250 $spell3 .= "FROM form_track_anything_results ";
251 $spell3 .= "INNER JOIN form_track_anything_type ON form_track_anything_results.itemid = form_track_anything_type.track_anything_type_id ";
252 $spell3 .= "WHERE track_anything_id = ? AND track_timestamp = ? AND form_track_anything_type.active = 1 ";
253 $spell3 .= "ORDER BY form_track_anything_results.track_timestamp " . escape_sort_order($ASC_DESC) . ", ";
254 $spell3 .= " form_track_anything_type.position ASC, the_name ASC ";
255 $query3 = sqlStatement($spell3, array($the_track, $thistime));
257 // print local <table>-heads
258 // ----------------------------
259 if ($shownameflag==1){
260 echo "<tr><th class='time'>" . xlt('Time') . "</th>";
261 while($myrow3 = sqlFetchArray($query3)){
262 echo "<th class='item'>&nbsp;" . text($myrow3['the_name']) . "&nbsp;</th>"; //
264 if($save_item_flag == 0) {
265 $items_n[$items_c] = $myrow3['the_name']; // save item names
266 $items_c++; // count number of items
268 $col++;
270 $save_item_flag++;
271 echo "</tr>";
273 //-----/end print local table head
275 // data-rows
276 echo "<tr><td class='time'>&nbsp;" . text($thistime) . "</td>";
277 $col_i = 0; // how many columns
278 $date_global[$row_gl] = $thistime; // save datetime into global array
279 $date_local[$row_lc] = $thistime; // save datetime into local array
281 $query3 = sqlStatement($spell3, array($the_track, $thistime));
282 while($myrow3 = sqlFetchArray($query3)){
283 echo "<td class='item'>&nbsp;" . text($myrow3['result']) . "&nbsp;</td>";
284 if (is_numeric($myrow3['result'])) {
285 $value_global[$col_i][$row_gl] = $myrow3['result']; // save value into global array
286 $value_local[$col_i][$row_lc] = $myrow3['result']; // save value into local array
288 $col_i++;
290 echo "</tr>";
291 $row++;
292 $row_gl++;
293 $row_lc++;
296 // check for each column if there is any numeric data
297 // and show checkbox if so...
298 //----------------------------------------------------
299 echo "<tr>";
300 echo "<td class='check'>" . xlt('Check items to graph') . " </td>"; //
301 for ($col_i = 0; $col_i < $col; $col_i++){
302 echo "<td class='check'>";
303 for ($row_b=0; $row_b <$row_lc; $row_b++) {
304 if(is_numeric($value_local[$col_i][$row_b])){
305 $localplot_c[$col_i]++; // count more than 1 to show graph-button
306 $globalplot_c[$col_i]++;
310 // show graph-checkbox only if we have more than 1 valid data
311 if ($localplot_c[$col_i] > 1 || $globalplot_c[$col_i] > 1){
312 echo "<input type='checkbox' name='check_col" . attr($track_count) . "' value='" . attr($col_i) . "'>";
313 if ($localplot_c[$col_i] > 1) {
314 $localplot++;
316 $globalplot++;
318 echo "</td>";
322 echo "</tr>";
323 echo "</tr></table>";
324 echo "<table>";
325 echo "<tr>";
326 echo "<td class='check'>" . xlt('With checked items plot') . ":</td>"; //
327 echo "<td class='check'>";
328 if ($localplot > 0){
329 echo "<input type='button' class='graph_button' onclick='get_my_graph" . attr($track_count) . "(\"local\")' name='' value='" . xla('encounter data') . "'>";
331 if ($localplot > 0 && $globalplot > 0){
332 echo "<br>";
334 if ($globalplot > 0){
335 echo "<input type='button' class='graph_button' onclick='get_my_graph" . attr($track_count) . "(\"global\")' name='' value='" . xla('data of all encounters so far') . "'>";
337 echo "</td>";
338 echo "</tr>";
339 echo "</table>";
340 echo "<br><hr>";
341 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
342 // onClick create graph javascript method
343 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
346 <script type="text/javascript">
347 function get_my_graph<?php echo attr($track_count); ?>(where){
348 top.restoreSession();
349 if(where=="local"){
350 //alert("local");
351 var thedates = JSON.stringify(<?php echo json_encode($date_local); ?>);
352 var thevalues = JSON.stringify(<?php echo json_encode($value_local); ?>);
354 if(where=="global"){
355 //alert("global");
356 var thedates = JSON.stringify(<?php echo json_encode($date_global); ?>);
357 var thevalues = JSON.stringify(<?php echo json_encode($value_global); ?>);
360 var checkedBoxes = JSON.stringify(getCheckedBoxes("check_col<?php echo attr($track_count) ?>"));
361 var theitems = JSON.stringify(<?php echo json_encode($items_n); ?>);
362 var thetrack = JSON.stringify(<?php echo json_encode($the_procedure_name); ?>);
363 plot_graph(checkedBoxes, theitems, thetrack, thedates, thevalues, <?php echo attr($track_count); ?>);
365 </script>
366 <?php
367 // ~~~~~~~~~~~~~~~~~ / end javascript method ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
368 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
369 } // end while get all trackdata
372 //#########################################################
373 // This is the End
374 echo "<p>" . xlt('End of report') . ".</p>";
376 echo "<hr>";
378 // Choose output mode (order ASC vs order DESC)
379 //---------------------------------------------
380 echo "<form method='post' action='history.php' onsubmit='return top.restoreSession()'>";
381 echo "<table><tr>";
382 echo "<td class='menu'><input type='radio' name='ASC_DESC' ";
383 if($ASC_DESC == 'ASC'){ echo "checked='checked' "; }
384 echo " value='ASC'> " . xlt('ASC') . " &nbsp;";
385 echo "<input type='radio' name='ASC_DESC' ";
386 if($ASC_DESC != 'ASC'){ echo "checked='checked' ";}
387 echo " value='DESC'> " . xlt('DESC');
388 echo "</td>";
389 echo "<td class='menu'><input class='graph_button' type='submit' name='submit' value='" . xlt('Order Tracks') . "' /></td>";
390 echo "<input type='hidden' name='formid' value='" . attr($formid) . "'>";
391 echo "<input type='hidden' name='fromencounter' value='" . attr($fromencounter) . "'>";
392 //--------/end Choose output ASC/DESC
395 // go to encounter or go to demographics
396 //---------------------------------------------
397 if($fromencounter == 1) {
398 echo "<td>&nbsp;&nbsp;&nbsp;<a class='css_button' href='".$GLOBALS['webroot'] . "/interface/patient_file/encounter/$returnurl' onclick='top.restoreSession()'><span>".xlt('Back to encounter')."</span></a></td>";
400 if($fromencounter == 0) {
401 echo "<td>&nbsp;&nbsp;&nbsp;<a href='../../patient_file/summary/demographics.php' ";
402 echo " class='css_button' onclick='top.restoreSession()'>";
403 echo "<span>" . htmlspecialchars(xl('Back to Patient'),ENT_NOQUOTES) . "</span></a></td>";
405 //---------------------------------------------
406 echo "</tr></table>";
407 echo "</form>";
408 echo "</div>";
409 //------------ that's it, bye
410 formFooter();