| Conditions | 22 |
| Paths | 3741 |
| Total Lines | 117 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 91 | #[Route('/sheet/save', name: 'chamilo_core_attendance_sheet_save', methods: ['POST'])] |
||
| 92 | public function saveAttendanceSheet( |
||
| 93 | Request $request, |
||
| 94 | UserRepository $userRepository, |
||
| 95 | CAttendanceSheetRepository $sheetRepository |
||
| 96 | ): JsonResponse { |
||
| 97 | $data = json_decode($request->getContent(), true); |
||
| 98 | |||
| 99 | if (empty($data['attendanceData']) || empty($data['courseId'])) { |
||
| 100 | return $this->json(['error' => 'Missing required parameters'], 400); |
||
| 101 | } |
||
| 102 | |||
| 103 | $attendanceData = $data['attendanceData']; |
||
| 104 | $courseId = (int) $data['courseId']; |
||
| 105 | $sessionId = isset($data['sessionId']) ? (int) $data['sessionId'] : null; |
||
| 106 | $groupId = isset($data['groupId']) ? (int) $data['groupId'] : null; |
||
| 107 | |||
| 108 | $usersInCourse = $userRepository->findUsersByContext($courseId, $sessionId, $groupId); |
||
| 109 | $userIdsInCourse = array_map(fn(User $user) => $user->getId(), $usersInCourse); |
||
| 110 | |||
| 111 | $affectedRows = 0; |
||
| 112 | |||
| 113 | try { |
||
| 114 | foreach ($attendanceData as $entry) { |
||
| 115 | $userId = (int) $entry['userId']; |
||
| 116 | $calendarId = (int) $entry['calendarId']; |
||
| 117 | $presence = array_key_exists('presence', $entry) ? $entry['presence'] : null; |
||
| 118 | $signature = $entry['signature'] ?? null; |
||
| 119 | $comment = $entry['comment'] ?? null; |
||
| 120 | |||
| 121 | $calendar = $this->attendanceCalendarRepository->find($calendarId); |
||
| 122 | if (!$calendar) { |
||
| 123 | return $this->json(['error' => "Attendance calendar with ID $calendarId not found"], 404); |
||
| 124 | } |
||
| 125 | |||
| 126 | $user = $this->em->getRepository(User::class)->find($userId); |
||
| 127 | if (!$user) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | |||
| 131 | $sheet = $sheetRepository->findOneBy([ |
||
| 132 | 'user' => $user, |
||
| 133 | 'attendanceCalendar' => $calendar, |
||
| 134 | ]); |
||
| 135 | |||
| 136 | if ($sheet && $presence === null) { |
||
| 137 | $this->em->remove($sheet); |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | |||
| 141 | if (!$sheet && $presence === null) { |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (!$sheet) { |
||
| 146 | $sheet = new CAttendanceSheet(); |
||
| 147 | } |
||
| 148 | |||
| 149 | $sheet->setUser($user) |
||
| 150 | ->setAttendanceCalendar($calendar) |
||
| 151 | ->setPresence($presence) |
||
| 152 | ->setSignature($signature); |
||
| 153 | |||
| 154 | $this->em->persist($sheet); |
||
| 155 | |||
| 156 | $this->em->flush(); |
||
| 157 | |||
| 158 | if ($comment !== null) { |
||
| 159 | $existingComment = $this->em->getRepository(CAttendanceResultComment::class)->findOneBy([ |
||
| 160 | 'attendanceSheetId' => $sheet->getIid(), |
||
| 161 | 'userId' => $user->getId(), |
||
| 162 | ]); |
||
| 163 | |||
| 164 | if (!$existingComment) { |
||
| 165 | $existingComment = new CAttendanceResultComment(); |
||
| 166 | $existingComment->setAttendanceSheetId($sheet->getIid()); |
||
| 167 | $existingComment->setUserId($user->getId()); |
||
| 168 | $existingComment->setAuthorUserId($this->getUser()->getId()); |
||
| 169 | } |
||
| 170 | |||
| 171 | $existingComment->setComment($comment); |
||
| 172 | $existingComment->setUpdatedAt(new \DateTime()); |
||
| 173 | |||
| 174 | $this->em->persist($existingComment); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | $calendarIds = array_unique(array_column($attendanceData, 'calendarId')); |
||
| 179 | foreach ($calendarIds as $calendarId) { |
||
| 180 | $calendar = $this->attendanceCalendarRepository->find($calendarId); |
||
| 181 | if ($calendar && !$calendar->getDoneAttendance()) { |
||
| 182 | $calendar->setDoneAttendance(true); |
||
| 183 | $this->em->persist($calendar); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $calendars = $this->attendanceCalendarRepository->findBy(['iid' => $calendarIds]); |
||
| 188 | $attendance = $calendars[0]->getAttendance(); |
||
| 189 | $this->updateAttendanceResults($attendance); |
||
| 190 | |||
| 191 | $lasteditType = $calendars[0]->getDoneAttendance() |
||
| 192 | ? 'UPDATED_ATTENDANCE_LOG_TYPE' |
||
| 193 | : 'DONE_ATTENDANCE_LOG_TYPE'; |
||
| 194 | |||
| 195 | foreach ($calendars as $calendar) { |
||
| 196 | $this->saveAttendanceLog($attendance, $lasteditType, $calendar); |
||
| 197 | } |
||
| 198 | |||
| 199 | $this->em->flush(); |
||
| 200 | |||
| 201 | return $this->json([ |
||
| 202 | 'message' => $this->translator->trans('Attendance data and comments saved successfully'), |
||
| 203 | 'affectedRows' => $affectedRows, |
||
| 204 | ]); |
||
| 205 | |||
| 206 | } catch (\Exception $e) { |
||
| 207 | return $this->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); |
||
| 208 | } |
||
| 257 |
This check looks for imports that have been defined, but are not used in the scope.