Conditions | 17 |
Paths | 2029 |
Total Lines | 104 |
Code Lines | 66 |
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 |
||
89 | #[Route('/sheet/save', name: 'chamilo_core_attendance_sheet_save', methods: ['POST'])] |
||
90 | public function saveAttendanceSheet( |
||
91 | Request $request, |
||
92 | UserRepository $userRepository, |
||
93 | CAttendanceSheetRepository $sheetRepository |
||
94 | ): JsonResponse { |
||
95 | $data = json_decode($request->getContent(), true); |
||
96 | |||
97 | if (empty($data['attendanceData']) || empty($data['courseId'])) { |
||
98 | return $this->json(['error' => 'Missing required parameters'], 400); |
||
99 | } |
||
100 | |||
101 | $attendanceData = $data['attendanceData']; |
||
102 | $courseId = (int) $data['courseId']; |
||
103 | $sessionId = isset($data['sessionId']) ? (int) $data['sessionId'] : null; |
||
104 | $groupId = isset($data['groupId']) ? (int) $data['groupId'] : null; |
||
105 | |||
106 | $usersInCourse = $userRepository->findUsersByContext($courseId, $sessionId, $groupId); |
||
107 | $userIdsInCourse = array_map(fn(User $user) => $user->getId(), $usersInCourse); |
||
108 | |||
109 | $affectedRows = 0; |
||
110 | |||
111 | try { |
||
112 | foreach ($attendanceData as $entry) { |
||
113 | $userId = (int) $entry['userId']; |
||
114 | $calendarId = (int) $entry['calendarId']; |
||
115 | $presence = isset($entry['presence']) ? (int)$entry['presence'] : null; |
||
116 | $signature = $entry['signature'] ?? null; |
||
117 | $comment = $entry['comment'] ?? null; |
||
118 | |||
119 | $calendar = $this->attendanceCalendarRepository->find($calendarId); |
||
120 | if (!$calendar) { |
||
121 | return $this->json(['error' => "Attendance calendar with ID $calendarId not found"], 404); |
||
122 | } |
||
123 | |||
124 | $user = $this->em->getRepository(User::class)->find($userId); |
||
125 | if (!$user) { |
||
126 | continue; |
||
127 | } |
||
128 | |||
129 | $sheet = $sheetRepository->findOneBy([ |
||
130 | 'user' => $user, |
||
131 | 'attendanceCalendar' => $calendar, |
||
132 | ]) ?? new CAttendanceSheet(); |
||
133 | |||
134 | $sheet->setUser($user) |
||
135 | ->setAttendanceCalendar($calendar) |
||
136 | ->setPresence($presence) |
||
137 | ->setSignature($signature); |
||
138 | |||
139 | $this->em->persist($sheet); |
||
140 | |||
141 | $this->em->flush(); |
||
142 | |||
143 | if ($comment !== null) { |
||
144 | $existingComment = $this->em->getRepository(CAttendanceResultComment::class)->findOneBy([ |
||
145 | 'attendanceSheetId' => $sheet->getIid(), |
||
146 | 'userId' => $user->getId(), |
||
147 | ]); |
||
148 | |||
149 | if (!$existingComment) { |
||
150 | $existingComment = new CAttendanceResultComment(); |
||
151 | $existingComment->setAttendanceSheetId($sheet->getIid()); |
||
152 | $existingComment->setUserId($user->getId()); |
||
153 | $existingComment->setAuthorUserId($this->getUser()->getId()); |
||
154 | } |
||
155 | |||
156 | $existingComment->setComment($comment); |
||
157 | $existingComment->setUpdatedAt(new \DateTime()); |
||
158 | |||
159 | $this->em->persist($existingComment); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | $calendarIds = array_unique(array_column($attendanceData, 'calendarId')); |
||
164 | foreach ($calendarIds as $calendarId) { |
||
165 | $calendar = $this->attendanceCalendarRepository->find($calendarId); |
||
166 | if ($calendar && !$calendar->getDoneAttendance()) { |
||
167 | $calendar->setDoneAttendance(true); |
||
168 | $this->em->persist($calendar); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | $calendars = $this->attendanceCalendarRepository->findBy(['iid' => $calendarIds]); |
||
173 | $attendance = $calendars[0]->getAttendance(); |
||
174 | $this->updateAttendanceResults($attendance); |
||
175 | |||
176 | $lasteditType = $calendars[0]->getDoneAttendance() |
||
177 | ? 'UPDATED_ATTENDANCE_LOG_TYPE' |
||
178 | : 'DONE_ATTENDANCE_LOG_TYPE'; |
||
179 | |||
180 | foreach ($calendars as $calendar) { |
||
181 | $this->saveAttendanceLog($attendance, $lasteditType, $calendar); |
||
182 | } |
||
183 | |||
184 | $this->em->flush(); |
||
185 | |||
186 | return $this->json([ |
||
187 | 'message' => 'Attendance data and comments saved successfully', |
||
188 | 'affectedRows' => $affectedRows, |
||
189 | ]); |
||
190 | |||
191 | } catch (\Exception $e) { |
||
192 | return $this->json(['error' => 'An error occurred: ' . $e->getMessage()], 500); |
||
193 | } |
||
242 |
This check looks for imports that have been defined, but are not used in the scope.