minor improvement to tabs style
[openemr.git] / portal / lib / portal_pnotes.inc
blob351ab7ea0927896aac537f7e5f77d4161831ba4b
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($pid, $newtext, $authorized = '0', $activity = '1',
29                 $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New",$master_note = '0')
31         if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
33         // make inactive if set as Done
34         if ($message_status == "Done") $activity = 0;
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("INSERT INTO pnotes (date, body, pid, user, groupname, " .
46                         "authorized, activity, title, assigned_to, message_status, portal_relation) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
47                         array($datetime, $body, $pid, $user, 'Default', $authorized, $activity, $title, $assigned_to, $message_status,$master_note) );
50 function getPortalPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
52         if($limit){
53                 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
54         }
55         $sql = "
56         SELECT
57         p.id,
58         p.date,
59         p.user,
60         p.title,
61         REPLACE(
62         p.body,
63         '-patient-',
64         CONCAT(pd.fname, ' ', pd.lname)
65         ) AS body,
66         p.message_status,
67         'Message' as `type`,
68         p.portal_relation
69         FROM
70         pnotes AS p
71         LEFT JOIN patient_data AS pd
72         ON pd.pid = p.pid
73         WHERE assigned_to = '-patient-'
74         AND p.deleted != 1
75         AND p.pid = ?
76         $search
77         ORDER BY `date` desc
78         $limit
79         ";
80         $res = sqlStatement($sql, array($pid));
81         for($iter = 0;$row = sqlFetchArray($res);$iter++){
82                 $all[$iter] = $row;
83         }
84         return $all;
87 function getPortalPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
89         if($limit){
90                 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
91         }
92         $sql = "
93         SELECT
94         pr.id,
95         date_created AS `date`,
96         'Patient Reminders' AS `user`,
97         due_status AS title,
98         CONCAT(lo.title, ':', lo2.title) AS body,
99         '' as message_status,
100         'Notification' as `type`
101         FROM
102         patient_reminders AS pr
103         LEFT JOIN list_options AS lo
104         ON lo.option_id = pr.category
105         AND lo.list_id = 'rule_action_category'
106         LEFT JOIN list_options AS lo2
107         ON lo2.option_id = pr.item
108         AND lo2.list_id = 'rule_action'
109         WHERE pid = ?
110         AND active = 1
111         AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
112         $search
113         ORDER BY `date` desc
114         $limit
115         ";
116         $res = sqlStatement($sql, array($pid));
117         for($iter = 0;$row = sqlFetchArray($res);$iter++){
118                 $all[$iter] = $row;
119         }
120         return $all;
123 function getPortalPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
125         if($limit){
126                 $limit = "LIMIT ".escape_limit($offset).", ".escape_limit($limit);
127         }
128         $sql = "
129         SELECT
130         p.id,
131         p.date,
132         p.assigned_to,
133         p.title,
134         REPLACE(
135         p.body,
136         '-patient-',
137         CONCAT(pd.fname, ' ', pd.lname)
138         ) AS body,
139         p.activity,
140         p.message_status,
141         'Message' as `type`,
142         p.portal_relation
143         FROM
144         pnotes AS p
145         LEFT JOIN patient_data AS pd
146         ON pd.pid = p.pid
147         WHERE `user` = ?
148         AND p.deleted != 1
149         AND p.pid = ?
150         AND p.message_status != 'Done'
151         $search
152         ORDER BY `date` desc
153         $limit
154         ";
155         $res = sqlStatement($sql, array($pid,$pid));
156         for($iter = 0;$row = sqlFetchArray($res);$iter++){
157                 $all[$iter] = $row;
158         }
159         return $all;
161 function updatePortalPnoteMessageStatus($id, $message_status)
163         if ($message_status == "Done") {
164                 sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
165         }
166         else {
167                 sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
168         }
170 function getMails($pid,$dotype,$nsrch,$nfsrch){
172         if($pid){
173                 if($dotype == "inbox"){
174                         if($nsrch && $nfsrch){
175                                 $result_notes = getPortalPatientNotes($pid,'','0',$nsrch);
176                                 $result_notifications = getPortalPatientNotifications($pid,'','0',$nfsrch);
177                                 $result = array_merge((array)$result_notes,(array)$result_notifications);
178                         }else{
179                                 $result_notes = getPortalPatientNotes($pid);
180                                 $result_notifications = getPortalPatientNotifications($pid);
181                                 $result = array_merge((array)$result_notes,(array)$result_notifications);
182                         }
183                         return $result;
184                 }elseif($dotype == "sent"){
185                         if($nsrch){
186                                 $result_sent_notes = getPortalPatientSentNotes($pid,'','0',$nsrch);
187                         }else{
188                                 $result_sent_notes = getPortalPatientSentNotes($pid);
189                         }
190                         return $result_sent_notes;
191                 }
192         }else{
193                 return 'failed';
194         }
197 function getMailDetails($id){
198         if($pid){
199                 $result = getPnoteById($id);
200                 if($result['assigned_to'] == '-patient-' && $result['message_status'] == 'New'){
201                         updatePortalPnoteMessageStatus($id,'Read');
202                 }
203                 return $result;
204         }else{
205                 return 'failed';
206         }
209 function sendMail($pid,$note,$title='Unassigned',$to, $noteid){
210         if($pid){
211                 addPortalMailboxPnote($pid,$note,'1','1',$title,$to,'','New',$noteid);
212                 return 1;
213         }else{
214                 return 'failed';
215         }
219 function updateStatus($id,$status){
220         if($pid){
221                 updatePortalPnoteMessageStatus($id,$status);
222         }else{
223                 return 'failed';
224         }