SECTION_COURSES,
    'tool_id' => 0,
    'tool_id_detail' => 0,
];
Event::registerLog($logInfo);
$userId = api_get_user_id();
if (array_key_exists('action', $_REQUEST)) {
    switch ($_REQUEST['action']) {
        case 'collapse_session':
            if (api_get_configuration_value('allow_user_session_collapsable')
                && array_key_exists('session_id', $_REQUEST)
            ) {
                $userRelSession = SessionManager::getUserSession($userId, $_REQUEST['session_id']);
                if ($userRelSession) {
                    $value = isset($_REQUEST['value']) ? (int) $_REQUEST['value'] : '';
                    $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
                    $sql = "UPDATE $table SET collapsed = $value WHERE id = ".$userRelSession['id'];
                    Database::query($sql);
                    Display::addFlash(Display::return_message(get_lang('Updated')));
                }
                header('Location: user_portal.php');
                exit;
            }
            break;
        case 'unsubscribe':
            if (\Security::check_token('get')) {
                $auth = new Auth();
                $sessionId = isset($_REQUEST['sid']) ? $_REQUEST['sid'] : 0;
                $courseCode = isset($_REQUEST['course_code']) ? $_REQUEST['course_code'] : '';
                if (empty($courseCode)) {
                    api_location(api_get_self());
                }
                if ($auth->remove_user_from_course($courseCode, $sessionId)) {
                    Display::addFlash(Display::return_message(get_lang('YouAreNowUnsubscribed')));
                }
                header('Location: user_portal.php');
                exit;
            }
            break;
    }
}
/* Constants and CONFIGURATION parameters */
$load_dirs = api_get_setting('show_documents_preview');
$displayMyCourseViewBySessionLink = api_get_setting('my_courses_view_by_session') === 'true';
$nameTools = get_lang('MyCourses');
$loadHistory = isset($_GET['history']) && intval($_GET['history']) == 1 ? true : false;
// Load course notification by ajax
$loadNotificationsByAjax = api_get_configuration_value('user_portal_load_notification_by_ajax');
if ($loadNotificationsByAjax) {
    $htmlHeadXtra[] = '';
}
/*
    Header
    Include the HTTP, HTML headers plus the top banner.
*/
if ($load_dirs) {
    $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=document_preview';
    $folder_icon = api_get_path(WEB_IMG_PATH).'icons/22/folder.png';
    $close_icon = api_get_path(WEB_IMG_PATH).'loading1.gif';
    $htmlHeadXtra[] = '';
}
if ($displayMyCourseViewBySessionLink) {
    $htmlHeadXtra[] = '
    ';
}
$myCourseListAsCategory = api_get_configuration_value('my_courses_list_as_category');
$controller = new IndexManager(get_lang('MyCourses'));
if (!$myCourseListAsCategory) {
    // Main courses and session list
    if (isset($_COOKIE['defaultMyCourseView'.$userId]) &&
        $_COOKIE['defaultMyCourseView'.$userId] == IndexManager::VIEW_BY_SESSION &&
        $displayMyCourseViewBySessionLink
    ) {
        $courseAndSessions = $controller->returnCoursesAndSessionsViewBySession($userId);
        IndexManager::setDefaultMyCourseView(IndexManager::VIEW_BY_SESSION, $userId);
    } else {
        $courseAndSessions = $controller->returnCoursesAndSessions($userId, true, null, true, $loadHistory);
        IndexManager::setDefaultMyCourseView(IndexManager::VIEW_BY_DEFAULT, $userId);
    }
    // if teacher, session coach or admin, display the button to change te course view
    if ($displayMyCourseViewBySessionLink &&
        (
            api_is_drh() ||
            api_is_session_general_coach() ||
            api_is_platform_admin() ||
            api_is_session_admin() ||
            api_is_teacher()
        )
    ) {
        $courseAndSessions['html'] = "
            
            
        ".$courseAndSessions['html'];
    }
} else {
    $categoryCode = isset($_GET['category']) ? $_GET['category'] : '';
    if (!$categoryCode) {
        $courseAndSessions = $controller->returnCourseCategoryListFromUser($userId);
    } else {
        $courseAndSessions = $controller->returnCoursesAndSessions(
            $userId,
            false,
            $categoryCode,
            true,
            $loadHistory
        );
        $getCategory = CourseCategory::getCategory($categoryCode);
        $controller->tpl->assign('category', $getCategory);
    }
}
// Check if a user is enrolled only in one course for going directly to the course after the login.
if (api_get_setting('go_to_course_after_login') == 'true') {
    $count_of_sessions = $courseAndSessions['session_count'];
    $count_of_courses_no_sessions = $courseAndSessions['course_count'];
    // User is subscribe in 1 session and 0 courses.
    if ($count_of_sessions == 1 && $count_of_courses_no_sessions == 0) {
        $sessions = SessionManager::get_sessions_by_user($userId);
        if (isset($sessions[0])) {
            $sessionInfo = $sessions[0];
            // Session only has 1 course.
            if (isset($sessionInfo['courses']) &&
                count($sessionInfo['courses']) == 1
            ) {
                foreach ($sessionInfo['courses'] as $courseItem) {
                    $courseInfo = api_get_course_info_by_id($courseItem['real_id']);
                    if (!empty($courseInfo)) {
                        $courseUrl = $courseInfo['course_public_url'].'?id_session='.$sessionInfo['session_id'];
                        header('Location:'.$courseUrl);
                        exit;
                    }
                }
            }
            // Session has many courses.
            if (isset($sessionInfo['session_id'])) {
                $url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$sessionInfo['session_id'];
                header('Location:'.$url);
                exit;
            }
        }
    }
    // User is subscribed to 1 course.
    if (!isset($_SESSION['coursesAlreadyVisited']) &&
        $count_of_sessions == 0 &&
        $count_of_courses_no_sessions == 1
    ) {
        if (!empty($courseAndSessions['courses']) && isset($courseAndSessions['courses'][0]) && !empty($courseAndSessions['courses'][0]['link'])) {
            header("Location: {$courseAndSessions['courses'][0]['link']}");
            exit;
        }
    }
}
// Show the chamilo mascot
if (empty($courseAndSessions['html']) && !isset($_GET['history'])) {
    $controller->tpl->assign(
        'welcome_to_course_block',
        $controller->return_welcome_to_course_block()
    );
}
$controller->tpl->assign('content', $courseAndSessions['html']);
// Display the Site Use Cookie Warning Validation
$useCookieValidation = api_get_setting('cookie_warning');
if ($useCookieValidation === 'true') {
    if (isset($_POST['acceptCookies'])) {
        api_set_site_use_cookie_warning_cookie();
    } else {
        if (!api_site_use_cookie_warning_cookie_exist()) {
            if (Template::isToolBarDisplayedForUser()) {
                $controller->tpl->assign('toolBarDisplayed', true);
            } else {
                $controller->tpl->assign('toolBarDisplayed', false);
            }
            $controller->tpl->assign('displayCookieUsageWarning', true);
        }
    }
}
//check for flash and message
$sniff_notification = '';
$some_activex = isset($_SESSION['sniff_check_some_activex']) ? $_SESSION['sniff_check_some_activex'] : null;
$some_plugins = isset($_SESSION['sniff_check_some_plugins']) ? $_SESSION['sniff_check_some_plugins'] : null;
if (!empty($some_activex) || !empty($some_plugins)) {
    if (!preg_match("/flash_yes/", $some_activex) && !preg_match("/flash_yes/", $some_plugins)) {
        $sniff_notification = Display::return_message(get_lang('NoFlash'), 'warning', true);
        //js verification - To annoying of redirecting every time the page
        $controller->tpl->assign('sniff_notification', $sniff_notification);
    }
}
$controller->tpl->assign('profile_block', $controller->return_profile_block());
$controller->tpl->assign('user_image_block', $controller->return_user_image_block());
$controller->tpl->assign('course_block', $controller->return_course_block());
$controller->tpl->assign('navigation_course_links', $controller->return_navigation_links());
$controller->tpl->assign('search_block', $controller->return_search_block());
$controller->tpl->assign('notice_block', $controller->return_notice());
$controller->tpl->assign('classes_block', $controller->returnClassesBlock());
$controller->tpl->assign('skills_block', $controller->returnSkillLinks());
$controller->tpl->assign('student_publication_block', $controller->studentPublicationBlock());
$historyClass = '';
if (!empty($_GET['history'])) {
    $historyClass = 'courses-history';
}
$controller->tpl->assign('course_history_page', $historyClass);
if ($myCourseListAsCategory) {
    $controller->tpl->assign('header', get_lang('MyCourses'));
}
$controller->setGradeBookDependencyBar($userId);
$controller->tpl->display_two_col_template();
// Deleting the session_id.
Session::erase('session_id');
Session::erase('id_session');
Session::erase('studentview');
api_remove_in_gradebook();