setting version, preparing ubuntu packages and prepping links for release
[openemr.git] / library / pnotes.inc
blobc2357c292726d18a5418c1ecb878c30e48fd54d2
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   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
51     $all[$iter] = $row;
52   return $all;
55 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
56   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
58 $sqlParameterArray = array();
59   if ($docid) {
60     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
61     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
62     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
63     array_push($sqlParameterArray, '%'.$date.'%', $docid);
64   }
65   else {
66     $sql = "SELECT $cols FROM pnotes AS p " .
67       "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
68     array_push($sqlParameterArray, '%'.$date.'%', $pid);
69   }
70   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
71   if ($activity != "all") {
72     $sql .= " AND activity = ?";
73     array_push($sqlParameterArray, $activity);
74   }
75   if ($username) {
76     $sql .= " AND assigned_to LIKE ?";
77     array_push($sqlParameterArray, $username);
78   }
79   if ($status)
80     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
81   $sql .= " ORDER BY date DESC";
82   if($limit != "all")
83     $sql .= " LIMIT $start, $limit";
85   $res = sqlStatement($sql, $sqlParameterArray);
87   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
88     $all[$iter] = $row;
89   return $all;
92 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
94   if($limit){
95     $limit = "LIMIT $offset, $limit";
96   }
97   $sql = "
98     SELECT
99       p.id,
100       p.date,
101       p.user,
102       p.title,
103       REPLACE(
104         p.body,
105         '-patient-',
106         CONCAT(pd.fname, ' ', pd.lname)
107       ) AS body,
108       p.message_status,
109       'Message' as `type`
110     FROM
111       pnotes AS p 
112       LEFT JOIN patient_data AS pd 
113         ON pd.id = p.pid 
114     WHERE assigned_to = '-patient-' 
115       AND p.deleted != 1 
116       AND p.pid = ?
117       $search
118     ORDER BY `date` desc
119     $limit
120   ";
121   $res = sqlStatement($sql, array($pid));
122   for($iter = 0;$row = sqlFetchArray($res);$iter++){
123     $all[$iter] = $row;
124   }
125   return $all;
128 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
130   if($limit){
131     $limit = "LIMIT $offset, $limit";
132   }
133   $sql = "
134     SELECT
135       pr.id,
136       date_created AS `date`,
137       'Patient Reminders' AS `user`,
138       due_status AS title,
139       CONCAT(lo.title, ':', lo2.title) AS body,
140       '' as message_status,
141       'Notification' as `type`
142     FROM
143       patient_reminders AS pr 
144       LEFT JOIN list_options AS lo 
145         ON lo.option_id = pr.category 
146         AND lo.list_id = 'rule_action_category' 
147       LEFT JOIN list_options AS lo2 
148         ON lo2.option_id = pr.item 
149         AND lo2.list_id = 'rule_action' 
150     WHERE pid = ?
151       AND active = 1
152       AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
153       $search
154     ORDER BY `date` desc
155     $limit
156   ";
157   $res = sqlStatement($sql, array($pid));
158   for($iter = 0;$row = sqlFetchArray($res);$iter++){
159     $all[$iter] = $row;
160   }
161   return $all;
164 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
166   if($limit){
167     $limit = "LIMIT $offset, $limit";
168   }
169   $sql = "
170     SELECT
171       p.id,
172       p.date,
173       p.assigned_to,
174       p.title,
175       REPLACE(
176         p.body,
177         '-patient-',
178         CONCAT(pd.lname, ' ', pd.fname)
179       ) AS body,
180       p.activity,
181       p.message_status,
182       'Message' as `type`
183     FROM
184       pnotes AS p 
185       LEFT JOIN patient_data AS pd 
186         ON pd.id = p.pid 
187     WHERE `user` = ?
188       AND p.deleted != 1 
189       AND p.pid = ?
190       $search
191     ORDER BY `date` desc
192     $limit
193   ";
194   $res = sqlStatement($sql, array($pid,$pid));
195   for($iter = 0;$row = sqlFetchArray($res);$iter++){
196     $all[$iter] = $row;
197   }
198   return $all;
201 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
203   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE '$pid' " .
204     "AND activity = '$activity' ".
205     " AND deleted != 1 ".
206     " ORDER BY date DESC LIMIT $start,$limit");
207   for ($iter = 0; $row = sqlFetchArray($res); $iter++)
208     $all[$iter] = $row;
209   return $all;
212 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
213   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
215   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
217   $body = date('Y-m-d H:i') . ' (' . $_SESSION['authUser'];
218   if ($assigned_to) $body .= " to $assigned_to";
219   $body = $body . ') ' . $newtext;
221   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
222     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
223     array($datetime, $body, $pid, $_SESSION['authUser'], $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
226 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
227   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
229   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
231   $body = date('Y-m-d H:i') . ' (' . $pid;
232   if ($assigned_to) $body .= " to $assigned_to";
233   $body = $body . ') ' . $newtext;
235   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
236     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
237     array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
240 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
242   $row = getPnoteById($id);
243   if (! $row) die("updatePnote() did not find id '$id'");
244   $activity = $assigned_to ? '1' : '0';
246   $body = $row['body'] . "\n" . date('Y-m-d H:i') .
247     ' (' . $_SESSION['authUser'];
248   if ($assigned_to) $body .= " to $assigned_to";
249   $body = $body . ') ' . $newtext;
251   if ($message_status) {
252     sqlStatement("UPDATE pnotes SET " .
253       "body = ?, activity = ?, title= ?, " .
254       "assigned_to = ?, message_status = ? WHERE id = ?",
255       array($body, $activity, $title, $assigned_to, $message_status, $id) );
256   }
257   else {
258     sqlStatement("UPDATE pnotes SET " .
259       "body = ?, activity = ?, title= ?, " .
260       "assigned_to = ? WHERE id = ?",
261       array($body, $activity, $title, $assigned_to, $id) );
262   }
265 function updatePnoteMessageStatus($id, $message_status)
267   sqlStatement("update pnotes set message_status = ? where id = ?", array($message_status, $id) );
270 function authorizePnote($id, $authorized = "1")
272   sqlQuery("UPDATE pnotes SET authorized = '$authorized' WHERE id = '$id'");
275 function disappearPnote($id)
277   sqlStatement("UPDATE pnotes SET activity = '0' WHERE id=?", array($id) );
278   return true;
281 function reappearPnote ($id)
283   sqlStatement("UPDATE pnotes SET activity = '1' WHERE id=?", array($id) );
284   return true;
287 function deletePnote($id)
289   sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );
290   return true;