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