From fd02ecab0f4f0bd858312bf22a9d1348562c393d Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Fri, 30 Oct 2015 14:03:54 +0100 Subject: [PATCH 1/4] fixed gevUserUtils::getDirectSuperiors --- .../GEV/Utils/classes/class.gevUserUtils.php | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/Services/GEV/Utils/classes/class.gevUserUtils.php b/Services/GEV/Utils/classes/class.gevUserUtils.php index b993088be583..ff19c1b517ed 100644 --- a/Services/GEV/Utils/classes/class.gevUserUtils.php +++ b/Services/GEV/Utils/classes/class.gevUserUtils.php @@ -1189,40 +1189,45 @@ static public function removeInactiveUsers($a_usr_ids) { public function getDirectSuperiors() { require_once("Modules/OrgUnit/classes/class.ilObjOrgUnitTree.php"); $tree = ilObjOrgUnitTree::_getInstance(); - $sups = array(); - $look_above_orgus = array(); - $orgus = $tree->getOrgUnitOfUser($this->user_id); - foreach( $orgus as $ref_id ) { - $employees = $tree->getEmployees($ref_id); - $superiors = $tree->getSuperiors($ref_id); - $any_superiors = count($superiors); - if(!$any_superiors) { - $look_above_orgus[] = $ref_id; + + // This starts with all the org units the user is member in. + // During the loop we might fill this array with more org units + // if we could not find any superiors for the user in them. + $orgus = array_values($tree->getOrgUnitOfUser($this->user_id)); + + if (count($orgus) == 0) { + return array(); + } + + $the_superiors = array(); + + $i = -1; + while ($i < count($orgus)) { + $i++; + $ref_id = $orgus[$i]; + + // Reached the top of the tree. + if (!$ref_id || $ref_id == ROOT_FOLDER_ID) { continue; } - if(in_array($this->user_id,$employees)) { - $sups = array_merge($sups,$superiors); - } else { - $look_above_orgus[] = $ref_id; - } - if(in_array($this->user_id,$superiors)) { - $look_above_orgus[] = $ref_id; - } - } - $look_above_orgus = array_unique($look_above_orgus); + $superiors = $tree->getSuperiors($ref_id); - foreach($look_above_orgus as $org) { - $sups_aux = array(); - $org_aux = $tree->getParent($org); - while (count($sups_aux) == 0 && $org_aux != ROOT_FOLDER_ID) { - $org_aux = $tree->getParent($org_aux); - $sups_aux = $tree->getSuperiors($org_aux); + // Skip this org unit if the user is superior there or the + // org unit has no superiors. We need to look in the unit + // above then. + if ( in_array($this->user_id, $superiors) + || count($superiors) == 0) { + $orgus[] = $tree->getParent($ref_id); + continue; } - $sups = array_merge($sups,$sups_aux); + + $the_superiors[] = $superiors; } - $sups = array_unique($sups); - return gevUserUtils::removeInactiveUsers($sups); + + $the_superiors = call_user_func_array("array_merge", $the_superiors); + + return gevUserUtils::removeInactiveUsers(array_unique($the_superiors)); } public function isEmployeeOf($a_user_id) { From cb9390d0e30ce262e87b5073005f6a6d6eb02606 Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Fri, 30 Oct 2015 14:28:07 +0100 Subject: [PATCH 2/4] gevUserUtils::getDirectSuperiors: use could be their own superiors if they are superiors in an org unit above one of their own. --- Services/GEV/Utils/classes/class.gevUserUtils.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Services/GEV/Utils/classes/class.gevUserUtils.php b/Services/GEV/Utils/classes/class.gevUserUtils.php index ff19c1b517ed..8f862f215fdc 100644 --- a/Services/GEV/Utils/classes/class.gevUserUtils.php +++ b/Services/GEV/Utils/classes/class.gevUserUtils.php @@ -1202,6 +1202,9 @@ public function getDirectSuperiors() { $the_superiors = array(); $i = -1; + $initial_amount = count($orgus); + // We need to check this on every loop as the amount of orgus might change + // during looping. while ($i < count($orgus)) { $i++; $ref_id = $orgus[$i]; @@ -1216,7 +1219,7 @@ public function getDirectSuperiors() { // Skip this org unit if the user is superior there or the // org unit has no superiors. We need to look in the unit // above then. - if ( in_array($this->user_id, $superiors) + if ( ($i < $initial_amount && in_array($this->user_id, $superiors)) || count($superiors) == 0) { $orgus[] = $tree->getParent($ref_id); continue; From 5c1fd9be48988688d393c564c9676184b501f49c Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Fri, 30 Oct 2015 14:43:30 +0100 Subject: [PATCH 3/4] gevUserUtils::getDirectSuperiors: user also could be his own superior if he is member and superior in the same orgu. --- Services/GEV/Utils/classes/class.gevUserUtils.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Services/GEV/Utils/classes/class.gevUserUtils.php b/Services/GEV/Utils/classes/class.gevUserUtils.php index 8f862f215fdc..9b99d6b3446e 100644 --- a/Services/GEV/Utils/classes/class.gevUserUtils.php +++ b/Services/GEV/Utils/classes/class.gevUserUtils.php @@ -1216,11 +1216,15 @@ public function getDirectSuperiors() { $superiors = $tree->getSuperiors($ref_id); - // Skip this org unit if the user is superior there or the - // org unit has no superiors. We need to look in the unit - // above then. - if ( ($i < $initial_amount && in_array($this->user_id, $superiors)) - || count($superiors) == 0) { + // Skip the orgu if there are no superiors there. + if ( count($superiors) == 0 + || ( $i < $initial_amount + // This is only about the org units the user actually is a member of + && in_array($this->user_id, $superiors) + // If a user is an employee and a superior in one orgunit, he + // actually seem to be his own superior. + && !in_array($this->user_id, $tree->getEmployees($ref_id))) + ) { $orgus[] = $tree->getParent($ref_id); continue; } From 263e07e8fcd62c00e0b762e101f9fb9696865347 Mon Sep 17 00:00:00 2001 From: Richard Klees Date: Fri, 30 Oct 2015 14:53:36 +0100 Subject: [PATCH 4/4] gevUserUtils::getDirectSuperiors: more edgecases. --- Services/GEV/Utils/classes/class.gevUserUtils.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Services/GEV/Utils/classes/class.gevUserUtils.php b/Services/GEV/Utils/classes/class.gevUserUtils.php index 9b99d6b3446e..9582a641cd0b 100644 --- a/Services/GEV/Utils/classes/class.gevUserUtils.php +++ b/Services/GEV/Utils/classes/class.gevUserUtils.php @@ -1215,12 +1215,20 @@ public function getDirectSuperiors() { } $superiors = $tree->getSuperiors($ref_id); + $user_is_superior = in_array($this->user_id, $superiors); + $in_initial_orgus = $i < $initial_amount; + + // I always need to go one org unit up if we are in the original + // orgu and the user is superior there. + if ( $in_initial_orgus && $user_is_superior) { + $orgus[] = $tree->getParent($ref_id); + } // Skip the orgu if there are no superiors there. if ( count($superiors) == 0 - || ( $i < $initial_amount + || ( $in_initial_orgus // This is only about the org units the user actually is a member of - && in_array($this->user_id, $superiors) + && $user_is_superior // If a user is an employee and a superior in one orgunit, he // actually seem to be his own superior. && !in_array($this->user_id, $tree->getEmployees($ref_id)))