Conditions | 23 |
Paths | 82 |
Total Lines | 124 |
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 |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
245 | if (empty($courses)) { |
||
246 | return null; |
||
247 | } |
||
248 | |||
249 | return sprintf($this->get_lang('SubscriptionToXRemedialCourses'), implode(' - ', $courses)); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * When a student takes an exam, and he gets an acceptable grade, he is enrolled in a series of courses that |
||
254 | * represent the next level BT#18165. |
||
255 | * |
||
256 | * @param Exercise $objExercise |
||
257 | * @param int $userId |
||
258 | * @param int $sessionId |
||
259 | * |
||
260 | * @return string|null |
||
261 | */ |
||
262 | public function getAdvancedCourseList(Exercise $objExercise, int $userId = 0, int $sessionId = 0): ?string |
||
263 | { |
||
264 | if ('true' !== $this->get(self::SETTING_ENABLED)) { |
||
265 | return null; |
||
266 | } |
||
267 | |||
268 | $field = new ExtraField('exercise'); |
||
269 | $advancedCourseField = $field->get_handler_field_info_by_field_variable(self::EXTRAFIELD_ADVACED_VARIABLE); |
||
270 | |||
271 | if (false === $advancedCourseField) { |
||
272 | return null; |
||
273 | } |
||
274 | |||
275 | $userId = empty($userId) ? api_get_user_id() : $userId; |
||
276 | $bestAttempt = Event::get_best_attempt_exercise_results_per_user( |
||
277 | $userId, |
||
278 | $objExercise->iId, |
||
279 | $objExercise->course_id, |
||
280 | $sessionId |
||
281 | ); |
||
282 | |||
283 | if (!isset($bestAttempt['exe_result'])) { |
||
284 | // In the case that the result is 0, get_best_attempt_exercise_results_per_user does not return data, |
||
285 | // for that this block is used |
||
286 | $exerciseStatInfo = Event::getExerciseResultsByUser( |
||
287 | $userId, |
||
288 | $objExercise->iId, |
||
289 | $objExercise->course_id, |
||
290 | $sessionId |
||
291 | ); |
||
292 | $bestAttempt['exe_result'] = 0; |
||
293 | |||
294 | foreach ($exerciseStatInfo as $attempt) { |
||
295 | if ($attempt['exe_result'] >= $bestAttempt['exe_result']) { |
||
296 | $bestAttempt = $attempt; |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | |||
301 | if ( |
||
302 | !isset($bestAttempt['exe_result']) |
||
303 | || !isset($bestAttempt['exe_id']) |
||
304 | || !isset($bestAttempt['exe_weighting']) |
||
305 | ) { |
||
306 | // No try, No exercise id, no defined total |
||
307 | return null; |
||
308 | } |
||
309 | |||
310 | $percentSuccess = $objExercise->selectPassPercentage(); |
||
311 | $pass = ExerciseLib::isPassPercentageAttemptPassed( |
||
312 | $objExercise, |
||
313 | $bestAttempt['exe_result'], |
||
314 | $bestAttempt['exe_weighting'] |
||
315 | ); |
||
316 | |||
317 | if (0 == $percentSuccess && false == $pass) { |
||
318 | return null; |
||
319 | } |
||
320 | |||
321 | $canRemedial = false === $pass; |
||
322 | // Advance Course |
||
323 | $extraFieldValue = new ExtraFieldValue('exercise'); |
||
324 | $advanceCourseExcerciseField = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
325 | $objExercise->iId, |
||
326 | self::EXTRAFIELD_ADVACED_VARIABLE |
||
327 | ); |
||
328 | |||
329 | if ($canRemedial || !isset($advanceCourseExcerciseField['value'])) { |
||
330 | return null; |
||
331 | } |
||
332 | |||
333 | $coursesIds = explode(';', $advanceCourseExcerciseField['value']); |
||
334 | |||
335 | if (empty($advanceCourseExcerciseField['value']) || count($coursesIds) == 0) { |
||
336 | return null; |
||
337 | } |
||
338 | |||
339 | $isInASession = !empty($sessionId); |
||
340 | $courses = []; |
||
341 | |||
342 | foreach ($coursesIds as $course) { |
||
343 | $courseData = api_get_course_info_by_id($course); |
||
344 | |||
345 | if (empty($courseData) || !isset($courseData['real_id'])) { |
||
346 | continue; |
||
347 | } |
||
348 | |||
349 | // if session is 0, always will be true |
||
350 | $courseExistsInSession = true; |
||
351 | |||
352 | if ($isInASession) { |
||
353 | $courseExistsInSession = SessionManager::sessionHasCourse($sessionId, $courseData['code']); |
||
354 | } |
||
355 | |||
356 | if (!$courseExistsInSession) { |
||
357 | continue; |
||
358 | } |
||
359 | |||
360 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
361 | $userId, |
||
362 | $courseData['code'], |
||
363 | $isInASession, |
||
364 | $sessionId |
||
365 | ); |
||
389 |