Added access controls for encounter categories
[openemr.git] / library / dated_reminder_functions.php
blob28248c55c67278964adbed4bc09e0ec898314e75
1 <?php
2 /**
3 * Contains functions used in the dated reminders.
5 * Copyright (C) 2012 tajemo.co.za <http://www.tajemo.co.za/>
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 3
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 Craig Bezuidenhout <http://www.tajemo.co.za/>
20 * @link http://www.open-emr.org
23 /**
24 * RemindersArray function
26 * @returns array reminders for specified user, defaults to current user if none specified
28 function RemindersArray($days_to_show,$today,$alerts_to_show,$userID = false)
30 if(!$userID) $userID = $_SESSION['authId'];
31 global $hasAlerts;
32 // ----- define a blank reminders array
33 $reminders = array();
35 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
36 $drSQL = sqlStatement(
37 "SELECT
38 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
39 u.fname ffname, u.mname fmname, u.lname flname
40 FROM `dated_reminders` dr
41 JOIN `users` u ON dr.dr_from_ID = u.id
42 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
43 WHERE drl.to_id = ?
44 AND dr.`message_processed` = 0
45 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)
46 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0,$alerts_to_show"
47 , array($userID)
50 // --------- loop through the results
51 for($i=0; $drRow=sqlFetchArray($drSQL); $i++){
52 // --------- need to run patient query seperately to allow for reminders not linked to a patient
53 $pRow = array();
54 if($drRow['pid'] > 0){
55 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?",array($drRow['pid']));
56 $pRow = sqlFetchArray($pSQL);
59 // --------- fill the $reminders array
60 $reminders[$i]['messageID'] = $drRow['dr_id'];
61 $reminders[$i]['PatientID'] = $drRow['pid'];
63 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
64 $reminders[$i]['PatientName'] = (empty($pRow) ? '' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
65 // -------------------------------------
67 $reminders[$i]['message'] = $drRow['dr_message_text'];
68 $reminders[$i]['dueDate'] = $drRow['dr_message_due_date'];
69 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
71 // --------- if the message is due or overdue set $hasAlerts to true, this will stop autohiding of reminders
72 if(strtotime($drRow['dr_message_due_date']) <= $today) $hasAlerts = true;
74 // --------- END OF loop through the results
76 return $reminders;
78 // ------------------------------------------------
79 // @ END OF RemindersArray function
80 // ------------------------------------------------
85 /**
86 * This function is used to get a count of the number of reminders due for a specified
87 * user.
89 * @param $days_to_show
90 * @param $today
91 * @param defaults to current user if none specified
92 * @returns int with number of due reminders for specified user
94 function GetDueReminderCount($days_to_show,$today,$userID = false)
96 if(!$userID) $userID = $_SESSION['authId'];
98 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
99 $drSQL = sqlStatement(
100 "SELECT count(dr.dr_id) c
101 FROM `dated_reminders` dr
102 JOIN `users` u ON dr.dr_from_ID = u.id
103 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
104 WHERE drl.to_id = ?
105 AND dr.`message_processed` = 0
106 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)"
107 , array($userID)
110 $drRow=sqlFetchArray($drSQL);
111 return $drRow['c'];
113 // ------------------------------------------------
114 // @ END OF GetDueReminder function
115 // ------------------------------------------------
117 // ------------------------------------------------
118 // @ GetAllReminderCount function
119 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
120 // ------------------------------------------------
121 function GetAllReminderCount($userID = false)
123 if(!$userID) $userID = $_SESSION['authId'];
125 // ----- sql statement for getting uncompleted reminders
126 $drSQL = sqlStatement(
127 "SELECT count(dr.dr_id) c
128 FROM `dated_reminders` dr
129 JOIN `users` u ON dr.dr_from_ID = u.id
130 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
131 WHERE drl.to_id = ?
132 AND dr.`message_processed` = 0"
133 , array($userID)
136 $drRow=sqlFetchArray($drSQL);
137 return $drRow['c'];
139 // ------------------------------------------------
140 // @ END OF GetAllReminderCount function
141 // ------------------------------------------------
143 // ------------------------------------------------
144 // @ getRemindersHTML(array $reminders)
145 // @ returns HTML as a string, for printing
146 // ------------------------------------------------
147 function getRemindersHTML($reminders = array(),$today)
149 global $hasAlerts;
150 // --- initialize the string as blank
151 $pdHTML = '';
152 // --- loop through the $reminders
153 foreach($reminders as $r){
154 // --- initialize $warning as the date, this is placed in front of the message
155 $warning = text($r['dueDate']);
156 // --- initialize $class as 'text dr', this is the basic class
157 $class='text dr';
159 // --------- check if reminder is overdue
160 if(strtotime($r['dueDate']) < $today){
161 $warning = '! '.xlt('OVERDUE');
162 $class = 'bold alert dr';
164 // --------- check if reminder is due
165 elseif(strtotime($r['dueDate']) == $today){
166 $warning = xlt('TODAY');
167 $class='bold alert dr';
169 // end check if reminder is due or overdue
170 // apend to html string
171 $pdHTML .= '<p id="p_'.attr($r['messageID']).'">
172 <a class="dnRemover css_button_small" onclick="updateme('."'".attr($r['messageID'])."'".')" id="'.attr($r['messageID']).'" href="#">
173 <span>'.xlt('Set As Completed').'</span>
174 </a>
175 <span title="'.($r['PatientID'] > 0 ? xla('Click Patient Name to Open Patient File') : '').'" class="'.attr($class).'">'.
176 $warning.'
177 <span onclick="goPid('.attr($r['PatientID']).')" class="patLink" id="'.attr($r['PatientID']).'">'.
178 text($r['PatientName']).'
179 </span> '.
180 text($r['message']).' - ['.text($r['fromName']).']
181 </span> ----->
182 <a onclick="openAddScreen('.attr($r['messageID']).')" class="dnForwarder" id="'.attr($r['messageID']).'" href="#">[ '.xlt('Forward').' ]</a>
183 </p>';
185 return ($pdHTML == '' ? '<p class="alert"><br />'.xlt('No Reminders').'</p>' : $pdHTML);
187 // ------------------------------------------------
188 // @ END OF getRemindersHTML function
189 // ------------------------------------------------
192 // ------------------------------------------------
193 // @ setReminderAsProccessed(int $rID)
194 // @ marks reminder as processed
195 // ------------------------------------------------
196 function setReminderAsProcessed($rID,$userID = false)
198 if(!$userID) $userID = $_SESSION['authId'];
199 if(is_numeric($rID) and $rID > 0){
200 // --- check if this user can remove this message
201 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
202 $rdrSQL = sqlStatement("SELECT count(dr.dr_id) c FROM `dated_reminders` dr JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID,$rID));
203 $rdrRow=sqlFetchArray($rdrSQL);
205 // --- if this user can delete this message (ie if it was sent to this user)
206 if($rdrRow['c'] == 1){
207 // ----- update the data, set the message to proccesses
208 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID),intval($rID)));
212 // ------------------------------------------------
213 // @ END OF setReminderAsProccessed function
214 // ------------------------------------------------
217 // ------------------------------------------------
218 // @ getReminderById(int $mID)
219 // @ returns an array with message details for forwarding
220 // ------------------------------------------------
221 function getReminderById($mID,$userID = false)
223 if(!$userID) $userID = $_SESSION['authId'];
224 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
225 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
226 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID,$mID));
227 $rdrRow=sqlFetchArray($rdrSQL);
228 if(!empty($rdrRow)){
229 return $rdrRow;
231 return false;
233 // ------------------------------------------------
234 // @ END OF getReminderById function
235 // ------------------------------------------------
238 // ------------------------------------------------
239 // @ getReminderById(
240 // array $sendTo
241 // int $fromID
242 // string $message
243 // date $dueDate
244 // int $patID
245 // int $priority
246 // )
247 // @ returns an array with message details for forwarding
248 // ------------------------------------------------
249 function sendReminder($sendTo,$fromID,$message,$dueDate,$patID,$priority)
252 // ------- Should run data checks before running this function for more accurate error reporting
253 // ------- check sendTo is not empty
254 !empty($sendTo) and
255 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
256 preg_match('/\d{4}[-]\d{2}[-]\d{2}/',$dueDate) and
257 // ------- check priority, only allow 1-3
258 intval($priority) <= 3 and
259 // ------- check message, only up to 255 characters
260 strlen($message) <= 255 and strlen($message) > 0 and
261 // ------- check if PatientID is set and in numeric
262 is_numeric($patID)
264 // ------- check for valid recipient
265 $cRow=sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?',array($sendDMTo)));
266 if($cRow == 0){
267 return false;
269 // ------- if no errors
270 // --------- insert the new message
271 $mID = sqlInsert("INSERT INTO `dated_reminders`
272 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
273 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
274 array($fromID,$message,$dueDate,$patID,$priority));
276 foreach($sendTo as $st){
277 sqlInsert("INSERT INTO `dated_reminders_link`
278 (`dr_id` ,`to_id`)
279 VALUES (?, ?);",
280 array($mID,$st));
282 return true;
283 } //---- end of if block
284 return false;
287 // ------- get current patient name
288 // ---- returns string, blank if no current patient
289 function getPatName($patientID)
291 $patientID = intval($patientID);
292 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?",array($patientID));
293 $pRow = sqlFetchArray($pSQL);
294 return (empty($pRow) ? '' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
297 // -- log reminders array function uses $_GET to filter
298 function logRemindersArray()
301 // set blank array for data to be parsed to sql
302 $input = array();
303 // set blank string for the query
304 $where = '';
305 $sentBy = $_GET['sentBy'];
306 $sentTo = $_GET['sentTo'];
307 //------------------------------------------
308 // ----- HANDLE SENT BY FILTER
309 if(!empty($sentBy)){
310 $sbCount = 0;
311 foreach($sentBy as $sb){
312 $where .= ($sbCount == 0 ? '(' : ' OR ').'dr.dr_from_ID = ? ';
313 $sbCount++;
314 $input[] = $sb;
316 $where .= ')';
318 //------------------------------------------
319 // ----- HANDLE SENT TO FILTER
320 if(!empty($sentTo)){
321 $where = ($where == '' ? '' : $where.' AND ');
322 $stCount = 0;
323 foreach($sentTo as $st){
324 $where .= ($stCount == 0 ? '(' : ' OR ').'drl.to_id = ? ';
325 $stCount++;
326 $input[] = $st;
328 $where .= ')';
330 //------------------------------------------
331 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
332 if(isset($_GET['processed']) and !isset($_GET['pending'])){
333 $where = ($where == '' ? 'dr.message_processed = 1' : $where.' AND dr.message_processed = 1');
335 elseif(!isset($_GET['processed']) and isset($_GET['pending'])){
336 $where = ($where == '' ? 'dr.message_processed = 0' : $where.' AND dr.message_processed = 0');
338 //------------------------------------------
339 // ----- HANDLE DATE RANGE FILTERS
340 if(isset($_GET['sd']) and $_GET['sd'] != ''){
341 $where = ($where == '' ? 'dr.dr_message_sent_date >= ?' : $where.' AND dr.dr_message_sent_date >= ?');
342 $input[] = $_GET['sd'].' 00:00:00';
344 if(isset($_GET['ed']) and $_GET['ed'] != ''){
345 $where = ($where == '' ? 'dr.dr_message_sent_date <= ?' : $where.' AND dr.dr_message_sent_date <= ?');
346 $input[] = $_GET['ed'].' 23:59:59';
348 //------------------------------------------
351 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
352 $where = ($where == '' ? '' : 'WHERE '.$where);
354 // ----- define a blank reminders array
355 $reminders = array();
357 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
358 $drSQL = sqlStatement(
359 "SELECT
360 dr.pid, dr.dr_id, dr.dr_message_text, dr.dr_message_due_date dDate, dr.dr_message_sent_date sDate,dr.processed_date processedDate, dr.dr_processed_by,
361 u.fname ffname, u.mname fmname, u.lname flname,
362 tu.fname tfname, tu.mname tmname, tu.lname tlname
363 FROM `dated_reminders` dr
364 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
365 JOIN `users` u ON dr.dr_from_ID = u.id
366 JOIN `users` tu ON drl.to_id = tu.id
367 $where"
368 ,$input);
369 // --------- loop through the results
370 for($i=0; $drRow=sqlFetchArray($drSQL); $i++){
371 // --------- need to run patient query seperately to allow for messages not linked to a patient
372 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?",array($drRow['pid']));
373 $pRow = sqlFetchArray($pSQL);
375 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?",array($drRow['dr_processed_by']));
376 $prRow = sqlFetchArray($prSQL );
378 // --------- fill the $reminders array
379 $reminders[$i]['messageID'] = $drRow['dr_id'];
380 $reminders[$i]['PatientID'] = $drRow['pid'];
382 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ? 'N/A' : $drRow['processedDate']);
383 $reminders[$i]['sDate'] = $drRow['sDate'];
384 $reminders[$i]['dDate'] = $drRow['dDate'];
386 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
387 $reminders[$i]['PatientName'] = (empty($pRow) ? 'N/A' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
388 // -------------------------------------
390 $reminders[$i]['message'] = $drRow['dr_message_text'];
391 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
392 $reminders[$i]['ToName'] = $drRow['tfname'].' '.$drRow['tmname'].' '.$drRow['tlname'];
393 $reminders[$i]['processedByName'] = (empty($prRow) ? 'N/A' : $prRow['ptitle'].' '.$prRow['pfname'].' '.$prRow['pmname'].' '.$prRow['plname']);
395 // --------- END OF loop through the results
397 return $reminders;