Auto-create encounter reason flag and some forgotten fixes. (#7681)
[openemr.git] / interface / forms / track_anything / new.php
blobbd98bc76b2086d462a4d26ac2e629b40325e39c3
1 <?php
3 /**
4 * Encounter form to track any clinical parameter.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Joe Slam <trackanything@produnis.de>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2014 Joe Slam <trackanything@produnis.de>
11 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once(__DIR__ . "/../../globals.php");
16 require_once("$srcdir/api.inc.php");
17 require_once("$srcdir/forms.inc.php");
19 use OpenEMR\Core\Header;
21 formHeader("Form: Track anything");
23 // check if we are inside an encounter
24 if (! $encounter) { // comes from globals.php
25 die("Internal error: we do not seem to be in an encounter!");
28 // get vars posted by FORMs
29 if (empty($formid)) {
30 $formid = $_GET['id'] ?? null;
31 if (!$formid) {
32 $formid = $_POST['formid'] ?? null;
36 $myprocedureid = $_POST['procedure2track'] ?? null;
38 echo "<html><head>";
40 <?php Header::setupHeader(['datetime-picker', 'track-anything']); ?>
42 <script>
43 $(function () {
44 $('.datetimepicker').datetimepicker({
45 <?php $datetimepicker_timepicker = true; ?>
46 <?php $datetimepicker_showseconds = true; ?>
47 <?php $datetimepicker_formatInput = false; ?>
48 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
49 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
50 });
51 });
52 </script>
54 <?php
55 echo "</head><body class='body_top'>";
56 echo "<div id='track_anything'>";
58 // check if this Track is new
59 if (!$formid) {
60 // this is a new Track
62 // check if procedure is selcted
63 if ($_POST['bn_select'] ?? null) {
64 // "save"-Button was clicked, saving Form into db
66 // save inbto db
67 if ($myprocedureid) {
68 $query = "INSERT INTO form_track_anything (procedure_type_id) VALUES (?)";
69 $formid = sqlInsert($query, $myprocedureid);
70 $spell = "SELECT name FROM form_track_anything_type WHERE track_anything_type_id = ?";
71 $myrow = sqlQuery($spell, array($myprocedureid));
72 $myprocedurename = $myrow["name"];
73 $register_as = "Track: " . $myprocedurename;
74 // adding Form
75 addForm($encounter, $register_as, $formid, "track_anything", $pid, $userauthorized);
76 } else {
77 echo xlt('No track selected') . ".<br />";
78 ?><input type='button' value='<?php echo xla('Back'); ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" /><?php
80 } else {
81 // procedure is not yet selected
82 echo "<table>";
83 echo "<tr>";
84 echo "<th>" . xlt('Select Track') . ":</th>";
85 echo "</tr><tr>";
86 echo "<td>";
87 echo "<form method='post' action='" . $rootdir . "/forms/track_anything/new.php' onsubmit='return top.restoreSession()'>";
89 echo "<select name='procedure2track' size='10' style='width: 300px'>";
90 $spell = "SELECT * FROM form_track_anything_type ";
91 $spell .= "WHERE parent = 0 AND active = 1 ";
92 $spell .= "ORDER BY position ASC, name ASC ";
93 $testi = sqlStatement($spell);
94 while ($myrow = sqlFetchArray($testi)) {
95 $myprocedureid = $myrow["track_anything_type_id"];
96 $myprocedurename = $myrow["name"];
97 echo "<option value='" . attr($myprocedureid) . "'>" . text($myprocedurename) . "</option>";
100 echo "</select>";
101 echo "</td></tr><tr><td align='center'>";
102 echo "<input type='submit' name='bn_select' value='" . xla('Select') . "' />";
103 ?><input type='button' value='<?php echo xla('Back'); ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" /><?php
104 echo "</form>";
105 echo "<br />&nbsp;</td></tr>";
107 echo "<tr><td align='center'>";
108 echo "<input type='submit' name='create_track' value='" . xla('Configure tracks') . "' ";
109 ?> onclick="top.restoreSession();location='<?php echo $web_root ?>/interface/forms/track_anything/create.php'"<?php
110 echo " />";
111 echo "</td></tr>";
112 echo "</table>";
117 // instead of "else", we check again for "formid"
118 if ($formid) {
119 // this is an existing Track
120 //----------------------------------------------------
121 // get submitted item-Ids
122 $mylist = $_POST['liste'] ?? null;
123 #echo $mylist;
124 $length = count($mylist ?? []);
125 $thedate = $_POST['datetime'] ?? null;
126 #echo $thedate;
127 //check if whole input is NULL
128 $all_are_null = 0;
129 for ($i = 0; $i < $length; $i++) {
130 #echo "beep";
131 $thisid = $mylist[$i];
132 $thisvalue = $_POST[$thisid];
133 if ($thisvalue != null && $thisvalue != '') {
134 $all_are_null++;
138 // if all of the input is NULL, we do nothing
139 // if at least one entrie is NOT NULL, we save all into db
140 if ($all_are_null > 0) {
141 for ($i = 0; $i < $length; $i++) {
142 $thisid = $mylist[$i];
143 $thisvalue = $_POST[$thisid];
145 // store data to track_anything_db
146 $query = "INSERT INTO form_track_anything_results (track_anything_id, track_timestamp, itemid, result) VALUES (?, ?, ?, ?)";
147 sqlStatement($query, array($formid,$thedate,$thisid,$thisvalue));
151 //----------------------------------------------------
155 // update corrected old items
156 // ---------------------------
158 // getting old entries from <form>
159 $old_id = $_POST['old_id'] ?? null;
160 $old_time = $_POST['old_time'] ?? null;
161 $old_value = $_POST['old_value'] ?? null;
163 $how_many = count($old_time ?? []);
164 // do this for each data row
165 for ($x = 0; $x <= $how_many; $x++) {
166 // how many columns do we have
167 $how_many_cols = count($old_value[$x] ?? []);
168 for ($y = 0; $y < $how_many_cols; $y++) {
169 // here goes the UPDATE sql-spruch
170 $insertspell = "UPDATE form_track_anything_results ";
171 $insertspell .= "SET track_timestamp = ? , result = ? ";
172 $insertspell .= "WHERE id = ? ";
173 sqlStatement($insertspell, array($old_time[$x], $old_value[$x][$y], $old_id[$x][$y]));
177 //--------------------------------------------------
180 //get procedure ID
181 if (!$myprocedureid) {
182 $spell = "SELECT procedure_type_id FROM form_track_anything WHERE id = ?";
183 $myrow = sqlQuery($spell, array($formid));
184 $myprocedureid = $myrow["procedure_type_id"];
187 echo "<br /><b>" . xlt('Enter new data') . "</b>:<br />";
188 echo "<form method='post' action='" . $rootdir . "/forms/track_anything/new.php' onsubmit='return top.restoreSession()'>";
189 echo "<table>";
190 echo "<tr><th class='item'>" . xlt('Item') . "</th>";
191 echo "<th class='value'>" . xlt('Value') . "</th></tr>";
194 echo "<tr><td>" . xlt('Date Time') . "</td>";
195 echo "<td><input type='text' size='16' name='datetime' id='datetime'" .
196 "value='" . attr(date('Y-m-d H:i:s', time())) . "'" .
197 "class='datetimepicker' /></td></tr>";
200 <?php
201 // get items to track
202 $liste = array();
203 $spell = "SELECT * FROM form_track_anything_type WHERE parent = ? AND active = 1 ORDER BY position ASC, name ASC ";
204 $query = sqlStatement($spell, array($myprocedureid));
205 while ($myrow = sqlFetchArray($query)) {
206 echo "<input type='hidden' name='liste[]' value='" . attr($myrow['track_anything_type_id']) . "'>";
207 echo "<tr><td> " . text($myrow['name']) . "</td>";
208 echo "<td><input size='12' type='text' name='" . attr($myrow['track_anything_type_id']) . "'></td></tr>";
211 echo "</table>";
212 echo "<input type='hidden' name='formid' value='" . attr($formid) . "'>";
213 echo "<input type='submit' name='bn_save' value='" . xla('Save') . "' />";
214 ?><input type='button' value='<?php echo xla('Stop'); ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" /><?php
217 // show old entries of track
218 //-----------------------------------
219 // get unique timestamps of track
220 echo "<br /><br /><hr><br />";
221 echo "<b>" . xlt('Edit your entered data') . ":</b><br />";
222 $shownameflag = 0; // flag if this is <table>-headline
223 echo "<table border='1'>";
225 $spell0 = "SELECT DISTINCT track_timestamp FROM form_track_anything_results WHERE track_anything_id = ? ORDER BY track_timestamp DESC";
226 $query = sqlStatement($spell0, array($formid));
227 $main_counter = 0; // this counts 'number of rows' of old entries
228 while ($myrow = sqlFetchArray($query)) {
229 $thistime = $myrow['track_timestamp'];
230 $shownameflag++;
232 $spell = "SELECT form_track_anything_results.id AS result_id, form_track_anything_results.itemid, form_track_anything_results.result, form_track_anything_type.name AS the_name ";
233 $spell .= "FROM form_track_anything_results ";
234 $spell .= "INNER JOIN form_track_anything_type ON form_track_anything_results.itemid = form_track_anything_type.track_anything_type_id ";
235 $spell .= "WHERE track_anything_id = ? AND track_timestamp = ? AND form_track_anything_type.active = 1 ";
236 $spell .= "ORDER BY form_track_anything_type.position ASC, the_name ASC ";
237 $query2 = sqlStatement($spell, array($formid ,$thistime));
239 // <table> heading line
240 if ($shownameflag == 1) {
241 echo "<tr><th class='time'>" . xlt('Time') . "</th>";
242 while ($myrow2 = sqlFetchArray($query2)) {
243 echo "<th class='item'>" . text($myrow2['the_name']) . "</th>";
246 echo "</tr>";
249 echo "<tr><td bgcolor=#eeeeec>";
250 $main_counter++; // next row
251 echo "<input type='text' class='datetimepicker' size='16' name='old_time[" . attr($main_counter) . "]' value='" . attr($thistime) . "'></td>";
252 $query2 = sqlStatement($spell, array($formid ,$thistime));
254 $counter = 0; // this counts columns
255 while ($myrow2 = sqlFetchArray($query2)) {
256 echo "<td>";
257 echo "<input type='hidden' name='old_id[" . attr($main_counter) . "][" . attr($counter) . "]' value='" . attr($myrow2['result_id']) . "'>";
258 echo "<input type='text' size='12' name='old_value[" . attr($main_counter) . "][" . attr($counter) . "]' value='" . attr($myrow2['result']) . "'></td>";
259 $counter++; // next cloumn
262 echo "</tr>";
265 echo "</tr></table>";
266 echo "<input type='hidden' name='formid' value='" . attr($formid) . "'>";
267 echo "<input type='submit' name='bn_save' value='" . xla('Save') . "' />";
268 ?><input type='button' value='<?php echo xla('Stop'); ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" /><?php
270 echo "</form>";
271 }//end if($formid)
272 echo "</div>";
273 formFooter();