Highway to PSR2
[openemr.git] / interface / forms / group_attendance / functions.php
blob9f95d404bebdf7b39f3c88119401adc8a74f7286
1 <?php
3 /**
4 * interface/forms/group_attendance/functions.php functions for form
6 * Contains the functions used for the group attendance form
8 * Copyright (C) 2016 Shachar Zilbershlag <shaharzi@matrix.co.il>
9 * Copyright (C) 2016 Amiel Elboim <amielel@matrix.co.il>
12 * LICENSE: This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 3
15 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
23 * @package OpenEMR
24 * @author Shachar Zilbershlag <shaharzi@matrix.co.il>
25 * @author Amiel Elboim <amielel@matrix.co.il>
26 * @link http://www.open-emr.org
29 require_once(dirname(__FILE__) . "/../../../library/api.inc");
30 require_once(dirname(__FILE__) . "/../../../library/forms.inc");
31 require_once(dirname(__FILE__) . "/../../../library/patient_tracker.inc.php");
33 /**
34 * Returns form_id of an existing attendance form for group encounter (if one already exists);
35 * @param $encounter
36 * @param $group_id
37 * @return array|null
39 function get_form_id_of_existing_attendance_form($encounter, $group_id)
41 $sql = "SELECT form_id FROM forms WHERE encounter = ? AND formdir = 'group_attendance' AND therapy_group_id = ? AND deleted = 0;";
42 $result = sqlQuery($sql, array($encounter, $group_id));
43 return $result;
46 /**
47 * Inserts participant data into DB
48 * @param $form_id
49 * @param $therapy_group
50 * @param $group_encounter_data
51 * @param $appt_data
53 function participant_insertions($form_id, $therapy_group, $group_encounter_data, $appt_data)
55 $patientData = $_POST['patientData'];
56 foreach ($patientData as $pid => $patient) {
57 //Insert into therapy_groups_participants_attendance table
58 insert_into_tgpa_table($form_id, $pid, $patient);
60 //Check if to create appt and encounter for each patient (if has certain status and 'bottom' submit was pressed, not 'add_patient' submit).
61 $create_for_patient = if_to_create_for_patient($patient['status']);
62 if ($create_for_patient) {
63 //Create encounter for each patient
64 $encounter_id = insert_patient_encounter($pid, $therapy_group, $group_encounter_data['date'], $patient, $appt_data['pc_aid']);
66 //Create appt for each patient (if there is appointment connected to encounter)
67 if (!empty($appt_data)) {
68 $pc_eid = insert_patient_appt($pid, $therapy_group, $appt_data['pc_aid'], $appt_data['pc_eventDate'], $appt_data['pc_startTime'], $patient);
69 manage_tracker_status($appt_data['pc_eventDate'], $appt_data['pc_startTime'], $pc_eid, $pid, $appt_data['pc_aid'], $patient['status'], $appt_data['pc_room'], $encounter_id);
75 /**
76 * Inserts data into therapy_groups_participant_attendance table
77 * @param $form_id
78 * @param $pid
79 * @param $participantData
81 function insert_into_tgpa_table($form_id, $pid, $participantData)
83 $sql_for_table_tgpa = "INSERT INTO therapy_groups_participant_attendance (form_id, pid, meeting_patient_comment, meeting_patient_status) " .
84 "VALUES(?,?,?,?);";
85 sqlInsert($sql_for_table_tgpa, array($form_id, $pid, $participantData['comment'], $participantData['status']));
88 /**
89 * Creates an appointment for patient from attendance form
90 * @param $pid
91 * @param $gid
92 * @param $pc_aid
93 * @param $pc_eventDate
94 * @param $pc_startTime
95 * @param $participantData
97 function insert_patient_appt($pid, $gid, $pc_aid, $pc_eventDate, $pc_startTime, $participantData)
99 $select_sql = "SELECT pc_eid FROM openemr_postcalendar_events WHERE pc_pid = ? AND pc_gid = ? AND pc_eventDate = ? AND pc_startTime = ?;";
100 $result = sqlStatement($select_sql, array($pid, $gid, $pc_eventDate, $pc_startTime));
101 $result_array = sqlFetchArray($result);
102 if ($result_array) {
103 $insert_sql = "UPDATE openemr_postcalendar_events SET pc_apptstatus = ? WHERE pc_eid = ?;";
104 sqlStatement($insert_sql, array($participantData['status'], $result_array['pc_eid']));
105 return $result_array['pc_eid'];
106 } else {
107 $insert_sql =
108 "INSERT INTO openemr_postcalendar_events " .
109 "(pc_catid, pc_aid, pc_pid, pc_gid, pc_title, pc_informant, pc_eventDate, pc_recurrspec, pc_startTime, pc_sharing, pc_apptstatus) ".
110 "VALUES (?, ?, ?, ?, 'Group Therapy', 1, ?, ?, ?, 0, ?); ";
111 $recurrspec = 'a:6:{s:17:"event_repeat_freq";s:1:"0";s:22:"event_repeat_freq_type";s:1:"0";s:19:"event_repeat_on_num";s:1:"1";s:19:"event_repeat_on_day";s:1:"0";s:20:"event_repeat_on_freq";s:1:"0";s:6:"exdate";s:0:"";}';
112 $sqlBindArray = array();
113 array_push($sqlBindArray, get_groups_cat_id(), $pc_aid, $pid, $gid, $pc_eventDate, $recurrspec, $pc_startTime, $participantData['status']);
114 $pc_eid = sqlInsert($insert_sql, $sqlBindArray);
115 return $pc_eid;
120 * Creates an encounter for patient from attendance form
121 * @param $pid
122 * @param $gid
123 * @param $group_encounter_date
124 * @param $participantData
125 * @param $pc_aid
127 function insert_patient_encounter($pid, $gid, $group_encounter_date, $participantData, $pc_aid)
129 $select_sql = "SELECT id, encounter FROM form_encounter WHERE pid = ? AND external_id = ? AND pc_catid = ? AND date = ?; ";
130 $result = sqlStatement($select_sql, array($pid, $gid, get_groups_cat_id(), $group_encounter_date));
131 $result_array = sqlFetchArray($result);
132 if ($result_array) {
133 $insert_sql = "UPDATE form_encounter SET reason = ? WHERE id = ?;";
134 sqlStatement($insert_sql, array($participantData['comment'], $result_array['id']));
135 return $result_array['encounter'];
136 } else {
137 $insert_encounter_sql =
138 "INSERT INTO form_encounter (date, reason, pid, encounter, pc_catid, provider_id, external_id) ".
139 "VALUES (?, ?, ?, ?, ?, ?, ?);";
140 $enc_id = generate_id();
141 $sqlBindArray = array();
142 $user = (is_null($pc_aid)) ? $_SESSION["authId"] : $pc_aid;
143 array_push($sqlBindArray, $group_encounter_date, $participantData['comment'], $pid, $enc_id, get_groups_cat_id(), $user, $gid);
144 $form_id = sqlInsert($insert_encounter_sql, $sqlBindArray);
146 global $userauthorized;
148 addForm($enc_id, "New Patient Encounter", $form_id, "newpatient", $pid, $userauthorized, $group_encounter_date, '', '', null);
150 return $enc_id;
155 * If the group encounter was created in relation to a group appointment, fetches the appointment relevant data.
156 * @param $encounter_id
157 * @return array
159 function get_appt_data($encounter_id)
161 $sql =
162 "SELECT ope.pc_aid, ope.pc_eventDate, ope.pc_startTime, ope.pc_room FROM form_groups_encounter as fge " .
163 "JOIN openemr_postcalendar_events as ope ON fge.appt_id = ope.pc_eid " .
164 "WHERE fge.encounter = ?;";
165 $result = sqlQuery($sql, array($encounter_id));
166 return $result;
170 * Gets group encounter data
171 * @param $encounter_id
172 * @return array
174 function get_group_encounter_data($encounter_id)
176 $sql = "SELECT date FROM form_groups_encounter WHERE encounter = ?";
177 $result = sqlQuery($sql, array($encounter_id));
178 return $result;
182 * Checks if to create appointment and encounter for patient himself based on the status in the attendance form.
183 * [Note: `toggle_setting_1` in table `list_options` is used as a flag to know the statuses for which an appt or encounter should be created.]
184 * @param $status
185 * @return bool
187 function if_to_create_for_patient($status)
189 $sql = 'SELECT toggle_setting_1 FROM list_options WHERE list_id = \'attendstat\' AND toggle_setting_1 = 1 AND option_id = ?';
190 $to_create = sqlQuery($sql, array($status));
191 return $to_create;
195 * Returns the number after the greatest id number in the table
196 * @param $table
197 * @return int
199 function largest_id_plus_one($table)
201 $maxId = largest_id($table);
202 if ($maxId) {
203 $newid = $maxId + 1;
204 } else {
205 $newid = 1;
208 return $newid;
212 * Returns greatest id number in the table
213 * @param $table
214 * @return mixed
216 function largest_id($table)
218 $res = sqlStatement("SELECT MAX(id) as largestId FROM `" . escape_table_name($table) . "`");
219 $getMaxid = sqlFetchArray($res);
220 return $getMaxid['largestId'];
224 function get_groups_cat_id()
226 $result = sqlQuery('SELECT pc_catid FROM openemr_postcalendar_categories WHERE pc_cattype = 3 AND pc_active = 1 LIMIT 1');
227 return !empty($result) ? $result['pc_catid'] : 0;