Added translation function
[openemr.git] / interface / therapy_groups / therapy_groups_controllers / therapy_groups_controller.php
blobe5d486d5a9ef27ef9e49926e8475402eef243781
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{
33 public $therapyGroupModel;
35 /* Note: Created functions to return arrays so that xl method can be used in array rendering. */
37 //list of group statuses
38 public static function prepareStatusesList()
40 $statuses = array(
41 '10' => xl('active'),
42 '20' => xl('deleted')
44 return $statuses;
47 //list of participant statuses
48 public static function prepareParticipantStatusesList()
50 $participant_statuses = array(
51 '10' => xl('active'),
52 '20' => xl('not active')
54 return $participant_statuses;
57 //list of group types
58 public static function prepareGroupTypesList()
60 $group_types = array(
61 '1' => xl('closed'),
62 '2' => xl('open'),
63 '3' => xl('training')
65 return $group_types;
68 //list of participation types
69 public static function prepareGroupParticipationList()
71 $group_participation = array(
72 '1' => xl('mandatory'),
73 '2' => xl('optional')
75 return $group_participation;
78 //Max length of notes preview in groups list
79 private $notes_preview_proper_length = 30;
82 /**
83 * add / edit therapy group
84 * making validation and saving in the match tables.
85 * @param null $groupId - must pass when edit group
87 public function index($groupId = null){
89 $data = array();
90 if($groupId) self::setSession($groupId);
91 //Load models
92 $this->therapyGroupModel = $this->loadModel('therapy_groups');
93 $this->counselorsModel = $this->loadModel('Therapy_Groups_Counselors');
94 $eventsModel = $this->loadModel('Therapy_Groups_Events');
95 $userModel = $this->loadModel('Users');
97 //Get group events
98 $events = $eventsModel->getGroupEvents($groupId);
99 $data['events'] = $events;
101 //Get users
102 $users = $userModel->getAllUsers();
103 $data['users'] = $users;
105 //Get statuses
106 $data['statuses'] = self::prepareStatusesList();
108 //print_r($_POST);die;
109 if(isset($_POST['save'])){
111 $isEdit = empty( $_POST['group_id']) ? false : true;
113 // for new group - checking if already exist same name
114 if($_POST['save'] != 'save_anyway' && $this->alreadyExist($_POST, $isEdit)){
115 $data['message'] = xlt('Failed - already has group with the same name') . '.';
116 $data['savingStatus'] = 'exist';
117 $data['groupData'] = $_POST;
118 if($isEdit){
119 $this->loadView('groupDetailsGeneralData', $data);
120 } else {
121 $this->loadView('addGroup', $data);
125 $filters = array(
126 'group_name' => FILTER_SANITIZE_STRING,
127 'group_start_date' => FILTER_SANITIZE_SPECIAL_CHARS,
128 'group_type' => FILTER_VALIDATE_INT,
129 'group_participation' => FILTER_VALIDATE_INT,
130 'group_status' => FILTER_VALIDATE_INT,
131 'group_notes' => FILTER_SANITIZE_STRING,
132 'group_guest_counselors' => FILTER_SANITIZE_STRING,
133 'counselors' => array('filter' => FILTER_VALIDATE_INT,
134 'flags' => FILTER_FORCE_ARRAY)
136 if($isEdit){
137 $filters['group_end_date'] = FILTER_SANITIZE_SPECIAL_CHARS;
138 $filters['group_id'] = FILTER_VALIDATE_INT;
140 //filter and sanitize all post data.
141 $data['groupData'] = filter_var_array($_POST, $filters);
142 if(!$data['groupData']){
143 $data['message'] = xlt('Failed to create new group') . '.';
144 $data['savingStatus'] = 'failed';
146 else {
148 if(!$isEdit){
149 // save new group
150 $id = $this->saveNewGroup($data['groupData']);
151 $data['groupData']['group_id'] = $id;
152 $data['message'] = xlt('New group was saved successfully') . '.';
153 $data['savingStatus'] = 'success';
154 self::setSession($id);
155 $events = $eventsModel->getGroupEvents($id);
156 $data['events'] = $events;
157 $data['readonly'] = 'disabled';
159 $this->loadView('groupDetailsGeneralData', $data);
160 } else {
161 //update group
162 $this->updateGroup($data['groupData']);
163 $data['message'] = xlt("Detail's group was saved successfully") . '.';
164 $data['savingStatus'] = 'success';
165 $data['readonly'] = 'disabled';
166 $this->loadView('groupDetailsGeneralData', $data);
170 // before saving
171 } else {
173 if(is_null($groupId)){
174 //for new form
175 $data['groupData'] = array('group_name' => null,
176 'group_start_date' => date('Y-m-d'),
177 'group_type' => null,
178 'group_participation' => null,
179 'group_notes' => null,
180 'group_guest_counselors' => null,
181 'group_status' => null
183 $this->loadView('addGroup', $data);
185 } else {
186 //for exist group screen
187 $data['groupData'] = $this->therapyGroupModel->getGroup($groupId);
188 $data['groupData']['counselors'] = $this->counselorsModel->getCounselors($groupId);
189 $data['readonly'] = isset($_GET['editGroup']) ? '' : 'disabled';
191 $this->loadView('groupDetailsGeneralData', $data);
199 * check if exist group with the same name and same start date
200 * @param $groupData
201 * @param $isEdit type of testing
202 * @return bool
204 private function alreadyExist($groupData, $isEdit = false){
206 if($isEdit){
207 //return false if not touched on name and date
208 $databaseData = $this->therapyGroupModel->getGroup($groupData['group_id']);
209 if($databaseData['group_name'] == $groupData['group_name'] && $databaseData['group_start_date'] == $groupData['group_start_date']){
210 return false;
214 $isExistGroup = $this->therapyGroupModel->existGroup($groupData['group_name'], $groupData['group_start_date'], $isEdit ? $groupData['group_id'] : null);
215 //true / false
216 return $isExistGroup;
220 * Controller for loading the therapy groups to be listed in 'listGroups' view.
222 public function listGroups(){
224 //If deleting a group
225 if($_GET['deleteGroup'] == 1){
226 $group_id = $_GET['group_id'];
227 $deletion_response = $this->deleteGroup($group_id);
228 $data['deletion_try'] = 1;
229 $data['deletion_response'] = $deletion_response;
232 //Load therapy groups from DB.
233 $therapy_groups_model = $this->loadModel('Therapy_Groups');
234 $therapy_groups = $therapy_groups_model->getAllGroups();
236 //Load counselors from DB.
237 $counselors_model = $this->loadModel('Therapy_Groups_Counselors');
238 $counselors = $counselors_model->getAllCounselors();
240 //Merge counselors with matching groups and prepare array for view.
241 $data['therapyGroups'] = $this->prepareGroups($therapy_groups, $counselors);
243 //Insert static arrays to send to view.
244 $data['statuses'] = SELF::prepareStatusesList();
245 $data['group_types'] = SELF::prepareGroupTypesList();
246 $data['group_participation'] = SELF::prepareGroupParticipationList();
247 $data['counselors'] = $this->prepareCounselorsList($counselors);
249 //Send groups array to view.
250 $this->loadView('listGroups', $data);
254 * Prepares the therapy group list that will be sent to view.
255 * @param $therapy_groups
256 * @param $counselors
257 * @return array
259 private function prepareGroups($therapy_groups, $counselors){
261 $new_array = array();
262 $users_model = $this->loadModel('Users');
264 //Insert groups into a new array and shorten notes for preview in list
265 foreach ($therapy_groups as $therapy_group) {
266 $gid = $therapy_group['group_id'];
267 $new_array[$gid] = $therapy_group;
268 $new_array[$gid]['group_notes'] = $this->shortenNotes($therapy_group['group_notes']);
269 $new_array[$gid]['counselors'] = array();
272 //Insert the counselors into their groups in new array.
273 foreach ($counselors as $counselor){
274 $group_id_of_counselor = $counselor['group_id'];
275 $counselor_id = $counselor['user_id'];
276 $counselor_name = $users_model->getUserNameById($counselor_id);
277 if(is_array($new_array[$group_id_of_counselor])) {
278 array_push($new_array[$group_id_of_counselor]['counselors'], $counselor_name);
282 return $new_array;
286 private function shortenNotes($notes){
288 $length = strlen($notes);
289 if($length > $this->notes_preview_proper_length){
290 $notes = mb_substr($notes,0,50).'...';
292 return $notes;
296 * Returns a list of counselors without duplicates.
297 * @param $counselors
298 * @return array
300 private function prepareCounselorsList($counselors){
302 $new_array = array();
303 $users_model = $this->loadModel('Users');
305 foreach ($counselors as $counselor){
306 $counselor_id = $counselor['user_id'];
307 $counselor_name = $users_model->getUserNameById($counselor_id);
308 $new_array[$counselor_id] = $counselor_name;
311 return $new_array;
315 * Change group status to 'deleted'. Can be done only if group has no encounters.
316 * @param $group_id
317 * @return array
319 private function deleteGroup($group_id){
321 $response = array();
323 //If group has encounters cannot delete the group.
324 $group_has_encounters = $this->checkIfHasApptOrEncounter($group_id);
325 if($group_has_encounters){
326 $response['success'] = 0;
327 $response['message'] = xl("Deletion failed because group has appointments or encounters");
329 else{
330 //Change group status to 'deleted'.
331 $therapy_groups_model = $this->loadModel('Therapy_Groups');
332 $therapy_groups_model->changeGroupStatus($group_id, 20);
333 $response['success'] = 1;
336 return $response;
340 * Checks if group has upcoming appointments or encounters
341 * @param $group_id
342 * @return bool
344 private function checkIfHasApptOrEncounter($group_id){
345 $therapy_groups_events_model = $this->loadModel('Therapy_Groups_Events');
346 $therapy_groups_encounters_model = $this->loadModel('Therapy_Groups_Encounters');
347 $events = $therapy_groups_events_model->getGroupEvents($group_id);
348 $encounters = $therapy_groups_encounters_model->getGroupEncounters($group_id);
349 if(empty($events) && empty($encounters)){
350 return false; //no appts or encounters so can delete
352 return true; //appts or encounters exist so can't delete
359 * insert a new group to therapy_group table and connect between user-counselor to group at therapy_Groups_Counselors table
360 * @param $groupData
361 * @return int $groupId
363 private function saveNewGroup($groupData){
365 $counselors = !empty($groupData['counselors']) ? $groupData['counselors'] : array();
366 unset($groupData['groupId'], $groupData['save'], $groupData['counselors']);
368 $groupId = $this->therapyGroupModel->saveNewGroup($groupData);
370 foreach($counselors as $counselorId){
371 $this->counselorsModel->save($groupId, $counselorId);
374 return $groupId;
378 * update group in therapy_group table and the connection between user-counselor to group at therapy_Groups_Counselors table
379 * @param $groupData
380 * @return int $groupId
382 private function updateGroup($groupData){
384 $counselors = !empty($groupData['counselors']) ? $groupData['counselors'] : array();
385 unset($groupData['save'], $groupData['counselors']);
387 $this->therapyGroupModel->updateGroup($groupData);
389 $this->counselorsModel->remove($groupData['group_id']);
390 foreach($counselors as $counselorId){
391 $this->counselorsModel->save($groupData['group_id'], $counselorId);
395 static function setSession($groupId){
397 setpid(0);
398 if($_SESSION['therapy_group'] != $groupId){
399 $_SESSION['therapy_group'] = $groupId;