preparing for alpine 3.15 (#4775)
[openemr.git] / portal / lib / portal_pnotes.inc
blob1f4bdf75312b162621b2da18b95bf268cb1a37b1
1 <?php
3 /**
4  * This file contains functions for handling on-site portal notes attached to patient files.
5  *
6  * @package   OpenEMR
7  * @link      https://www.open-emr.org
8  * @author    Jerry Padgett <sjpadgett@gmail.com>
9  * @copyright Copyright (c) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
10  * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
11  */
13 require_once("$srcdir/pnotes.inc");
15 function addPortalMailboxPnote(
16     $pid,
17     $newtext,
18     $authorized = '0',
19     $activity = '1',
20     $title = 'Unassigned',
21     $assigned_to = '',
22     $datetime = '',
23     $message_status = "New",
24     $master_note = '0'
25 ) {
27     if (empty($datetime)) {
28         $datetime = date('Y-m-d H:i:s');
29     }
31     // make inactive if set as Done
32     if ($message_status == "Done") {
33         $activity = 0;
34     }
36     $user = $_SESSION['authUser'] ? $_SESSION['authUser'] : $pid;
37     $pname = $_SESSION['ptName'] ? $_SESSION['ptName'] : $user;
39     /* $body =  " ($pname";
40     if ($assigned_to) $body .= " to $assigned_to";
41     $body = $body . ') ' . $newtext; */
43     $body = $newtext;
45     return sqlInsert(
46         "INSERT INTO pnotes (date, body, pid, user, groupname, " .
47             "authorized, activity, title, assigned_to, message_status, portal_relation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
48         array($datetime, $body, $pid, $user, 'Default', $authorized, $activity, $title, $assigned_to, $message_status,$master_note)
49     );
52 function getPortalPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
54     if ($limit) {
55         $limit = "LIMIT " . escape_limit($offset) . ", " . escape_limit($limit);
56     }
58     $sql = "
59         SELECT
60         p.id,
61         p.date,
62         p.user,
63         p.title,
64         REPLACE(
65         p.body,
66         '-patient-',
67         CONCAT(pd.fname, ' ', pd.lname)
68         ) AS body,
69         p.message_status,
70         'Message' as `type`,
71         p.portal_relation
72         FROM
73         pnotes AS p
74         LEFT JOIN patient_data AS pd
75         ON pd.pid = p.pid
76         WHERE assigned_to = '-patient-'
77         AND p.deleted != 1
78         AND p.pid = ?
79         $search
80         ORDER BY `date` desc
81         $limit
82         ";
83     $res = sqlStatement($sql, array($pid));
84     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
85         $all[$iter] = $row;
86     }
88     return $all;
91 function getPortalPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
93     if ($limit) {
94         $limit = "LIMIT " . escape_limit($offset) . ", " . escape_limit($limit);
95     }
97     $sql = "
98         SELECT
99         pr.id,
100         date_created AS `date`,
101         'Patient Reminders' AS `user`,
102         due_status AS title,
103         CONCAT(lo.title, ':', lo2.title) AS body,
104         '' as message_status,
105         'Notification' as `type`
106         FROM
107         patient_reminders AS pr
108         LEFT JOIN list_options AS lo
109         ON lo.option_id = pr.category
110         AND lo.list_id = 'rule_action_category'
111         LEFT JOIN list_options AS lo2
112         ON lo2.option_id = pr.item
113         AND lo2.list_id = 'rule_action'
114         WHERE pid = ?
115         AND active = 1
116         AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
117         $search
118         ORDER BY `date` desc
119         $limit
120         ";
121     $res = sqlStatement($sql, array($pid));
122     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
123         $all[$iter] = $row;
124     }
126     return $all;
129 function getPortalPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
131     if ($limit) {
132         $limit = "LIMIT " . escape_limit($offset) . ", " . escape_limit($limit);
133     }
135     $sql = "
136         SELECT
137         p.id,
138         p.date,
139         p.assigned_to,
140         p.title,
141         REPLACE(
142         p.body,
143         '-patient-',
144         CONCAT(pd.fname, ' ', pd.lname)
145         ) AS body,
146         p.activity,
147         p.message_status,
148         'Message' as `type`,
149         p.portal_relation
150         FROM
151         pnotes AS p
152         LEFT JOIN patient_data AS pd
153         ON pd.pid = p.pid
154         WHERE `user` = ?
155         AND p.deleted != 1
156         AND p.pid = ?
157         AND p.message_status != 'Done'
158         $search
159         ORDER BY `date` desc
160         $limit
161         ";
162     $res = sqlStatement($sql, array($pid,$pid));
163     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
164         $all[$iter] = $row;
165     }
167     return $all;
169 function updatePortalPnoteMessageStatus($id, $message_status)
171     if ($message_status == "Done") {
172         sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id));
173     } else {
174         sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id));
175     }
177 function getMails($pid, $dotype, $nsrch, $nfsrch)
180     if ($pid) {
181         if ($dotype == "inbox") {
182             if ($nsrch && $nfsrch) {
183                 $result_notes = getPortalPatientNotes($pid, '', '0', $nsrch);
184                 $result_notifications = getPortalPatientNotifications($pid, '', '0', $nfsrch);
185                 $result = array_merge((array)$result_notes, (array)$result_notifications);
186             } else {
187                 $result_notes = getPortalPatientNotes($pid);
188                 $result_notifications = getPortalPatientNotifications($pid);
189                 $result = array_merge((array)$result_notes, (array)$result_notifications);
190             }
192             return $result;
193         } elseif ($dotype == "sent") {
194             if ($nsrch) {
195                 $result_sent_notes = getPortalPatientSentNotes($pid, '', '0', $nsrch);
196             } else {
197                 $result_sent_notes = getPortalPatientSentNotes($pid);
198             }
200             return $result_sent_notes;
201         }
202     } else {
203         return 'failed';
204     }
207 function getMailDetails($id)
209     if ($pid) {
210         $result = getPnoteById($id);
211         if ($result['assigned_to'] == '-patient-' && $result['message_status'] == 'New') {
212             updatePortalPnoteMessageStatus($id, 'Read');
213         }
215         return $result;
216     } else {
217         return 'failed';
218     }
221 function sendMail($pid, $note, string $title = null, $to, $noteid)
223     if (!$title) {
224         $title = 'Unassigned';
225     }
226     if ($pid) {
227         addPortalMailboxPnote($pid, $note, '1', '1', $title, $to, '', 'New', $noteid);
228         return 1;
229     } else {
230         return 'failed';
231     }
235 function updateStatus($id, $status)
237     if ($pid) {
238         updatePortalPnoteMessageStatus($id, $status);
239     } else {
240         return 'failed';
241     }