Display fix: width in manila for demographics (#1909)
[openemr.git] / library / dated_reminder_functions.php
blob86e6fcac4d86596b3c53764facdeab7e51c85229
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) {
31 $userID = $_SESSION['authId'];
34 global $hasAlerts;
35 // ----- define a blank reminders array
36 $reminders = array();
38 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
39 $drSQL = sqlStatement(
40 "SELECT
41 dr.pid, dr.dr_id, dr.dr_message_text,dr.dr_message_due_date,
42 u.fname ffname, u.mname fmname, u.lname flname
43 FROM `dated_reminders` dr
44 JOIN `users` u ON dr.dr_from_ID = u.id
45 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
46 WHERE drl.to_id = ?
47 AND dr.`message_processed` = 0
48 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)
49 ORDER BY `dr_message_due_date` ASC , `message_priority` ASC LIMIT 0,$alerts_to_show",
50 array($userID)
53 // --------- loop through the results
54 for ($i=0; $drRow=sqlFetchArray($drSQL); $i++) {
55 // --------- need to run patient query seperately to allow for reminders not linked to a patient
56 $pRow = array();
57 if ($drRow['pid'] > 0) {
58 $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']));
59 $pRow = sqlFetchArray($pSQL);
62 // --------- fill the $reminders array
63 $reminders[$i]['messageID'] = $drRow['dr_id'];
64 $reminders[$i]['PatientID'] = $drRow['pid'];
66 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
67 $reminders[$i]['PatientName'] = (empty($pRow) ? '' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
68 // -------------------------------------
70 $reminders[$i]['message'] = $drRow['dr_message_text'];
71 $reminders[$i]['dueDate'] = $drRow['dr_message_due_date'];
72 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
74 // --------- if the message is due or overdue set $hasAlerts to true, this will stop autohiding of reminders
75 if (strtotime($drRow['dr_message_due_date']) <= $today) {
76 $hasAlerts = true;
80 // --------- END OF loop through the results
82 return $reminders;
84 // ------------------------------------------------
85 // @ END OF RemindersArray function
86 // ------------------------------------------------
91 /**
92 * This function is used to get a count of the number of reminders due for a specified
93 * user.
95 * @param $days_to_show
96 * @param $today
97 * @param defaults to current user if none specified
98 * @returns int with number of due reminders for specified user
100 function GetDueReminderCount($days_to_show, $today, $userID = false)
102 if (!$userID) {
103 $userID = $_SESSION['authId'];
106 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
107 $drSQL = sqlStatement(
108 "SELECT count(dr.dr_id) c
109 FROM `dated_reminders` dr
110 JOIN `users` u ON dr.dr_from_ID = u.id
111 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
112 WHERE drl.to_id = ?
113 AND dr.`message_processed` = 0
114 AND dr.`dr_message_due_date` < ADDDATE(NOW(), INTERVAL $days_to_show DAY)",
115 array($userID)
118 $drRow=sqlFetchArray($drSQL);
119 return $drRow['c'];
121 // ------------------------------------------------
122 // @ END OF GetDueReminder function
123 // ------------------------------------------------
125 // ------------------------------------------------
126 // @ GetAllReminderCount function
127 // @ returns int with number of unprocessed reminders for specified user, defaults to current user if none specified
128 // ------------------------------------------------
129 function GetAllReminderCount($userID = false)
131 if (!$userID) {
132 $userID = $_SESSION['authId'];
135 // ----- sql statement for getting uncompleted reminders
136 $drSQL = sqlStatement(
137 "SELECT count(dr.dr_id) c
138 FROM `dated_reminders` dr
139 JOIN `users` u ON dr.dr_from_ID = u.id
140 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
141 WHERE drl.to_id = ?
142 AND dr.`message_processed` = 0",
143 array($userID)
146 $drRow=sqlFetchArray($drSQL);
147 return $drRow['c'];
149 // ------------------------------------------------
150 // @ END OF GetAllReminderCount function
151 // ------------------------------------------------
153 // ------------------------------------------------
154 // @ getRemindersHTML(array $reminders)
155 // @ returns HTML as a string, for printing
156 // ------------------------------------------------
157 function getRemindersHTML($today, $reminders = array())
159 global $hasAlerts;
160 // --- initialize the string as blank
161 $pdHTML = '';
162 // --- loop through the $reminders
163 foreach ($reminders as $r) {
164 // --- initialize $warning as the date, this is placed in front of the message
165 $warning = text($r['dueDate']);
166 // --- initialize $class as 'text dr', this is the basic class
167 $class='text dr';
169 // --------- check if reminder is overdue
170 if (strtotime($r['dueDate']) < $today) {
171 $warning = '<i class=\'fa fa-exclamation-triangle fa-lg\' style=\'color:red\' aria-hidden=\'true\'></i> '.xlt('OVERDUE');
172 //$class = 'bold alert dr';
173 $class = '';
174 } elseif (strtotime($r['dueDate']) == $today) {
175 // --------- check if reminder is due
176 $warning = '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:orange\' aria-hidden=\'true\'></i> '.xlt('TODAY');
177 $class = '';
178 } elseif (strtotime($r['dueDate']) > $today) {
179 $warning = '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:green\' aria-hidden=\'true\'></i> '.xlt('UPCOMING');
180 $class = '';
183 // end check if reminder is due or overdue
184 // apend to html string
185 $pdHTML .= '<p id="p_'.attr($r['messageID']).'">
186 <a onclick="openAddScreen('.attr($r['messageID']).')" class="dnForwarder btn btn-default btn-send-msg" id="'.attr($r['messageID']).'" href="#"> '.xlt('Forward').' </a>
187 <a class="dnRemover btn btn-default btn-save" onclick="updateme('."'".attr($r['messageID'])."'".')" id="'.attr($r['messageID']).'" href="#">
188 <span>'.xlt('Set As Completed').'</span>
189 </a>
190 <span title="'.($r['PatientID'] > 0 ? xla('Click Patient Name to Open Patient File') : '').'" class="'.attr($class).'">'.
191 $warning.'
192 <span onclick="goPid('.attr($r['PatientID']).')" class="patLink" id="'.attr($r['PatientID']).'">'.
193 text($r['PatientName']).'
194 </span> '.
195 text($r['message']).' - ['.text($r['fromName']).']
196 </span>
197 </p>';
200 return ($pdHTML == '' ? '<i class=\'fa fa-exclamation-circle fa-lg\' style=\'color:green\' aria-hidden=\'true\'></i> '.xlt('No Reminders') : $pdHTML);
202 // ------------------------------------------------
203 // @ END OF getRemindersHTML function
204 // ------------------------------------------------
207 // ------------------------------------------------
208 // @ setReminderAsProccessed(int $rID)
209 // @ marks reminder as processed
210 // ------------------------------------------------
211 function setReminderAsProcessed($rID, $userID = false)
213 if (!$userID) {
214 $userID = $_SESSION['authId'];
217 if (is_numeric($rID) and $rID > 0) {
218 // --- check if this user can remove this message
219 // --- need a better way of checking the current user, I don't like using $_SESSION for checks
220 $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));
221 $rdrRow=sqlFetchArray($rdrSQL);
223 // --- if this user can delete this message (ie if it was sent to this user)
224 if ($rdrRow['c'] == 1) {
225 // ----- update the data, set the message to proccesses
226 sqlStatement("UPDATE `dated_reminders` SET `message_processed` = 1, `processed_date` = NOW(), `dr_processed_by` = ? WHERE `dr_id` = ? ", array(intval($userID),intval($rID)));
230 // ------------------------------------------------
231 // @ END OF setReminderAsProccessed function
232 // ------------------------------------------------
235 // ------------------------------------------------
236 // @ getReminderById(int $mID)
237 // @ returns an array with message details for forwarding
238 // ------------------------------------------------
239 function getReminderById($mID, $userID = false)
241 if (!$userID) {
242 $userID = $_SESSION['authId'];
245 $rdrSQL = sqlStatement("SELECT * FROM `dated_reminders` dr
246 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
247 WHERE drl.to_id = ? AND dr.`dr_id` = ? LIMIT 0,1", array($userID,$mID));
248 $rdrRow=sqlFetchArray($rdrSQL);
249 if (!empty($rdrRow)) {
250 return $rdrRow;
253 return false;
255 // ------------------------------------------------
256 // @ END OF getReminderById function
257 // ------------------------------------------------
260 // ------------------------------------------------
261 // @ getReminderById(
262 // array $sendTo
263 // int $fromID
264 // string $message
265 // date $dueDate
266 // int $patID
267 // int $priority
268 // )
269 // @ returns an array with message details for forwarding
270 // ------------------------------------------------
271 function sendReminder($sendTo, $fromID, $message, $dueDate, $patID, $priority)
273 if (// ------- Should run data checks before running this function for more accurate error reporting
274 // ------- check sendTo is not empty
275 !empty($sendTo) and
276 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
277 preg_match('/\d{4}[-]\d{2}[-]\d{2}/', $dueDate) and
278 // ------- check priority, only allow 1-3
279 intval($priority) <= 3 and
280 // ------- check message, only up to 255 characters
281 strlen($message) <= 255 and strlen($message) > 0 and
282 // ------- check if PatientID is set and in numeric
283 is_numeric($patID)
285 // ------- check for valid recipient
286 $cRow=sqlFetchArray(sqlStatement('SELECT count(id) FROM `users` WHERE `id` = ?', array($sendDMTo)));
287 if ($cRow == 0) {
288 return false;
291 // ------- if no errors
292 // --------- insert the new message
293 $mID = sqlInsert(
294 "INSERT INTO `dated_reminders`
295 (`dr_from_ID` ,`dr_message_text` ,`dr_message_sent_date` ,`dr_message_due_date` ,`pid` ,`message_priority` ,`message_processed` ,`processed_date`)
296 VALUES (?, ?, NOW( ), ?, ?, ?, '0', '');",
297 array($fromID,$message,$dueDate,$patID,$priority)
300 foreach ($sendTo as $st) {
301 sqlInsert(
302 "INSERT INTO `dated_reminders_link`
303 (`dr_id` ,`to_id`)
304 VALUES (?, ?);",
305 array($mID,$st)
308 return true;
309 } //---- end of if block
310 return false;
313 // ------- get current patient name
314 // ---- returns string, blank if no current patient
315 function getPatName($patientID)
317 $patientID = intval($patientID);
318 $pSQL = sqlStatement("SELECT pd.title ptitle, pd.fname pfname, pd.mname pmname, pd.lname plname FROM `patient_data` pd WHERE pd.pid = ?", array($patientID));
319 $pRow = sqlFetchArray($pSQL);
320 return (empty($pRow) ? '' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
323 // -- log reminders array function uses $_GET to filter
324 function logRemindersArray()
327 // set blank array for data to be parsed to sql
328 $input = array();
329 // set blank string for the query
330 $where = '';
331 $sentBy = $_GET['sentBy'];
332 $sentTo = $_GET['sentTo'];
333 //------------------------------------------
334 // ----- HANDLE SENT BY FILTER
335 if (!empty($sentBy)) {
336 $sbCount = 0;
337 foreach ($sentBy as $sb) {
338 $where .= ($sbCount == 0 ? '(' : ' OR ').'dr.dr_from_ID = ? ';
339 $sbCount++;
340 $input[] = $sb;
343 $where .= ')';
346 //------------------------------------------
347 // ----- HANDLE SENT TO FILTER
348 if (!empty($sentTo)) {
349 $where = ($where == '' ? '' : $where.' AND ');
350 $stCount = 0;
351 foreach ($sentTo as $st) {
352 $where .= ($stCount == 0 ? '(' : ' OR ').'drl.to_id = ? ';
353 $stCount++;
354 $input[] = $st;
357 $where .= ')';
360 //------------------------------------------
361 // ----- HANDLE PROCCESSED/PENDING FILTER ONLY RUN THIS IF BOTH ARE NOT SET
362 if (isset($_GET['processed']) and !isset($_GET['pending'])) {
363 $where = ($where == '' ? 'dr.message_processed = 1' : $where.' AND dr.message_processed = 1');
364 } elseif (!isset($_GET['processed']) and isset($_GET['pending'])) {
365 $where = ($where == '' ? 'dr.message_processed = 0' : $where.' AND dr.message_processed = 0');
368 //------------------------------------------
369 // ----- HANDLE DATE RANGE FILTERS
370 if (isset($_GET['sd']) and $_GET['sd'] != '') {
371 $where = ($where == '' ? 'dr.dr_message_sent_date >= ?' : $where.' AND dr.dr_message_sent_date >= ?');
372 $input[] = $_GET['sd'].' 00:00:00';
375 if (isset($_GET['ed']) and $_GET['ed'] != '') {
376 $where = ($where == '' ? 'dr.dr_message_sent_date <= ?' : $where.' AND dr.dr_message_sent_date <= ?');
377 $input[] = $_GET['ed'].' 23:59:59';
380 //------------------------------------------
383 //-------- add the "WHERE" the string if string is not blank, avoid sql errors for blannk WHERE statements
384 $where = ($where == '' ? '' : 'WHERE '.$where);
386 // ----- define a blank reminders array
387 $reminders = array();
389 // ----- sql statement for getting uncompleted reminders (sorts by date, then by priority)
390 $drSQL = sqlStatement(
391 "SELECT
392 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,
393 u.fname ffname, u.mname fmname, u.lname flname,
394 tu.fname tfname, tu.mname tmname, tu.lname tlname
395 FROM `dated_reminders` dr
396 JOIN `dated_reminders_link` drl ON dr.dr_id = drl.dr_id
397 JOIN `users` u ON dr.dr_from_ID = u.id
398 JOIN `users` tu ON drl.to_id = tu.id
399 $where",
400 $input
402 // --------- loop through the results
403 for ($i=0; $drRow=sqlFetchArray($drSQL); $i++) {
404 // --------- need to run patient query seperately to allow for messages not linked to a patient
405 $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']));
406 $pRow = sqlFetchArray($pSQL);
408 $prSQL = sqlStatement("SELECT u.fname pfname, u.mname pmname, u.lname plname FROM `users` u WHERE u.id = ?", array($drRow['dr_processed_by']));
409 $prRow = sqlFetchArray($prSQL);
411 // --------- fill the $reminders array
412 $reminders[$i]['messageID'] = $drRow['dr_id'];
413 $reminders[$i]['PatientID'] = $drRow['pid'];
415 $reminders[$i]['pDate'] = ($drRow['processedDate'] == '0000-00-00 00:00:00' ? 'N/A' : $drRow['processedDate']);
416 $reminders[$i]['sDate'] = $drRow['sDate'];
417 $reminders[$i]['dDate'] = $drRow['dDate'];
419 // ------------------------------------- if there was a patient linked, set the name, else set it to blank
420 $reminders[$i]['PatientName'] = (empty($pRow) ? 'N/A' : $pRow['ptitle'].' '.$pRow['pfname'].' '.$pRow['pmname'].' '.$pRow['plname']);
421 // -------------------------------------
423 $reminders[$i]['message'] = $drRow['dr_message_text'];
424 $reminders[$i]['fromName'] = $drRow['ffname'].' '.$drRow['fmname'].' '.$drRow['flname'];
425 $reminders[$i]['ToName'] = $drRow['tfname'].' '.$drRow['tmname'].' '.$drRow['tlname'];
426 $reminders[$i]['processedByName'] = (empty($prRow) ? 'N/A' : $prRow['ptitle'].' '.$prRow['pfname'].' '.$prRow['pmname'].' '.$prRow['plname']);
429 // --------- END OF loop through the results
431 return $reminders;