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.
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
24 $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
26 else if ($activity=='0') {
27 $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
29 else { //$activity=='all'
30 $activity_query = " ";
33 // Set whether to show chosen user or all users
34 if ($show_all == 'yes' ) {
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);
52 $result = sqlStatement($sql, array($usrvar));
56 if(sqlNumRows($result) != 0) {
57 $total = sqlNumRows($result);
69 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
70 $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
72 $sqlParameterArray = array();
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);
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);
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') ";
90 else { // $activity == '1'
92 $sql .= " AND activity = '1' AND message_status != 'Done' ";
96 $sql .= " AND assigned_to LIKE ?";
97 array_push($sqlParameterArray, $username);
100 $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
101 $sql .= " ORDER BY date DESC";
103 $sql .= " LIMIT $start, $limit";
105 $res = sqlStatement($sql, $sqlParameterArray);
108 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
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();
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);
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);
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') ";
135 else { // $activity == '1'
136 // only return active
137 $sql .= " AND activity = '1' AND message_status != 'Done' ";
141 $sql .= " AND assigned_to LIKE ?";
142 array_push($sqlParameterArray, $username);
145 $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
146 $sql .= " ORDER BY date DESC";
148 $sql .= " LIMIT $start, $limit";
150 $res = sqlStatement($sql, $sqlParameterArray);
153 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
158 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
161 $limit = "LIMIT $offset, $limit";
172 CONCAT(pd.fname, ' ', pd.lname)
178 LEFT JOIN patient_data AS pd
180 WHERE assigned_to = '-patient-'
187 $res = sqlStatement($sql, array($pid));
188 for($iter = 0;$row = sqlFetchArray($res);$iter++){
194 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
197 $limit = "LIMIT $offset, $limit";
202 date_created AS `date`,
203 'Patient Reminders' AS `user`,
205 CONCAT(lo.title, ':', lo2.title) AS body,
206 '' as message_status,
207 'Notification' as `type`
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'
218 AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
223 $res = sqlStatement($sql, array($pid));
224 for($iter = 0;$row = sqlFetchArray($res);$iter++){
230 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
233 $limit = "LIMIT $offset, $limit";
244 CONCAT(pd.lname, ' ', pd.fname)
251 LEFT JOIN patient_data AS pd
256 AND p.message_status != 'Done'
261 $res = sqlStatement($sql, array($pid,$pid));
262 for($iter = 0;$row = sqlFetchArray($res);$iter++){
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++)
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.
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) );
367 sqlStatement("UPDATE pnotes SET " .
368 "body = ?, activity = ?, title= ?, " .
369 "assigned_to = ? WHERE id = ?",
370 array($body, $activity, $title, $assigned_to, $id) );
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) );
380 sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
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) );
395 function reappearPnote ($id)
397 sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
401 function deletePnote($id)
403 sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );