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) );
19 // activity can be 0, 1, or 'all'
20 function getPnotesByUser($activity="1",$show_all="no",$user='',$count=false,$sortby='',$sortorder='',$begin='',$listnumber='')
23 // Set the activity part of query
25 $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
27 else if ($activity=='0') {
28 $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
30 else { //$activity=='all'
31 $activity_query = " ";
34 // Set whether to show chosen user or all users
35 if ($show_all == 'yes' ) {
42 $sql = "SELECT pnotes.id, pnotes.user, pnotes.pid, pnotes.title, pnotes.date, pnotes.message_status,
43 IF(pnotes.user != pnotes.pid,users.fname,patient_data.fname) as users_fname,
44 IF(pnotes.user != pnotes.pid,users.lname,patient_data.lname) as users_lname,
45 patient_data.fname as patient_data_fname, patient_data.lname as patient_data_lname
46 FROM ((pnotes LEFT JOIN users ON pnotes.user = users.username)
47 JOIN patient_data ON pnotes.pid = patient_data.pid) WHERE $activity_query
48 pnotes.deleted != '1' AND pnotes.assigned_to LIKE ?";
49 if (!empty($sortby) || !empty($sortorder) || !empty($begin) || !empty($listnumber)) {
50 $sql .= " order by ".add_escape_custom($sortby)." ".add_escape_custom($sortorder).
51 " limit ".add_escape_custom($begin).", ".add_escape_custom($listnumber);
53 $result = sqlStatement($sql, array($usrvar));
57 if(sqlNumRows($result) != 0) {
58 $total = sqlNumRows($result);
71 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
72 $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
74 $sqlParameterArray = array();
76 $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
77 "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
78 "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
79 array_push($sqlParameterArray, '%'.$date.'%', $docid);
82 $sql = "SELECT $cols FROM pnotes AS p " .
83 "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
84 array_push($sqlParameterArray, '%'.$date.'%', $pid);
86 $sql .= " AND deleted != 1"; // exclude ALL deleted notes
87 if ($activity != "all") {
88 if ($activity == '0') {
89 // only return inactive
90 $sql .= " AND (activity = '0' OR message_status = 'Done') ";
92 else { // $activity == '1'
94 $sql .= " AND activity = '1' AND message_status != 'Done' ";
98 $sql .= " AND assigned_to LIKE ?";
99 array_push($sqlParameterArray, $username);
102 $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
103 $sql .= " ORDER BY date DESC";
105 $sql .= " LIMIT $start, $limit";
107 $res = sqlStatement($sql, $sqlParameterArray);
110 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
115 // activity can only be 0, 1, or 'all
116 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
117 $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
119 $sqlParameterArray = array();
121 $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
122 "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
123 "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
124 array_push($sqlParameterArray, '%'.$date.'%', $docid);
127 $sql = "SELECT $cols FROM pnotes AS p " .
128 "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
129 array_push($sqlParameterArray, '%'.$date.'%', $pid);
131 $sql .= " AND deleted != 1"; // exclude ALL deleted notes
132 if ($activity != "all") {
133 if ($activity == '0') {
134 // only return inactive
135 $sql .= " AND (activity = '0' OR message_status = 'Done') ";
137 else { // $activity == '1'
138 // only return active
139 $sql .= " AND activity = '1' AND message_status != 'Done' ";
143 $sql .= " AND assigned_to LIKE ?";
144 array_push($sqlParameterArray, $username);
147 $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
148 $sql .= " ORDER BY date DESC";
150 $sql .= " LIMIT $start, $limit";
152 $res = sqlStatement($sql, $sqlParameterArray);
155 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
160 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
163 $limit = "LIMIT $offset, $limit";
174 CONCAT(pd.fname, ' ', pd.lname)
180 LEFT JOIN patient_data AS pd
182 WHERE assigned_to = '-patient-'
189 $res = sqlStatement($sql, array($pid));
190 for($iter = 0;$row = sqlFetchArray($res);$iter++){
196 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
199 $limit = "LIMIT $offset, $limit";
204 date_created AS `date`,
205 'Patient Reminders' AS `user`,
207 CONCAT(lo.title, ':', lo2.title) AS body,
208 '' as message_status,
209 'Notification' as `type`
211 patient_reminders AS pr
212 LEFT JOIN list_options AS lo
213 ON lo.option_id = pr.category
214 AND lo.list_id = 'rule_action_category'
215 LEFT JOIN list_options AS lo2
216 ON lo2.option_id = pr.item
217 AND lo2.list_id = 'rule_action'
220 AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
225 $res = sqlStatement($sql, array($pid));
226 for($iter = 0;$row = sqlFetchArray($res);$iter++){
232 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
235 $limit = "LIMIT $offset, $limit";
246 CONCAT(pd.lname, ' ', pd.fname)
253 LEFT JOIN patient_data AS pd
258 AND p.message_status != 'Done'
263 $res = sqlStatement($sql, array($pid,$pid));
264 for($iter = 0;$row = sqlFetchArray($res);$iter++){
270 // activity can be 0, 1, or 'all'
271 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
273 if ($activity == '1') {
274 // return only active
275 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
276 "AND activity = '1' ".
277 " AND message_status != 'Done' ".
278 " AND deleted != 1 ".
279 " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
281 else if ($activity == '0') {
282 // return only inactive
283 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
284 "AND (activity = '0' ".
285 " OR message_status = 'Done') ".
286 " AND deleted != 1 ".
287 " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
289 else { // $activity == "all"
290 // return both active and inactive
291 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
292 " AND deleted != 1 ".
293 " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
295 for ($iter = 0; $row = sqlFetchArray($res); $iter++)
300 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
301 $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
303 if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
305 // make inactive if set as Done
306 if ($message_status == "Done") $activity = 0;
308 $body = date('Y-m-d H:i') . ' (' . $_SESSION['authUser'];
309 if ($assigned_to) $body .= " to $assigned_to";
310 $body = $body . ') ' . $newtext;
312 return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
313 "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
314 array($datetime, $body, $pid, $_SESSION['authUser'], $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
317 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
318 $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
320 if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
322 // make inactive if set as Done
323 if ($message_status == "Done") $activity = 0;
325 $body = date('Y-m-d H:i') . ' (' . $pid;
326 if ($assigned_to) $body .= " to $assigned_to";
327 $body = $body . ') ' . $newtext;
329 return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
330 "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
331 array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
334 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
336 $row = getPnoteById($id);
337 if (! $row) die("updatePnote() did not find id '$id'");
338 $activity = $assigned_to ? '1' : '0';
340 // make inactive if set as Done
341 if ($message_status == "Done") $activity = 0;
343 $body = $row['body'] . "\n" . date('Y-m-d H:i') .
344 ' (' . $_SESSION['authUser'];
345 if ($assigned_to) $body .= " to $assigned_to";
346 $body = $body . ') ' . $newtext;
348 if ($message_status) {
349 sqlStatement("UPDATE pnotes SET " .
350 "body = ?, activity = ?, title= ?, " .
351 "assigned_to = ?, message_status = ? WHERE id = ?",
352 array($body, $activity, $title, $assigned_to, $message_status, $id) );
355 sqlStatement("UPDATE pnotes SET " .
356 "body = ?, activity = ?, title= ?, " .
357 "assigned_to = ? WHERE id = ?",
358 array($body, $activity, $title, $assigned_to, $id) );
362 function updatePnoteMessageStatus($id, $message_status)
364 if ($message_status == "Done") {
365 sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
368 sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
372 function authorizePnote($id, $authorized = "1")
374 sqlQuery("UPDATE pnotes SET authorized = ? WHERE id = ?", array ($authorized,$id) );
377 function disappearPnote($id)
379 sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done' WHERE id=?", array($id) );
383 function reappearPnote ($id)
385 sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
389 function deletePnote($id)
391 sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );