Merge pull request #1154 for user interface improvements of left nav and main title
[openemr.git] / portal / lib / portal_pnotes.inc
blobb37f0924f0876776f8cece5e1f4b8d0d4fef0001
1 <?php
2 /**
3  *
4  * This file contains functions for handling on-site portal notes attached to patient files.
5  *
6  * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
7  *
8  * LICENSE: This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU Affero General Public License as
10  *  published by the Free Software Foundation, either version 2 of the
11  *  License, or (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU Affero General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Affero General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * @package OpenEMR
22  * @author Jerry Padgett <sjpadgett@gmail.com>
23  * @link http://www.open-emr.org
24  */
26 require_once("$srcdir/pnotes.inc");
28 function addPortalMailboxPnote(
29     $pid,
30     $newtext,
31     $authorized = '0',
32     $activity = '1',
33     $title = 'Unassigned',
34     $assigned_to = '',
35     $datetime = '',
36     $message_status = "New",
37     $master_note = '0'
38 ) {
40     if (empty($datetime)) {
41         $datetime = date('Y-m-d H:i:s');
42     }
44     // make inactive if set as Done
45     if ($message_status == "Done") {
46         $activity = 0;
47     }
49     $user = $_SESSION['authUser']?$_SESSION['authUser']:$pid;
50     $pname = $_SESSION['ptName']?$_SESSION['ptName']:$user;
52     /* $body =  " ($pname";
53         if ($assigned_to) $body .= " to $assigned_to";
54         $body = $body . ') ' . $newtext; */
56     $body = $newtext;
58     return sqlInsert(
59         "INSERT INTO pnotes (date, body, pid, user, groupname, " .
60             "authorized, activity, title, assigned_to, message_status, portal_relation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
61         array($datetime, $body, $pid, $user, 'Default', $authorized, $activity, $title, $assigned_to, $message_status,$master_note)
62     );
65 function getPortalPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
67     if ($limit) {
68         $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
69     }
71     $sql = "
72         SELECT
73         p.id,
74         p.date,
75         p.user,
76         p.title,
77         REPLACE(
78         p.body,
79         '-patient-',
80         CONCAT(pd.fname, ' ', pd.lname)
81         ) AS body,
82         p.message_status,
83         'Message' as `type`,
84         p.portal_relation
85         FROM
86         pnotes AS p
87         LEFT JOIN patient_data AS pd
88         ON pd.pid = p.pid
89         WHERE assigned_to = '-patient-'
90         AND p.deleted != 1
91         AND p.pid = ?
92         $search
93         ORDER BY `date` desc
94         $limit
95         ";
96     $res = sqlStatement($sql, array($pid));
97     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
98         $all[$iter] = $row;
99     }
101     return $all;
104 function getPortalPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
106     if ($limit) {
107         $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
108     }
110     $sql = "
111         SELECT
112         pr.id,
113         date_created AS `date`,
114         'Patient Reminders' AS `user`,
115         due_status AS title,
116         CONCAT(lo.title, ':', lo2.title) AS body,
117         '' as message_status,
118         'Notification' as `type`
119         FROM
120         patient_reminders AS pr
121         LEFT JOIN list_options AS lo
122         ON lo.option_id = pr.category
123         AND lo.list_id = 'rule_action_category'
124         LEFT JOIN list_options AS lo2
125         ON lo2.option_id = pr.item
126         AND lo2.list_id = 'rule_action'
127         WHERE pid = ?
128         AND active = 1
129         AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
130         $search
131         ORDER BY `date` desc
132         $limit
133         ";
134     $res = sqlStatement($sql, array($pid));
135     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
136         $all[$iter] = $row;
137     }
139     return $all;
142 function getPortalPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
144     if ($limit) {
145         $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
146     }
148     $sql = "
149         SELECT
150         p.id,
151         p.date,
152         p.assigned_to,
153         p.title,
154         REPLACE(
155         p.body,
156         '-patient-',
157         CONCAT(pd.fname, ' ', pd.lname)
158         ) AS body,
159         p.activity,
160         p.message_status,
161         'Message' as `type`,
162         p.portal_relation
163         FROM
164         pnotes AS p
165         LEFT JOIN patient_data AS pd
166         ON pd.pid = p.pid
167         WHERE `user` = ?
168         AND p.deleted != 1
169         AND p.pid = ?
170         AND p.message_status != 'Done'
171         $search
172         ORDER BY `date` desc
173         $limit
174         ";
175     $res = sqlStatement($sql, array($pid,$pid));
176     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
177         $all[$iter] = $row;
178     }
180     return $all;
182 function updatePortalPnoteMessageStatus($id, $message_status)
184     if ($message_status == "Done") {
185         sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id));
186     } else {
187         sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id));
188     }
190 function getMails($pid, $dotype, $nsrch, $nfsrch)
193     if ($pid) {
194         if ($dotype == "inbox") {
195             if ($nsrch && $nfsrch) {
196                 $result_notes = getPortalPatientNotes($pid, '', '0', $nsrch);
197                 $result_notifications = getPortalPatientNotifications($pid, '', '0', $nfsrch);
198                 $result = array_merge((array)$result_notes, (array)$result_notifications);
199             } else {
200                 $result_notes = getPortalPatientNotes($pid);
201                 $result_notifications = getPortalPatientNotifications($pid);
202                 $result = array_merge((array)$result_notes, (array)$result_notifications);
203             }
205             return $result;
206         } elseif ($dotype == "sent") {
207             if ($nsrch) {
208                 $result_sent_notes = getPortalPatientSentNotes($pid, '', '0', $nsrch);
209             } else {
210                 $result_sent_notes = getPortalPatientSentNotes($pid);
211             }
213             return $result_sent_notes;
214         }
215     } else {
216         return 'failed';
217     }
220 function getMailDetails($id)
222     if ($pid) {
223         $result = getPnoteById($id);
224         if ($result['assigned_to'] == '-patient-' && $result['message_status'] == 'New') {
225             updatePortalPnoteMessageStatus($id, 'Read');
226         }
228         return $result;
229     } else {
230         return 'failed';
231     }
234 function sendMail($pid, $note, $title = 'Unassigned', $to, $noteid)
236     if ($pid) {
237         addPortalMailboxPnote($pid, $note, '1', '1', $title, $to, '', 'New', $noteid);
238         return 1;
239     } else {
240         return 'failed';
241     }
245 function updateStatus($id, $status)
247     if ($pid) {
248         updatePortalPnoteMessageStatus($id, $status);
249     } else {
250         return 'failed';
251     }