';
echo '';
if ($display == 'useroverview' || $display == 'sessionoverview' || $display == 'courseoverview') {
    echo '';
    echo Display::return_icon("export_csv.png", get_lang('ExportAsCSV'), array(), 32);
    echo '';
}
echo ''.
    Display::return_icon('printer.png', get_lang('Print'), '', ICON_SIZE_MEDIUM).'';
echo '';
if (!empty($session_id) &&
    !in_array($display, array('accessoverview','lpprogressoverview','progressoverview','exerciseprogress', 'surveyoverview'))
) {
    echo ''.Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'';
    if (!api_is_platform_admin()) {
        if (api_get_setting('add_users_by_coach') == 'true') {
            if ($is_coach) {
                echo "
";
            }
        }
    } else {
        echo "
";
    }
} else {
	echo Display::url(
        Display::return_icon('stats.png', get_lang('MyStats'),'',ICON_SIZE_MEDIUM),
        api_get_path(WEB_CODE_PATH)."auth/my_progress.php"
    );
    echo Display::url(
        Display::return_icon("certificate_list.png", get_lang("GradebookSeeListOfStudentsCertificates"), array(), ICON_SIZE_MEDIUM),
        api_get_path(WEB_CODE_PATH) . "gradebook/certificate_report.php"
    );
}
// Actions menu
$nb_menu_items = count($menu_items);
if (empty($session_id) ||
    in_array($display, array('accessoverview','lpprogressoverview', 'progressoverview', 'exerciseprogress', 'surveyoverview'))
) {
    if ($nb_menu_items > 1) {
        foreach ($menu_items as $key => $item) {
            echo $item;
        }
    }
}
echo '';
$userId  = api_get_user_id();
$stats = Tracking::getStats($userId);
$students = $stats['students'];
$teachers = $stats['teachers'];
$humanResourcesUsers = $stats['drh'];
$assignedCourses = $stats['assignedCourses'];
$courses = $stats['courses'];
$sessions = $stats['sessions'];
$sessionIdList = array();
if (!empty($sessions)) {
    foreach ($sessions as $session) {
        $sessionIdList[] = $session['id'];
    }
}
// Courses for the user
$countAssignedCourses = count($assignedCourses);
$count_courses = count($courses);
// Sessions for the user
$count_sessions = count($sessions);
$total_time_spent = 0;
$total_courses = 0;
$avg_total_progress = 0;
$avg_results_to_exercises = 0;
$nb_inactive_students = 0;
$nb_posts = $nb_assignments = 0;
$inactiveTime = time() - (3600 * 24 * 7);
$nb_students = 0;
$numberTeachers = 0;
$countHumanResourcesUsers = 0;
$daysAgo = 7;
$studentIds = array();
if (!empty($students)) {
    // Students
    $nb_students = count($students);
    $studentIds = array_values($students);
    $countHumanResourcesUsers = count($humanResourcesUsers);
    // average progress
    $avg_total_progress = $progress / $nb_students;
    // average assignments
    $nb_assignments = $countAssignments / $nb_students;
    $avg_courses_per_student = $count_courses / $nb_students;
}
if (!empty($teachers)) {
    $numberTeachers = count($teachers);
}
// Inactive students
//$countInactiveUsers = Tracking::getInactiveUsers($studentIds, $daysAgo);
$totalTimeSpent = Tracking::get_time_spent_on_the_platform($studentIds);
$posts = Tracking::count_student_messages($studentIds);
$countAssignments = Tracking::count_student_assignments($studentIds);
$progress  = Tracking::get_avg_student_progress($studentIds);
$averageScore = Tracking::getAverageStudentScore($studentIds);
$avg_results_to_exercises = $averageScore;
// average posts
$nb_posts = $posts;
$avg_time_spent = $totalTimeSpent;
$linkAddUser = null;
$linkCourseDetailsAsTeacher = null;
$linkAddCourse = null;
$linkAddSession = null;
if (api_is_platform_admin()) {
    $linkAddUser = ' '.Display::url(
        Display::return_icon('2rightarrow.gif', get_lang('Add')),
        api_get_path(WEB_CODE_PATH).'admin/dashboard_add_users_to_user.php?user='.api_get_user_id(),
        array('class' => '')
    );
    $linkCourseDetailsAsTeacher = ' '.Display::url(
        Display::return_icon('2rightarrow.gif', get_lang('Details')),
        api_get_path(WEB_CODE_PATH).'mySpace/course.php',
        array('class' => '')
    );
    $linkAddCourse = ' '.Display::url(
        Display::return_icon('2rightarrow.gif', get_lang('Details')),
        api_get_path(WEB_CODE_PATH).'mySpace/course.php?follow',
        array('class' => '')
    );
    $linkAddSession = ' '.Display::url(
        Display::return_icon('2rightarrow.gif', get_lang('Add')),
        api_get_path(WEB_CODE_PATH).'admin/dashboard_add_sessions_to_user.php?user='.api_get_user_id(),
        array('class' => '')
    );
}
echo Display::page_subheader(get_lang('Overview'));
echo '
        
            
                | '.Display::url(
                    get_lang('FollowedStudents'),
                    api_get_path(WEB_CODE_PATH).'mySpace/student.php'
                ).' | 
                '.$nb_students.' | 
            
            
                | '.Display::url(
                    get_lang('FollowedTeachers'),
                    api_get_path(WEB_CODE_PATH).'mySpace/teachers.php'
                ).
                ' | 
                '.$numberTeachers.' | 
            
            
                | '.Display::url(
                get_lang('FollowedHumanResources'),
                api_get_path(WEB_CODE_PATH).'mySpace/users.php?status='.DRH
            ).
            ' | 
            '.$countHumanResourcesUsers.' | 
            
            
             | '.Display::url(
                get_lang('FollowedUsers'),
                api_get_path(WEB_CODE_PATH).'mySpace/users.php'
            ).
            ' | 
                '.($nb_students + $numberTeachers + $countHumanResourcesUsers).$linkAddUser.' | 
            
            
                | '.Display::url(
                    get_lang('AssignedCourses'),
                    api_get_path(WEB_CODE_PATH).'mySpace/course.php'
                ).
                ' | 
                '.$count_courses.$linkCourseDetailsAsTeacher.' | 
            
            
                | '.Display::url(
                    get_lang('FollowedCourses'),
                    api_get_path(WEB_CODE_PATH).'mySpace/course.php?follow'
                ).
                ' | 
                '.$countAssignedCourses.$linkAddCourse.' | 
            
            
                | '.Display::url(
                    get_lang('FollowedSessions'),
                    api_get_path(WEB_CODE_PATH).'mySpace/session.php'
                ).
                ' | 
    '.$count_sessions.$linkAddSession.' | 
            
            
';
echo '
 ';
echo Display::page_subheader(get_lang('Students').' ('.$nb_students.')');
if ($export_csv) {
    //csv part
    $csv_content[] = array(get_lang('Students'));
    $csv_content[] = array(get_lang('InactivesStudents'), $nb_inactive_students);
    $csv_content[] = array(get_lang('AverageTimeSpentOnThePlatform'), $avg_time_spent);
    $csv_content[] = array(get_lang('AverageCoursePerStudent'), $avg_courses_per_student);
    $csv_content[] = array(get_lang('AverageProgressInLearnpath'), is_null($avg_total_progress) ? null : round($avg_total_progress, 2).'%');
    $csv_content[] = array(get_lang('AverageResultsToTheExercices'), is_null($avg_results_to_exercises) ? null : round($avg_results_to_exercises, 2).'%');
    $csv_content[] = array(get_lang('AveragePostsInForum'), $nb_posts);
    $csv_content[] = array(get_lang('AverageAssignments'), $nb_assignments);
    $csv_content[] = array();
} else {
    $lastConnectionDate = api_get_utc_datetime(strtotime('15 days ago'));
    $countActiveUsers = SessionManager::getCountUserTracking(null, 1, null, array(), array());
    $countSleepingTeachers = SessionManager::getTeacherTracking(
        api_get_user_id(),
        1,
        $lastConnectionDate,
        true,
        $sessionIdList
    );
    $countSleepingStudents = SessionManager::getCountUserTracking(
        null,
        1,
        $lastConnectionDate,
        $sessionIdList,
        $studentIds
    );
    $form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/student.php');
    $form = Tracking::setUserSearchForm($form);
    $form->display();
    // html part
    echo '
            
                
                    | '.get_lang('AverageCoursePerStudent').' | 
                    '.(is_null($avg_courses_per_student) ? '' : round($avg_courses_per_student, 2)).' | 
                
                
                    | '.get_lang('InactivesStudents').' | 
                    '.$nb_inactive_students.' | 
                
                
                    | '.get_lang('AverageTimeSpentOnThePlatform').' | 
                    '.(is_null($avg_time_spent) ? '' : api_time_to_hms($avg_time_spent)).' | 
                
                
                    | '.get_lang('AverageProgressInLearnpath').' | 
                    '.(is_null($avg_total_progress) ? '' : round($avg_total_progress, 2).'%').' | 
                
                
                    | '.get_lang('AvgCourseScore').' | 
                    '.(is_null($avg_results_to_exercises) ? '' : round($avg_results_to_exercises, 2).'%').' | 
                
                
                    | '.get_lang('AveragePostsInForum').' | 
                    '.(is_null($nb_posts) ? '' : round($nb_posts, 2)).' | 
                
                
                    | '.get_lang('AverageAssignments').' | 
                    '.(is_null($nb_assignments) ? '' : round($nb_assignments, 2)).' | 
                
            
          ';
}
// Send the csv file if asked
if ($export_csv) {
	ob_end_clean();
	Export :: export_table_csv($csv_content, 'reporting_index');
	exit;
}
if (!$export_csv) {
	Display::display_footer();
}