Start adding documentation to initial comment block, fix up some quoting, and add...
[openemr.git] / library / pnotes.inc
blob20e1b8e8f78e6fe444fd6c3d400e8ac3c15b0045
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 require_once($GLOBALS['srcdir'].'/sql.inc');
13 /**
14  * Retrieve a note, given its ID
15  *
16  * @param string $id the ID of the note to retrieve.
17  * @param string $cols A list of columns to retrieve. defaults to '*' for all.
18  */
19 function getPnoteById($id, $cols = "*")
21   return sqlQuery("SELECT $cols FROM pnotes WHERE id=? " .
22     ' AND deleted != 1 '. // exclude ALL deleted notes
23     'order by date DESC limit 0,1', array($id) );
26 // activity can be 0, 1, or 'all'
27 function getPnotesByUser($activity="1",$show_all="no",$user='',$count=false,$sortby='',$sortorder='',$begin='',$listnumber='')
30   // Set the activity part of query
31   if ($activity=='1') {
32     $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
33   }
34   else if ($activity=='0') {
35     $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
36   }
37   else { //$activity=='all'
38     $activity_query = " ";
39   }
41   // Set whether to show chosen user or all users
42   if ($show_all == 'yes' ) {
43     $usrvar='_%';
44   } else {
45     $usrvar=$user;
46   } 
48   // run the query
49   $sql = "SELECT pnotes.id, pnotes.user, pnotes.pid, pnotes.title, pnotes.date, pnotes.message_status,
50           IF(pnotes.user != pnotes.pid,users.fname,patient_data.fname) as users_fname,
51           IF(pnotes.user != pnotes.pid,users.lname,patient_data.lname) as users_lname,
52           patient_data.fname as patient_data_fname, patient_data.lname as patient_data_lname
53           FROM ((pnotes LEFT JOIN users ON pnotes.user = users.username)
54           JOIN patient_data ON pnotes.pid = patient_data.pid) WHERE $activity_query
55           pnotes.deleted != '1' AND pnotes.assigned_to LIKE ?";
56   if (!empty($sortby) || !empty($sortorder)  || !empty($begin) || !empty($listnumber)) {
57     $sql .= " order by ".add_escape_custom($sortby)." ".add_escape_custom($sortorder).
58            " limit ".add_escape_custom($begin).", ".add_escape_custom($listnumber);
59   }
60   $result = sqlStatement($sql, array($usrvar));
62   // return the results
63   if ($count) {
64     if(sqlNumRows($result) != 0) {
65         $total = sqlNumRows($result);
66     }
67     else {
68         $total = 0;
69     }
70     return $total;
71   }
72   else {
73     return $result;
74   }
77 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
78   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
80 $sqlParameterArray = array();
81   if ($docid) {
82     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
83     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
84     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
85     array_push($sqlParameterArray, '%'.$date.'%', $docid);
86   }
87   else {
88     $sql = "SELECT $cols FROM pnotes AS p " .
89       "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
90     array_push($sqlParameterArray, '%'.$date.'%', $pid);
91   }
92   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
93   if ($activity != "all") {
94     if ($activity == '0') {
95       // only return inactive
96       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
97     }
98     else { // $activity == '1'
99       // only return active
100       $sql .= " AND activity = '1' AND message_status != 'Done' ";
101     }
102   }
103   if ($username) {
104     $sql .= " AND assigned_to LIKE ?";
105     array_push($sqlParameterArray, $username);
106   }
107   if ($status)
108     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
109   $sql .= " ORDER BY date DESC";
110   if($limit != "all")
111     $sql .= " LIMIT $start, $limit";
113   $res = sqlStatement($sql, $sqlParameterArray);
115   $all=array();
116   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
117     $all[$iter] = $row;
118   return $all;
121 // activity can only be 0, 1, or 'all'
122 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
123   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
125 $sqlParameterArray = array();
126   if ($docid) {
127     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
128     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
129     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
130     array_push($sqlParameterArray, '%'.$date.'%', $docid);
131   }
132   else {
133     $sql = "SELECT $cols FROM pnotes AS p " .
134       "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
135     array_push($sqlParameterArray, '%'.$date.'%', $pid);
136   }
137   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
138   if ($activity != "all") {
139     if ($activity == '0') {
140       // only return inactive
141       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
142     }
143     else { // $activity == '1'
144       // only return active
145       $sql .= " AND activity = '1' AND message_status != 'Done' ";
146     }
147   }
148   if ($username) {
149     $sql .= " AND assigned_to LIKE ?";
150     array_push($sqlParameterArray, $username);
151   }
152   if ($status)
153     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
154   $sql .= " ORDER BY date DESC";
155   if($limit != "all")
156     $sql .= " LIMIT $start, $limit";
158   $res = sqlStatement($sql, $sqlParameterArray);
160   $all=array();
161   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
162     $all[$iter] = $row;
163   return $all;
166 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
168   if($limit){
169     $limit = "LIMIT $offset, $limit";
170   }
171   $sql = "
172     SELECT
173       p.id,
174       p.date,
175       p.user,
176       p.title,
177       REPLACE(
178         p.body,
179         '-patient-',
180         CONCAT(pd.fname, ' ', pd.lname)
181       ) AS body,
182       p.message_status,
183       'Message' as `type`
184     FROM
185       pnotes AS p 
186       LEFT JOIN patient_data AS pd 
187         ON pd.id = p.pid 
188     WHERE assigned_to = '-patient-' 
189       AND p.deleted != 1 
190       AND p.pid = ?
191       $search
192     ORDER BY `date` desc
193     $limit
194   ";
195   $res = sqlStatement($sql, array($pid));
196   for($iter = 0;$row = sqlFetchArray($res);$iter++){
197     $all[$iter] = $row;
198   }
199   return $all;
202 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
204   if($limit){
205     $limit = "LIMIT $offset, $limit";
206   }
207   $sql = "
208     SELECT
209       pr.id,
210       date_created AS `date`,
211       'Patient Reminders' AS `user`,
212       due_status AS title,
213       CONCAT(lo.title, ':', lo2.title) AS body,
214       '' as message_status,
215       'Notification' as `type`
216     FROM
217       patient_reminders AS pr 
218       LEFT JOIN list_options AS lo 
219         ON lo.option_id = pr.category 
220         AND lo.list_id = 'rule_action_category' 
221       LEFT JOIN list_options AS lo2 
222         ON lo2.option_id = pr.item 
223         AND lo2.list_id = 'rule_action' 
224     WHERE pid = ?
225       AND active = 1
226       AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
227       $search
228     ORDER BY `date` desc
229     $limit
230   ";
231   $res = sqlStatement($sql, array($pid));
232   for($iter = 0;$row = sqlFetchArray($res);$iter++){
233     $all[$iter] = $row;
234   }
235   return $all;
238 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
240   if($limit){
241     $limit = "LIMIT $offset, $limit";
242   }
243   $sql = "
244     SELECT
245       p.id,
246       p.date,
247       p.assigned_to,
248       p.title,
249       REPLACE(
250         p.body,
251         '-patient-',
252         CONCAT(pd.lname, ' ', pd.fname)
253       ) AS body,
254       p.activity,
255       p.message_status,
256       'Message' as `type`
257     FROM
258       pnotes AS p 
259       LEFT JOIN patient_data AS pd 
260         ON pd.id = p.pid 
261     WHERE `user` = ?
262       AND p.deleted != 1 
263       AND p.pid = ?
264       AND p.message_status != 'Done'
265       $search
266     ORDER BY `date` desc
267     $limit
268   ";
269   $res = sqlStatement($sql, array($pid,$pid));
270   for($iter = 0;$row = sqlFetchArray($res);$iter++){
271     $all[$iter] = $row;
272   }
273   return $all;
276 // activity can be 0, 1, or 'all'
277 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
279  if ($activity == '1') {
280   // return only active
281   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
282     "AND activity = '1' ".
283     " AND message_status != 'Done' ".
284     " AND deleted != 1 ".
285     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
287  else if ($activity == '0') {
288   // return only inactive
289   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
290     "AND (activity = '0' ".
291     " OR message_status = 'Done') ".
292     " AND deleted != 1 ".
293     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
295  else { // $activity == "all"
296   // return both active and inactive
297   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
298     " AND deleted != 1 ".
299     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
301   for ($iter = 0; $row = sqlFetchArray($res); $iter++)
302     $all[$iter] = $row;
303   return $all;
306 /** Add a note to a patient's medical record.
308  * @param int $pid the ID of the patient whos medical record this note is going to be attached to.
309  * @param string $newtext the note contents.
310  * @param int $authorized
311  * @param int $activity
312  * @param string $title
313  * @param string $assigned_to
314  * @param string $datetime
315  * @param string $message_status
316  * @return int the ID of the added note.
317  */
318 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
319   $title= 'Unassigned', $assigned_to = '', $datetime = '',
320   $message_status = 'New')
322   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
324   // make inactive if set as Done
325   if ($message_status == 'Done') $activity = 0;
327   $body = date('Y-m-d H:i') . ' (' . $_SESSION['authUser'];
328   if ($assigned_to) $body .= " to $assigned_to";
329   $body = $body . ') ' . $newtext;
331   return sqlInsert('INSERT INTO pnotes (date, body, pid, user, groupname, ' .
332     'authorized, activity, title, assigned_to, message_status) VALUES ' .
333     '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
334     array($datetime, $body, $pid, $_SESSION['authUser'], $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
337 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
338   $title='Unassigned', $assigned_to = '', $datetime = '', $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') . ' (' . $pid;
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     array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
354 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
356   $row = getPnoteById($id);
357   if (! $row) die("updatePnote() did not find id '$id'");
358   $activity = $assigned_to ? '1' : '0';
360   // make inactive if set as Done
361   if ($message_status == "Done") $activity = 0;
363   $body = $row['body'] . "\n" . date('Y-m-d H:i') .
364     ' (' . $_SESSION['authUser'];
365   if ($assigned_to) $body .= " to $assigned_to";
366   $body = $body . ') ' . $newtext;
368   if ($message_status) {
369     sqlStatement("UPDATE pnotes SET " .
370       "body = ?, activity = ?, title= ?, " .
371       "assigned_to = ?, message_status = ? WHERE id = ?",
372       array($body, $activity, $title, $assigned_to, $message_status, $id) );
373   }
374   else {
375     sqlStatement("UPDATE pnotes SET " .
376       "body = ?, activity = ?, title= ?, " .
377       "assigned_to = ? WHERE id = ?",
378       array($body, $activity, $title, $assigned_to, $id) );
379   }
382 function updatePnoteMessageStatus($id, $message_status)
384   if ($message_status == "Done") {
385     sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
386   }
387   else {
388     sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
389   }
392 function authorizePnote($id, $authorized = "1")
394   sqlQuery("UPDATE pnotes SET authorized = ? WHERE id = ?", array ($authorized,$id) );
397 function disappearPnote($id)
399   sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done' WHERE id=?", array($id) );
400   return true;
403 function reappearPnote ($id)
405   sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
406   return true;
409 function deletePnote($id)
411   sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );
412   return true;