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.
10 * 2013-02-08 EMR Direct: changes to allow notes added by background-services with pid=0
13 /* for sqlQuery(), sqlStatement(), sqlNumRows(), sqlFetchArray(). */
14 require_once($GLOBALS['srcdir'].'/sql.inc');
17 * Retrieve a note, given its ID
19 * @param string $id the ID of the note to retrieve.
20 * @param string $cols A list of columns to retrieve. defaults to '*' for all.
22 function getPnoteById($id, $cols = "*")
24 return sqlQuery("SELECT $cols FROM pnotes WHERE id=? " .
25 ' AND deleted != 1 '. // exclude ALL deleted notes
26 'order by date DESC limit 0,1', array($id) );
30 * Get the patient notes for the given user.
32 * This function is used to retrieve notes assigned to the given user, or
33 * optionally notes assigned to any user.
35 * @param string $activity 0 for deleted notes, 1 (the default) for active
36 * notes, or 'All' for all.
37 * @param string $show_all whether to display only the selected user's
38 * messages, or all users' messages.
39 * @param string $user The user whom's notes you want to retrieve.
40 * @param bool $count Whether to return a count, or just return 0.
41 * @param string $sortby A field to sort results by. (options are users.lname,patient_data.lname,pnotes.title,pnotes.date,pnotes.message_status) (will default to users.lname)
42 * @param string $sortorder whether to sort ascending or descending.
43 * @param string $begin what row to start retrieving results from.
44 * @param string $listnumber number of rows to return.
45 * @return int The number of rows retrieved, or 0 if $count was true.
47 function getPnotesByUser($activity="1",$show_all="no",$user='',$count=false,$sortby='',$sortorder='',$begin='',$listnumber='')
50 // Set the activity part of query
52 $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
54 else if ($activity=='0') {
55 $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
57 else { //$activity=='all'
58 $activity_query = " ";
61 // Set whether to show chosen user or all users
62 if ($show_all == 'yes' ) {
69 // 2013-02-08 EMR Direct: minor changes to query so notes with pid=0 don't disappear
70 $sql = "SELECT pnotes.id, pnotes.user, pnotes.pid, pnotes.title, pnotes.date, pnotes.message_status,
71 IF(pnotes.pid = 0 OR pnotes.user != pnotes.pid,users.fname,patient_data.fname) as users_fname,
72 IF(pnotes.pid = 0 OR pnotes.user != pnotes.pid,users.lname,patient_data.lname) as users_lname,
73 patient_data.fname as patient_data_fname, patient_data.lname as patient_data_lname
74 FROM ((pnotes LEFT JOIN users ON pnotes.user = users.username)
75 LEFT JOIN patient_data ON pnotes.pid = patient_data.pid) WHERE $activity_query
76 pnotes.deleted != '1' AND pnotes.assigned_to LIKE ?";
77 if (!empty($sortby) || !empty($sortorder) || !empty($begin) || !empty($listnumber)) {
78 $sql .= " order by ".escape_sql_column_name($sortby,array('users','patient_data','pnotes'),TRUE).
79 " ".escape_sort_order($sortorder).
80 " limit ".escape_limit($begin).", ".escape_limit($listnumber);
82 $result = sqlStatement($sql, array($usrvar));
86 if(sqlNumRows($result) != 0) {
87 $total = sqlNumRows($result);
99 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
100 $limit = "all", $start = 0, $username = '', $docid = 0, $status = "", $orderid = 0)
102 $sqlParameterArray = array();
104 $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
105 "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
106 "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
107 array_push($sqlParameterArray, '%'.$date.'%', $docid);
110 $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
111 "WHERE p.date LIKE ? AND r.type1 = 2 AND " .
112 "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
113 array_push($sqlParameterArray, '%'.$date.'%', $orderid);
116 $sql = "SELECT $cols FROM pnotes AS p " .
117 "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
118 array_push($sqlParameterArray, '%'.$date.'%', $pid);
120 $sql .= " AND deleted != 1"; // exclude ALL deleted notes
121 if ($activity != "all") {
122 if ($activity == '0') {
123 // only return inactive
124 $sql .= " AND (activity = '0' OR message_status = 'Done') ";
126 else { // $activity == '1'
127 // only return active
128 $sql .= " AND activity = '1' AND message_status != 'Done' ";
132 $sql .= " AND assigned_to LIKE ?";
133 array_push($sqlParameterArray, $username);
136 $sql .= " AND message_status IN ('".str_replace(",", "','", add_escape_custom($status) )."')";
137 $sql .= " ORDER BY date DESC";
139 $sql .= " LIMIT ".escape_limit($start).", ".escape_limit($limit);
141 $res = sqlStatement($sql, $sqlParameterArray);
144 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
149 // activity can only be 0, 1, or 'all'
150 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
151 $limit = "all", $start = 0, $username = '', $docid = 0, $status = "", $orderid = 0)
153 $sqlParameterArray = array();
155 $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
156 "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
157 "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
158 array_push($sqlParameterArray, '%'.$date.'%', $docid);
161 $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
162 "WHERE p.date LIKE ? AND r.type1 = 2 AND " .
163 "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
164 array_push($sqlParameterArray, '%'.$date.'%', $orderid);
167 $sql = "SELECT $cols FROM pnotes AS p " .
168 "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
169 array_push($sqlParameterArray, '%'.$date.'%', $pid);
171 $sql .= " AND deleted != 1"; // exclude ALL deleted notes
172 if ($activity != "all") {
173 if ($activity == '0') {
174 // only return inactive
175 $sql .= " AND (activity = '0' OR message_status = 'Done') ";
177 else { // $activity == '1'
178 // only return active
179 $sql .= " AND activity = '1' AND message_status != 'Done' ";
183 $sql .= " AND assigned_to LIKE ?";
184 array_push($sqlParameterArray, $username);
187 $sql .= " AND message_status IN ('".str_replace(",", "','", add_escape_custom($status) )."')";
188 $sql .= " ORDER BY date DESC";
190 $sql .= " LIMIT ".escape_limit($start).", ".escape_limit($limit);
192 $res = sqlStatement($sql, $sqlParameterArray);
195 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
200 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
203 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
214 CONCAT(pd.fname, ' ', pd.lname)
220 LEFT JOIN patient_data AS pd
222 WHERE assigned_to = '-patient-'
229 $res = sqlStatement($sql, array($pid));
230 for($iter = 0;$row = sqlFetchArray($res);$iter++){
236 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
239 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
244 date_created AS `date`,
245 'Patient Reminders' AS `user`,
247 CONCAT(lo.title, ':', lo2.title) AS body,
248 '' as message_status,
249 'Notification' as `type`
251 patient_reminders AS pr
252 LEFT JOIN list_options AS lo
253 ON lo.option_id = pr.category
254 AND lo.list_id = 'rule_action_category'
255 LEFT JOIN list_options AS lo2
256 ON lo2.option_id = pr.item
257 AND lo2.list_id = 'rule_action'
260 AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
265 $res = sqlStatement($sql, array($pid));
266 for($iter = 0;$row = sqlFetchArray($res);$iter++){
272 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
275 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
286 CONCAT(pd.lname, ' ', pd.fname)
293 LEFT JOIN patient_data AS pd
298 AND p.message_status != 'Done'
303 $res = sqlStatement($sql, array($pid,$pid));
304 for($iter = 0;$row = sqlFetchArray($res);$iter++){
310 // activity can be 0, 1, or 'all'
311 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
313 if ($activity == '1') {
314 // return only active
315 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
316 "AND activity = '1' ".
317 " AND message_status != 'Done' ".
318 " AND deleted != 1 ".
319 " ORDER BY date DESC LIMIT ".escape_limit($start).",".escape_limit($limit), array($pid) );
321 else if ($activity == '0') {
322 // return only inactive
323 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
324 "AND (activity = '0' ".
325 " OR message_status = 'Done') ".
326 " AND deleted != 1 ".
327 " ORDER BY date DESC LIMIT ".escape_limit($start).",".escape_limit($limit), array($pid) );
329 else { // $activity == "all"
330 // return both active and inactive
331 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
332 " AND deleted != 1 ".
333 " ORDER BY date DESC LIMIT ".escape_limit($start).",".escape_limit($limit), array($pid) );
335 for ($iter = 0; $row = sqlFetchArray($res); $iter++)
340 /** Add a note to a patient's medical record.
342 * @param int $pid the ID of the patient whos medical record this note is going to be attached to.
343 * @param string $newtext the note contents.
344 * @param int $authorized
345 * @param int $activity
346 * @param string $title
347 * @param string $assigned_to
348 * @param string $datetime
349 * @param string $message_status
350 * @param string $background_user if set then the pnote is created by a background-service rather than a user
351 * @return int the ID of the added note.
353 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
354 $title= 'Unassigned', $assigned_to = '', $datetime = '',
355 $message_status = 'New', $background_user="")
357 if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
359 // make inactive if set as Done
360 if ($message_status == 'Done') $activity = 0;
362 $user = ($background_user!="" ? $background_user : $_SESSION['authUser']);
363 $body = date('Y-m-d H:i') . ' (' . $user;
364 if ($assigned_to) $body .= " to $assigned_to";
365 $body = $body . ') ' . $newtext;
367 return sqlInsert('INSERT INTO pnotes (date, body, pid, user, groupname, ' .
368 'authorized, activity, title, assigned_to, message_status) VALUES ' .
369 '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
370 array($datetime, $body, $pid, $user, $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
373 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
374 $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
376 if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
378 // make inactive if set as Done
379 if ($message_status == "Done") $activity = 0;
381 $body = date('Y-m-d H:i') . ' (' . $pid;
382 if ($assigned_to) $body .= " to $assigned_to";
383 $body = $body . ') ' . $newtext;
385 return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
386 "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
387 array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
390 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
392 $row = getPnoteById($id);
393 if (! $row) die("updatePnote() did not find id '".text($id)."'");
394 $activity = $assigned_to ? '1' : '0';
396 // make inactive if set as Done
397 if ($message_status == "Done") $activity = 0;
399 $body = $row['body'] . "\n" . date('Y-m-d H:i') .
400 ' (' . $_SESSION['authUser'];
401 if ($assigned_to) $body .= " to $assigned_to";
402 $body = $body . ') ' . $newtext;
404 if ($message_status) {
405 sqlStatement("UPDATE pnotes SET " .
406 "body = ?, activity = ?, title= ?, " .
407 "assigned_to = ?, message_status = ? WHERE id = ?",
408 array($body, $activity, $title, $assigned_to, $message_status, $id) );
411 sqlStatement("UPDATE pnotes SET " .
412 "body = ?, activity = ?, title= ?, " .
413 "assigned_to = ? WHERE id = ?",
414 array($body, $activity, $title, $assigned_to, $id) );
418 function updatePnoteMessageStatus($id, $message_status)
420 if ($message_status == "Done") {
421 sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
424 sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
429 * Set the patient id in an existing message where pid=0
430 * @param $id the id of the existing note
431 * @param $patient_id the patient id to associate with the note
432 * @author EMR Direct <http://www.emrdirect.com/>
434 function updatePnotePatient($id, $patient_id)
436 $row = getPnoteById($id);
437 if (! $row) die("updatePnotePatient() did not find id '".text($id)."'");
438 $activity = $assigned_to ? '1' : '0';
441 if($pid != 0 || (int)$patient_id < 1) die("updatePnotePatient invalid operation");
443 $pid = (int) $patient_id;
444 $newtext = "\n" . date('Y-m-d H:i') . " (patient set by " . $_SESSION['authUser'] .")";
445 $body = $row['body'] . $newtext;
447 sqlStatement("UPDATE pnotes SET pid = ?, body = ? WHERE id = ?", array($pid, $body, $id) );
450 function authorizePnote($id, $authorized = "1")
452 sqlQuery("UPDATE pnotes SET authorized = ? WHERE id = ?", array ($authorized,$id) );
455 function disappearPnote($id)
457 sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done' WHERE id=?", array($id) );
461 function reappearPnote ($id)
463 sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
467 function deletePnote($id)
469 sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );