|
435 | 435 | } |
436 | 436 | } |
437 | 437 |
|
438 | | -// Direct Link Subscription feature #5299 |
| 438 | +/** |
| 439 | + * Build the redirect URL for a "direct registration" link. |
| 440 | + * - Default: course home. |
| 441 | + * - If an exercise ID is provided: redirect to the exercise tool inside the course. |
| 442 | + */ |
| 443 | +$buildDirectLinkRedirectUrl = static function (int $courseId, int $exerciseId = 0): string { |
| 444 | + // Course home |
| 445 | + $courseHomeUrl = api_get_path(WEB_PATH) . 'course/' . $courseId . '/home?sid=0'; |
| 446 | + |
| 447 | + if ($exerciseId <= 0) { |
| 448 | + return $courseHomeUrl; |
| 449 | + } |
| 450 | + |
| 451 | + $courseInfo = api_get_course_info_by_id($courseId); |
| 452 | + $courseCode = $courseInfo['code'] ?? $courseInfo['course_code'] ?? $courseInfo['directory'] ?? ''; |
| 453 | + |
| 454 | + if (empty($courseCode)) { |
| 455 | + return $courseHomeUrl; |
| 456 | + } |
| 457 | + |
| 458 | + // Go to the exercise entrypoint |
| 459 | + $query = http_build_query([ |
| 460 | + 'cid' => $courseId, |
| 461 | + 'sid' => 0, |
| 462 | + 'gid' => 0, |
| 463 | + 'exerciseId' => $exerciseId, |
| 464 | + ]); |
| 465 | + |
| 466 | + return api_get_path(WEB_CODE_PATH) . 'exercise/overview.php?' . $query; |
| 467 | +}; |
| 468 | + |
439 | 469 | $courseIdRedirect = isset($_REQUEST['c']) && !empty($_REQUEST['c']) ? (int) $_REQUEST['c'] : null; |
440 | | -$exercise_redirect = isset($_REQUEST['e']) && !empty($_REQUEST['e']) ? (int) $_REQUEST['e'] : null; |
| 470 | +$exercise_redirect = isset($_REQUEST['e']) && !empty($_REQUEST['e']) ? (int) $_REQUEST['e'] : 0; |
441 | 471 |
|
442 | 472 | if (!empty($courseIdRedirect)) { |
| 473 | + $courseInfo = api_get_course_info_by_id($courseIdRedirect); |
| 474 | + $visibility = (int) ($courseInfo['visibility'] ?? -1); |
| 475 | + |
| 476 | + $isOpenCourse = in_array( |
| 477 | + $visibility, |
| 478 | + [COURSE_VISIBILITY_OPEN_PLATFORM, COURSE_VISIBILITY_OPEN_WORLD], |
| 479 | + true |
| 480 | + ); |
| 481 | + |
443 | 482 | if (!api_is_anonymous()) { |
444 | | - $subscribed = CourseManager::autoSubscribeToCourse($courseIdRedirect); |
445 | | - if ($subscribed) { |
446 | | - header('Location: ' . api_get_path(WEB_PATH) . 'course/'.$courseIdRedirect.'/home?sid=0'); |
447 | | - } else { |
448 | | - header('Location: ' . api_get_path(WEB_PATH) . 'course/'.$courseIdRedirect.'/about'); |
| 483 | + if ($isOpenCourse) { |
| 484 | + if ($exercise_redirect > 0) { |
| 485 | + CourseManager::autoSubscribeToCourse($courseIdRedirect); |
| 486 | + } |
| 487 | + |
| 488 | + header('Location: ' . $buildDirectLinkRedirectUrl($courseIdRedirect, $exercise_redirect)); |
| 489 | + exit; |
449 | 490 | } |
| 491 | + |
| 492 | + header('Location: ' . api_get_path(WEB_PATH) . 'course/' . $courseIdRedirect . '/about'); |
450 | 493 | exit; |
451 | 494 | } |
452 | 495 | Session::write('course_redirect', $courseIdRedirect); |
@@ -1475,6 +1518,39 @@ function ($email) { |
1475 | 1518 | // Stats |
1476 | 1519 | Container::getTrackELoginRepository()->createLoginRecord($userEntity, new DateTime(), $request->getClientIp()); |
1477 | 1520 |
|
| 1521 | + /** |
| 1522 | + * Direct link redirect (course + optional exercise). |
| 1523 | + */ |
| 1524 | + $directCourseId = (int) Session::read('course_redirect'); |
| 1525 | + $directExerciseId = (int) Session::read('exercise_redirect'); |
| 1526 | + |
| 1527 | + if ($directCourseId > 0) { |
| 1528 | + Session::erase('course_redirect'); |
| 1529 | + Session::erase('exercise_redirect'); |
| 1530 | + |
| 1531 | + $courseInfo = api_get_course_info_by_id($directCourseId); |
| 1532 | + $visibility = (int) ($courseInfo['visibility'] ?? -1); |
| 1533 | + |
| 1534 | + $isOpenCourse = in_array( |
| 1535 | + $visibility, |
| 1536 | + [COURSE_VISIBILITY_OPEN_PLATFORM, COURSE_VISIBILITY_OPEN_WORLD], |
| 1537 | + true |
| 1538 | + ); |
| 1539 | + |
| 1540 | + if ($isOpenCourse) { |
| 1541 | + // Only for exercises: helps tracking, but must not gate redirect. |
| 1542 | + if ($directExerciseId > 0) { |
| 1543 | + CourseManager::autoSubscribeToCourse($directCourseId); |
| 1544 | + } |
| 1545 | + |
| 1546 | + header('Location: ' . $buildDirectLinkRedirectUrl($directCourseId, $directExerciseId)); |
| 1547 | + exit; |
| 1548 | + } |
| 1549 | + |
| 1550 | + header('Location: ' . api_get_path(WEB_PATH) . 'course/' . $directCourseId . '/about'); |
| 1551 | + exit; |
| 1552 | + } |
| 1553 | + |
1478 | 1554 | // last user login date is now |
1479 | 1555 | $user_last_login_datetime = 0; // used as a unix timestamp it will correspond to : 1 1 1970 |
1480 | 1556 | Session::write('user_last_login_datetime', $user_last_login_datetime); |
|
0 commit comments