| Conditions | 31 |
| Paths | 18192 |
| Total Lines | 168 |
| Code Lines | 111 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 337 | private function autoLaunch(): void |
||
| 338 | { |
||
| 339 | $autoLaunchWarning = ''; |
||
| 340 | $showAutoLaunchLpWarning = false; |
||
| 341 | $course_id = api_get_course_int_id(); |
||
| 342 | $lpAutoLaunch = api_get_course_setting('enable_lp_auto_launch'); |
||
| 343 | $session_id = api_get_session_id(); |
||
| 344 | $allowAutoLaunchForCourseAdmins = |
||
| 345 | api_is_platform_admin() || |
||
| 346 | api_is_allowed_to_edit(true, true) || |
||
| 347 | api_is_coach(); |
||
| 348 | |||
| 349 | if (!empty($lpAutoLaunch)) { |
||
| 350 | if (2 === $lpAutoLaunch) { |
||
| 351 | // LP list |
||
| 352 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 353 | $showAutoLaunchLpWarning = true; |
||
| 354 | } else { |
||
| 355 | $session_key = 'lp_autolaunch_'.$session_id.'_'.$course_id.'_'.api_get_user_id(); |
||
| 356 | if (!isset($_SESSION[$session_key])) { |
||
| 357 | // Redirecting to the LP |
||
| 358 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq(); |
||
| 359 | $_SESSION[$session_key] = true; |
||
| 360 | header(sprintf('Location: %s', $url)); |
||
| 361 | exit; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } else { |
||
| 365 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 366 | $condition = ''; |
||
| 367 | if (!empty($session_id)) { |
||
| 368 | $condition = api_get_session_condition($session_id); |
||
| 369 | $sql = "SELECT id FROM {$lp_table} |
||
| 370 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 371 | LIMIT 1"; |
||
| 372 | $result = Database::query($sql); |
||
| 373 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 374 | if (0 === Database::num_rows($result)) { |
||
| 375 | $condition = ''; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | $sql = "SELECT iid FROM {$lp_table} |
||
| 380 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 381 | LIMIT 1"; |
||
| 382 | $result = Database::query($sql); |
||
| 383 | if (Database::num_rows($result) > 0) { |
||
| 384 | $lp_data = Database::fetch_array($result, 'ASSOC'); |
||
| 385 | if (!empty($lp_data['iid'])) { |
||
| 386 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 387 | $showAutoLaunchLpWarning = true; |
||
| 388 | } else { |
||
| 389 | $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id(); |
||
| 390 | if (!isset($_SESSION[$session_key])) { |
||
| 391 | // Redirecting to the LP |
||
| 392 | $url = api_get_path(WEB_CODE_PATH). |
||
| 393 | 'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp_data['iid']; |
||
| 394 | |||
| 395 | $_SESSION[$session_key] = true; |
||
| 396 | header(sprintf('Location: %s', $url)); |
||
| 397 | exit; |
||
| 398 | } |
||
| 399 | } |
||
| 400 | } |
||
| 401 | } |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | if ($showAutoLaunchLpWarning) { |
||
| 406 | $autoLaunchWarning = get_lang( |
||
| 407 | 'The learning path auto-launch setting is ON. When learners enter this course, they will be automatically redirected to the learning path marked as auto-launch.' |
||
| 408 | ); |
||
| 409 | } |
||
| 410 | |||
| 411 | $forumAutoLaunch = (int) api_get_course_setting('enable_forum_auto_launch'); |
||
| 412 | if (1 === $forumAutoLaunch) { |
||
| 413 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 414 | if (empty($autoLaunchWarning)) { |
||
| 415 | $autoLaunchWarning = get_lang( |
||
| 416 | "The forum's auto-launch setting is on. Students will be redirected to the forum tool when entering this course." |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | } else { |
||
| 420 | $url = api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq(); |
||
| 421 | header(sprintf('Location: %s', $url)); |
||
| 422 | exit; |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | if (api_get_configuration_value('allow_exercise_auto_launch')) { |
||
| 427 | $exerciseAutoLaunch = (int) api_get_course_setting('enable_exercise_auto_launch'); |
||
| 428 | if (2 === $exerciseAutoLaunch) { |
||
| 429 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 430 | if (empty($autoLaunchWarning)) { |
||
| 431 | $autoLaunchWarning = get_lang( |
||
| 432 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToTheExerciseList' |
||
| 433 | ); |
||
| 434 | } |
||
| 435 | } else { |
||
| 436 | // Redirecting to the document |
||
| 437 | $url = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq(); |
||
| 438 | header(sprintf('Location: %s', $url)); |
||
| 439 | exit; |
||
| 440 | } |
||
| 441 | } elseif (1 === $exerciseAutoLaunch) { |
||
| 442 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 443 | if (empty($autoLaunchWarning)) { |
||
| 444 | $autoLaunchWarning = get_lang( |
||
| 445 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToAnSpecificExercise' |
||
| 446 | ); |
||
| 447 | } |
||
| 448 | } else { |
||
| 449 | // Redirecting to an exercise |
||
| 450 | $table = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 451 | $condition = ''; |
||
| 452 | if (!empty($session_id)) { |
||
| 453 | $condition = api_get_session_condition($session_id); |
||
| 454 | $sql = "SELECT iid FROM {$table} |
||
| 455 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 456 | LIMIT 1"; |
||
| 457 | $result = Database::query($sql); |
||
| 458 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 459 | if (0 === Database::num_rows($result)) { |
||
| 460 | $condition = ''; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | $sql = "SELECT iid FROM {$table} |
||
| 465 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 466 | LIMIT 1"; |
||
| 467 | $result = Database::query($sql); |
||
| 468 | if (Database::num_rows($result) > 0) { |
||
| 469 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 470 | $exerciseId = $row['iid']; |
||
| 471 | $url = api_get_path(WEB_CODE_PATH). |
||
| 472 | 'exercise/overview.php?exerciseId='.$exerciseId.'&'.api_get_cidreq(); |
||
| 473 | header(sprintf('Location: %s', $url)); |
||
| 474 | exit; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | $documentAutoLaunch = (int) api_get_course_setting('enable_document_auto_launch'); |
||
| 481 | if (1 === $documentAutoLaunch) { |
||
| 482 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 483 | if (empty($autoLaunchWarning)) { |
||
| 484 | $autoLaunchWarning = get_lang( |
||
| 485 | 'The document auto-launch feature configuration is enabled. Learners will be automatically redirected to document tool.' |
||
| 486 | ); |
||
| 487 | } |
||
| 488 | } else { |
||
| 489 | // Redirecting to the document |
||
| 490 | $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq(); |
||
| 491 | header("Location: $url"); |
||
| 492 | exit; |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | /* SWITCH TO A DIFFERENT HOMEPAGE VIEW |
||
| 497 | the setting homepage_view is adjustable through |
||
| 498 | the platform administration section */ |
||
| 499 | if (!empty($autoLaunchWarning)) { |
||
| 500 | $this->addFlash( |
||
| 501 | 'warning', |
||
| 502 | Display::return_message( |
||
| 503 | $autoLaunchWarning, |
||
| 504 | 'warning' |
||
| 505 | ) |
||
| 510 |