DATE_SUB(NOW(), INTERVAL 1 MONTH) $search ORDER BY `date` desc $limit "; $res = sqlStatement($sql, array($pid)); for ($iter = 0; $row = sqlFetchArray($res); $iter++) { $all[$iter] = $row; } return $all; } function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '') { if ($limit) { $limit = "LIMIT " . escape_limit($offset) . ", " . escape_limit($limit); } $sql = " SELECT p.id, p.date, p.assigned_to, p.title, REPLACE( p.body, '-patient-', CONCAT(pd.lname, ' ', pd.fname) ) AS body, p.activity, p.message_status, 'Message' as `type` FROM pnotes AS p LEFT JOIN patient_data AS pd ON pd.id = p.pid WHERE `user` = ? AND p.deleted != 1 AND p.pid = ? AND p.message_status != 'Done' $search ORDER BY `date` desc $limit "; $res = sqlStatement($sql, array($pid,$pid)); for ($iter = 0; $row = sqlFetchArray($res); $iter++) { $all[$iter] = $row; } return $all; } /** Add a note to a patient's medical record. * * @param int $pid the ID of the patient whos medical record this note is going to be attached to. * @param string $newtext the note contents. * @param int $authorized * @param int $activity * @param string $title * @param string $assigned_to * @param string $datetime * @param string $message_status * @param string $background_user if set then the pnote is created by a background-service rather than a user * @return int the ID of the added note. */ function addPnote( $pid, $newtext, $authorized = '0', $activity = '1', $title = 'Unassigned', $assigned_to = '', $datetime = '', $message_status = 'New', $background_user = "" ) { if (empty($datetime)) { $datetime = date('Y-m-d H:i:s'); } // make inactive if set as Done if ($message_status == 'Done') { $activity = 0; } $user = ($background_user != "" ? $background_user : $_SESSION['authUser']); $body = date('Y-m-d H:i') . ' (' . $user; if ($assigned_to) { $body .= " to $assigned_to"; } $body = $body . ') ' . $newtext; return sqlInsert( 'INSERT INTO pnotes (date, body, pid, user, groupname, ' . 'authorized, activity, title, assigned_to, message_status, update_by, update_date) VALUES ' . '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())', array($datetime, $body, $pid, $user, $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status, $_SESSION['authUserID']) ); } function addMailboxPnote( $pid, $newtext, $authorized = '0', $activity = '1', $title = 'Unassigned', $assigned_to = '', $datetime = '', $message_status = "New" ) { if (empty($datetime)) { $datetime = date('Y-m-d H:i:s'); } // make inactive if set as Done if ($message_status == "Done") { $activity = 0; } $body = date('Y-m-d H:i') . ' (' . $pid; if ($assigned_to) { $body .= " to $assigned_to"; } $body = $body . ') ' . $newtext; return sqlInsert( "INSERT INTO pnotes (date, body, pid, user, groupname, " . "authorized, activity, title, assigned_to, message_status, update_by, update_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())", array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status, $_SESSION['authUserID']) ); } function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "", $datetime = "") { $row = getPnoteById($id); if (! $row) { die("updatePnote() did not find id '" . text($id) . "'"); } if (empty($datetime)) { $datetime = date('Y-m-d H:i:s'); } $activity = $assigned_to ? '1' : '0'; // make inactive if set as Done if ($message_status == "Done") { $activity = 0; } $body = $row['body'] . "\n" . date('Y-m-d H:i') . ' (' . $_SESSION['authUser']; if ($assigned_to) { $body .= " to $assigned_to"; } $body = $body . ') ' . $newtext; $sql = "UPDATE pnotes SET " . "body = ?, activity = ?, title= ?, " . "assigned_to = ?, update_by = ?, update_date = NOW()"; $bindingParams = array($body, $activity, $title, $assigned_to, $_SESSION['authUserID']); if ($message_status) { $sql .= " ,message_status = ?"; $bindingParams[] = $message_status; } if ($GLOBALS['messages_due_date']) { $sql .= " ,date = ?"; $bindingParams[] = $datetime; } $sql .= " WHERE id = ?"; $bindingParams[] = $id; sqlStatement($sql, $bindingParams); } function updatePnoteMessageStatus($id, $message_status) { if ($message_status == "Done") { sqlStatement("update pnotes set message_status = ?, activity = '0', update_by = ?, update_date = NOW() where id = ?", array($message_status, $_SESSION['authUserID'], $id)); } else { sqlStatement("update pnotes set message_status = ?, activity = '1', update_by = ?, update_date = NOW() where id = ?", array($message_status, $_SESSION['authUserID'], $id)); } } /** * Set the patient id in an existing message where pid=0 * @param $id the id of the existing note * @param $patient_id the patient id to associate with the note * @author EMR Direct */ function updatePnotePatient($id, $patient_id) { $row = getPnoteById($id); if (! $row) { die("updatePnotePatient() did not find id '" . text($id) . "'"); } $activity = $assigned_to ? '1' : '0'; $pid = $row['pid']; if ($pid != 0 || (int)$patient_id < 1) { (new SystemLogger())->errorLogCaller("invalid operation", ['id' => $id, 'patient_id' => $patient_id, 'pid' => $pid]); die("updatePnotePatient invalid operation"); } $pid = (int) $patient_id; $newtext = "\n" . date('Y-m-d H:i') . " (patient set by " . $_SESSION['authUser'] . ")"; $body = $row['body'] . $newtext; sqlStatement("UPDATE pnotes SET pid = ?, body = ?, update_by = ?, update_date = NOW() WHERE id = ?", array($pid, $body, $_SESSION['authUserID'], $id)); } function authorizePnote($id, $authorized = "1") { sqlQuery("UPDATE pnotes SET authorized = ? , update_by = ?, update_date = NOW() WHERE id = ?", array ($authorized, $_SESSION['authUserID'], $id)); } function disappearPnote($id) { sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done', update_by = ?, update_date = NOW() WHERE id=?", array($_SESSION['authUserID'], $id)); return true; } function reappearPnote($id) { sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status), update_by = ?, update_date = NOW() WHERE id=?", array($_SESSION['authUserID'], $id)); return true; } function deletePnote($id) { if ( getAssignedToById($id) == $_SESSION['authUser'] || getMessageStatusById($id) == 'Done' ) { sqlStatement("UPDATE pnotes SET deleted = '1', update_by = ?, update_date = NOW() WHERE id=?", array($_SESSION['authUserID'], $id)); return true; } else { return false; } } // Note that it is assumed that html escaping has happened before this function is called function pnoteConvertLinks($note) { $noteActiveLink = preg_replace('!(https://[-a-zA-Z()0-9@:%_+.~#?&;//=]+)!i', '$1', $note); if (empty($noteActiveLink)) { // something bad happened (preg_replace returned null) or the $note was empty return $note; } else { return $noteActiveLink; } } /** * Retrieve assigned_to field given the note ID * * @param string $id the ID of the note to retrieve. */ function getAssignedToById($id) { $result = sqlQuery("SELECT assigned_to FROM pnotes WHERE id=?", array($id)); return $result['assigned_to']; } /** * Retrieve message_status field given the note ID * * @param string $id the ID of the note to retrieve. */ function getMessageStatusById($id) { $result = sqlQuery("SELECT message_status FROM pnotes WHERE id=?", array($id)); return $result['message_status']; }