Conditions | 25 |
Paths | 291 |
Total Lines | 131 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
108 | public function getRemedialCourseList( |
||
109 | Exercise $objExercise, |
||
110 | int $userId = 0, |
||
111 | int $sessionId = 0, |
||
112 | bool $review = false |
||
113 | ): ?string { |
||
114 | if ('true' !== $this->get(self::SETTING_ENABLED)) { |
||
115 | return null; |
||
116 | } |
||
117 | |||
118 | $field = new ExtraField('exercise'); |
||
119 | $remedialField = $field->get_handler_field_info_by_field_variable(self::EXTRAFIELD_REMEDIAL_VARIABLE); |
||
120 | |||
121 | if (empty($remedialField)) { |
||
122 | return null; |
||
123 | } |
||
124 | |||
125 | $extraFieldValue = new ExtraFieldValue('exercise'); |
||
126 | $remedialExcerciseField = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
127 | $objExercise->iId, |
||
128 | self::EXTRAFIELD_REMEDIAL_VARIABLE |
||
129 | ); |
||
130 | $remedialCourseIds = explode(';', $remedialExcerciseField['value']); |
||
131 | |||
132 | if (empty($remedialExcerciseField['value']) || count($remedialCourseIds) == 0) { |
||
133 | return null; |
||
134 | } |
||
135 | |||
136 | $questionExcluded = [ |
||
137 | FREE_ANSWER, |
||
138 | ORAL_EXPRESSION, |
||
139 | ANNOTATION, |
||
140 | ]; |
||
141 | |||
142 | $userId = empty($userId) ? api_get_user_id() : $userId; |
||
143 | |||
144 | $exerciseStatInfo = Event::getExerciseResultsByUser( |
||
145 | $userId, |
||
146 | $objExercise->iId, |
||
147 | $objExercise->course_id, |
||
148 | $sessionId |
||
149 | ); |
||
150 | $bestAttempt = Event::get_best_attempt_exercise_results_per_user( |
||
151 | $userId, |
||
152 | $objExercise->iId, |
||
153 | $objExercise->course_id, |
||
154 | $sessionId |
||
155 | ); |
||
156 | |||
157 | foreach ($exerciseStatInfo as $attempt) { |
||
158 | if (!isset($bestAttempt['exe_result']) || $attempt['exe_result'] >= $bestAttempt['exe_result']) { |
||
159 | $bestAttempt = $attempt; |
||
160 | } |
||
161 | |||
162 | if (!isset($attempt['question_list'])) { |
||
163 | continue; |
||
164 | } |
||
165 | |||
166 | foreach ($attempt['question_list'] as $questionId => $answer) { |
||
167 | $question = Question::read($questionId, api_get_course_info_by_id($attempt['c_id'])); |
||
168 | $questionOpen = in_array($question->type, $questionExcluded) && !$review; |
||
169 | |||
170 | if (!$questionOpen) { |
||
171 | continue; |
||
172 | } |
||
173 | |||
174 | $score = $attempt['exe_result']; |
||
175 | $comments = Event::get_comments($objExercise->iId, $questionId); |
||
176 | |||
177 | if (empty($comments) || $score == 0) { |
||
178 | return null; |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | if (empty($bestAttempt)) { |
||
184 | return null; |
||
185 | } |
||
186 | |||
187 | $bestAttempt['exe_result'] = (int) $bestAttempt['exe_result']; |
||
188 | |||
189 | $isPassedPercentage = ExerciseLib::isPassPercentageAttemptPassed( |
||
190 | $objExercise, |
||
191 | $bestAttempt['exe_result'], |
||
192 | $bestAttempt['exe_weighting'] |
||
193 | ); |
||
194 | |||
195 | if ($isPassedPercentage) { |
||
196 | return null; |
||
197 | } |
||
198 | |||
199 | $hasAttempts = count($exerciseStatInfo) < $objExercise->selectAttempts(); |
||
200 | |||
201 | $doSubscriptionToRemedial = !$hasAttempts || $objExercise->isBlockedByPercentage($bestAttempt); |
||
202 | |||
203 | if (!$doSubscriptionToRemedial) { |
||
204 | return null; |
||
205 | } |
||
206 | |||
207 | $courses = []; |
||
208 | $isInASession = !empty($sessionId); |
||
209 | |||
210 | foreach ($remedialCourseIds as $courseId) { |
||
211 | $courseData = api_get_course_info_by_id($courseId); |
||
212 | |||
213 | if (empty($courseData)) { |
||
214 | continue; |
||
215 | } |
||
216 | |||
217 | if ($isInASession) { |
||
218 | $courseExistsInSession = SessionManager::sessionHasCourse($sessionId, $courseData['code']); |
||
219 | |||
220 | if ($courseExistsInSession) { |
||
221 | SessionManager::subscribe_users_to_session_course([$userId], $sessionId, $courseData['code']); |
||
222 | $courses[] = $courseData['title']; |
||
223 | } |
||
224 | } else { |
||
225 | $isSubscribed = CourseManager::is_user_subscribed_in_course($userId, $courseData['code']); |
||
226 | |||
227 | if (!$isSubscribed) { |
||
228 | CourseManager::subscribeUser($userId, $courseData['code']); |
||
229 | $courses[] = $courseData['title']; |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | if (empty($courses)) { |
||
235 | return null; |
||
236 | } |
||
237 | |||
238 | return sprintf($this->get_lang('SubscriptionToXRemedialCourses'), implode(' - ', $courses)); |
||
239 | } |
||
368 |