3 * Report tracking, storing and viewing functions using the report_results sql table.
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. Also supports itemization of results (per patient tracking).
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)
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.
24 * Copyright (C) 2012 Brady Miller <brady.g.miller@gmail.com>
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>;.
38 * @author Brady Miller <brady.g.miller@gmail.com>
39 * @link http://www.open-emr.org
43 * Return listing of report results.
45 * @param timestamp $start Start of date range
46 * @param timestamp $end End of date range
47 * @return sql-query Listing of report results
49 function listingReportDatabase($start_date = '', $end_date = '')
52 // set $end_date to today's date if empty
53 $end_date = ($end_date) ? $end_date : date('Y-m-d H:i:s');
55 // Collect pertinent information as a pivot table (ie. converting vertical to horizontal row)
56 if (empty($start_date)) {
57 $res = sqlStatement("SELECT *, TIMESTAMPDIFF(MINUTE,pt.date_report,pt.date_report_complete) as `report_time_processing`
60 MAX(if( `field_id` = 'date_report', `field_value`, 0 )) as `date_report`,
61 MAX(if( `field_id` = 'date_report_complete', `field_value`, 0 )) as `date_report_complete`,
62 MAX(if( `field_id` = 'progress', `field_value`, 0 )) as `progress`,
63 MAX(if( `field_id` = 'total_items', `field_value`, 0 )) as `total_items`,
64 MAX(if( `field_id` = 'progress_items', `field_value`, 0 )) as `progress_items`,
65 MAX(if( `field_id` = 'type', `field_value`, 0 )) as `type`
69 WHERE pt.date_report < ?
70 ORDER BY pt.report_id", array($end_date));
72 $res = sqlStatement("SELECT *, TIMESTAMPDIFF(MINUTE,pt.date_report,pt.date_report_complete) as `report_time_processing`
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`
84 WHERE pt.date_report > ? AND pt.date_report < ?
85 ORDER BY pt.report_id", array($start_date,$end_date));
92 * Simply reserves a report id for use in the report results tracking/storing/viewing item in database..
94 * @return integer Report id that was assigned in database
96 function bookmarkReportDatabase()
99 // Retrieve a new report id
100 $query = sqlQuery("SELECT max(`report_id`) as max_report_id FROM `report_results`");
104 $new_report_id = $query['max_report_id'] + 1;
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
121 function beginReportDatabase($type, $fields, $report_id = null)
124 // Retrieve a new report id, if needed.
125 if (empty($report_id)) {
126 $query = sqlQuery("SELECT max(`report_id`) as max_report_id FROM `report_results`");
130 $new_report_id = $query['max_report_id'] + 1;
133 $new_report_id = $report_id;
136 // Set the required tokens
137 sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"progress","pending"));
138 sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"type",$type));
139 sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"progress_items","0"));
140 sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,"data",""));
141 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")));
143 // Set the fields tokens
144 if (!empty($fields)) {
145 foreach ($fields as $key => $value) {
146 // skip the special tokens
147 if (($key == "type") ||
149 ($key == "progress") ||
150 ($key == "progress_items") ||
151 ($key == "total_items") ||
152 ($key == "date_report") ||
153 ($key == "date_report_complete") ||
154 ($key == "bookmark") ) {
159 sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($new_report_id,$key,$value));
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
174 function setTotalItemsReportDatabase($report_id, $total_items)
176 // Insert the total items that are to be processed
177 sqlStatement("INSERT INTO `report_results` (`report_id`,`field_id`,`field_value`) VALUES (?,?,?)", array ($report_id,"total_items",$total_items));
181 * Update report results in database(basically update number of items (patients) that has been processed in pending reports).
182 * For performance reasons, it is assumed that the progress_items token already exists in current database entry.
184 * @param integer $report_id Report id
185 * @param integer $items_processed Number of items that have been processed
187 function updateReportDatabase($report_id, $items_processed)
189 // Update the items that have been processed
190 sqlStatement("UPDATE `report_results` SET `field_value`=? WHERE `report_id`=? AND `field_id`='progress_items'", array($items_processed,$report_id));
194 * Store (finished) report results (in json format) in database.
195 * For performance reasons, it is assumed that the data and progress tokens already exists in current database entry.
196 * For performance reasons, it is assumed that the date_report_complete does not already exists in current database entry.
198 * @param integer $report_id Report id
199 * @param string $data Report results/data
201 function finishReportDatabase($report_id, $data)
205 sqlStatement("UPDATE `report_results` SET `field_value`=? WHERE `report_id`=? AND `field_id`='data'", array($data,$report_id));
207 // Record the finish date/time
208 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")));
210 // Set progress to complete
211 sqlStatement("UPDATE `report_results` SET `field_value`='complete' WHERE `report_id`=? AND `field_id`='progress'", array($report_id));
215 * Collect report results from database.
217 * @param integer $report_id Report id
218 * @return array Array of id/values for a report
220 function collectReportDatabase($report_id)
223 // Collect the rows of data
224 $res = sqlStatement("SELECT * FROM `report_results` WHERE `report_id`=?", array($report_id));
226 // Convert data into an array
227 $final_array = array();
228 while ($row = sqlFetchArray($res)) {
229 $final_array = array_merge($final_array, array($row['field_id']=>$row['field_value']));
236 * Get status of report from database.
238 * @param integer $report_id Report id
239 * @return string Status report (PENDING, COMPLETE, or return a string with progress)
241 function getStatusReportDatabase($report_id)
244 // Collect the pertinent rows of data
245 $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));
247 // If empty, then just return Pending, since stil haven't likely created the entries yet
248 if (sqlNumRows($res) < 1) {
252 // Place into an array for quick processing
253 $final_array = array();
254 while ($row = sqlFetchArray($res)) {
255 $final_array = array_merge($final_array, array($row['field_id']=>$row['field_value']));
258 if ($final_array['progress'] == "complete") {
262 $final_array['progress_items'] = ($final_array['progress_items']) ? $final_array['progress_items'] : 0;
263 return $final_array['progress_items'] . " / " . $final_array['total_items'] . " " . xl("Patients");
268 * Insert itemization item into database.
270 * @param integer $report_id Report id
271 * @param integer $itemized_test_id Itemized test id
272 * @param integer $pass 0 is fail, 1 is pass, 2 is exclude
273 * @param integer $patient_id Patient pid
274 * @param integer $$numerator_label Numerator label (if applicable)
276 function insertItemReportTracker($report_id, $itemized_test_id, $pass, $patient_id, $numerator_label = '')
278 $sqlParameters = array($report_id,$itemized_test_id,$numerator_label,$pass,$patient_id);
279 sqlStatementCdrEngine("INSERT INTO `report_itemized` (`report_id`,`itemized_test_id`,`numerator_label`,`pass`,`pid`) VALUES (?,?,?,?,?)", $sqlParameters);
283 * Collect a rules display title for itemized report.
285 * @param integer $report_id Report id
286 * @param integer $itemized_test_id Itemized test id
287 * @param integer $numerator_label Numerator label (if applicable)
288 * @return string/boolean Rule title for itemization display (false if nothing found)
290 function collectItemizedRuleDisplayTitle($report_id, $itemized_test_id, $numerator_label = '')
293 $report_view = collectReportDatabase($report_id);
294 $type_report = $report_view['type'];
295 $dataSheet = json_decode($report_view['data'], true);
296 foreach ($dataSheet as $row) {
297 if (isset($row['is_main']) || isset($row['is_sub'])) {
298 if (isset($row['is_main'])) {
300 $dispTitle = generate_display_field(array('data_type'=>'1','list_id'=>'clinical_rules'), $row['id']);
303 if (($row['itemized_test_id'] == $itemized_test_id) && ($row['numerator_label'] == $numerator_label)) {
304 // We have a hit, build on the $dispTitle created above
305 if (isset($row['is_main'])) {
306 $tempCqmAmcString = "";
307 if (($type_report == "cqm") || ($type_report == "cqm_2011") || ($type_report == "cqm_2014")) {
308 if (!empty($row['cqm_pqri_code'])) {
309 $tempCqmAmcString .= " " . xlt('PQRI') . ":" . text($row['cqm_pqri_code']) . " ";
312 if (!empty($row['cqm_nqf_code'])) {
313 $tempCqmAmcString .= " " . xlt('NQF') . ":" . text($row['cqm_nqf_code']) . " ";
317 if ($type_report == "amc") {
318 if (!empty($row['amc_code'])) {
319 $tempCqmAmcString .= " " . xlt('AMC-2011') . ":" . text($row['amc_code']) . " ";
322 if (!empty($row['amc_code_2014'])) {
323 $tempCqmAmcString .= " " . xlt('AMC-2014') . ":" . text($row['amc_code_2014']) . " ";
327 if ($type_report == "amc_2011") {
328 if (!empty($row['amc_code'])) {
329 $tempCqmAmcString .= " " . xlt('AMC-2011') . ":" . text($row['amc_code']) . " ";
333 if ($type_report == "amc_2014_stage1") {
334 if (!empty($row['amc_code_2014'])) {
335 $tempCqmAmcString .= " " . xlt('AMC-2014 Stage I'). ":" . text($row['amc_code_2014']) . " ";
339 if ($type_report == "amc_2014_stage2") {
340 if (!empty($row['amc_code_2014'])) {
341 $tempCqmAmcString .= " " . xlt('AMC-2014 Stage II'). ":" . text($row['amc_code_2014']) . " ";
345 if (!empty($tempCqmAmcString)) {
346 $dispTitle .= "(".$tempCqmAmcString.")";
349 if (!(empty($row['concatenated_label']))) {
350 $dispTitle .= ", " . xlt($row['concatenated_label']) . " ";
352 } else { // isset($row['is_sub']
353 $dispTitle .= " - " . generate_display_field(array('data_type'=>'1','list_id'=>'rule_action_category'), $row['action_category']);
354 $dispTitle .= ": " . generate_display_field(array('data_type'=>'1','list_id'=>'rule_action'), $row['action_item']);
366 * Collect patient listing from CDR reports itemization.
368 * @param integer $report_id Report id
369 * @param integer $itemized_test_id Itemized test id
370 * @param string $pass options are 'fail', 'pass', 'exclude', 'init_patients', 'exception' and 'all'
371 * @param integer $numerator_label Numerator label (if applicable)
372 * @param integer $sqllimit Sql query pagination info
373 * @param integer $fstart Sql query pagination info
374 * @return array/integer Array list or a count
376 function collectItemizedPatientsCdrReport($report_id, $itemized_test_id, $pass = 'all', $numerator_label = '', $count = false, $sqllimit = 'all', $fstart = 0)
380 $given = " COUNT(DISTINCT `patient_data`.`pid`) AS total_listings ";
382 $given = " DISTINCT `patient_data`.*, DATE_FORMAT(`patient_data`.`DOB`,'%m/%d/%Y') as DOB_TS ";
385 $orderby = " `patient_data`.`lname` ASC, `patient_data`.`fname` ASC ";
398 case "init_patients":
406 $sqlParameters = array($report_id,$itemized_test_id,$numerator_label);
408 if ($pass == "all") {
409 $sql_where = " WHERE `report_itemized`.`pass` != 3 AND `report_itemized`.`report_id` = ? AND `report_itemized`.`itemized_test_id` = ? AND `report_itemized`.`numerator_label` = ? ";
410 } else if ($pass == "fail") {
411 $exlPidArr = array();
412 $exludeResult = collectItemizedPatientsCdrReport($report_id, $itemized_test_id, 'exclude', $numerator_label, false, $sqllimit, $fstart);
413 foreach ($exludeResult as $exlResArr) {
414 $exlPidArr[] = $exlResArr['pid'];
417 $sql_where = " WHERE `report_itemized`.`report_id` = ? AND `report_itemized`.`itemized_test_id` = ? AND `report_itemized`.`numerator_label` = ? AND `report_itemized`.`pass` = ? ";
419 if (count($exlPidArr) > 0) {
420 $exlPids = implode(",", $exlPidArr);
421 $sql_where .= " AND patient_data.pid NOT IN(" . add_escape_custom($exlPids) . ") ";
424 array_push($sqlParameters, $pass_sql);
426 $sql_where = " WHERE `report_itemized`.`report_id` = ? AND `report_itemized`.`itemized_test_id` = ? AND `report_itemized`.`numerator_label` = ? AND `report_itemized`.`pass` = ? ";
427 array_push($sqlParameters, $pass_sql);
430 $sql_query = "SELECT " . $given . " FROM `patient_data` JOIN `report_itemized` ON `patient_data`.`pid` = `report_itemized`.`pid` " . $sql_where. " ORDER BY " . $orderby;
432 if ($sqllimit != "all") {
433 $sql_query .= " limit " . escape_limit($fstart) . ", " . escape_limit($sqllimit);
437 $rez = sqlQueryCdrEngine($sql_query, $sqlParameters);
438 return $rez['total_listings'];
440 $rez = sqlStatementCdrEngine($sql_query, $sqlParameters);
441 // create array of listing for return
442 $returnval = array();
443 for ($iter=0; $row=sqlFetchArray($rez); $iter++) {
444 $returnval[$iter]=$row;