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 = "")
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 " .
111 "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
112 array_push($sqlParameterArray, '%'.$date.'%', $pid);
114 $sql .= " AND deleted != 1"; // exclude ALL deleted notes
115 if ($activity != "all") {
116 if ($activity == '0') {
117 // only return inactive
118 $sql .= " AND (activity = '0' OR message_status = 'Done') ";
120 else { // $activity == '1'
121 // only return active
122 $sql .= " AND activity = '1' AND message_status != 'Done' ";
126 $sql .= " AND assigned_to LIKE ?";
127 array_push($sqlParameterArray, $username);
130 $sql .= " AND message_status IN ('".str_replace(",", "','", add_escape_custom($status) )."')";
131 $sql .= " ORDER BY date DESC";
133 $sql .= " LIMIT ".escape_limit($start).", ".escape_limit($limit);
135 $res = sqlStatement($sql, $sqlParameterArray);
138 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
143 // activity can only be 0, 1, or 'all'
144 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
145 $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
147 $sqlParameterArray = array();
149 $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
150 "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
151 "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
152 array_push($sqlParameterArray, '%'.$date.'%', $docid);
155 $sql = "SELECT $cols FROM pnotes AS p " .
156 "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
157 array_push($sqlParameterArray, '%'.$date.'%', $pid);
159 $sql .= " AND deleted != 1"; // exclude ALL deleted notes
160 if ($activity != "all") {
161 if ($activity == '0') {
162 // only return inactive
163 $sql .= " AND (activity = '0' OR message_status = 'Done') ";
165 else { // $activity == '1'
166 // only return active
167 $sql .= " AND activity = '1' AND message_status != 'Done' ";
171 $sql .= " AND assigned_to LIKE ?";
172 array_push($sqlParameterArray, $username);
175 $sql .= " AND message_status IN ('".str_replace(",", "','", add_escape_custom($status) )."')";
176 $sql .= " ORDER BY date DESC";
178 $sql .= " LIMIT ".escape_limit($start).", ".escape_limit($limit);
180 $res = sqlStatement($sql, $sqlParameterArray);
183 for ($iter = 0;$row = sqlFetchArray($res);$iter++)
188 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
191 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
202 CONCAT(pd.fname, ' ', pd.lname)
208 LEFT JOIN patient_data AS pd
210 WHERE assigned_to = '-patient-'
217 $res = sqlStatement($sql, array($pid));
218 for($iter = 0;$row = sqlFetchArray($res);$iter++){
224 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
227 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
232 date_created AS `date`,
233 'Patient Reminders' AS `user`,
235 CONCAT(lo.title, ':', lo2.title) AS body,
236 '' as message_status,
237 'Notification' as `type`
239 patient_reminders AS pr
240 LEFT JOIN list_options AS lo
241 ON lo.option_id = pr.category
242 AND lo.list_id = 'rule_action_category'
243 LEFT JOIN list_options AS lo2
244 ON lo2.option_id = pr.item
245 AND lo2.list_id = 'rule_action'
248 AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
253 $res = sqlStatement($sql, array($pid));
254 for($iter = 0;$row = sqlFetchArray($res);$iter++){
260 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
263 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
274 CONCAT(pd.lname, ' ', pd.fname)
281 LEFT JOIN patient_data AS pd
286 AND p.message_status != 'Done'
291 $res = sqlStatement($sql, array($pid,$pid));
292 for($iter = 0;$row = sqlFetchArray($res);$iter++){
298 // activity can be 0, 1, or 'all'
299 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
301 if ($activity == '1') {
302 // return only active
303 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
304 "AND activity = '1' ".
305 " AND message_status != 'Done' ".
306 " AND deleted != 1 ".
307 " ORDER BY date DESC LIMIT ".escape_limit($start).",".escape_limit($limit), array($pid) );
309 else if ($activity == '0') {
310 // return only inactive
311 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
312 "AND (activity = '0' ".
313 " OR message_status = 'Done') ".
314 " AND deleted != 1 ".
315 " ORDER BY date DESC LIMIT ".escape_limit($start).",".escape_limit($limit), array($pid) );
317 else { // $activity == "all"
318 // return both active and inactive
319 $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
320 " AND deleted != 1 ".
321 " ORDER BY date DESC LIMIT ".escape_limit($start).",".escape_limit($limit), array($pid) );
323 for ($iter = 0; $row = sqlFetchArray($res); $iter++)
328 /** Add a note to a patient's medical record.
330 * @param int $pid the ID of the patient whos medical record this note is going to be attached to.
331 * @param string $newtext the note contents.
332 * @param int $authorized
333 * @param int $activity
334 * @param string $title
335 * @param string $assigned_to
336 * @param string $datetime
337 * @param string $message_status
338 * @param string $background_user if set then the pnote is created by a background-service rather than a user
339 * @return int the ID of the added note.
341 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
342 $title= 'Unassigned', $assigned_to = '', $datetime = '',
343 $message_status = 'New', $background_user="")
345 if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
347 // make inactive if set as Done
348 if ($message_status == 'Done') $activity = 0;
350 $user = ($background_user!="" ? $background_user : $_SESSION['authUser']);
351 $body = date('Y-m-d H:i') . ' (' . $user;
352 if ($assigned_to) $body .= " to $assigned_to";
353 $body = $body . ') ' . $newtext;
355 return sqlInsert('INSERT INTO pnotes (date, body, pid, user, groupname, ' .
356 'authorized, activity, title, assigned_to, message_status) VALUES ' .
357 '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
358 array($datetime, $body, $pid, $user, $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
361 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
362 $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
364 if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
366 // make inactive if set as Done
367 if ($message_status == "Done") $activity = 0;
369 $body = date('Y-m-d H:i') . ' (' . $pid;
370 if ($assigned_to) $body .= " to $assigned_to";
371 $body = $body . ') ' . $newtext;
373 return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
374 "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
375 array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
378 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
380 $row = getPnoteById($id);
381 if (! $row) die("updatePnote() did not find id '".text($id)."'");
382 $activity = $assigned_to ? '1' : '0';
384 // make inactive if set as Done
385 if ($message_status == "Done") $activity = 0;
387 $body = $row['body'] . "\n" . date('Y-m-d H:i') .
388 ' (' . $_SESSION['authUser'];
389 if ($assigned_to) $body .= " to $assigned_to";
390 $body = $body . ') ' . $newtext;
392 if ($message_status) {
393 sqlStatement("UPDATE pnotes SET " .
394 "body = ?, activity = ?, title= ?, " .
395 "assigned_to = ?, message_status = ? WHERE id = ?",
396 array($body, $activity, $title, $assigned_to, $message_status, $id) );
399 sqlStatement("UPDATE pnotes SET " .
400 "body = ?, activity = ?, title= ?, " .
401 "assigned_to = ? WHERE id = ?",
402 array($body, $activity, $title, $assigned_to, $id) );
406 function updatePnoteMessageStatus($id, $message_status)
408 if ($message_status == "Done") {
409 sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
412 sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
417 * Set the patient id in an existing message where pid=0
418 * @param $id the id of the existing note
419 * @param $patient_id the patient id to associate with the note
420 * @author EMR Direct <http://www.emrdirect.com/>
422 function updatePnotePatient($id, $patient_id)
424 $row = getPnoteById($id);
425 if (! $row) die("updatePnotePatient() did not find id '".text($id)."'");
426 $activity = $assigned_to ? '1' : '0';
429 if($pid != 0 || (int)$patient_id < 1) die("updatePnotePatient invalid operation");
431 $pid = (int) $patient_id;
432 $newtext = "\n" . date('Y-m-d H:i') . " (patient set by " . $_SESSION['authUser'] .")";
433 $body = $row['body'] . $newtext;
435 sqlStatement("UPDATE pnotes SET pid = ?, body = ? WHERE id = ?", array($pid, $body, $id) );
438 function authorizePnote($id, $authorized = "1")
440 sqlQuery("UPDATE pnotes SET authorized = ? WHERE id = ?", array ($authorized,$id) );
443 function disappearPnote($id)
445 sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done' WHERE id=?", array($id) );
449 function reappearPnote ($id)
451 sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
455 function deletePnote($id)
457 sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );