| Total Complexity | 94 |
| Total Lines | 639 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AttendanceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AttendanceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | #[Route('/attendance')] |
||
| 41 | class AttendanceController extends AbstractController |
||
| 42 | { |
||
| 43 | public function __construct( |
||
| 44 | private readonly CAttendanceCalendarRepository $attendanceCalendarRepository, |
||
| 45 | private readonly EntityManagerInterface $em, |
||
| 46 | private readonly TranslatorInterface $translator |
||
| 47 | ) {} |
||
| 48 | |||
| 49 | #[Route('/full-data', name: 'chamilo_core_attendance_get_full_data', methods: ['GET'])] |
||
| 50 | public function getFullAttendanceData(Request $request): JsonResponse |
||
| 51 | { |
||
| 52 | $attendanceId = (int) $request->query->get('attendanceId', 0); |
||
| 53 | |||
| 54 | if (!$attendanceId) { |
||
| 55 | return $this->json(['error' => 'Attendance ID is required'], 400); |
||
| 56 | } |
||
| 57 | |||
| 58 | $data = $this->attendanceCalendarRepository->findAttendanceWithData($attendanceId); |
||
| 59 | |||
| 60 | return $this->json($data, 200); |
||
| 61 | } |
||
| 62 | |||
| 63 | #[Route('/{id}/users/context', name: 'chamilo_core_get_users_with_faults', methods: ['GET'])] |
||
| 64 | public function getUsersWithFaults( |
||
| 65 | int $id, |
||
| 66 | Request $request, |
||
| 67 | UserRepository $userRepository, |
||
| 68 | CAttendanceCalendarRepository $calendarRepository, |
||
| 69 | CAttendanceSheetRepository $sheetRepository |
||
| 70 | ): JsonResponse { |
||
| 71 | $courseId = (int) $request->query->get('courseId', 0); |
||
| 72 | $sessionId = $request->query->get('sessionId') ? (int) $request->query->get('sessionId') : null; |
||
| 73 | $groupId = $request->query->get('groupId') ? (int) $request->query->get('groupId') : null; |
||
| 74 | |||
| 75 | $attendance = $this->em->getRepository(CAttendance::class)->find($id); |
||
| 76 | if (!$attendance) { |
||
| 77 | return $this->json(['error' => 'Attendance not found'], 404); |
||
| 78 | } |
||
| 79 | |||
| 80 | $calendars = $attendance->getCalendars(); |
||
| 81 | $totalCalendars = \count($calendars); |
||
| 82 | |||
| 83 | $users = $userRepository->findUsersByContext($courseId, $sessionId, $groupId); |
||
| 84 | |||
| 85 | $formattedUsers = array_map(function ($user) use ($userRepository, $sheetRepository, $calendars) { |
||
| 86 | $absences = 0; |
||
| 87 | |||
| 88 | foreach ($calendars as $calendar) { |
||
| 89 | $sheet = $sheetRepository->findOneBy([ |
||
| 90 | 'user' => $user, |
||
| 91 | 'attendanceCalendar' => $calendar, |
||
| 92 | ]); |
||
| 93 | |||
| 94 | if (!$sheet || null === $sheet->getPresence()) { |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | if (1 !== $sheet->getPresence()) { |
||
| 99 | $absences++; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | $percentage = \count($calendars) > 0 ? round(($absences * 100) / \count($calendars)) : 0; |
||
| 104 | |||
| 105 | return [ |
||
| 106 | 'id' => $user->getId(), |
||
| 107 | 'firstname' => $user->getFirstname(), |
||
| 108 | 'lastname' => $user->getLastname(), |
||
| 109 | 'email' => $user->getEmail(), |
||
| 110 | 'username' => $user->getUsername(), |
||
| 111 | 'photo' => $userRepository->getUserPicture($user->getId()), |
||
| 112 | 'notAttended' => "$absences/".\count($calendars)." ({$percentage}%)", |
||
| 113 | ]; |
||
| 114 | }, $users); |
||
| 115 | |||
| 116 | return $this->json($formattedUsers, 200); |
||
| 117 | } |
||
| 118 | |||
| 119 | #[Route('/list_with_done_count', name: 'attendance_list_with_done_count', methods: ['GET'])] |
||
| 147 | } |
||
| 148 | |||
| 149 | #[Route('/{id}/export/pdf', name: 'attendance_export_pdf', methods: ['GET'])] |
||
| 150 | public function exportToPdf(int $id, Request $request): Response |
||
| 151 | { |
||
| 152 | $courseId = (int) $request->query->get('cid'); |
||
| 153 | $sessionId = ((int) $request->query->get('sid')) ?: null; |
||
| 154 | $groupId = ((int) $request->query->get('gid')) ?: null; |
||
| 155 | |||
| 156 | $attendance = $this->em->getRepository(CAttendance::class)->find($id); |
||
| 157 | if (!$attendance) { |
||
| 158 | throw $this->createNotFoundException('Attendance not found'); |
||
| 159 | } |
||
| 160 | |||
| 161 | $calendars = $attendance->getCalendars(); |
||
| 162 | $totalCalendars = \count($calendars); |
||
| 163 | |||
| 164 | $students = $this->em->getRepository(User::class)->findUsersByContext($courseId, $sessionId, $groupId); |
||
| 165 | $sheetRepo = $this->em->getRepository(CAttendanceSheet::class); |
||
| 166 | |||
| 167 | $course = $this->em->getRepository(Course::class)->find($courseId); |
||
| 168 | $teacher = null; |
||
| 169 | |||
| 170 | if ($sessionId) { |
||
|
|
|||
| 171 | $session = $this->em->getRepository(Session::class)->find($sessionId); |
||
| 172 | $rel = $session?->getCourseCoachesSubscriptions() |
||
| 173 | ->filter(fn ($rel) => $rel->getCourse()?->getId() === $courseId) |
||
| 174 | ->first() |
||
| 175 | ; |
||
| 176 | |||
| 177 | $teacher = $rel instanceof SessionRelCourseRelUser |
||
| 178 | ? $rel->getUser()?->getFullName() |
||
| 179 | : null; |
||
| 180 | } else { |
||
| 181 | $rel = $course?->getTeachersSubscriptions()?->first(); |
||
| 182 | |||
| 183 | $teacher = $rel instanceof CourseRelUser |
||
| 184 | ? $rel->getUser()?->getFullName() |
||
| 185 | : null; |
||
| 186 | } |
||
| 187 | |||
| 188 | // Header |
||
| 189 | $dataTable = []; |
||
| 190 | $header = ['#', 'Last Name', 'First Name', 'Not Attended']; |
||
| 191 | foreach ($calendars as $calendar) { |
||
| 192 | $header[] = $calendar->getDateTime()->format('d/m H:i'); |
||
| 193 | } |
||
| 194 | $dataTable[] = $header; |
||
| 195 | |||
| 196 | // Rows |
||
| 197 | $count = 1; |
||
| 198 | $stateLabels = CAttendanceSheet::getPresenceLabels(); |
||
| 199 | |||
| 200 | foreach ($students as $student) { |
||
| 201 | $row = [ |
||
| 202 | $count++, |
||
| 203 | $student->getLastname(), |
||
| 204 | $student->getFirstname(), |
||
| 205 | '', |
||
| 206 | ]; |
||
| 207 | |||
| 208 | $absences = 0; |
||
| 209 | foreach ($calendars as $calendar) { |
||
| 210 | $sheetEntity = $sheetRepo->findOneBy([ |
||
| 211 | 'user' => $student, |
||
| 212 | 'attendanceCalendar' => $calendar, |
||
| 213 | ]); |
||
| 214 | |||
| 215 | if (!$sheetEntity || null === $sheetEntity->getPresence()) { |
||
| 216 | $row[] = ''; |
||
| 217 | |||
| 218 | continue; |
||
| 219 | } |
||
| 220 | |||
| 221 | $presence = $sheetEntity->getPresence(); |
||
| 222 | $row[] = $stateLabels[$presence] ?? 'NP'; |
||
| 223 | |||
| 224 | if (CAttendanceSheet::ABSENT === $presence) { |
||
| 225 | $absences++; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | $percentage = $totalCalendars > 0 ? round(($absences * 100) / $totalCalendars) : 0; |
||
| 230 | $row[3] = "$absences/$totalCalendars ($percentage%)"; |
||
| 231 | $dataTable[] = $row; |
||
| 232 | } |
||
| 233 | |||
| 234 | // Render HTML |
||
| 235 | $html = ' |
||
| 236 | <style> |
||
| 237 | body { font-family: sans-serif; font-size: 12px; } |
||
| 238 | h2 { text-align: center; margin-bottom: 5px; } |
||
| 239 | table.meta { margin: 0 auto 10px auto; width: 80%; } |
||
| 240 | table.meta td { padding: 2px 5px; } |
||
| 241 | table.attendance { border-collapse: collapse; width: 100%; } |
||
| 242 | .attendance th, .attendance td { border: 1px solid #000; padding: 4px; text-align: center; } |
||
| 243 | .np { color: red; font-weight: bold; } |
||
| 244 | </style> |
||
| 245 | |||
| 246 | <h2>'.htmlspecialchars($attendance->getTitle()).'</h2> |
||
| 247 | |||
| 248 | <table class="meta"> |
||
| 249 | <tr><td><strong>Trainer:</strong></td><td>'.htmlspecialchars($teacher ?? '-').'</td></tr> |
||
| 250 | <tr><td><strong>Course:</strong></td><td>'.htmlspecialchars($course?->getTitleAndCode() ?? '-').'</td></tr> |
||
| 251 | <tr><td><strong>Date:</strong></td><td>'.date('F d, Y \a\t h:i A').'</td></tr> |
||
| 252 | </table> |
||
| 253 | |||
| 254 | <table class="attendance"> |
||
| 255 | <tr>'; |
||
| 256 | foreach ($dataTable[0] as $cell) { |
||
| 257 | $html .= '<th>'.htmlspecialchars((string) $cell).'</th>'; |
||
| 258 | } |
||
| 259 | $html .= '</tr>'; |
||
| 260 | |||
| 261 | foreach (\array_slice($dataTable, 1) as $row) { |
||
| 262 | $html .= '<tr>'; |
||
| 263 | foreach ($row as $cell) { |
||
| 264 | $class = 'NP' === $cell ? ' class="np"' : ''; |
||
| 265 | $html .= "<td$class>".htmlspecialchars((string) $cell).'</td>'; |
||
| 266 | } |
||
| 267 | $html .= '</tr>'; |
||
| 268 | } |
||
| 269 | |||
| 270 | $html .= '</table>'; |
||
| 271 | |||
| 272 | try { |
||
| 273 | $mpdf = new Mpdf([ |
||
| 274 | 'orientation' => 'L', |
||
| 275 | 'tempDir' => api_get_path(SYS_ARCHIVE_PATH).'mpdf/', |
||
| 276 | ]); |
||
| 277 | $mpdf->WriteHTML($html); |
||
| 278 | |||
| 279 | return new Response( |
||
| 280 | $mpdf->Output('', Destination::INLINE), |
||
| 281 | 200, |
||
| 282 | [ |
||
| 283 | 'Content-Type' => 'application/pdf', |
||
| 284 | 'Content-Disposition' => 'attachment; filename="attendance-'.$id.'.pdf"', |
||
| 285 | ] |
||
| 286 | ); |
||
| 287 | } catch (MpdfException $e) { |
||
| 288 | throw new RuntimeException('Failed to generate PDF: '.$e->getMessage(), 500, $e); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | #[Route('/{id}/export/xls', name: 'attendance_export_xls', methods: ['GET'])] |
||
| 293 | public function exportToXls(int $id, Request $request): Response |
||
| 294 | { |
||
| 295 | $courseId = (int) $request->query->get('cid'); |
||
| 296 | $sessionId = $request->query->get('sid') ? (int) $request->query->get('sid') : null; |
||
| 297 | $groupId = $request->query->get('gid') ? (int) $request->query->get('gid') : null; |
||
| 298 | |||
| 299 | $attendance = $this->em->getRepository(CAttendance::class)->find($id); |
||
| 300 | if (!$attendance) { |
||
| 301 | throw $this->createNotFoundException('Attendance not found'); |
||
| 302 | } |
||
| 303 | |||
| 304 | $calendars = $attendance->getCalendars(); |
||
| 305 | $totalCalendars = \count($calendars); |
||
| 306 | $students = $this->em->getRepository(User::class)->findUsersByContext($courseId, $sessionId, $groupId); |
||
| 307 | $sheetRepo = $this->em->getRepository(CAttendanceSheet::class); |
||
| 308 | |||
| 309 | $stateLabels = CAttendanceSheet::getPresenceLabels(); |
||
| 310 | |||
| 311 | $spreadsheet = new Spreadsheet(); |
||
| 312 | $sheet = $spreadsheet->getActiveSheet(); |
||
| 313 | $sheet->setTitle('Attendance'); |
||
| 314 | |||
| 315 | // Header |
||
| 316 | $headers = ['#', 'Last Name', 'First Name', 'Not Attended']; |
||
| 317 | foreach ($calendars as $calendar) { |
||
| 318 | $headers[] = $calendar->getDateTime()->format('d/m H:i'); |
||
| 319 | } |
||
| 320 | $sheet->fromArray($headers, null, 'A1'); |
||
| 321 | |||
| 322 | // Rows |
||
| 323 | $rowNumber = 2; |
||
| 324 | $count = 1; |
||
| 325 | foreach ($students as $student) { |
||
| 326 | $row = [$count++, $student->getLastname(), $student->getFirstname()]; |
||
| 327 | $absences = 0; |
||
| 328 | |||
| 329 | foreach ($calendars as $calendar) { |
||
| 330 | $sheetEntity = $sheetRepo->findOneBy([ |
||
| 331 | 'user' => $student, |
||
| 332 | 'attendanceCalendar' => $calendar, |
||
| 333 | ]); |
||
| 334 | |||
| 335 | if (!$sheetEntity || null === $sheetEntity->getPresence()) { |
||
| 336 | $row[] = ''; |
||
| 337 | |||
| 338 | continue; |
||
| 339 | } |
||
| 340 | |||
| 341 | $presence = $sheetEntity->getPresence(); |
||
| 342 | $row[] = $stateLabels[$presence] ?? 'NP'; |
||
| 343 | |||
| 344 | if (CAttendanceSheet::ABSENT === $presence) { |
||
| 345 | $absences++; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | $percentage = $totalCalendars > 0 ? round(($absences * 100) / $totalCalendars) : 0; |
||
| 350 | array_splice($row, 3, 0, "$absences/$totalCalendars ($percentage%)"); |
||
| 351 | |||
| 352 | $sheet->fromArray($row, null, 'A'.$rowNumber++); |
||
| 353 | } |
||
| 354 | |||
| 355 | // Output |
||
| 356 | $writer = new Xls($spreadsheet); |
||
| 357 | $response = new StreamedResponse(fn () => $writer->save('php://output')); |
||
| 358 | $disposition = $response->headers->makeDisposition( |
||
| 359 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
| 360 | "attendance-$id.xls" |
||
| 361 | ); |
||
| 362 | |||
| 363 | $response->headers->set('Content-Type', 'application/vnd.ms-excel'); |
||
| 364 | $response->headers->set('Content-Disposition', $disposition); |
||
| 365 | |||
| 366 | return $response; |
||
| 367 | } |
||
| 368 | |||
| 369 | #[Route('/{id}/qrcode', name: 'attendance_qrcode', methods: ['GET'])] |
||
| 404 | } |
||
| 405 | |||
| 406 | #[Route('/sheet/save', name: 'chamilo_core_attendance_sheet_save', methods: ['POST'])] |
||
| 407 | public function saveAttendanceSheet( |
||
| 408 | Request $request, |
||
| 524 | } |
||
| 525 | } |
||
| 526 | |||
| 527 | #[Route('/{id}/student-dates', name: 'attendance_student_dates', methods: ['GET'])] |
||
| 582 | ]); |
||
| 583 | } |
||
| 584 | |||
| 585 | #[Route('/{attendanceId}/date/{calendarId}/sheet', name: 'attendance_date_sheet', methods: ['GET'])] |
||
| 631 | ]); |
||
| 632 | } |
||
| 633 | |||
| 634 | private function updateAttendanceResults(CAttendance $attendance): void |
||
| 665 | } |
||
| 666 | } |
||
| 667 | |||
| 668 | private function saveAttendanceLog(CAttendance $attendance, string $lasteditType, CAttendanceCalendar $calendar): void |
||
| 681 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: