report = model(ParentAttendanceReport::class); $this->student = model(Student::class); $this->config = model(Configuration::class); $this->schoolYear = (string) ($this->config->getConfig('school_year') ?? ''); } public function submit() { $user = $this->getCurrentUser(); if (!$user) { return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED); } $payload = $this->payloadData(); if (empty($payload)) { return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST); } $rules = [ 'student_ids' => 'required|is_array', 'date' => 'required|date_format:Y-m-d', 'type' => 'required|in:absent,late,early_dismissal', ]; $errors = $this->validateRequest($payload, $rules); if (!empty($errors)) { return $this->respondValidationError($errors); } $studentIds = (array) $payload['student_ids']; $date = $payload['date']; $type = $payload['type']; $arrivalTime = $payload['arrival_time'] ?? null; $dismissTime = $payload['dismiss_time'] ?? null; $reason = $payload['reason'] ?? null; $reports = []; try { foreach ($studentIds as $studentId) { $reportData = [ 'parent_id' => (int) $user->id, 'student_id' => (int) $studentId, 'report_date' => $date, 'type' => $type, 'arrival_time'=> $arrivalTime, 'dismiss_time'=> $dismissTime, 'reason' => $reason, 'school_year' => $this->schoolYear, ]; $report = $this->report->create($reportData); $reports[] = $report->toArray(); } return $this->success($reports, 'Attendance reports submitted successfully', Response::HTTP_CREATED); } catch (\Throwable $e) { log_message('error', 'Attendance report submission error: ' . $e->getMessage()); return $this->respondError('Failed to submit attendance report', Response::HTTP_INTERNAL_SERVER_ERROR); } } public function index() { $user = $this->getCurrentUser(); if (!$user) { return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED); } $page = max(1, (int) ($this->request->getGet('page') ?? 1)); $perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20))); $schoolYear = $this->request->getGet('school_year') ?? $this->schoolYear; $query = $this->report->newQuery() ->where('parent_id', (int) $user->id) ->where('school_year', $schoolYear) ->orderBy('report_date', 'DESC'); $result = $this->paginate($query, $page, $perPage); return $this->success($result, 'Attendance reports retrieved successfully'); } public function update($id = null) { $user = $this->getCurrentUser(); if (!$user) { return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED); } $report = $this->report->find($id); if (!$report) { return $this->error('Attendance report not found', Response::HTTP_NOT_FOUND); } if ((int) ($report['parent_id'] ?? 0) !== (int) $user->id) { return $this->error('Access denied', Response::HTTP_FORBIDDEN); } $payload = $this->payloadData(); if (empty($payload)) { return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST); } $allowedFields = ['type', 'arrival_time', 'dismiss_time', 'reason']; $updateData = []; foreach ($allowedFields as $field) { if (array_key_exists($field, $payload)) { $updateData[$field] = $payload[$field]; } } if (empty($updateData)) { return $this->error('No valid fields to update', Response::HTTP_BAD_REQUEST); } try { $this->report->update($id, $updateData); $updatedReport = $this->report->find($id); return $this->success($updatedReport, 'Attendance report updated successfully'); } catch (\Throwable $e) { log_message('error', 'Attendance report update error: ' . $e->getMessage()); return $this->respondError('Failed to update attendance report', Response::HTTP_INTERNAL_SERVER_ERROR); } } }