3 * This file contains functions for handling notes attached to patient files.
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.
11 require_once($GLOBALS['srcdir'].'/sql.inc');
14 * Retrieve a note, given its ID
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.
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
32 $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
34 else if ($activity=='0') {
35 $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
37 else { //$activity=='all'
38 $activity_query = " ";
41 // Set whether to show chosen user or all users
42 if ($show_all == 'yes' ) {
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);
60 $result = sqlStatement($sql, array($usrvar));
64 if(sqlNumRows($result) != 0) {
65 $total = sqlNumRows($result);
77 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
78 $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
80 $sqlParameterArray = array();
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);
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);
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') ";
98 else { // $activity == '1'
100 $sql .= " AND activity = '1' AND message_status != 'Done' ";
104 $sql .= " AND assigned_to LIKE ?";
105 array_push($sqlParameterArray, $username);
108 $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
109 $sql .= " ORDER BY date DESC";
111 $sql .= " LIMIT $start, $limit";
113 $res = sqlStatement($sql, $sqlParameterArray);
116 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
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();
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);
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);
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') ";
143 else { // $activity == '1'
144 // only return active
145 $sql .= " AND activity = '1' AND message_status != 'Done' ";
149 $sql .= " AND assigned_to LIKE ?";
150 array_push($sqlParameterArray, $username);
153 $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
154 $sql .= " ORDER BY date DESC";
156 $sql .= " LIMIT $start, $limit";
158 $res = sqlStatement($sql, $sqlParameterArray);
161 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
166 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
169 $limit = "LIMIT $offset, $limit";
180 CONCAT(pd.fname, ' ', pd.lname)
186 LEFT JOIN patient_data AS pd
188 WHERE assigned_to = '-patient-'
195 $res = sqlStatement($sql, array($pid));
196 for($iter = 0;$row = sqlFetchArray($res);$iter++){
202 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
205 $limit = "LIMIT $offset, $limit";
210 date_created AS `date`,
211 'Patient Reminders' AS `user`,
213 CONCAT(lo.title, ':', lo2.title) AS body,
214 '' as message_status,
215 'Notification' as `type`
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'
226 AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
231 $res = sqlStatement($sql, array($pid));
232 for($iter = 0;$row = sqlFetchArray($res);$iter++){
238 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
241 $limit = "LIMIT $offset, $limit";
252 CONCAT(pd.lname, ' ', pd.fname)
259 LEFT JOIN patient_data AS pd
264 AND p.message_status != 'Done'
269 $res = sqlStatement($sql, array($pid,$pid));
270 for($iter = 0;$row = sqlFetchArray($res);$iter++){
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++)
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.
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) );
375 sqlStatement("UPDATE pnotes SET " .
376 "body = ?, activity = ?, title= ?, " .
377 "assigned_to = ? WHERE id = ?",
378 array($body, $activity, $title, $assigned_to, $id) );
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) );
388 sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
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) );
403 function reappearPnote ($id)
405 sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
409 function deletePnote($id)
411 sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );