minor bug fix
[openemr.git] / library / pnotes.inc
blob0de44a5cb842a884a68499a0a693a81f89a67f4b
1 <?php
2 /**
3  * This file contains functions for handling notes attached to patient files. 
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  */
11 /* for sqlQuery(), sqlStatement(), sqlNumRows(), sqlFetchArray(). */
12 require_once($GLOBALS['srcdir'].'/sql.inc');
14 /**
15  * Retrieve a note, given its ID
16  *
17  * @param string $id the ID of the note to retrieve.
18  * @param string $cols A list of columns to retrieve. defaults to '*' for all.
19  */
20 function getPnoteById($id, $cols = "*")
22   return sqlQuery("SELECT $cols FROM pnotes WHERE id=? " .
23     ' AND deleted != 1 '. // exclude ALL deleted notes
24     'order by date DESC limit 0,1', array($id) );
27 /**
28  * Get the patient notes for the given user.
29  *
30  * This function is used to retrieve notes assigned to the given user, or
31  * optionally notes assigned to any user.
32  *
33  * @param string $activity 0 for deleted notes, 1 (the default) for active
34  *                         notes, or 'All' for all.
35  * @param string $show_all whether to display only the selected user's 
36  *                         messages, or all users' messages.
37  * @param string $user The user whom's notes you want to retrieve.
38  * @param bool $count Whether to return a count, or just return 0.
39  * @param string $sortby A field to sort results by.
40  * @param string $sortorder whether to sort ascending or descending.
41  * @param string $begin what row to start retrieving results from.
42  * @param string $listnumber number of rows to return.
43  * @return int The number of rows retrieved, or 0 if $count was true.
44  */
45 function getPnotesByUser($activity="1",$show_all="no",$user='',$count=false,$sortby='',$sortorder='',$begin='',$listnumber='')
48   // Set the activity part of query
49   if ($activity=='1') {
50     $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
51   }
52   else if ($activity=='0') {
53     $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
54   }
55   else { //$activity=='all'
56     $activity_query = " ";
57   }
59   // Set whether to show chosen user or all users
60   if ($show_all == 'yes' ) {
61     $usrvar='_%';
62   } else {
63     $usrvar=$user;
64   } 
66   // run the query
67   $sql = "SELECT pnotes.id, pnotes.user, pnotes.pid, pnotes.title, pnotes.date, pnotes.message_status,
68           IF(pnotes.user != pnotes.pid,users.fname,patient_data.fname) as users_fname,
69           IF(pnotes.user != pnotes.pid,users.lname,patient_data.lname) as users_lname,
70           patient_data.fname as patient_data_fname, patient_data.lname as patient_data_lname
71           FROM ((pnotes LEFT JOIN users ON pnotes.user = users.username)
72           JOIN patient_data ON pnotes.pid = patient_data.pid) WHERE $activity_query
73           pnotes.deleted != '1' AND pnotes.assigned_to LIKE ?";
74   if (!empty($sortby) || !empty($sortorder)  || !empty($begin) || !empty($listnumber)) {
75     $sql .= " order by ".add_escape_custom($sortby)." ".add_escape_custom($sortorder).
76            " limit ".add_escape_custom($begin).", ".add_escape_custom($listnumber);
77   }
78   $result = sqlStatement($sql, array($usrvar));
80   // return the results
81   if ($count) {
82     if(sqlNumRows($result) != 0) {
83         $total = sqlNumRows($result);
84     }
85     else {
86         $total = 0;
87     }
88     return $total;
89   }
90   else {
91     return $result;
92   }
95 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
96   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
98 $sqlParameterArray = array();
99   if ($docid) {
100     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
101     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
102     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
103     array_push($sqlParameterArray, '%'.$date.'%', $docid);
104   }
105   else {
106     $sql = "SELECT $cols FROM pnotes AS p " .
107       "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
108     array_push($sqlParameterArray, '%'.$date.'%', $pid);
109   }
110   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
111   if ($activity != "all") {
112     if ($activity == '0') {
113       // only return inactive
114       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
115     }
116     else { // $activity == '1'
117       // only return active
118       $sql .= " AND activity = '1' AND message_status != 'Done' ";
119     }
120   }
121   if ($username) {
122     $sql .= " AND assigned_to LIKE ?";
123     array_push($sqlParameterArray, $username);
124   }
125   if ($status)
126     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
127   $sql .= " ORDER BY date DESC";
128   if($limit != "all")
129     $sql .= " LIMIT $start, $limit";
131   $res = sqlStatement($sql, $sqlParameterArray);
133   $all=array();
134   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
135     $all[$iter] = $row;
136   return $all;
139 // activity can only be 0, 1, or 'all'
140 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
141   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
143 $sqlParameterArray = array();
144   if ($docid) {
145     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
146     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
147     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
148     array_push($sqlParameterArray, '%'.$date.'%', $docid);
149   }
150   else {
151     $sql = "SELECT $cols FROM pnotes AS p " .
152       "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
153     array_push($sqlParameterArray, '%'.$date.'%', $pid);
154   }
155   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
156   if ($activity != "all") {
157     if ($activity == '0') {
158       // only return inactive
159       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
160     }
161     else { // $activity == '1'
162       // only return active
163       $sql .= " AND activity = '1' AND message_status != 'Done' ";
164     }
165   }
166   if ($username) {
167     $sql .= " AND assigned_to LIKE ?";
168     array_push($sqlParameterArray, $username);
169   }
170   if ($status)
171     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
172   $sql .= " ORDER BY date DESC";
173   if($limit != "all")
174     $sql .= " LIMIT $start, $limit";
176   $res = sqlStatement($sql, $sqlParameterArray);
178   $all=array();
179   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
180     $all[$iter] = $row;
181   return $all;
184 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
186   if($limit){
187     $limit = "LIMIT $offset, $limit";
188   }
189   $sql = "
190     SELECT
191       p.id,
192       p.date,
193       p.user,
194       p.title,
195       REPLACE(
196         p.body,
197         '-patient-',
198         CONCAT(pd.fname, ' ', pd.lname)
199       ) AS body,
200       p.message_status,
201       'Message' as `type`
202     FROM
203       pnotes AS p 
204       LEFT JOIN patient_data AS pd 
205         ON pd.id = p.pid 
206     WHERE assigned_to = '-patient-' 
207       AND p.deleted != 1 
208       AND p.pid = ?
209       $search
210     ORDER BY `date` desc
211     $limit
212   ";
213   $res = sqlStatement($sql, array($pid));
214   for($iter = 0;$row = sqlFetchArray($res);$iter++){
215     $all[$iter] = $row;
216   }
217   return $all;
220 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
222   if($limit){
223     $limit = "LIMIT $offset, $limit";
224   }
225   $sql = "
226     SELECT
227       pr.id,
228       date_created AS `date`,
229       'Patient Reminders' AS `user`,
230       due_status AS title,
231       CONCAT(lo.title, ':', lo2.title) AS body,
232       '' as message_status,
233       'Notification' as `type`
234     FROM
235       patient_reminders AS pr 
236       LEFT JOIN list_options AS lo 
237         ON lo.option_id = pr.category 
238         AND lo.list_id = 'rule_action_category' 
239       LEFT JOIN list_options AS lo2 
240         ON lo2.option_id = pr.item 
241         AND lo2.list_id = 'rule_action' 
242     WHERE pid = ?
243       AND active = 1
244       AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
245       $search
246     ORDER BY `date` desc
247     $limit
248   ";
249   $res = sqlStatement($sql, array($pid));
250   for($iter = 0;$row = sqlFetchArray($res);$iter++){
251     $all[$iter] = $row;
252   }
253   return $all;
256 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
258   if($limit){
259     $limit = "LIMIT $offset, $limit";
260   }
261   $sql = "
262     SELECT
263       p.id,
264       p.date,
265       p.assigned_to,
266       p.title,
267       REPLACE(
268         p.body,
269         '-patient-',
270         CONCAT(pd.lname, ' ', pd.fname)
271       ) AS body,
272       p.activity,
273       p.message_status,
274       'Message' as `type`
275     FROM
276       pnotes AS p 
277       LEFT JOIN patient_data AS pd 
278         ON pd.id = p.pid 
279     WHERE `user` = ?
280       AND p.deleted != 1 
281       AND p.pid = ?
282       AND p.message_status != 'Done'
283       $search
284     ORDER BY `date` desc
285     $limit
286   ";
287   $res = sqlStatement($sql, array($pid,$pid));
288   for($iter = 0;$row = sqlFetchArray($res);$iter++){
289     $all[$iter] = $row;
290   }
291   return $all;
294 // activity can be 0, 1, or 'all'
295 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
297  if ($activity == '1') {
298   // return only active
299   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
300     "AND activity = '1' ".
301     " AND message_status != 'Done' ".
302     " AND deleted != 1 ".
303     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
305  else if ($activity == '0') {
306   // return only inactive
307   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
308     "AND (activity = '0' ".
309     " OR message_status = 'Done') ".
310     " AND deleted != 1 ".
311     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
313  else { // $activity == "all"
314   // return both active and inactive
315   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
316     " AND deleted != 1 ".
317     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
319   for ($iter = 0; $row = sqlFetchArray($res); $iter++)
320     $all[$iter] = $row;
321   return $all;
324 /** Add a note to a patient's medical record.
326  * @param int $pid the ID of the patient whos medical record this note is going to be attached to.
327  * @param string $newtext the note contents.
328  * @param int $authorized
329  * @param int $activity
330  * @param string $title
331  * @param string $assigned_to
332  * @param string $datetime
333  * @param string $message_status
334  * @return int the ID of the added note.
335  */
336 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
337   $title= 'Unassigned', $assigned_to = '', $datetime = '',
338   $message_status = 'New')
340   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
342   // make inactive if set as Done
343   if ($message_status == 'Done') $activity = 0;
345   $body = date('Y-m-d H:i') . ' (' . $_SESSION['authUser'];
346   if ($assigned_to) $body .= " to $assigned_to";
347   $body = $body . ') ' . $newtext;
349   return sqlInsert('INSERT INTO pnotes (date, body, pid, user, groupname, ' .
350     'authorized, activity, title, assigned_to, message_status) VALUES ' .
351     '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
352     array($datetime, $body, $pid, $_SESSION['authUser'], $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
355 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
356   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
358   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
360   // make inactive if set as Done
361   if ($message_status == "Done") $activity = 0;
363   $body = date('Y-m-d H:i') . ' (' . $pid;
364   if ($assigned_to) $body .= " to $assigned_to";
365   $body = $body . ') ' . $newtext;
367   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
368     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
369     array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
372 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
374   $row = getPnoteById($id);
375   if (! $row) die("updatePnote() did not find id '$id'");
376   $activity = $assigned_to ? '1' : '0';
378   // make inactive if set as Done
379   if ($message_status == "Done") $activity = 0;
381   $body = $row['body'] . "\n" . date('Y-m-d H:i') .
382     ' (' . $_SESSION['authUser'];
383   if ($assigned_to) $body .= " to $assigned_to";
384   $body = $body . ') ' . $newtext;
386   if ($message_status) {
387     sqlStatement("UPDATE pnotes SET " .
388       "body = ?, activity = ?, title= ?, " .
389       "assigned_to = ?, message_status = ? WHERE id = ?",
390       array($body, $activity, $title, $assigned_to, $message_status, $id) );
391   }
392   else {
393     sqlStatement("UPDATE pnotes SET " .
394       "body = ?, activity = ?, title= ?, " .
395       "assigned_to = ? WHERE id = ?",
396       array($body, $activity, $title, $assigned_to, $id) );
397   }
400 function updatePnoteMessageStatus($id, $message_status)
402   if ($message_status == "Done") {
403     sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
404   }
405   else {
406     sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
407   }
410 function authorizePnote($id, $authorized = "1")
412   sqlQuery("UPDATE pnotes SET authorized = ? WHERE id = ?", array ($authorized,$id) );
415 function disappearPnote($id)
417   sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done' WHERE id=?", array($id) );
418   return true;
421 function reappearPnote ($id)
423   sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
424   return true;
427 function deletePnote($id)
429   sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );
430   return true;