| Conditions | 31 |
| Paths | 18192 |
| Total Lines | 166 |
| Code Lines | 108 |
| 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 |
||
| 286 | private function autoLaunch(): void |
||
| 287 | { |
||
| 288 | /* Auto launch code */ |
||
| 289 | $autoLaunchWarning = ''; |
||
| 290 | $showAutoLaunchLpWarning = false; |
||
| 291 | $course_id = api_get_course_int_id(); |
||
| 292 | $lpAutoLaunch = api_get_course_setting('enable_lp_auto_launch'); |
||
| 293 | $session_id = api_get_session_id(); |
||
| 294 | $allowAutoLaunchForCourseAdmins = api_is_platform_admin() || api_is_allowed_to_edit(true, true) || api_is_coach(); |
||
| 295 | |||
| 296 | if (!empty($lpAutoLaunch)) { |
||
| 297 | if (2 === $lpAutoLaunch) { |
||
| 298 | // LP list |
||
| 299 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 300 | $showAutoLaunchLpWarning = true; |
||
| 301 | } else { |
||
| 302 | $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id(); |
||
| 303 | if (!isset($_SESSION[$session_key])) { |
||
| 304 | // Redirecting to the LP |
||
| 305 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq(); |
||
| 306 | $_SESSION[$session_key] = true; |
||
| 307 | header("Location: {$url}"); |
||
| 308 | exit; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | } else { |
||
| 312 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 313 | $condition = ''; |
||
| 314 | if (!empty($session_id)) { |
||
| 315 | $condition = api_get_session_condition($session_id); |
||
| 316 | $sql = "SELECT id FROM {$lp_table} |
||
| 317 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 318 | LIMIT 1"; |
||
| 319 | $result = Database::query($sql); |
||
| 320 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 321 | if (0 === Database::num_rows($result)) { |
||
| 322 | $condition = ''; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | $sql = "SELECT iid FROM {$lp_table} |
||
| 327 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 328 | LIMIT 1"; |
||
| 329 | $result = Database::query($sql); |
||
| 330 | if (Database::num_rows($result) > 0) { |
||
| 331 | $lp_data = Database::fetch_array($result, 'ASSOC'); |
||
| 332 | if (!empty($lp_data['iid'])) { |
||
| 333 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 334 | $showAutoLaunchLpWarning = true; |
||
| 335 | } else { |
||
| 336 | $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id(); |
||
| 337 | if (!isset($_SESSION[$session_key])) { |
||
| 338 | // Redirecting to the LP |
||
| 339 | $url = api_get_path(WEB_CODE_PATH). |
||
| 340 | 'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp_data['iid']; |
||
| 341 | |||
| 342 | $_SESSION[$session_key] = true; |
||
| 343 | header("Location: {$url}"); |
||
| 344 | exit; |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | if ($showAutoLaunchLpWarning) { |
||
| 353 | $autoLaunchWarning = get_lang( |
||
| 354 | '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.' |
||
| 355 | ); |
||
| 356 | } |
||
| 357 | |||
| 358 | $forumAutoLaunch = (int) api_get_course_setting('enable_forum_auto_launch'); |
||
| 359 | if (1 === $forumAutoLaunch) { |
||
| 360 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 361 | if (empty($autoLaunchWarning)) { |
||
| 362 | $autoLaunchWarning = get_lang( |
||
| 363 | 'The forum\'s auto-launch setting is on. Students will be redirected to the forum tool when entering this course.' |
||
| 364 | ); |
||
| 365 | } |
||
| 366 | } else { |
||
| 367 | $url = api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq(); |
||
| 368 | header("Location: {$url}"); |
||
| 369 | exit; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | if (api_get_configuration_value('allow_exercise_auto_launch')) { |
||
| 374 | $exerciseAutoLaunch = (int) api_get_course_setting('enable_exercise_auto_launch'); |
||
| 375 | if (2 === $exerciseAutoLaunch) { |
||
| 376 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 377 | if (empty($autoLaunchWarning)) { |
||
| 378 | $autoLaunchWarning = get_lang( |
||
| 379 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToTheExerciseList' |
||
| 380 | ); |
||
| 381 | } |
||
| 382 | } else { |
||
| 383 | // Redirecting to the document |
||
| 384 | $url = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq(); |
||
| 385 | header("Location: {$url}"); |
||
| 386 | exit; |
||
| 387 | } |
||
| 388 | } elseif (1 === $exerciseAutoLaunch) { |
||
| 389 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 390 | if (empty($autoLaunchWarning)) { |
||
| 391 | $autoLaunchWarning = get_lang( |
||
| 392 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToAnSpecificExercise' |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | } else { |
||
| 396 | // Redirecting to an exercise |
||
| 397 | $table = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 398 | $condition = ''; |
||
| 399 | if (!empty($session_id)) { |
||
| 400 | $condition = api_get_session_condition($session_id); |
||
| 401 | $sql = "SELECT iid FROM {$table} |
||
| 402 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 403 | LIMIT 1"; |
||
| 404 | $result = Database::query($sql); |
||
| 405 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 406 | if (0 === Database::num_rows($result)) { |
||
| 407 | $condition = ''; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | $sql = "SELECT iid FROM {$table} |
||
| 412 | WHERE c_id = {$course_id} AND autolaunch = 1 {$condition} |
||
| 413 | LIMIT 1"; |
||
| 414 | $result = Database::query($sql); |
||
| 415 | if (Database::num_rows($result) > 0) { |
||
| 416 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 417 | $exerciseId = $row['iid']; |
||
| 418 | $url = api_get_path(WEB_CODE_PATH). |
||
| 419 | 'exercise/overview.php?exerciseId='.$exerciseId.'&'.api_get_cidreq(); |
||
| 420 | header("Location: {$url}"); |
||
| 421 | exit; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | $documentAutoLaunch = (int) api_get_course_setting('enable_document_auto_launch'); |
||
| 428 | if (1 === $documentAutoLaunch) { |
||
| 429 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 430 | if (empty($autoLaunchWarning)) { |
||
| 431 | $autoLaunchWarning = get_lang( |
||
| 432 | 'The document auto-launch feature configuration is enabled. Learners will be automatically redirected to document tool.' |
||
| 433 | ); |
||
| 434 | } |
||
| 435 | } else { |
||
| 436 | // Redirecting to the document |
||
| 437 | $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq(); |
||
| 438 | header("Location: {$url}"); |
||
| 439 | exit; |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | /* SWITCH TO A DIFFERENT HOMEPAGE VIEW |
||
| 444 | the setting homepage_view is adjustable through |
||
| 445 | the platform administration section */ |
||
| 446 | if (!empty($autoLaunchWarning)) { |
||
| 447 | $this->addFlash( |
||
| 448 | 'warning', |
||
| 449 | Display::return_message( |
||
| 450 | $autoLaunchWarning, |
||
| 451 | 'warning' |
||
| 452 | ) |
||
| 457 |