90f9857b06
API CI/CD / Validate (composer + pint) (push) Successful in 3m7s
API CI/CD / Test (PHPUnit) (push) Failing after 5m46s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
69 lines
2.5 KiB
PHP
69 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\ClassProgress;
|
|
|
|
use App\Http\Resources\Admin\Progress\ProgressGroupResource;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
|
|
|
class ClassProgressGroupCollection extends ResourceCollection
|
|
{
|
|
public function toArray($request): array
|
|
{
|
|
return $this->collection->toArray();
|
|
}
|
|
|
|
public static function fromPaginator(LengthAwarePaginator $paginator): array
|
|
{
|
|
$groups = [];
|
|
foreach ($paginator->items() as $report) {
|
|
$weekStart = $report->week_start?->format('Y-m-d') ?? '';
|
|
$sectionId = (int) ($report->class_section_id ?? 0);
|
|
if ($weekStart === '' || $sectionId <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$key = $sectionId.'|'.$weekStart;
|
|
|
|
if (! isset($groups[$key])) {
|
|
$reportId = (int) $report->id;
|
|
$groups[$key] = [
|
|
'id' => $reportId,
|
|
'report_id' => $reportId,
|
|
'view_url' => url('/api/v1/class-progress/'.$reportId),
|
|
'parent_view_url' => url('/api/v1/parents/progress/'.$reportId),
|
|
'week_start' => $weekStart,
|
|
'week_end' => $report->week_end?->format('Y-m-d'),
|
|
'class_section_id' => $sectionId,
|
|
'class_section_name' => $report->classSection?->class_section_name ?? '',
|
|
'reports' => [],
|
|
'weekly_reports' => [],
|
|
'reports_array' => [],
|
|
'report_list' => [],
|
|
'subjects' => [],
|
|
];
|
|
}
|
|
|
|
$reportArray = (new ClassProgressReportResource($report))->toArray(request());
|
|
$groups[$key]['reports'][$report->subject] = $reportArray;
|
|
$groups[$key]['weekly_reports'][] = $reportArray;
|
|
$groups[$key]['reports_array'][] = $reportArray;
|
|
$groups[$key]['report_list'][] = $reportArray;
|
|
$groups[$key]['subjects'][] = $report->subject;
|
|
}
|
|
|
|
$items = ProgressGroupResource::collection(collect(array_values($groups)));
|
|
|
|
return [
|
|
'items' => $items,
|
|
'groups' => $items,
|
|
'pagination' => [
|
|
'current_page' => $paginator->currentPage(),
|
|
'per_page' => $paginator->perPage(),
|
|
'total' => $paginator->total(),
|
|
'last_page' => $paginator->lastPage(),
|
|
],
|
|
];
|
|
}
|
|
}
|