bug fix march continued (#1921)
[openemr.git] / interface / de_identification_forms / de_identification_screen2.php
blob32c716ae8d3ab1968a590f862793cad79254de6d
1 <?php
2 /********************************************************************************\
3 * Copyright (C) ViCarePlus, Visolve (vicareplus_engg@visolve.com) *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 \********************************************************************************/
21 require_once("../globals.php");
22 require_once("$srcdir/lists.inc");
23 require_once("$srcdir/patient.inc");
24 require_once("$srcdir/acl.inc");
25 require_once("$srcdir/options.inc.php");
27 if (!acl_check('admin', 'super')) {
28 die(xlt('Not authorized'));
31 if (!verifyCsrfToken($_POST["csrf_token_form"])) {
32 csrfNotVerified();
35 /*executes the De Identification process, using the parameters chosen from the
36 de_identification_screen1.php */
37 $begin_date = $_POST["begin_date"];
38 $end_date = $_POST["end_date"];
40 if ($_POST["unstructured"]) {
41 $include_unstructured = 1;
42 } else {
43 $include_unstructured = 0;
46 if ($_POST["all"]) {
47 $include_tables = "all";
48 } else {
49 if ($_POST["history_data"]) {
50 $include_tables = $include_tables . $_POST["history_data"]. "#";
53 if ($_POST["prescriptions"]) {
54 $include_tables = $include_tables . $_POST["prescriptions"]. "#";
57 if ($_POST["immunization"]) {
58 $include_tables = $include_tables . $_POST["immunization"]. "#";
61 if ($_POST["lists"]) {
62 $include_tables = $include_tables . $_POST["lists"]. "#";
65 if ($_POST["transactions"]) {
66 $include_tables = $include_tables . $_POST["transactions"]. "#";
69 if ($_POST["insurance_data"]) {
70 $include_tables = $include_tables . $_POST["insurance_data"]. "#";
73 if ($_POST["billing_data"]) {
74 $include_tables = $include_tables . "billing#payments";
78 $diagnosis_text = $_POST["diagnosis_text"];
79 $drug_text = $_POST["drug_text"];
80 $immunization_text = $_POST["immunization_text"];
82 $query = "select status from de_identification_status";
83 $res = sqlStatement($query);
84 if ($row = sqlFetchArray($res)) {
85 $deIdentificationStatus = $row['status'];
86 /* $deIdentificationStatus:
87 * 0 - There is no De Identification in progress. (start new De Identification process)
88 * 1 - A De Identification process is currently in progress.
89 * 2 - The De Identification process completed and xls file is ready to download
90 * 3 - The De Identification process completed with error
94 if ($deIdentificationStatus == 0) {
95 //0 - There is no De Identification in progress. (start new De Identification process)
97 <html>
98 <head>
99 <title>De Identification</title>
100 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
101 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
102 <style type="text/css">
103 .style1 {
104 text-align: center;
106 </style>
107 </head>
108 <body class="body_top">
109 <strong>De Identification</strong>
110 <form name="De Identification1" id="De Identification1" method="post"><br />
111 <?php
113 $query = "SELECT count(*) as count FROM metadata_de_identification";
114 $res = sqlStatement($query);
115 if ($row = sqlFetchArray($res)) {
116 $no_of_items = $row['count'];
117 if ($no_of_items == 0) {
118 $cmd="cp " . escapeshellarg($GLOBALS['webserver_root']."/sql/metadata_de_identification.txt") . " " . escapeshellarg($GLOBALS['temporary_files_dir']."/metadata_de_identification.txt");
119 $output3=shell_exec($cmd);
120 $query = "LOAD DATA INFILE '" . add_escape_custom($GLOBALS['temporary_files_dir']) ."/metadata_de_identification.txt' INTO TABLE metadata_de_identification FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'";
121 $res = sqlStatement($query);
125 //create transaction tables
126 $query = "call create_transaction_tables()";
127 $res = sqlStatement($query);
129 //write input to data base
130 $query = "delete from param_include_tables";
131 $res = sqlStatement($query);
133 $query = "insert into param_include_tables values (?, ?)";
134 $res = sqlStatement($query, array($include_tables, $include_unstructured));
136 $query = "delete from param_filter_pid";
137 $res = sqlStatement($query);
139 $query = "insert into param_filter_pid values (?, ?, ?, ?, ?)";
140 $res = sqlStatement($query, array($begin_date, $end_date, $diagnosis_text, $drug_text, $immunization_text));
142 //process running
143 $query = "update de_identification_status set status = 1";
144 $res = sqlStatement($query);
146 try {
147 //call procedure - execute in background
148 $sh_cmd='./de_identification_procedure.sh ' . escapeshellarg($sqlconf["host"]) . ' ' . escapeshellarg($sqlconf["login"]) . ' ' . escapeshellarg($sqlconf["pass"]) . ' ' . escapeshellarg($sqlconf["dbase"]) . ' &';
149 system($sh_cmd);
152 $query = "SELECT status FROM de_identification_status ";
153 $res = sqlStatement($query);
154 if ($row = sqlFetchArray($res)) {
155 $de_identification_status = $row['status'];
156 if ($de_identification_status == 2 || $de_identification_status == 3) {
157 //2 - The De Identification process completed and xls file is ready to download
158 //3 - The De Identification process completed with error
159 $query = "SELECT count(*) as count FROM de_identified_data ";
160 $res = sqlStatement($query);
161 if ($row = sqlFetchArray($res)) {
162 $no_of_items = $row['count'];
163 if ($no_of_items <= 1) {
165 <table>
166 <tr>
167 <td>&nbsp;</td>
168 <td>&nbsp;</td>
169 </tr>
170 <tr>
171 <td>&nbsp;</td>
172 <td>&nbsp;</td>
173 </tr>
174 </table>
175 <table class="de_identification_status_message" align="center">
176 <tr valign="top">
178 <td>&nbsp;</td>
179 <td rowspan="3"><br>
180 <?php echo xlt('No Patient record found for given Selection criteria');
181 echo "</br></br>";
182 echo xlt('Please start new De Identification process');
183 echo "</br>"; ?> </br>
184 </td>
185 <td>&nbsp;</td>
186 </tr>
187 <tr>
188 <td>&nbsp;</td>
189 <td>&nbsp;</td>
190 </tr>
191 <tr>
192 <td>&nbsp;</td>
193 <td>&nbsp;</td>
194 </tr>
195 </table>
196 <table align="center">
197 <tr>
198 <td>&nbsp;</td>
199 <td>&nbsp;</td>
200 </tr>
201 </table>
203 <?php
204 } else { //delete old de_identified_data.xls file
205 $timestamp=0;
206 $query = "select now() as timestamp";
207 $res = sqlStatement($query);
208 if ($row = sqlFetchArray($res)) {
209 $timestamp = $row['timestamp'];
212 $timestamp = str_replace(" ", "_", $timestamp);
213 $de_identified_file = $GLOBALS['temporary_files_dir']."/de_identified_data".$timestamp.".xls";
214 $query = "update de_identification_status set last_available_de_identified_data_file = ?";
215 $res = sqlStatement($query, array($de_identified_file));
216 $query = "select * from de_identified_data into outfile '" . add_escape_custom($de_identified_file) . "' ";
217 $res = sqlStatement($query);
219 <table>
220 <tr>
221 <td>&nbsp;</td>
222 <td>&nbsp;</td>
223 </tr>
224 <tr>
225 <td>&nbsp;</td>
226 <td>&nbsp;</td>
227 </tr>
228 </table>
229 <table class="de_identification_status_message" align="center">
230 <tr valign="top">
231 <td>&nbsp;</td>
232 <td rowspan="3"><br>
233 <?php echo xlt('De Identification Process is ongoing');
234 echo "</br></br>";
235 echo xlt('Please visit De Identification screen after some time');
236 echo "</br>"; ?> </br>
237 </td>
238 <td>&nbsp;</td>
239 </tr>
240 <tr>
241 <td>&nbsp;</td>
242 <td>&nbsp;</td>
243 </tr>
244 <tr>
245 <td>&nbsp;</td>
246 <td>&nbsp;</td>
247 </tr>
248 </table>
249 <table align="center">
250 <tr>
251 <td>&nbsp;</td>
252 <td>&nbsp;</td>
253 </tr>
254 </table>
255 <?php
260 } catch (Exception $e) {
261 //error status
262 $query = "update de_identification_status set status = 3";
263 $res = sqlStatement($query);
265 } else if ($deIdentificationStatus == 2 or $deIdentificationStatus == 3) {
266 //2 - The De Identification process completed and xls file is ready to download
267 //3 - The De Identification process completed with error
268 $query = "select last_available_de_identified_data_file from de_identification_status";
269 $res = sqlStatement($query);
270 if ($row = sqlFetchArray($res)) {
271 $filename = $row['last_available_de_identified_data_file'];
274 ob_end_clean();
275 if (file_exists($filename)) {
276 header('Content-Description: File Transfer');
277 header('Content-Type: application/octet-stream');
278 header('Content-Disposition: attachment; filename='.basename($filename));
279 header('Content-Transfer-Encoding: binary');
280 header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera
281 header("Content-type: application/x-msexcel"); // This should work for the rest
282 header('Expires: 0');
283 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
284 header('Pragma: public');
285 header('Content-Length: ' . filesize($filename));
286 ob_clean();
287 flush();
288 readfile($filename);
291 //xls file downloaded complete
292 $query = "update de_identification_status set status = 0";
293 $res = sqlStatement($query);
296 </body>
297 </html>