Ngiler SH3LL 360
Home
Information
Create File
Create Folder
:
/
home
/
tbf
/
cursbackend
/
app
/
Http
/
Resources
/
Information Server
MySQL :
OFF
Perl :
OFF
CURL :
ON
WGET :
OFF
PKEXEC :
OFF
Directive
Local Value
IP Address
89.40.16.97
System
Linux server.atelieruldeit.ro 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
User
tbf
PHP Version
7.3.33
Software
Apache
Doc root
Writable
close
Edit File :
MasterGoalResource.php
| Size :
6.38
KB
Copy
<?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; use App\Models\MasterGoal; use App\Models\Objective; use App\Models\User; use Carbon\Carbon; use Auth; use Arr; class MasterGoalResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { $authUser = Auth::user(); $masterGoals = $authUser->instance->master_goals()->whereNotNull('master_goal_id')->get(); // check if this auth user can see a private master goals $noPermisionToSee = $authUser && !$authUser->isAdmin() && isPrivateMasterGoal($this->id, $authUser); $var = [ 'has_one_overdue' => false, 'remaining_days' => 0, 'linked_master_goals' => 0, 'people_involved' => 0, 'linked_objectives' => 0, 'people_involved_ids' => [], 'involved' => false, ]; // populate var with pointer from recursive function $children = $this->recursiveMasterGoal($masterGoals, $this->id, $var, $authUser->id); // populate var with pointer for main master goal $this->objectiveData($this, $var, $authUser->id); // calculate total percentage for main master goal $var['percentage_finished'] = count($children) > 0 ? array_sum(Arr::pluck($children, 'children_percent')) / count($children) : 0; return [ 'id' => $this->id, 'name' => $noPermisionToSee ? null : $this->name, 'slug' => $this->slug, 'description' => $noPermisionToSee ? null : $this->description, 'uuid' => $this->uuid, 'status' => $this->status, 'share_status' => $this->share_status, 'related_to' => $this->master_goal_id, 'is_private' => $this->is_private, 'has_one_overdue' => $var['has_one_overdue'], 'percentage_finished' => $var['percentage_finished'], 'remaining_days' => $var['remaining_days'], 'linked_master_goals' => $var['linked_master_goals'], 'people_involved' => $var['people_involved'], 'linked_objectives' => $var['linked_objectives'], 'involved' => $var['involved'], 'rights' => [ 'edit' => $authUser->can('crudActionAndEntityInstance', [User::class, $this->resource]), 'delete' => $authUser->can('crudActionAndEntityInstance', [User::class, $this->resource]), ], ]; } public function recursiveMasterGoal($masterGoals, $parentId, &$var, $authUserID) { $data = []; foreach ($masterGoals as $masterGoal) { if ($masterGoal->master_goal_id == $parentId) { // count master goals linked $var['linked_master_goals'] += 1; $element = [ 'children_percent' => 0, ]; // check for the children of this master goal $children = $this->recursiveMasterGoal($masterGoals, $masterGoal->id, $var, $authUserID); if($children){ // master goals percentage array $percentageArr = array_filter(Arr::pluck($children, 'children_percent'), function ($child) { return $child != null; }); // master goals percentage of children $percentageMasterGoalChildren = count($percentageArr) > 0 ? array_sum($percentageArr) / count($percentageArr) : 0; // percentage of goals of this master goal $objectivePercent = $masterGoal->objectives->sum('percent'); $objectivesCount = $masterGoal->objectives->count(); // master goal percentage finished $element['children_percent'] = $objectivesCount > 0 ? ($percentageMasterGoalChildren + $objectivePercent) / ($objectivesCount + 1) : $percentageMasterGoalChildren; }else{ // percentage of goals of this master goal for last child $objectivePercent = $masterGoal->objectives->sum('percent'); $objectivesCount = $masterGoal->objectives->count(); $element['children_percent'] = $objectivesCount > 0 ? ($objectivePercent / $objectivesCount) : 0; } // check if there are any objectives to this master goal $this->objectiveData($masterGoal, $var, $authUserID); $data[] = $element; } } return $data; } public function objectiveData($masterGoal, &$var, $authUserID) { // check if there are any objectives to this master goal foreach($masterGoal->objectives as $objective){ // count unique people involved in each objective if(!in_array($objective->user_id, $var['people_involved_ids']) && $objective->user){ $var['people_involved'] += 1; array_push($var['people_involved_ids'], $objective->user_id); } // check if the auth user is involved in this objective if($objective->user_id == $authUserID){ $var['involved'] = true; } // check if the auth user is involved in any key results if(!$var['involved'] && $objective->key_results->where('user_id', $authUserID)->first()){ $var['involved'] = true; } // count objectives $var['linked_objectives'] += 1; // check for an overdue objective if($objective->status == Objective::STATUS_OVERDUE){ $var['has_one_overdue'] = true; } // check for objective end date and difference in days until the end of objective if($objective->end_date >= Carbon::today()->toDateString()){ // difference in days until the end of objective $diffInDays = Carbon::today()->diffInDays($objective->end_date); if($diffInDays > $var['remaining_days']){ $var['remaining_days'] = $diffInDays; } } } } }
Back