/**
 * PDF report creation
 */
function export_pdf_flatview($cat, $users, $alleval, $alllinks, $params = array())
{
    global $flatviewtable;
    //Getting data
    $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks, $params);
    // HTML report creation first
    $course_code = trim($cat[0]->get_course_code());
    $displayscore = ScoreDisplay::instance();
    $customdisplays = $displayscore->get_custom_score_display_settings();
    $total = array();
    if (is_array($customdisplays) && count($customdisplays)) {
        foreach ($customdisplays as $custom) {
            $total[$custom['display']] = 0;
        }
        $user_results = $flatviewtable->datagen->get_data_to_graph2();
        foreach ($user_results as $user_result) {
            $total[$user_result[count($user_result) - 1][1]]++;
        }
    }
    $parent_id = $cat[0]->get_parent_id();
    if (isset($cat[0]) && isset($parent_id)) {
        if ($parent_id == 0) {
            $grade_model_id = $cat[0]->get_grade_model_id();
        } else {
            $parent_cat = Category::load($parent_id);
            $grade_model_id = $parent_cat[0]->get_grade_model_id();
        }
    }
    $use_grade_model = true;
    if (empty($grade_model_id) || $grade_model_id == -1) {
        $use_grade_model = false;
    }
    if ($use_grade_model) {
        if ($parent_id == 0) {
            $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
        } else {
            $title = api_strtoupper(get_lang('Average')) . '<br />' . $cat[0]->get_description() . ' - (' . $cat[0]->get_name() . ')';
        }
    } else {
        if ($parent_id == 0) {
            $title = api_strtoupper(get_lang('Average')) . '<br />' . get_lang('Detailed');
        } else {
            $title = api_strtoupper(get_lang('Average'));
        }
    }
    $columns = count($printable_data[0]);
    $has_data = is_array($printable_data[1]) && count($printable_data[1]) > 0;
    $table = new HTML_Table(array('class' => 'data_table'));
    $row = 0;
    $column = 0;
    $table->setHeaderContents($row, $column, get_lang('NumberAbbreviation'));
    $column++;
    foreach ($printable_data[0] as $printable_data_cell) {
        $printable_data_cell = strip_tags($printable_data_cell);
        $table->setHeaderContents($row, $column, $printable_data_cell);
        $column++;
    }
    $row++;
    if ($has_data) {
        $counter = 1;
        foreach ($printable_data[1] as &$printable_data_row) {
            $column = 0;
            $table->setCellContents($row, $column, $counter);
            $table->updateCellAttributes($row, $column, 'align="center"');
            $column++;
            $counter++;
            foreach ($printable_data_row as $key => &$printable_data_cell) {
                $attributes = array();
                $attributes['align'] = 'center';
                $attributes['style'] = null;
                if ($key === 'name') {
                    $attributes['align'] = 'left';
                }
                if ($key === 'total') {
                    $attributes['style'] = 'font-weight:bold';
                }
                $table->setCellContents($row, $column, $printable_data_cell);
                $table->updateCellAttributes($row, $column, $attributes);
                $column++;
            }
            $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
            $row++;
        }
    } else {
        $column = 0;
        $table->setCellContents($row, $column, get_lang('NoResults'));
        $table->updateCellAttributes($row, $column, 'colspan="' . $columns . '" align="center" class="row_odd"');
    }
    $params = array('filename' => get_lang('FlatView') . '_' . api_get_utc_datetime(), 'pdf_title' => $title, 'course_code' => $course_code, 'add_signatures' => true);
    $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
    $pdf = new PDF($page_format, $params['orientation'], $params);
    $pdf->html_to_pdf_with_template($table->toHtml());
    exit;
}
if (isset($_GET['print'])) {
    $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks, $params, $mainCourseCategory[0]);
    echo print_table($printable_data[1], $printable_data[0], get_lang('FlatView'), $cat[0]->get_name());
    exit;
}
if (!empty($_GET['export_report']) && $_GET['export_report'] == 'export_report') {
    if (api_is_platform_admin() || api_is_course_admin() || api_is_course_coach() || $isDrhOfCourse) {
        $user_id = null;
        if (empty($_SESSION['export_user_fields'])) {
            $_SESSION['export_user_fields'] = false;
        }
        if (!api_is_allowed_to_edit(false, false) and !api_is_course_tutor()) {
            $user_id = api_get_user_id();
        }
        require_once 'gradebook_result.class.php';
        $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks, $params, $mainCourseCategory[0]);
        switch ($_GET['export_format']) {
            case 'xls':
                $export = new GradeBookResult();
                $export->exportCompleteReportXLS($printable_data);
                break;
            case 'doc':
                $export = new GradeBookResult();
                $export->exportCompleteReportDOC($printable_data);
                exit;
                break;
            case 'csv':
            default:
                $export = new GradeBookResult();
                $export->exportCompleteReportCSV($printable_data);
                exit;
if (isset($_GET['print'])) {
    $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks);
    echo print_table($printable_data[1], $printable_data[0], get_lang('FlatView'), $cat[0]->get_name());
    exit;
}
if (!empty($_GET['export_report']) && $_GET['export_report'] == 'export_report') {
    if (api_is_platform_admin() || api_is_course_admin() || api_is_course_coach()) {
        $user_id = null;
        if (empty($_SESSION['export_user_fields'])) {
            $_SESSION['export_user_fields'] = false;
        }
        if (!api_is_allowed_to_edit(false, false) and !api_is_course_tutor()) {
            $user_id = api_get_user_id();
        }
        require_once 'gradebook_result.class.php';
        $printable_data = get_printable_data($cat[0], $users, $alleval, $alllinks);
        switch ($_GET['export_format']) {
            case 'xls':
                $export = new GradeBookResult();
                $export->exportCompleteReportXLS($printable_data);
                break;
            case 'doc':
                $export = new GradeBookResult();
                $export->exportCompleteReportDOC($printable_data);
                exit;
                break;
            case 'csv':
            default:
                $export = new GradeBookResult();
                $export->exportCompleteReportCSV($printable_data);
                exit;