add report card logic
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Reports;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Requests\Reports\ReportCards\ReportCardAcknowledgementRequest;
|
||||
use App\Http\Requests\Reports\ReportCards\ReportCardCompletenessRequest;
|
||||
use App\Http\Requests\Reports\ReportCards\ReportCardMetaRequest;
|
||||
use App\Http\Requests\Reports\ReportCards\ReportCardPdfRequest;
|
||||
use App\Http\Resources\Reports\ReportCards\ReportCardAcknowledgementResource;
|
||||
use App\Http\Resources\Reports\ReportCards\ReportCardClassSectionResource;
|
||||
use App\Http\Resources\Reports\ReportCards\ReportCardCompletenessStudentResource;
|
||||
use App\Http\Resources\Reports\ReportCards\ReportCardStudentMetaResource;
|
||||
use App\Models\Configuration;
|
||||
use App\Services\Reports\ReportCards\ReportCardService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class ReportCardsController extends BaseApiController
|
||||
{
|
||||
public function __construct(private ReportCardService $service)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function meta(ReportCardMetaRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
|
||||
$result = $this->service->meta(
|
||||
$payload['school_year'] ?? null,
|
||||
$payload['semester'] ?? null
|
||||
);
|
||||
|
||||
return $this->success([
|
||||
'schoolYears' => $result['schoolYears'] ?? [],
|
||||
'selectedYear' => $result['selectedYear'] ?? null,
|
||||
'semesters' => $result['semesters'] ?? [],
|
||||
'selectedSemester' => $result['selectedSemester'] ?? null,
|
||||
'students' => ReportCardStudentMetaResource::collection($result['students'] ?? []),
|
||||
'classSections' => ReportCardClassSectionResource::collection($result['classSections'] ?? []),
|
||||
]);
|
||||
}
|
||||
|
||||
public function completeness(ReportCardCompletenessRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
$schoolYear = $this->resolveSchoolYear($payload['school_year'] ?? null);
|
||||
$semester = $this->resolveSemester($payload['semester'] ?? null);
|
||||
$classSectionId = (int) ($payload['class_section_id'] ?? 0);
|
||||
|
||||
$result = $this->service->completeness($schoolYear, $semester, $classSectionId);
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return $this->error($result['message'] ?? 'Unable to generate completeness report.', Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'summary' => $result['summary'] ?? null,
|
||||
'students' => ReportCardCompletenessStudentResource::collection($result['students'] ?? []),
|
||||
'class_section_name' => $result['class_section_name'] ?? null,
|
||||
'class_section_id' => $result['class_section_id'] ?? null,
|
||||
'school_year' => $result['school_year'] ?? $schoolYear,
|
||||
'semester' => $result['semester'] ?? $semester,
|
||||
'roster_semester' => $result['roster_semester'] ?? null,
|
||||
'used_fallback' => (bool) ($result['used_fallback'] ?? false),
|
||||
], $result['message'] ?? 'Success');
|
||||
}
|
||||
|
||||
public function acknowledgement(ReportCardAcknowledgementRequest $request): JsonResponse
|
||||
{
|
||||
$payload = $request->validated();
|
||||
$studentId = (int) ($payload['student_id'] ?? 0);
|
||||
$schoolYear = $this->resolveSchoolYear($payload['school_year'] ?? null);
|
||||
$semester = $this->resolveSemester($payload['semester'] ?? null);
|
||||
|
||||
$result = $this->service->acknowledgement($studentId, $schoolYear, $semester);
|
||||
|
||||
return $this->success(new ReportCardAcknowledgementResource($result));
|
||||
}
|
||||
|
||||
public function studentReport(ReportCardPdfRequest $request, int $studentId)
|
||||
{
|
||||
try {
|
||||
$result = $this->service->generateSingleReport($studentId, $request->validated());
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Report card PDF generation failed: ' . $e->getMessage());
|
||||
return $this->error('Failed to generate report card.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return $this->error($result['message'] ?? 'Unable to generate report card.', Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
$disposition = ($request->boolean('download') ? 'attachment' : 'inline');
|
||||
|
||||
return response($result['pdf'], 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => $disposition . '; filename="' . ($result['filename'] ?? 'ReportCard.pdf') . '"',
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
public function classReport(ReportCardPdfRequest $request, int $classSectionId)
|
||||
{
|
||||
try {
|
||||
$result = $this->service->generateClassReport($classSectionId, $request->validated());
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Class report card PDF generation failed: ' . $e->getMessage());
|
||||
return $this->error('Failed to generate class report cards.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
return $this->error($result['message'] ?? 'Unable to generate class report cards.', Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
$disposition = ($request->boolean('download') ? 'attachment' : 'inline');
|
||||
|
||||
return response($result['pdf'], 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => $disposition . '; filename="' . ($result['filename'] ?? 'ClassReportCards.pdf') . '"',
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
private function resolveSchoolYear(?string $schoolYear): string
|
||||
{
|
||||
$value = trim((string) ($schoolYear ?? ''));
|
||||
if ($value !== '') {
|
||||
return $value;
|
||||
}
|
||||
return trim((string) (Configuration::getConfig('school_year') ?? ''));
|
||||
}
|
||||
|
||||
private function resolveSemester(?string $semester): string
|
||||
{
|
||||
$value = trim((string) ($semester ?? ''));
|
||||
if ($value !== '') {
|
||||
return $value;
|
||||
}
|
||||
return trim((string) (Configuration::getConfig('semester') ?? ''));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Reports\ReportCards;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class ReportCardAcknowledgementRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'student_id' => ['required', 'integer', 'min:1'],
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Reports\ReportCards;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class ReportCardCompletenessRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
'class_section_id' => ['required', 'integer', 'min:1'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Reports\ReportCards;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class ReportCardMetaRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Reports\ReportCards;
|
||||
|
||||
use App\Http\Requests\ApiFormRequest;
|
||||
|
||||
class ReportCardPdfRequest extends ApiFormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return auth()->check();
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'school_year' => ['nullable', 'string', 'max:20'],
|
||||
'semester' => ['nullable', 'string', 'max:20'],
|
||||
'report_date' => ['nullable', 'date'],
|
||||
'download' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Reports\ReportCards;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ReportCardAcknowledgementResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'student_id' => (int) ($this['student_id'] ?? 0),
|
||||
'school_year' => (string) ($this['school_year'] ?? ''),
|
||||
'semester' => (string) ($this['semester'] ?? ''),
|
||||
'viewed_at' => $this['viewed_at'] ?? null,
|
||||
'signed_at' => $this['signed_at'] ?? null,
|
||||
'signed_name' => $this['signed_name'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Reports\ReportCards;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ReportCardClassSectionResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) ($this['id'] ?? 0),
|
||||
'class_section_id' => (int) ($this['class_section_id'] ?? ($this['id'] ?? 0)),
|
||||
'class_section_name' => (string) ($this['class_section_name'] ?? ''),
|
||||
'school_year' => (string) ($this['school_year'] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Reports\ReportCards;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ReportCardCompletenessStudentResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) ($this['id'] ?? 0),
|
||||
'name' => (string) ($this['name'] ?? ''),
|
||||
'missing' => array_values($this['missing'] ?? []),
|
||||
'warnings' => array_values($this['warnings'] ?? []),
|
||||
'complete' => (bool) ($this['complete'] ?? false),
|
||||
'viewed_at' => $this['viewed_at'] ?? null,
|
||||
'signed_at' => $this['signed_at'] ?? null,
|
||||
'signed_name' => $this['signed_name'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Reports\ReportCards;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ReportCardStudentMetaResource extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) ($this['id'] ?? 0),
|
||||
'firstname' => (string) ($this['firstname'] ?? ''),
|
||||
'lastname' => (string) ($this['lastname'] ?? ''),
|
||||
'class_section_name' => (string) ($this['class_section_name'] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,120 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\View;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\AttendanceRecordModel;
|
||||
use App\Models\BadgePrintLogModel;
|
||||
use App\Models\ClassSectionModel;
|
||||
use App\Models\ConfigurationModel;
|
||||
use App\Models\ScoreCommentModel;
|
||||
use App\Models\SemesterScoreModel;
|
||||
use App\Models\StaffModel;
|
||||
use App\Models\StudentClassModel;
|
||||
use App\Models\StudentModel;
|
||||
use App\Models\TeacherClassModel;
|
||||
use App\Models\UserModel;
|
||||
|
||||
require_once APPPATH . 'ThirdParty/fpdf/fpdf.php';
|
||||
|
||||
class PrintablesBaseController extends BaseController
|
||||
{
|
||||
protected $userModel;
|
||||
protected $studentModel;
|
||||
protected $db;
|
||||
protected $classSectionModel;
|
||||
protected $configModel;
|
||||
protected $schoolYear;
|
||||
protected $semester;
|
||||
protected $staffModel;
|
||||
protected $teacherClassModel;
|
||||
protected $semesterScoreModel;
|
||||
protected $studentClassModel;
|
||||
protected $stickerWidth;
|
||||
protected $stickerHeight;
|
||||
protected $pageW;
|
||||
protected $pageH;
|
||||
protected $marginL;
|
||||
protected $marginT;
|
||||
protected $gapX;
|
||||
protected $gapY;
|
||||
protected $badgePrintLogModel;
|
||||
protected $scoreCommentModel;
|
||||
protected $attendanceRecordModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = \Config\Database::connect();
|
||||
$this->userModel = new UserModel();
|
||||
$this->studentModel = new StudentModel();
|
||||
$this->classSectionModel = new ClassSectionModel();
|
||||
$this->configModel = new ConfigurationModel();
|
||||
$this->semesterScoreModel = new SemesterScoreModel();
|
||||
$this->studentClassModel = new StudentClassModel();
|
||||
$this->teacherClassModel = new TeacherClassModel();
|
||||
$this->staffModel = new StaffModel();
|
||||
$this->badgePrintLogModel = new BadgePrintLogModel();
|
||||
$this->scoreCommentModel = new ScoreCommentModel();
|
||||
$this->attendanceRecordModel = new AttendanceRecordModel();
|
||||
|
||||
$this->schoolYear = $this->configModel->getConfig('school_year');
|
||||
$this->semester = $this->configModel->getConfig('semester');
|
||||
$this->stickerWidth = $this->configModel->getConfig('stickerWidth');
|
||||
$this->stickerHeight = $this->configModel->getConfig('stickerHeight');
|
||||
$this->pageW = $this->configModel->getConfig('pageWidth');
|
||||
$this->pageH = $this->configModel->getConfig('pageHeight');
|
||||
$this->marginL = $this->configModel->getConfig('leftMargin');
|
||||
$this->marginT = $this->configModel->getConfig('topMargin');
|
||||
$this->gapX = $this->configModel->getConfig('horizontalGap');
|
||||
$this->gapY = $this->configModel->getConfig('verticalGap');
|
||||
}
|
||||
|
||||
protected function firstExisting(array $paths)
|
||||
{
|
||||
foreach ($paths as $p) {
|
||||
if (is_string($p) && file_exists($p)) {
|
||||
return $p;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function resolveClassName($db, $classId)
|
||||
{
|
||||
$tables = [
|
||||
['classSection', 'class_section_id'],
|
||||
['classSection', 'id'],
|
||||
['class_sections', 'class_section_id'],
|
||||
['class_sections', 'id'],
|
||||
];
|
||||
|
||||
foreach ($tables as [$table, $pk]) {
|
||||
$result = $db->table($table)
|
||||
->select('class_section_name')
|
||||
->where($pk, $classId)
|
||||
->get()
|
||||
->getRowArray();
|
||||
|
||||
if ($result) {
|
||||
return $result['class_section_name'] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function fetchStudentsByClass(int $sectionId, ?string $schoolYear)
|
||||
{
|
||||
$b = $this->studentClassModel
|
||||
->select('s.id, s.firstname, s.lastname, s.registration_grade, s.gender')
|
||||
->join('students s', 's.id = student_class.student_id', 'inner')
|
||||
->where('student_class.class_section_id', $sectionId)
|
||||
->orderBy('s.lastname', 'ASC');
|
||||
|
||||
if (!empty($schoolYear)) {
|
||||
$b->where('student_class.school_year', $schoolYear);
|
||||
}
|
||||
|
||||
return $b->findAll();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user