$users = GradebookUtils::find_students($keyword);
} else {
    if (isset($alleval) && isset($alllinks)) {
        $users = GradebookUtils::get_all_users($alleval, $alllinks);
    } else {
        $users = null;
    }
}
$offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
$addparams = array('selectcat' => $cat[0]->get_id());
if (isset($_GET['search'])) {
    $addparams['search'] = $keyword;
}
// Main course category
$mainCourseCategory = Category::load(null, null, api_get_course_id(), null, null, api_get_session_id());
$flatviewtable = new FlatViewTable($cat[0], $users, $alleval, $alllinks, true, $offset, $addparams, $mainCourseCategory[0]);
$flatviewtable->setAutoFill(false);
$parameters = array('selectcat' => intval($_GET['selectcat']));
$flatviewtable->set_additional_parameters($parameters);
$params = array();
if (isset($_GET['export_pdf']) && $_GET['export_pdf'] == 'category') {
    $params['only_total_category'] = true;
    $params['join_firstname_lastname'] = true;
    $params['show_official_code'] = true;
    $params['export_pdf'] = true;
    if ($cat[0]->is_locked() == true || api_is_platform_admin()) {
        Display::set_header(null, false, false);
        GradebookUtils::export_pdf_flatview($flatviewtable, $cat, $users, $alleval, $alllinks, $params, $mainCourseCategory[0]);
    }
}
if (isset($_GET['exportpdf'])) {
if ($simple_search_form->validate() && empty($keyword)) {
    $keyword = $values['keyword'];
}
if (!empty($keyword)) {
    $users = find_students($keyword);
} else {
    if (isset($alleval) && isset($alllinks)) {
        $users = get_all_users($alleval, $alllinks);
    } else {
        $users = null;
    }
}
$offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
// Main course category
$mainCourseCategory = Category::load(null, null, api_get_course_id(), null, null, api_get_session_id());
$flatviewtable = new FlatViewTable($cat[0], $users, $alleval, $alllinks, true, $offset, $addparams, $mainCourseCategory[0]);
$parameters = array('selectcat' => intval($_GET['selectcat']));
$flatviewtable->set_additional_parameters($parameters);
$params = array();
if (isset($_GET['export_pdf']) && $_GET['export_pdf'] == 'category') {
    $params['only_total_category'] = true;
    $params['join_firstname_lastname'] = true;
    $params['show_official_code'] = true;
    $params['export_pdf'] = true;
    if ($cat[0]->is_locked() == true || api_is_platform_admin()) {
        Display::set_header(null, false, false);
        export_pdf_flatview($flatviewtable, $cat, $users, $alleval, $alllinks, $params, $mainCourseCategory[0]);
    }
}
if (isset($_GET['exportpdf'])) {
    $interbreadcrumb[] = array('url' => api_get_self() . '?selectcat=' . Security::remove_XSS($_GET['selectcat']), 'name' => get_lang('FlatView'));
 /**
  * @param FlatViewTable $flatviewtable
  * @param Category $cat
  * @param $users
  * @param $alleval
  * @param $alllinks
  * @param array $params
  * @param null $mainCourseCategory
  */
 public static function export_pdf_flatview($flatviewtable, $cat, $users, $alleval, $alllinks, $params = array(), $mainCourseCategory = null)
 {
     // Getting data
     $printable_data = self::get_printable_data($cat[0], $users, $alleval, $alllinks, $params, $mainCourseCategory);
     // 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(false);
         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) {
         if (!is_array($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"');
     }
     $pdfParams = 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, $page_format, $pdfParams);
     $pdf->html_to_pdf_with_template($flatviewtable->return_table());
     exit;
 }
}
if ($simple_search_form->validate() && empty($keyword)) {
    $keyword = $values['keyword'];
}
if (!empty($keyword)) {
    $users = find_students($keyword);
} else {
    if (isset($alleval) && isset($alllinks)) {
        $users = get_all_users($alleval, $alllinks);
    } else {
        $users = null;
    }
}
$offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
//$flatviewtable = new FlatViewTable($cat[0], $users, $alleval, $alllinks, true, $offset, $addparams);
$flatviewtable = new FlatViewTable($cat[0], $users, $alleval, $alllinks, true, $offset);
$parameters = array('selectcat' => intval($_GET['selectcat']));
$flatviewtable->set_additional_parameters($parameters);
if (isset($_GET['export_pdf']) && $_GET['export_pdf'] == 'category') {
    $params = array();
    $params['only_total_category'] = true;
    $params['join_firstname_lastname'] = true;
    $params['show_official_code'] = true;
    $params['export_pdf'] = true;
    if ($cat[0]->is_locked() == true || api_is_platform_admin()) {
        Display::set_header(null, false, false);
        export_pdf_flatview($cat, $users, $alleval, $alllinks, $params);
    }
}
if (isset($_GET['exportpdf'])) {
    $interbreadcrumb[] = array('url' => api_get_self() . '?selectcat=' . Security::remove_XSS($_GET['selectcat']), 'name' => get_lang('FlatView'));