Support new security model in the formSubmit function - bug fix
[openemr.git] / library / pnotes.inc
blob99a4f7bc4c1eca64298fa7b42714ae126b00615d
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 require_once("{$GLOBALS['srcdir']}/sql.inc");
9 // 06-2009, BM migrated the patient_note_types array to the list_options table
11 function getPnoteById($id, $cols = "*")
13   return sqlQuery("SELECT $cols FROM pnotes WHERE id=? " .
14     " AND deleted != 1 ". // exclude ALL deleted notes
15     "order by date DESC limit 0,1", array($id) );
19 // activity can be 0, 1, or 'all'
20 function getPnotesByUser($activity="1",$show_all="no",$user='',$count=false,$sortby='',$sortorder='',$begin='',$listnumber='')
23   // Set the activity part of query
24   if ($activity=='1') {
25     $activity_query = " pnotes.message_status != 'Done' AND pnotes.activity = 1 AND ";
26   }
27   else if ($activity=='0') {
28     $activity_query = " (pnotes.message_status = 'Done' OR pnotes.activity = 0) AND ";
29   }
30   else { //$activity=='all'
31     $activity_query = " ";
32   }
34   // Set whether to show chosen user or all users
35   if ($show_all == 'yes' ) {
36     $usrvar='_%';
37   } else {
38     $usrvar=$user;
39   } 
41   // run the query
42   $sql = "SELECT pnotes.id, pnotes.user, pnotes.pid, pnotes.title, pnotes.date, pnotes.message_status,
43           IF(pnotes.user != pnotes.pid,users.fname,patient_data.fname) as users_fname,
44           IF(pnotes.user != pnotes.pid,users.lname,patient_data.lname) as users_lname,
45           patient_data.fname as patient_data_fname, patient_data.lname as patient_data_lname
46           FROM ((pnotes LEFT JOIN users ON pnotes.user = users.username)
47           JOIN patient_data ON pnotes.pid = patient_data.pid) WHERE $activity_query
48           pnotes.deleted != '1' AND pnotes.assigned_to LIKE ?";
49   if (!empty($sortby) || !empty($sortorder)  || !empty($begin) || !empty($listnumber)) {
50     $sql .= " order by ".add_escape_custom($sortby)." ".add_escape_custom($sortorder).
51            " limit ".add_escape_custom($begin).", ".add_escape_custom($listnumber);
52   }
53   $result = sqlStatement($sql, array($usrvar));
55   // return the results
56   if ($count) {
57     if(sqlNumRows($result) != 0) {
58         $total = sqlNumRows($result);
59     }
60     else {
61         $total = 0;
62     }
63     return $total;
64   }
65   else {
66     return $result;
67   }
71 function getPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
72   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
74 $sqlParameterArray = array();
75   if ($docid) {
76     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
77     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
78     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid != p.user";
79     array_push($sqlParameterArray, '%'.$date.'%', $docid);
80   }
81   else {
82     $sql = "SELECT $cols FROM pnotes AS p " .
83       "WHERE date LIKE ? AND pid LIKE ? AND p.pid != p.user";
84     array_push($sqlParameterArray, '%'.$date.'%', $pid);
85   }
86   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
87   if ($activity != "all") {
88     if ($activity == '0') {
89       // only return inactive
90       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
91     }
92     else { // $activity == '1'
93       // only return active
94       $sql .= " AND activity = '1' AND message_status != 'Done' ";
95     }
96   }
97   if ($username) {
98     $sql .= " AND assigned_to LIKE ?";
99     array_push($sqlParameterArray, $username);
100   }
101   if ($status)
102     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
103   $sql .= " ORDER BY date DESC";
104   if($limit != "all")
105     $sql .= " LIMIT $start, $limit";
107   $res = sqlStatement($sql, $sqlParameterArray);
109   $all=array();
110   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
111     $all[$iter] = $row;
112   return $all;
115 // activity can only be 0, 1, or 'all
116 function getSentPnotesByDate($date, $activity = "1", $cols = "*", $pid = "%",
117   $limit = "all", $start = 0, $username = '', $docid = 0, $status = "")
119 $sqlParameterArray = array();
120   if ($docid) {
121     $sql = "SELECT $cols FROM pnotes AS p, gprelations AS r " .
122     "WHERE p.date LIKE ? AND r.type1 = 1 AND " .
123     "r.id1 = ? AND r.type2 = 6 AND p.id = r.id2 AND p.pid = p.user";
124     array_push($sqlParameterArray, '%'.$date.'%', $docid);
125   }
126   else {
127     $sql = "SELECT $cols FROM pnotes AS p " .
128       "WHERE date LIKE ? AND pid LIKE ? AND p.pid = p.user";
129     array_push($sqlParameterArray, '%'.$date.'%', $pid);
130   }
131   $sql .= " AND deleted != 1"; // exclude ALL deleted notes
132   if ($activity != "all") {
133     if ($activity == '0') {
134       // only return inactive
135       $sql .= " AND (activity = '0' OR message_status = 'Done') ";
136     }
137     else { // $activity == '1'
138       // only return active
139       $sql .= " AND activity = '1' AND message_status != 'Done' ";
140     }
141   }
142   if ($username) {
143     $sql .= " AND assigned_to LIKE ?";
144     array_push($sqlParameterArray, $username);
145   }
146   if ($status)
147     $sql .= " AND message_status IN ('".str_replace(",", "','", $status)."')";
148   $sql .= " ORDER BY date DESC";
149   if($limit != "all")
150     $sql .= " LIMIT $start, $limit";
152   $res = sqlStatement($sql, $sqlParameterArray);
154   $all=array();
155   for ($iter = 0;$row = sqlFetchArray($res);$iter++)
156     $all[$iter] = $row;
157   return $all;
160 function getPatientNotes($pid = '', $limit = '', $offset = 0, $search = '')
162   if($limit){
163     $limit = "LIMIT $offset, $limit";
164   }
165   $sql = "
166     SELECT
167       p.id,
168       p.date,
169       p.user,
170       p.title,
171       REPLACE(
172         p.body,
173         '-patient-',
174         CONCAT(pd.fname, ' ', pd.lname)
175       ) AS body,
176       p.message_status,
177       'Message' as `type`
178     FROM
179       pnotes AS p 
180       LEFT JOIN patient_data AS pd 
181         ON pd.id = p.pid 
182     WHERE assigned_to = '-patient-' 
183       AND p.deleted != 1 
184       AND p.pid = ?
185       $search
186     ORDER BY `date` desc
187     $limit
188   ";
189   $res = sqlStatement($sql, array($pid));
190   for($iter = 0;$row = sqlFetchArray($res);$iter++){
191     $all[$iter] = $row;
192   }
193   return $all;
196 function getPatientNotifications($pid = '', $limit = '', $offset = 0, $search = '')
198   if($limit){
199     $limit = "LIMIT $offset, $limit";
200   }
201   $sql = "
202     SELECT
203       pr.id,
204       date_created AS `date`,
205       'Patient Reminders' AS `user`,
206       due_status AS title,
207       CONCAT(lo.title, ':', lo2.title) AS body,
208       '' as message_status,
209       'Notification' as `type`
210     FROM
211       patient_reminders AS pr 
212       LEFT JOIN list_options AS lo 
213         ON lo.option_id = pr.category 
214         AND lo.list_id = 'rule_action_category' 
215       LEFT JOIN list_options AS lo2 
216         ON lo2.option_id = pr.item 
217         AND lo2.list_id = 'rule_action' 
218     WHERE pid = ?
219       AND active = 1
220       AND date_created > DATE_SUB(NOW(), INTERVAL 1 MONTH)
221       $search
222     ORDER BY `date` desc
223     $limit
224   ";
225   $res = sqlStatement($sql, array($pid));
226   for($iter = 0;$row = sqlFetchArray($res);$iter++){
227     $all[$iter] = $row;
228   }
229   return $all;
232 function getPatientSentNotes($pid = '', $limit = '', $offset = 0, $search = '')
234   if($limit){
235     $limit = "LIMIT $offset, $limit";
236   }
237   $sql = "
238     SELECT
239       p.id,
240       p.date,
241       p.assigned_to,
242       p.title,
243       REPLACE(
244         p.body,
245         '-patient-',
246         CONCAT(pd.lname, ' ', pd.fname)
247       ) AS body,
248       p.activity,
249       p.message_status,
250       'Message' as `type`
251     FROM
252       pnotes AS p 
253       LEFT JOIN patient_data AS pd 
254         ON pd.id = p.pid 
255     WHERE `user` = ?
256       AND p.deleted != 1 
257       AND p.pid = ?
258       AND p.message_status != 'Done'
259       $search
260     ORDER BY `date` desc
261     $limit
262   ";
263   $res = sqlStatement($sql, array($pid,$pid));
264   for($iter = 0;$row = sqlFetchArray($res);$iter++){
265     $all[$iter] = $row;
266   }
267   return $all;
270 // activity can be 0, 1, or 'all'
271 function getPnotesByPid ($pid, $activity = "1", $cols = "*", $limit=10, $start=0)
273  if ($activity == '1') {
274   // return only active
275   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
276     "AND activity = '1' ".
277     " AND message_status != 'Done' ".
278     " AND deleted != 1 ".
279     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
281  else if ($activity == '0') {
282   // return only inactive
283   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
284     "AND (activity = '0' ".
285     " OR message_status = 'Done') ".
286     " AND deleted != 1 ".
287     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
289  else { // $activity == "all"
290   // return both active and inactive
291   $res = sqlStatement("SELECT $cols FROM pnotes WHERE pid LIKE ? " .
292     " AND deleted != 1 ".
293     " ORDER BY date DESC LIMIT $start,$limit", array($pid) );
295   for ($iter = 0; $row = sqlFetchArray($res); $iter++)
296     $all[$iter] = $row;
297   return $all;
300 function addPnote($pid, $newtext, $authorized = '0', $activity = '1',
301   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
303   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
305   // make inactive if set as Done
306   if ($message_status == "Done") $activity = 0;
308   $body = date('Y-m-d H:i') . ' (' . $_SESSION['authUser'];
309   if ($assigned_to) $body .= " to $assigned_to";
310   $body = $body . ') ' . $newtext;
312   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
313     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
314     array($datetime, $body, $pid, $_SESSION['authUser'], $_SESSION['authProvider'], $authorized, $activity, $title, $assigned_to, $message_status) );
317 function addMailboxPnote($pid, $newtext, $authorized = '0', $activity = '1',
318   $title='Unassigned', $assigned_to = '', $datetime = '', $message_status = "New")
320   if (empty($datetime)) $datetime = date('Y-m-d H:i:s');
322   // make inactive if set as Done
323   if ($message_status == "Done") $activity = 0;
325   $body = date('Y-m-d H:i') . ' (' . $pid;
326   if ($assigned_to) $body .= " to $assigned_to";
327   $body = $body . ') ' . $newtext;
329   return sqlInsert("INSERT INTO pnotes (date, body, pid, user, groupname, " .
330     "authorized, activity, title, assigned_to, message_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
331     array($datetime, $body, $pid, $pid, 'Default', $authorized, $activity, $title, $assigned_to, $message_status) );
334 function updatePnote($id, $newtext, $title, $assigned_to, $message_status = "")
336   $row = getPnoteById($id);
337   if (! $row) die("updatePnote() did not find id '$id'");
338   $activity = $assigned_to ? '1' : '0';
340   // make inactive if set as Done
341   if ($message_status == "Done") $activity = 0;
343   $body = $row['body'] . "\n" . date('Y-m-d H:i') .
344     ' (' . $_SESSION['authUser'];
345   if ($assigned_to) $body .= " to $assigned_to";
346   $body = $body . ') ' . $newtext;
348   if ($message_status) {
349     sqlStatement("UPDATE pnotes SET " .
350       "body = ?, activity = ?, title= ?, " .
351       "assigned_to = ?, message_status = ? WHERE id = ?",
352       array($body, $activity, $title, $assigned_to, $message_status, $id) );
353   }
354   else {
355     sqlStatement("UPDATE pnotes SET " .
356       "body = ?, activity = ?, title= ?, " .
357       "assigned_to = ? WHERE id = ?",
358       array($body, $activity, $title, $assigned_to, $id) );
359   }
362 function updatePnoteMessageStatus($id, $message_status)
364   if ($message_status == "Done") {
365     sqlStatement("update pnotes set message_status = ?, activity = '0' where id = ?", array($message_status, $id) );
366   }
367   else {
368     sqlStatement("update pnotes set message_status = ?, activity = '1' where id = ?", array($message_status, $id) );
369   }
372 function authorizePnote($id, $authorized = "1")
374   sqlQuery("UPDATE pnotes SET authorized = ? WHERE id = ?", array ($authorized,$id) );
377 function disappearPnote($id)
379   sqlStatement("UPDATE pnotes SET activity = '0', message_status = 'Done' WHERE id=?", array($id) );
380   return true;
383 function reappearPnote ($id)
385   sqlStatement("UPDATE pnotes SET activity = '1', message_status = IF(message_status='Done','New',message_status) WHERE id=?", array($id) );
386   return true;
389 function deletePnote($id)
391   sqlStatement("UPDATE pnotes SET deleted = '1' WHERE id=?", array($id) );
392   return true;