Separate Display and Search of Organization, and person names
[openemr.git] / library / pnotes.inc
blobe27f31b59c8fed8d61cef790c9edd1f54322d757
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("{$GLOBALS['srcdir']}/sql.inc");
9 // 06-2009, BM migrated the patient_note_types array to the list_options table
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 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
19   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
21 $sqlParameterArray = array();
22   if ($docid) {
23     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
24     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
25     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
26     array_push($sqlParameterArray, '%'.$date.'%', $docid);
27   }
28   else {
29     $sql = "SELECT $cols FROM pnotes AS p " .
30       "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
31     array_push($sqlParameterArray, '%'.$date.'%', $pid);
32   }
33   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
34   if ($activity != "all") {
35     $sql .= " AND activity = ?";
36     array_push($sqlParameterArray, $activity);
37   }
38   if ($username) {
39     $sql .= " AND assigned_to LIKE ?";
40     array_push($sqlParameterArray, $username);
41   }
42   if ($status)
43     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
44   $sql .= " ORDER BY date DESC";
45   if($limit != "all")
46     $sql .= " LIMIT $start, $limit";
48   $res = sqlStatement($sql, $sqlParameterArray);
50   $all=array();
51   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
52     $all[$iter] = $row;
53   return $all;
56 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
57   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
59 $sqlParameterArray = array();
60   if ($docid) {
61     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
62     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
63     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
64     array_push($sqlParameterArray, '%'.$date.'%', $docid);
65   }
66   else {
67     $sql = "SELECT $cols FROM pnotes AS p " .
68       "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
69     array_push($sqlParameterArray, '%'.$date.'%', $pid);
70   }
71   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
72   if ($activity != "all") {
73     $sql .= " AND activity = ?";
74     array_push($sqlParameterArray, $activity);
75   }
76   if ($username) {
77     $sql .= " AND assigned_to LIKE ?";
78     array_push($sqlParameterArray, $username);
79   }
80   if ($status)
81     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
82   $sql .= " ORDER BY date DESC";
83   if($limit != "all")
84     $sql .= " LIMIT $start, $limit";
86   $res = sqlStatement($sql, $sqlParameterArray);
88   $all=array();
89   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
90     $all[$iter] = $row;
91   return $all;
94 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
96   if($limit){
97     $limit = "LIMIT $offset, $limit";
98   }
99   $sql = "
100     SELECT
101       p.id,
102       p.date,
103       p.user,
104       p.title,
105       REPLACE(
106         p.body,
107         '-patient-',
108         CONCAT(pd.fname, ' ', pd.lname)
109       ) AS body,
110       p.message_status,
111       'Message' as `type`
112     FROM
113       pnotes AS p 
114       LEFT JOIN patient_data AS pd 
115         ON pd.id = p.pid 
116     WHERE assigned_to = '-patient-' 
117       AND p.deleted != 1 
118       AND p.pid = ?
119       $search
120     ORDER BY `date` desc
121     $limit
122   ";
123   $res = sqlStatement($sql, array($pid));
124   for($iter = 0;$row = sqlFetchArray($res);$iter++){
125     $all[$iter] = $row;
126   }
127   return $all;
130 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
132   if($limit){
133     $limit = "LIMIT $offset, $limit";
134   }
135   $sql = "
136     SELECT
137       pr.id,
138       date_created AS `date`,
139       'Patient Reminders' AS `user`,
140       due_status AS title,
141       CONCAT(lo.title, ':', lo2.title) AS body,
142       '' as message_status,
143       'Notification' as `type`
144     FROM
145       patient_reminders AS pr 
146       LEFT JOIN list_options AS lo 
147         ON lo.option_id = pr.category 
148         AND lo.list_id = 'rule_action_category' 
149       LEFT JOIN list_options AS lo2 
150         ON lo2.option_id = pr.item 
151         AND lo2.list_id = 'rule_action' 
152     WHERE pid = ?
153       AND active = 1
154       AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
155       $search
156     ORDER BY `date` desc
157     $limit
158   ";
159   $res = sqlStatement($sql, array($pid));
160   for($iter = 0;$row = sqlFetchArray($res);$iter++){
161     $all[$iter] = $row;
162   }
163   return $all;
166 function getPatientSentNotes($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.assigned_to,
176       p.title,
177       REPLACE(
178         p.body,
179         '-patient-',
180         CONCAT(pd.lname, ' ', pd.fname)
181       ) AS body,
182       p.activity,
183       p.message_status,
184       'Message' as `type`
185     FROM
186       pnotes AS p 
187       LEFT JOIN patient_data AS pd 
188         ON pd.id = p.pid 
189     WHERE `user` = ?
190       AND p.deleted != 1 
191       AND p.pid = ?
192       $search
193     ORDER BY `date` desc
194     $limit
195   ";
196   $res = sqlStatement($sql, array($pid,$pid));
197   for($iter = 0;$row = sqlFetchArray($res);$iter++){
198     $all[$iter] = $row;
199   }
200   return $all;
203 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
205   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE '$pid' " .
206     "AND activity = '$activity' ".
207     " AND deleted != 1 ".
208     " ORDER BY date DESC LIMIT $start,$limit");
209   for ($iter = 0; $row = sqlFetchArray($res); $iter++)
210     $all[$iter] = $row;
211   return $all;
214 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
215   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
217   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
219   $body = date('Y-m-d H:i') . ' (' . $_SESSION['authUser'];
220   if ($assigned_to) $body .= " to $assigned_to";
221   $body = $body . ') ' . $newtext;
223   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
224     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
225     array($datetime, $body, $pid, $_SESSION['authUser'], $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
228 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
229   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
231   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
233   $body = date('Y-m-d H:i') . ' (' . $pid;
234   if ($assigned_to) $body .= " to $assigned_to";
235   $body = $body . ') ' . $newtext;
237   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
238     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
239     array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
242 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
244   $row = getPnoteById($id);
245   if (! $row) die("updatePnote() did not find id '$id'");
246   $activity = $assigned_to ? '1' : '0';
248   $body = $row['body'] . "\n" . date('Y-m-d H:i') .
249     ' (' . $_SESSION['authUser'];
250   if ($assigned_to) $body .= " to $assigned_to";
251   $body = $body . ') ' . $newtext;
253   if ($message_status) {
254     sqlStatement("UPDATE pnotes SET " .
255       "body = ?, activity = ?, title= ?, " .
256       "assigned_to = ?, message_status = ? WHERE id = ?",
257       array($body, $activity, $title, $assigned_to, $message_status, $id) );
258   }
259   else {
260     sqlStatement("UPDATE pnotes SET " .
261       "body = ?, activity = ?, title= ?, " .
262       "assigned_to = ? WHERE id = ?",
263       array($body, $activity, $title, $assigned_to, $id) );
264   }
267 function updatePnoteMessageStatus($id, $message_status)
269   sqlStatement("update pnotes set message_status = ? where id = ?", array($message_status, $id) );
272 function authorizePnote($id, $authorized = "1")
274   sqlQuery("UPDATE pnotes SET authorized = '$authorized' WHERE id = '$id'");
277 function disappearPnote($id)
279   sqlStatement("UPDATE pnotes SET activity = '0' WHERE id=?", array($id) );
280   return true;
283 function reappearPnote ($id)
285   sqlStatement("UPDATE pnotes SET activity = '1' WHERE id=?", array($id) );
286   return true;
289 function deletePnote($id)
291   sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );
292   return true;