-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from wri/feat/TM-617/626/627/658_tree_restora…
…tion_and_export [TM-617, 626, 627, 658] feat: add tree restoration controller and export csv
- Loading branch information
Showing
38 changed files
with
1,816 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
app/Http/Controllers/V2/Dashboard/ActiveProjectsTableController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\Dashboard; | ||
|
||
use App\Helpers\TerrafundDashboardQueryHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\V2\Forms\FormOptionList; | ||
use App\Models\V2\Forms\FormOptionListOption; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
use Illuminate\Support\Collection; | ||
|
||
class ActiveProjectsTableController extends Controller | ||
{ | ||
public function __invoke(Request $request) | ||
{ | ||
$perPage = $request->input('per_page', PHP_INT_MAX); | ||
$page = $request->input('page', 1); | ||
|
||
$projects = $this->getAllProjects($request, $perPage, $page); | ||
$count = $this->getQuery($request)->count(); | ||
|
||
return response()->json([ | ||
'current_page' => $page, | ||
'data' => $projects, | ||
'per_page' => $perPage, | ||
'last_page' => ceil($count / $perPage), | ||
'total' => $count, | ||
]); | ||
} | ||
|
||
public function getQuery($request) | ||
{ | ||
return TerrafundDashboardQueryHelper::buildQueryFromRequest($request) | ||
->with('organisation') | ||
->withCount(['sites', 'nurseries']); | ||
} | ||
|
||
public function getAllProjects($request, $perPage, $page) | ||
{ | ||
$query = $this->getQuery($request) | ||
->skip(($page - 1) * $perPage) | ||
->take($perPage); | ||
|
||
$projects = $query->get(); | ||
|
||
return $projects->map(function ($project) { | ||
return [ | ||
'uuid' => $project->uuid, | ||
'name' => $project->name, | ||
'organisation' => $project->organisation->name, | ||
'trees_under_restoration' => $this->treesUnderRestoration($project), | ||
'jobs_created' => $this->jobsCreated($project), | ||
'volunteers' => $this->volunteers($project), | ||
'beneficiaries' => $this->beneficiaries($project), | ||
'survival_rate' => $project->survival_rate, | ||
'number_of_sites' => $project->sites_count, | ||
'number_of_nurseries' => $project->nurseries_count, | ||
'project_country' => $this->projectCountry($project->country), | ||
'country_slug' => $project->country, | ||
'number_of_trees_goal' => $project->trees_grown_goal, | ||
'date_added' => $project->created_at, | ||
]; | ||
}); | ||
} | ||
|
||
public function treesUnderRestoration($project) | ||
{ | ||
return $project->trees_planted_count; | ||
} | ||
|
||
public function jobsCreated($project) | ||
{ | ||
$projectReport = $project->reports() | ||
->selectRaw('SUM(ft_total) as total_ft, SUM(pt_total) as total_pt') | ||
->groupBy('project_id') | ||
->first(); | ||
|
||
if ($projectReport) { | ||
return $projectReport->total_ft + $projectReport->total_pt; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
public function volunteers($project) | ||
{ | ||
$totalVolunteers = $project->reports()->selectRaw('SUM(volunteer_total) as total')->first(); | ||
|
||
return $totalVolunteers ? intval($totalVolunteers->total) : 0; | ||
} | ||
|
||
public function beneficiaries($project) | ||
{ | ||
$totalBeneficiaries = $project->reports()->selectRaw('SUM(beneficiaries) as total')->first(); | ||
|
||
return $totalBeneficiaries ? intval($totalBeneficiaries->total) : 0; | ||
} | ||
|
||
public function projectCountry($slug) | ||
{ | ||
$countryId = FormOptionList::where('key', 'countries')->value('id'); | ||
|
||
return FormOptionListOption::where('form_option_list_id', $countryId) | ||
->where('slug', $slug) | ||
->value('label'); | ||
} | ||
|
||
public function paginate($items, $perPage = 10, $page = null, $options = []) | ||
{ | ||
$page = $page ?: (LengthAwarePaginator::resolveCurrentPage() ?: 1); | ||
$items = $items instanceof Collection ? $items : Collection::make($items); | ||
|
||
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
app/Http/Controllers/V2/Dashboard/CountryDataController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\Dashboard; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\V2\Projects\Project; | ||
use App\Models\V2\Sites\SitePolygon; | ||
use App\Models\V2\WorldCountryGeneralized; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class CountryDataController extends Controller | ||
{ | ||
public function getCountryBbox(string $iso) | ||
{ | ||
// Get the bbox of the country and the name | ||
$countryData = WorldCountryGeneralized::where('iso', $iso) | ||
->selectRaw('ST_AsGeoJSON(ST_Envelope(geometry)) AS bbox, country') | ||
->first(); | ||
|
||
if (! $countryData) { | ||
return response()->json(['error' => 'Country not found'], 404); | ||
} | ||
|
||
// Decode the GeoJSON bbox | ||
$geoJson = json_decode($countryData->bbox); | ||
|
||
// Extract the bounding box coordinates | ||
$coordinates = $geoJson->coordinates[0]; | ||
|
||
// Get the country name | ||
$countryName = $countryData->country; | ||
|
||
// Construct the bbox data in the specified format | ||
$countryBbox = [ | ||
$countryName, | ||
[$coordinates[0][0], $coordinates[0][1], $coordinates[2][0], $coordinates[2][1]], | ||
]; | ||
|
||
return response()->json(['bbox' => $countryBbox]); | ||
} | ||
|
||
public function getPolygonData(string $uuid) | ||
{ | ||
$sitePolygon = SitePolygon::where('poly_id', $uuid)->first(); | ||
|
||
if (! $sitePolygon) { | ||
return response()->json(['error' => 'Polygon not found'], 404); | ||
} | ||
|
||
$project = $sitePolygon->project()->first(); | ||
|
||
if (! $project) { | ||
Log::error("Project not found for site polygon with ID: $sitePolygon->id"); | ||
} | ||
|
||
$site = $sitePolygon->site()->first(); | ||
|
||
if(! $site) { | ||
Log::error("Site not found for site polygon with ID: $sitePolygon->id"); | ||
|
||
} | ||
|
||
$data = [ | ||
['key' => 'poly_name', 'title' => 'title', 'value' => $sitePolygon->poly_name ?? null], | ||
['key' => 'project_name', 'title' => 'Project', 'value' => $project->name ?? null], | ||
['key' => 'site_name', 'title' => 'Site', 'value' => $site?->name ?? null], | ||
['key' => 'num_trees', 'title' => 'Number of trees', 'value' => $sitePolygon->num_trees ?? null], | ||
['key' => 'plantstart', 'title' => 'Plant Start Date', 'value' => $sitePolygon->plantstart ?? null], | ||
['key' => 'status', 'title' => 'Status', 'value' => $sitePolygon->status ?? null], | ||
|
||
]; | ||
|
||
return response()->json(['data' => $data]); | ||
} | ||
|
||
public function getProjectData(string $uuid) | ||
{ | ||
try { | ||
$project = Project::isUuid($uuid)->first(); | ||
|
||
if (! $project) { | ||
Log::error("Project not found for project with UUID: $uuid"); | ||
|
||
return response()->json(['error' => 'Project not found'], 404); | ||
} | ||
$countSitePolygons = $project->total_site_polygons; | ||
|
||
$organization = $project->organisation()->first(); | ||
if (! $organization) { | ||
Log::error("Organization not found for project with ID: $project->id"); | ||
} | ||
|
||
$country = WorldCountryGeneralized::where('iso', $project->country)->first(); | ||
$data = [ | ||
['key' => 'project_name', 'title' => 'title', 'value' => $project->name], | ||
['key' => 'country', 'title' => 'Country', 'value' => $country?->country], | ||
['key' => 'polygon_counts', 'title' => 'No. of Site - Polygons', 'value' => $countSitePolygons], | ||
['key' => 'organizations', 'title' => 'Organization', 'value' => $organization?->name], | ||
]; | ||
|
||
return response()->json(['data' => $data]); | ||
} catch (\Exception $e) { | ||
Log::error($e->getMessage()); | ||
|
||
return response()->json(['error' => 'An error occurred while fetching project data', 'message' => $e->getMessage()], 500); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.