From fbd7ac1eef579b2c0add6526fe270fc982af8067 Mon Sep 17 00:00:00 2001 From: Nito Martinez Date: Fri, 15 Feb 2019 11:55:24 +0100 Subject: [PATCH 1/2] Allow to list more than one level of collaborators, fixes #264 --- application/config/config.php | 8 +++++- application/models/Leaves_model.php | 41 ++++++++++++++++++++++++++++- application/models/Users_model.php | 26 ++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/application/config/config.php b/application/config/config.php index dc9ef8571..798b3b2ad 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -452,6 +452,12 @@ //Set this value to TRUE if you want to allow manager to create leave requests in behalf of their collaborators $config['requests_by_manager'] = TRUE; +//____________________________________________________________________________ +//Set this value to TRUE if you want to allow manager to see the collaborators of the collaboratos +//By default only the immediate collaborators (direct reports) are shown +//(beware that it will impact the performance setting this to TRUE) +$config['manager_sees_multiple_level_collaborators'] = FALSE; + //Set this value to true if you want to force the manager to comment rejections $config['mandatory_comment_on_reject'] = FALSE; @@ -480,7 +486,7 @@ //____________________________________________________________________________ // Hide/Disable features $config['disable_overtime'] = FALSE; //Set this value to TRUE if you want to hide the menu entries related to overtime -$config['hide_global_cals_to_users'] = FALSE; //Set this value to TRUE if you want to hide global calendars (global/tabular) to users +$config['hide_global_cals_to_users'] = TRUE; //Set this value to TRUE if you want to hide global calendars (global/tabular) to users $config['disable_department_calendar'] = FALSE; //Set this value to TRUE in order to disable the menu entry 'departement' $config['disable_workmates_calendar'] = FALSE; //Set this value to TRUE in order to disable the menu entry 'my workmates' diff --git a/application/models/Leaves_model.php b/application/models/Leaves_model.php index 4fbb127df..91120debc 100644 --- a/application/models/Leaves_model.php +++ b/application/models/Leaves_model.php @@ -863,13 +863,52 @@ public function workmates($user_id, $start = "", $end = "") { * @return string JSON encoded list of full calendar events * @author Benjamin BALET */ - public function collaborators($user_id, $start = "", $end = "") { + + private function getCollaboratorLeaves($user_id, $start = "", $end = "") { + if ($this->config->item('manager_sees_multiple_level_collaborators') === TRUE) { + $this->load->model('users_model'); + $collaborators = $this->users_model->getCollaboratorsOfManager($user_id); + $leaves = $this->getLeavesForUsers($collaborators, $start, $end); + return $leaves; + } + // This one should be much more efficient it is only based on one SQL query + $leaves = $this->getDirectCollaboratorLeaves($user_id, $start, $end); + return $leaves; + } + + private function getLeavesForUsers($collaborators, $start = "", $end = "") { + $expandedleaves = array(); + foreach ($collaborators as $collaborator) { + $leaves = $this->getIndividualLeaves($collaborator['id'], $start, $end); + if (!empty($leaves)) { + $expandedleaves = array_merge($expandedleaves, $leaves); + } + } + return $expandedleaves; + } + + private function getIndividualLeaves($user_id, $start = "", $end = "") { + $this->db->join('users', 'users.id = leaves.employee'); + $this->db->where('leaves.employee', $user_id); + $this->db->where('(leaves.startdate <= DATE(' . $this->db->escape($end) . ') AND leaves.enddate >= DATE(' . $this->db->escape($start) . '))'); + $this->db->order_by('startdate', 'desc'); + $this->db->limit(1024); //Security limit + $events = $this->db->get('leaves')->result(); + return $events; + } + + private function getDirectCollaboratorLeaves($user_id, $start = "", $end = "") { $this->db->join('users', 'users.id = leaves.employee'); $this->db->where('users.manager', $user_id); $this->db->where('(leaves.startdate <= DATE(' . $this->db->escape($end) . ') AND leaves.enddate >= DATE(' . $this->db->escape($start) . '))'); $this->db->order_by('startdate', 'desc'); $this->db->limit(1024); //Security limit $events = $this->db->get('leaves')->result(); + return $events; + } + + public function collaborators($user_id, $start = "", $end = "") { + $events = $this->getCollaboratorLeaves($user_id, $start, $end); $jsonevents = array(); foreach ($events as $entry) { diff --git a/application/models/Users_model.php b/application/models/Users_model.php index 1f17e5389..58489558b 100644 --- a/application/models/Users_model.php +++ b/application/models/Users_model.php @@ -99,6 +99,14 @@ public function getName($id) { * @author Benjamin BALET */ public function getCollaboratorsOfManager($id = 0) { + if ($this->config->item('manager_sees_multiple_level_collaborators') === TRUE) { + $collaborators = $this->getMultipleLevelOfCollaboratorsOfManager($id); + return $collaborators; + } + return $this->getDirectCollaboratorsOfManager($id); + } + + private function getDirectCollaboratorsOfManager($id = 0) { $this->db->select('users.*'); $this->db->select('organization.name as department_name, positions.name as position_name, contracts.name as contract_name'); $this->db->from('users'); @@ -112,6 +120,24 @@ public function getCollaboratorsOfManager($id = 0) { return $query->result_array(); } + private function getMultipleLevelOfCollaboratorsOfManager($id = 0) { + $collaborators = $this->getDirectCollaboratorsOfManager($id); + if (empty($collaborators)) { + return $collaborators; + } + $expandedcollaborators = $collaborators; + foreach ($collaborators as $collaborator) { + if ($collaborator['id'] === $id) { + // already added, so do not add again (avoid infinite loop). Case of manager of himself. + continue; + } + $collaboratorsofcollaborator = $this->getMultipleLevelOfCollaboratorsOfManager($collaborator['id']); + $expandedcollaborators = array_merge($expandedcollaborators, $collaboratorsofcollaborator); + } + return $expandedcollaborators; + } + + /** * Check if an employee is the collaborator of the given user * @param int $employee identifier of the collaborator From 7ea7653b9a3f00f3c41543a58f2ae5c7b9422074 Mon Sep 17 00:00:00 2001 From: Nito Martinez Date: Fri, 15 Feb 2019 15:48:23 +0100 Subject: [PATCH 2/2] Do not change default settings in config.php --- application/config/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/config/config.php b/application/config/config.php index 798b3b2ad..05c9b7953 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -486,7 +486,7 @@ //____________________________________________________________________________ // Hide/Disable features $config['disable_overtime'] = FALSE; //Set this value to TRUE if you want to hide the menu entries related to overtime -$config['hide_global_cals_to_users'] = TRUE; //Set this value to TRUE if you want to hide global calendars (global/tabular) to users +$config['hide_global_cals_to_users'] = FALSE; //Set this value to TRUE if you want to hide global calendars (global/tabular) to users $config['disable_department_calendar'] = FALSE; //Set this value to TRUE in order to disable the menu entry 'departement' $config['disable_workmates_calendar'] = FALSE; //Set this value to TRUE in order to disable the menu entry 'my workmates'