Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to show info of non direct reports per manager #264 #265 #271

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
41 changes: 40 additions & 1 deletion application/models/Leaves_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,52 @@ public function workmates($user_id, $start = "", $end = "") {
* @return string JSON encoded list of full calendar events
* @author Benjamin BALET <[email protected]>
*/
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) {
Expand Down
26 changes: 26 additions & 0 deletions application/models/Users_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public function getName($id) {
* @author Benjamin BALET <[email protected]>
*/
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');
Expand All @@ -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
Expand Down