3d55b4aff68e11abddbcd72e2a7dec8bf81c514e
[openemr.git] / library / pnotes.inc
blob3d55b4aff68e11abddbcd72e2a7dec8bf81c514e
1 <?php
2 /**
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  */
9 require_once("{$GLOBALS['srcdir']}/sql.inc");
11 function getPnoteById($id, $cols = "*")
13   return sqlQuery("SELECT $cols FROM pnotes WHERE id=? " .
14     " AND deleted != 1 ". // exclude ALL deleted notes
15     "order by date DESC limit 0,1", array($id) );
18 // activity can be 0, 1, or 'all'
19 function getPnotesByUser($activity="1",$show_all="no",$user='',$count=false,$sortby='',$sortorder='',$begin='',$listnumber='')
22   // Set the activity part of query
23   if ($activity=='1') {
24     $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
25   }
26   else if ($activity=='0') {
27     $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
28   }
29   else { //$activity=='all'
30     $activity_query = " ";
31   }
33   // Set whether to show chosen user or all users
34   if ($show_all == 'yes' ) {
35     $usrvar='_%';
36   } else {
37     $usrvar=$user;
38   } 
40   // run the query
41   $sql = "SELECT pnotes.id, pnotes.user, pnotes.pid, pnotes.title, pnotes.date, pnotes.message_status,
42           IF(pnotes.user != pnotes.pid,users.fname,patient_data.fname) as users_fname,
43           IF(pnotes.user != pnotes.pid,users.lname,patient_data.lname) as users_lname,
44           patient_data.fname as patient_data_fname, patient_data.lname as patient_data_lname
45           FROM ((pnotes LEFT JOIN users ON pnotes.user = users.username)
46           JOIN patient_data ON pnotes.pid = patient_data.pid) WHERE $activity_query
47           pnotes.deleted != '1' AND pnotes.assigned_to LIKE ?";
48   if (!empty($sortby) || !empty($sortorder)  || !empty($begin) || !empty($listnumber)) {
49     $sql .= " order by ".add_escape_custom($sortby)." ".add_escape_custom($sortorder).
50            " limit ".add_escape_custom($begin).", ".add_escape_custom($listnumber);
51   }
52   $result = sqlStatement($sql, array($usrvar));
54   // return the results
55   if ($count) {
56     if(sqlNumRows($result) != 0) {
57         $total = sqlNumRows($result);
58     }
59     else {
60         $total = 0;
61     }
62     return $total;
63   }
64   else {
65     return $result;
66   }
69 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
70   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
72 $sqlParameterArray = array();
73   if ($docid) {
74     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
75     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
76     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
77     array_push($sqlParameterArray, '%'.$date.'%', $docid);
78   }
79   else {
80     $sql = "SELECT $cols FROM pnotes AS p " .
81       "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
82     array_push($sqlParameterArray, '%'.$date.'%', $pid);
83   }
84   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
85   if ($activity != "all") {
86     if ($activity == '0') {
87       // only return inactive
88       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
89     }
90     else { // $activity == '1'
91       // only return active
92       $sql .= " AND activity = '1' AND message_status != 'Done' ";
93     }
94   }
95   if ($username) {
96     $sql .= " AND assigned_to LIKE ?";
97     array_push($sqlParameterArray, $username);
98   }
99   if ($status)
100     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
101   $sql .= " ORDER BY date DESC";
102   if($limit != "all")
103     $sql .= " LIMIT $start, $limit";
105   $res = sqlStatement($sql, $sqlParameterArray);
107   $all=array();
108   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
109     $all[$iter] = $row;
110   return $all;
113 // activity can only be 0, 1, or 'all'
114 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
115   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
117 $sqlParameterArray = array();
118   if ($docid) {
119     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
120     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
121     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
122     array_push($sqlParameterArray, '%'.$date.'%', $docid);
123   }
124   else {
125     $sql = "SELECT $cols FROM pnotes AS p " .
126       "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
127     array_push($sqlParameterArray, '%'.$date.'%', $pid);
128   }
129   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
130   if ($activity != "all") {
131     if ($activity == '0') {
132       // only return inactive
133       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
134     }
135     else { // $activity == '1'
136       // only return active
137       $sql .= " AND activity = '1' AND message_status != 'Done' ";
138     }
139   }
140   if ($username) {
141     $sql .= " AND assigned_to LIKE ?";
142     array_push($sqlParameterArray, $username);
143   }
144   if ($status)
145     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
146   $sql .= " ORDER BY date DESC";
147   if($limit != "all")
148     $sql .= " LIMIT $start, $limit";
150   $res = sqlStatement($sql, $sqlParameterArray);
152   $all=array();
153   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
154     $all[$iter] = $row;
155   return $all;
158 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
160   if($limit){
161     $limit = "LIMIT $offset, $limit";
162   }
163   $sql = "
164     SELECT
165       p.id,
166       p.date,
167       p.user,
168       p.title,
169       REPLACE(
170         p.body,
171         '-patient-',
172         CONCAT(pd.fname, ' ', pd.lname)
173       ) AS body,
174       p.message_status,
175       'Message' as `type`
176     FROM
177       pnotes AS p 
178       LEFT JOIN patient_data AS pd 
179         ON pd.id = p.pid 
180     WHERE assigned_to = '-patient-' 
181       AND p.deleted != 1 
182       AND p.pid = ?
183       $search
184     ORDER BY `date` desc
185     $limit
186   ";
187   $res = sqlStatement($sql, array($pid));
188   for($iter = 0;$row = sqlFetchArray($res);$iter++){
189     $all[$iter] = $row;
190   }
191   return $all;
194 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
196   if($limit){
197     $limit = "LIMIT $offset, $limit";
198   }
199   $sql = "
200     SELECT
201       pr.id,
202       date_created AS `date`,
203       'Patient Reminders' AS `user`,
204       due_status AS title,
205       CONCAT(lo.title, ':', lo2.title) AS body,
206       '' as message_status,
207       'Notification' as `type`
208     FROM
209       patient_reminders AS pr 
210       LEFT JOIN list_options AS lo 
211         ON lo.option_id = pr.category 
212         AND lo.list_id = 'rule_action_category' 
213       LEFT JOIN list_options AS lo2 
214         ON lo2.option_id = pr.item 
215         AND lo2.list_id = 'rule_action' 
216     WHERE pid = ?
217       AND active = 1
218       AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
219       $search
220     ORDER BY `date` desc
221     $limit
222   ";
223   $res = sqlStatement($sql, array($pid));
224   for($iter = 0;$row = sqlFetchArray($res);$iter++){
225     $all[$iter] = $row;
226   }
227   return $all;
230 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
232   if($limit){
233     $limit = "LIMIT $offset, $limit";
234   }
235   $sql = "
236     SELECT
237       p.id,
238       p.date,
239       p.assigned_to,
240       p.title,
241       REPLACE(
242         p.body,
243         '-patient-',
244         CONCAT(pd.lname, ' ', pd.fname)
245       ) AS body,
246       p.activity,
247       p.message_status,
248       'Message' as `type`
249     FROM
250       pnotes AS p 
251       LEFT JOIN patient_data AS pd 
252         ON pd.id = p.pid 
253     WHERE `user` = ?
254       AND p.deleted != 1 
255       AND p.pid = ?
256       AND p.message_status != 'Done'
257       $search
258     ORDER BY `date` desc
259     $limit
260   ";
261   $res = sqlStatement($sql, array($pid,$pid));
262   for($iter = 0;$row = sqlFetchArray($res);$iter++){
263     $all[$iter] = $row;
264   }
265   return $all;
268 // activity can be 0, 1, or 'all'
269 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
271  if ($activity == '1') {
272   // return only active
273   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
274     "AND activity = '1' ".
275     " AND message_status != 'Done' ".
276     " AND deleted != 1 ".
277     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
279  else if ($activity == '0') {
280   // return only inactive
281   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
282     "AND (activity = '0' ".
283     " OR message_status = 'Done') ".
284     " AND deleted != 1 ".
285     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
287  else { // $activity == "all"
288   // return both active and inactive
289   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
290     " AND deleted != 1 ".
291     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
293   for ($iter = 0; $row = sqlFetchArray($res); $iter++)
294     $all[$iter] = $row;
295   return $all;
298 /** Add a note to a patient's medical record.
300  * @param int $pid the ID of the patient whos medical record this note is going to be attached to.
301  * @param string $newtext the note contents.
302  * @param int $authorized
303  * @param int $activity
304  * @param string $title
305  * @param string $assigned_to
306  * @param string $datetime
307  * @param string $message_status
308  * @return int the ID of the added note.
309  */
310 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
311   $title= 'Unassigned', $assigned_to = '', $datetime = '',
312   $message_status = 'New')
314   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
316   // make inactive if set as Done
317   if ($message_status == 'Done') $activity = 0;
319   $body = date('Y-m-d H:i') . ' (' . $_SESSION['authUser'];
320   if ($assigned_to) $body .= " to $assigned_to";
321   $body = $body . ') ' . $newtext;
323   return sqlInsert('INSERT INTO pnotes (date, body, pid, user, groupname, ' .
324     'authorized, activity, title, assigned_to, message_status) VALUES ' .
325     '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
326     array($datetime, $body, $pid, $_SESSION['authUser'], $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
329 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
330   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
332   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
334   // make inactive if set as Done
335   if ($message_status == "Done") $activity = 0;
337   $body = date('Y-m-d H:i') . ' (' . $pid;
338   if ($assigned_to) $body .= " to $assigned_to";
339   $body = $body . ') ' . $newtext;
341   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
342     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
343     array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
346 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
348   $row = getPnoteById($id);
349   if (! $row) die("updatePnote() did not find id '$id'");
350   $activity = $assigned_to ? '1' : '0';
352   // make inactive if set as Done
353   if ($message_status == "Done") $activity = 0;
355   $body = $row['body'] . "\n" . date('Y-m-d H:i') .
356     ' (' . $_SESSION['authUser'];
357   if ($assigned_to) $body .= " to $assigned_to";
358   $body = $body . ') ' . $newtext;
360   if ($message_status) {
361     sqlStatement("UPDATE pnotes SET " .
362       "body = ?, activity = ?, title= ?, " .
363       "assigned_to = ?, message_status = ? WHERE id = ?",
364       array($body, $activity, $title, $assigned_to, $message_status, $id) );
365   }
366   else {
367     sqlStatement("UPDATE pnotes SET " .
368       "body = ?, activity = ?, title= ?, " .
369       "assigned_to = ? WHERE id = ?",
370       array($body, $activity, $title, $assigned_to, $id) );
371   }
374 function updatePnoteMessageStatus($id, $message_status)
376   if ($message_status == "Done") {
377     sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
378   }
379   else {
380     sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
381   }
384 function authorizePnote($id, $authorized = "1")
386   sqlQuery("UPDATE pnotes SET authorized = ? WHERE id = ?", array ($authorized,$id) );
389 function disappearPnote($id)
391   sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done' WHERE id=?", array($id) );
392   return true;
395 function reappearPnote ($id)
397   sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
398   return true;
401 function deletePnote($id)
403   sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );
404   return true;