Code type module improvements:
[openemr.git] / library / report_database.inc
blob47a1fca2ac869746cb9f3dca77e7e878ed81d798
1 <?php
2 /**
3  * Report tracking, storing and viewing functions using the report_results sql table.
4  *
5  * Supports generic tracking, storing and viewing of reports by utilizing a vertical
6  * table entitled report_results. This allows flexible placement of tokens for report
7  * setting etc.
8  * <pre>Tokens that are reserved include:
9  *   'bookmark'          - Allows bookmarking of a new report id (used to allow tracking
10  *                         progress via ajax calls). If exist, value is always set to '1'.
11  *   'progress'          - Either set to 'pending' or 'complete'.
12  *   'type'              - Set to type of report
13  *   'total_items'       - Set to total number of items that will be processed (ie. such as patients)
14  *   'progress_items'    - Set to number of items (ie. such as patients)
15  *   'data'              - Contains the data of the report
16  *   'date_report'       - Set to date of the report (date and time)
17  *   'date_report_complete'       - Set to date of the report completion (date and time)
18  * </pre>
19  *
20  * These functions should not ever attempt to write to
21  * session variables, because the session_write_close() function
22  * is typically called before utilizing these functions.
23  *
24  * Copyright (C) 2012 Brady Miller <brady@sparmy.com>
25  *
26  * LICENSE: This program is free software; you can redistribute it and/or
27  * modify it under the terms of the GNU General Public License
28  * as published by the Free Software Foundation; either version 2
29  * of the License, or (at your option) any later version.
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33  * GNU General Public License for more details.
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
36  *
37  * @package OpenEMR
38  * @author  Brady Miller <brady@sparmy.com>
39  * @link    http://www.open-emr.org
40  */
42 /**
43  * Return listing of report results.
44  *
45  * @param   timestamp  $start  Start of date range
46  * @param   timestamp  $end    End of date range
47  * @return  sql-query          Listing of report results
48  */
49 function listingReportDatabase($start_date='',$end_date='') {
51   // set $end_date to today's date if empty
52   $end_date = ($end_date) ? $end_date : date('Y-m-d H:i:s'); 
54   // Collect pertinent information as a pivot table (ie. converting vertical to horizontal row)
55   if (empty($start_date)) {
56     $res = sqlStatement("SELECT *, TIMESTAMPDIFF(MINUTE,pt.date_report,pt.date_report_complete) as `report_time_processing`
57                          FROM (
58                            SELECT `report_id`,
59                            MAX(if( `field_id` = 'date_report', `field_value`, 0 )) as `date_report`,
60                            MAX(if( `field_id` = 'date_report_complete', `field_value`, 0 )) as `date_report_complete`,
61                            MAX(if( `field_id` = 'progress', `field_value`, 0 )) as `progress`,
62                            MAX(if( `field_id` = 'total_items', `field_value`, 0 )) as `total_items`,
63                            MAX(if( `field_id` = 'progress_items', `field_value`, 0 )) as `progress_items`,
64                            MAX(if( `field_id` = 'type', `field_value`, 0 )) as `type`
65                            FROM `report_results`
66                            GROUP BY `report_id`
67                          ) AS pt
68                          WHERE pt.date_report < ?
69                          ORDER BY pt.report_id", array($end_date));
70   }
71   else {
72     $res = sqlStatement("SELECT *, TIMESTAMPDIFF(MINUTE,pt.date_report,pt.date_report_complete) as `report_time_processing`
73                          FROM (
74                            SELECT `report_id`,
75                            MAX(if( `field_id` = 'date_report', `field_value`, 0 )) as `date_report`,
76                            MAX(if( `field_id` = 'date_report_complete', `field_value`, 0 )) as `date_report_complete`,
77                            MAX(if( `field_id` = 'progress', `field_value`, 0 )) as `progress`,
78                            MAX(if( `field_id` = 'total_items', `field_value`, 0 )) as `total_items`,
79                            MAX(if( `field_id` = 'progress_items', `field_value`, 0 )) as `progress_items`,
80                            MAX(if( `field_id` = 'type', `field_value`, 0 )) as `type`
81                            FROM `report_results`
82                            GROUP BY `report_id`
83                          ) AS pt
84                          WHERE pt.date_report > ? AND pt.date_report < ?
85                          ORDER BY pt.report_id", array($start_date,$end_date));    
86   }
88   return $res;
91 /**
92  * Simply reserves a report id for use in the report results tracking/storing/viewing item in database..
93  *
94  * @return  integer           Report id that was assigned in database
95  */
96 function bookmarkReportDatabase() {
98   // Retrieve a new report id
99   $query = sqlQuery("SELECT max(`report_id`) as max_report_id FROM `report_results`");
100   if (empty($query)) {
101     $new_report_id = 1;
102   }
103   else {
104    $new_report_id = $query['max_report_id'] + 1;
105   }
107   // Set the bookmark token
108   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"bookmark",1));
110   return $new_report_id;
114  * Initiate a report results tracking/storing/viewing item in database.
116  * @param   string   $type       Report type identifier
117  * @param   array    $fields     Array containing pertinent report details (Do NOT use 'bookmark', 'progress','type','progress_patients', 'data', 'date_report' or 'no_json_support' as keys in array; they will be ignored)
118  * @param   integer  $report_id  Report id (if have already bookmarked a report id)
119  * @return  integer              Report id that is assigned to the report
120  */
121 function beginReportDatabase($type,$fields,$report_id=NULL) {
123   // Retrieve a new report id, if needed.
124   if (empty($report_id)) {
125     $query = sqlQuery("SELECT max(`report_id`) as max_report_id FROM `report_results`");
126     if (empty($query)) {
127       $new_report_id = 1;
128     }
129     else {
130      $new_report_id = $query['max_report_id'] + 1;
131     }
132   }
133   else {
134     $new_report_id = $report_id;
135   }
137   // Set the required tokens
138   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"progress","pending"));
139   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"type",$type));
140   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"progress_items","0"));
141   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"data",""));
142   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"date_report",date("Y-m-d H:i:s")));
144   // Set the fields tokens
145   if (!empty($fields)) {
146    foreach ($fields as $key=>$value) {
148     // skip the special tokens
149     if (($key == "type") ||
150         ($key == "data") ||
151         ($key == "progress") ||
152         ($key == "progress_items") ||
153         ($key == "total_items") ||
154         ($key == "date_report") ||
155         ($key == "date_report_complete") ||
156         ($key == "bookmark") ) continue;
158     // place the token
159     sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,$key,$value));
160    }
161   }
163   // Return the report id
164   return $new_report_id;
168  * Insert total items to process in database.
169  * For performance reasons, it is assumed that the total_items does not already exists in current database entry.
171  * @param   integer  $report_id    Report id
172  * @param   integer  $total_items  Total number of items that will be processed
173  */
174 function setTotalItemsReportDatabase($report_id,$total_items) {
175   // Insert the total items that are to be processed
176   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($report_id,"total_items",$total_items));
180  * Update report results in database(basically update number of items (patients) that has been processed in pending reports).
181  * For performance reasons, it is assumed that the progress_items token already exists in current database entry.
183  * @param   integer  $report_id           Report id
184  * @param   integer  $items_processed  Number of items that have been processed
185  */
186 function updateReportDatabase($report_id,$items_processed) {
187   // Update the items that have been processed
188   sqlStatement("UPDATE `report_results` SET `field_value`=? WHERE `report_id`=? AND `field_id`='progress_items'", array($items_processed,$report_id) );
192  * Store (finished) report results (in json format) in database.
193  * For performance reasons, it is assumed that the data and progress tokens already exists in current database entry.
194  * For performance reasons, it is assumed that the date_report_complete does not already exists in current database entry.
196  * @param   integer  $report_id  Report id
197  * @param   string   $data       Report results/data
198  */
199 function finishReportDatabase($report_id,$data) {
201   // Record the data
202   sqlStatement("UPDATE `report_results` SET `field_value`=? WHERE `report_id`=? AND `field_id`='data'", array($data,$report_id) );
204   // Record the finish date/time
205   sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($report_id,"date_report_complete",date("Y-m-d H:i:s")));
207   // Set progress to complete
208   sqlStatement("UPDATE `report_results` SET `field_value`='complete' WHERE `report_id`=? AND `field_id`='progress'", array($report_id) );
212  * Collect report results from database.
214  * @param   integer  $report_id  Report id
215  * @return  array                Array of id/values for a report
216  */
217 function collectReportDatabase($report_id) {
219   // Collect the rows of data
220   $res = sqlStatement("SELECT * FROM `report_results` WHERE `report_id`=?", array($report_id));
221   
222   // Convert data into an array
223   $final_array = array();
224   while ($row = sqlFetchArray($res)) {
225     $final_array = array_merge($final_array,array($row['field_id']=>$row['field_value']));
226   }
228   return $final_array;
232  * Get status of report from database.
234  * @param   integer  $report_id  Report id
235  * @return  string               Status report (PENDING, COMPLETE, or return a string with progress)
236  */
237 function getStatusReportDatabase($report_id) {
239   // Collect the pertinent rows of data
240   $res = sqlStatement("SELECT `field_id`, `field_value` FROM `report_results` WHERE `report_id`=? AND (`field_id`='progress' OR `field_id`='total_items' OR `field_id`='progress_items')", array($report_id));
242   // If empty, then just return Pending, since stil haven't likely created the entries yet
243   if (sqlNumRows($res) < 1) {
244     return "PENDING";
245   }
247   // Place into an array for quick processing
248   $final_array = array();
249   while ($row = sqlFetchArray($res)) {
250     $final_array = array_merge($final_array,array($row['field_id']=>$row['field_value']));
251   }
253   if ($final_array['progress'] == "complete") {
254     // return COMPLETE
255     return "COMPLETE";
256   }
257   else {
258     $final_array['progress_items'] = ($final_array['progress_items']) ? $final_array['progress_items'] : 0;
259     return $final_array['progress_items'] . " / " . $final_array['total_items'] . " " . xl("Patients");
260   }