Highway to PSR2
[openemr.git] / interface / therapy_groups / therapy_groups_controllers / therapy_groups_controller.php
blobc2adc07c24468d1554b19e5a2051686c658ad684
1 <?php
2 /**
3 * interface/therapy_groups/therapy_groups_controllers/therapy_groups_controller.php contains the main controller for therapy groups.
5 * This is the main controller for the therapy group views and functionality.
7 * Copyright (C) 2016 Shachar Zilbershlag <shaharzi@matrix.co.il>
8 * Copyright (C) 2016 Amiel Elboim <amielel@matrix.co.il>
10 * LICENSE: This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
21 * @package OpenEMR
22 * @author Shachar Zilbershlag <shaharzi@matrix.co.il>
23 * @author Amiel Elboim <amielel@matrix.co.il>
24 * @link http://www.open-emr.org
27 require_once dirname(__FILE__) . '/base_controller.php';
28 require_once("{$GLOBALS['srcdir']}/appointments.inc.php");
29 require_once("{$GLOBALS['srcdir']}/pid.inc");
31 class TherapyGroupsController extends BaseController
34 public $therapyGroupModel;
36 /* Note: Created functions to return arrays so that xl method can be used in array rendering. */
38 //list of group statuses
39 public static function prepareStatusesList()
41 $statuses = array(
42 '10' => xl('Active'),
43 '20' => xl('Finished'),
44 '30' => xl('Canceled')
46 return $statuses;
49 //list of participant statuses
50 public static function prepareParticipantStatusesList()
52 $participant_statuses = array(
53 '10' => xl('Active'),
54 '20' => xl('Not active')
56 return $participant_statuses;
59 //list of group types
60 public static function prepareGroupTypesList()
62 $group_types = array(
63 '1' => xl('Closed'),
64 '2' => xl('Open'),
65 '3' => xl('Training')
67 return $group_types;
70 //list of participation types
71 public static function prepareGroupParticipationList()
73 $group_participation = array(
74 '1' => xl('Mandatory'),
75 '2' => xl('Optional')
77 return $group_participation;
80 //Max length of notes preview in groups list
81 private $notes_preview_proper_length = 30;
84 /**
85 * add / edit therapy group
86 * making validation and saving in the match tables.
87 * @param null $groupId - must pass when edit group
89 public function index($groupId = null)
92 $data = array();
93 if ($groupId) {
94 self::setSession($groupId);
97 //Load models
98 $this->therapyGroupModel = $this->loadModel('therapy_groups');
99 $this->counselorsModel = $this->loadModel('Therapy_Groups_Counselors');
100 $eventsModel = $this->loadModel('Therapy_Groups_Events');
101 $userModel = $this->loadModel('Users');
103 //Get group events
104 $events = $eventsModel->getGroupEvents($groupId);
105 $data['events'] = $events;
107 //Get users
108 $users = $userModel->getAllUsers();
109 $data['users'] = $users;
111 //Get statuses
112 $data['statuses'] = self::prepareStatusesList();
114 $_POST['group_start_date'] = DateToYYYYMMDD($_POST['group_start_date']);
115 $_POST['group_end_date'] = DateToYYYYMMDD($_POST['group_end_date']);
117 if (isset($_POST['save'])) {
118 $isEdit = empty($_POST['group_id']) ? false : true;
120 // for new group - checking if already exist same name
121 if ($_POST['save'] != 'save_anyway' && $this->alreadyExist($_POST, $isEdit)) {
122 $data['message'] = xlt('Failed - already has group with the same name') . '.';
123 $data['savingStatus'] = 'exist';
124 $data['groupData'] = $_POST;
125 if ($isEdit) {
126 $this->loadView('groupDetailsGeneralData', $data);
127 } else {
128 $this->loadView('addGroup', $data);
132 $filters = array(
133 'group_name' => FILTER_DEFAULT,
134 'group_start_date' => FILTER_SANITIZE_SPECIAL_CHARS,
135 'group_type' => FILTER_VALIDATE_INT,
136 'group_participation' => FILTER_VALIDATE_INT,
137 'group_status' => FILTER_VALIDATE_INT,
138 'group_notes' => FILTER_DEFAULT,
139 'group_guest_counselors' => FILTER_DEFAULT,
140 'counselors' => array('filter' => FILTER_VALIDATE_INT,
141 'flags' => FILTER_FORCE_ARRAY)
143 if ($isEdit) {
144 $filters['group_end_date'] = FILTER_SANITIZE_SPECIAL_CHARS;
145 $filters['group_id'] = FILTER_VALIDATE_INT;
148 //filter and sanitize all post data.
149 $data['groupData'] = filter_var_array($_POST, $filters);
150 if (!$data['groupData']) {
151 $data['message'] = xlt('Failed to create new group') . '.';
152 $data['savingStatus'] = 'failed';
153 } else {
154 if (!$isEdit) {
155 // save new group
156 $id = $this->saveNewGroup($data['groupData']);
157 $data['groupData']['group_id'] = $id;
158 $data['message'] = xlt('New group was saved successfully') . '.';
159 $data['savingStatus'] = 'success';
160 self::setSession($id);
161 $events = $eventsModel->getGroupEvents($id);
162 $data['events'] = $events;
163 $data['readonly'] = 'disabled';
165 $this->loadView('groupDetailsGeneralData', $data);
166 } else {
167 //update group
168 $this->updateGroup($data['groupData']);
169 $data['message'] = xlt("Detail's group was saved successfully") . '.';
170 $data['savingStatus'] = 'success';
171 $data['readonly'] = 'disabled';
172 $this->loadView('groupDetailsGeneralData', $data);
176 // before saving
177 } else {
178 if (is_null($groupId)) {
179 //for new form
180 $data['groupData'] = array('group_name' => null,
181 'group_start_date' => date('Y-m-d'),
182 'group_type' => null,
183 'group_participation' => null,
184 'group_notes' => null,
185 'group_guest_counselors' => null,
186 'group_status' => null
188 $this->loadView('addGroup', $data);
189 } else {
190 //for exist group screen
191 $data['groupData'] = $this->therapyGroupModel->getGroup($groupId);
192 $data['groupData']['counselors'] = $this->counselorsModel->getCounselors($groupId);
193 $data['readonly'] = isset($_GET['editGroup']) ? '' : 'disabled';
195 $this->loadView('groupDetailsGeneralData', $data);
201 * check if exist group with the same name and same start date
202 * @param $groupData
203 * @param $isEdit type of testing
204 * @return bool
206 private function alreadyExist($groupData, $isEdit = false)
209 if ($isEdit) {
210 //return false if not touched on name and date
211 $databaseData = $this->therapyGroupModel->getGroup($groupData['group_id']);
212 if ($databaseData['group_name'] == $groupData['group_name'] && $databaseData['group_start_date'] == $groupData['group_start_date']) {
213 return false;
217 $isExistGroup = $this->therapyGroupModel->existGroup($groupData['group_name'], $groupData['group_start_date'], $isEdit ? $groupData['group_id'] : null);
218 //true / false
219 return $isExistGroup;
223 * Controller for loading the therapy groups to be listed in 'listGroups' view.
225 public function listGroups()
228 //If deleting a group
229 if ($_GET['deleteGroup'] == 1) {
230 $group_id = $_GET['group_id'];
231 $deletion_response = $this->deleteGroup($group_id);
232 $data['deletion_try'] = 1;
233 $data['deletion_response'] = $deletion_response;
236 //Load therapy groups from DB.
237 $therapy_groups_model = $this->loadModel('Therapy_Groups');
238 $therapy_groups = $therapy_groups_model->getAllGroups();
240 //Load counselors from DB.
241 $counselors_model = $this->loadModel('Therapy_Groups_Counselors');
242 $counselors = $counselors_model->getAllCounselors();
244 //Merge counselors with matching groups and prepare array for view.
245 $data['therapyGroups'] = $this->prepareGroups($therapy_groups, $counselors);
247 //Insert static arrays to send to view.
248 $data['statuses'] = self::prepareStatusesList();
249 $data['group_types'] = self::prepareGroupTypesList();
250 $data['group_participation'] = self::prepareGroupParticipationList();
251 $data['counselors'] = $this->prepareCounselorsList($counselors);
253 //Send groups array to view.
254 $this->loadView('listGroups', $data);
258 * Prepares the therapy group list that will be sent to view.
259 * @param $therapy_groups
260 * @param $counselors
261 * @return array
263 private function prepareGroups($therapy_groups, $counselors)
266 $new_array = array();
267 $users_model = $this->loadModel('Users');
269 //Insert groups into a new array and shorten notes for preview in list
270 foreach ($therapy_groups as $therapy_group) {
271 $gid = $therapy_group['group_id'];
272 $new_array[$gid] = $therapy_group;
273 $new_array[$gid]['group_notes'] = $this->shortenNotes($therapy_group['group_notes']);
274 $new_array[$gid]['counselors'] = array();
277 //Insert the counselors into their groups in new array.
278 foreach ($counselors as $counselor) {
279 $group_id_of_counselor = $counselor['group_id'];
280 $counselor_id = $counselor['user_id'];
281 $counselor_name = $users_model->getUserNameById($counselor_id);
282 if (is_array($new_array[$group_id_of_counselor])) {
283 array_push($new_array[$group_id_of_counselor]['counselors'], $counselor_name);
287 return $new_array;
290 private function shortenNotes($notes)
293 $length = strlen($notes);
294 if ($length > $this->notes_preview_proper_length) {
295 $notes = mb_substr($notes, 0, 50).'...';
298 return $notes;
302 * Returns a list of counselors without duplicates.
303 * @param $counselors
304 * @return array
306 private function prepareCounselorsList($counselors)
309 $new_array = array();
310 $users_model = $this->loadModel('Users');
312 foreach ($counselors as $counselor) {
313 $counselor_id = $counselor['user_id'];
314 $counselor_name = $users_model->getUserNameById($counselor_id);
315 $new_array[$counselor_id] = $counselor_name;
318 return $new_array;
322 * Change group status to 'deleted'. Can be done only if group has no encounters.
323 * @param $group_id
324 * @return array
326 private function deleteGroup($group_id)
329 $response = array();
331 //If group has encounters cannot delete the group.
332 $group_has_encounters = $this->checkIfHasApptOrEncounter($group_id);
333 if ($group_has_encounters) {
334 $response['success'] = 0;
335 $response['message'] = xl("Deletion failed because group has appointments or encounters");
336 } else {
337 //Change group status to 'canceled'.
338 $therapy_groups_model = $this->loadModel('Therapy_Groups');
339 $therapy_groups_model->changeGroupStatus($group_id, 30);
340 $response['success'] = 1;
343 return $response;
347 * Checks if group has upcoming appointments or encounters
348 * @param $group_id
349 * @return bool
351 private function checkIfHasApptOrEncounter($group_id)
353 $therapy_groups_events_model = $this->loadModel('Therapy_Groups_Events');
354 $therapy_groups_encounters_model = $this->loadModel('Therapy_Groups_Encounters');
355 $events = $therapy_groups_events_model->getGroupEvents($group_id);
356 $encounters = $therapy_groups_encounters_model->getGroupEncounters($group_id);
357 if (empty($events) && empty($encounters)) {
358 return false; //no appts or encounters so can delete
361 return true; //appts or encounters exist so can't delete
367 * insert a new group to therapy_group table and connect between user-counselor to group at therapy_Groups_Counselors table
368 * @param $groupData
369 * @return int $groupId
371 private function saveNewGroup($groupData)
374 $counselors = !empty($groupData['counselors']) ? $groupData['counselors'] : array();
375 unset($groupData['groupId'], $groupData['save'], $groupData['counselors']);
377 $groupId = $this->therapyGroupModel->saveNewGroup($groupData);
379 foreach ($counselors as $counselorId) {
380 $this->counselorsModel->save($groupId, $counselorId);
383 return $groupId;
387 * update group in therapy_group table and the connection between user-counselor to group at therapy_Groups_Counselors table
388 * @param $groupData
389 * @return int $groupId
391 private function updateGroup($groupData)
394 $counselors = !empty($groupData['counselors']) ? $groupData['counselors'] : array();
395 unset($groupData['save'], $groupData['counselors']);
397 $this->therapyGroupModel->updateGroup($groupData);
399 $this->counselorsModel->remove($groupData['group_id']);
400 foreach ($counselors as $counselorId) {
401 $this->counselorsModel->save($groupData['group_id'], $counselorId);
405 static function setSession($groupId)
408 setpid(0);
409 if ($_SESSION['therapy_group'] != $groupId) {
410 $_SESSION['therapy_group'] = $groupId;