/**
     * Displays the header for the gradebook containing the navigation tree and links
     * @param Category $catobj
     * @param int $showtree '1' will show the browse tree and naviation buttons
     * @param boolean $is_course_admin
     * @param boolean $is_platform_admin
     * @param boolean Whether to show or not the link to add a new qualification
     * (we hide it in case of the course-embedded tool where we have only one
     * calification per course or session)
     * @param boolean Whether to show or not the link to add a new item inside
     * the qualification (we hide it in case of the course-embedded tool
     * where we have only one calification per course or session)
     * @return void Everything is printed on screen upon closing
     */
    static function display_header_gradebook($catobj, $showtree, $selectcat, $is_course_admin, $is_platform_admin, $simple_search_form, $show_add_qualification = true, $show_add_link = true, $certificateLinkInfo = null)
    {
        // Student.
        $status = CourseManager::get_user_in_course_status(api_get_user_id(), api_get_course_id());
        $objcat = new Category();
        $course_id = Database::get_course_by_category($selectcat);
        $message_resource = $objcat->show_message_resource_delete($course_id);
        $grade_model_id = $catobj->get_grade_model_id();
        $header = null;
        //@todo move these in a function
        $sum_categories_weight_array = array();
        if (isset($catobj) && !empty($catobj)) {
            $categories = Category::load(null, null, null, $catobj->get_id());
            if (!empty($categories)) {
                foreach ($categories as $category) {
                    $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
                }
            } else {
                $sum_categories_weight_array[$catobj->get_id()] = $catobj->get_weight();
            }
        }
        if (!$is_course_admin && $status != 1 && $selectcat != 0) {
            $user_id = api_get_user_id();
            $catcourse = Category::load($catobj->get_id());
            $main_weight = $catcourse[0]->get_weight();
            $scoredisplay = ScoreDisplay::instance();
            //$categories = Category::getCategories($catcourse[0]->get_id());
            // generating the total score for a course
            /*if (count($categories) > 0) {
                  foreach ($categories as $category) {
                      $allevals = $category->get_evaluations($user_id, true);
                      $alllinks = $category->get_links($user_id, true);
                      $catEvalsLinks = array_merge($allevals, $alllinks);
                  }
              }*/
            $allevals = $catcourse[0]->get_evaluations($user_id, true);
            $alllinks = $catcourse[0]->get_links($user_id, true);
            $allEvalsLinks = array_merge($allevals, $alllinks);
            $item_value_total = 0;
            $scoreinfo = null;
            for ($count = 0; $count < count($allEvalsLinks); $count++) {
                $item = $allEvalsLinks[$count];
                $score = $item->calc_score($user_id);
                if (!empty($score)) {
                    $divide = $score[1] == 0 ? 1 : $score[1];
                    //$sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
                    //$item_value = $score[0] / $divide * $item->get_weight() / $sub_cat_percentage * $sub_cat_percentage / $main_weight * $main_weight;
                    $item_value = $score[0] / $divide * $item->get_weight();
                    //var_dump($score[0], $divide, $item->get_weight(), $sub_cat_percentage, $main_weight, $item_value);
                    $item_value_total += $item_value;
                }
            }
            $item_total = $main_weight;
            $total_score = array($item_value_total, $item_total);
            $scorecourse_display = $scoredisplay->display_score($total_score, SCORE_DIV_PERCENT);
            if (!$catobj->get_id() == '0' && !isset($_GET['studentoverview']) && !isset($_GET['search'])) {
                $certificateLink = null;
                if (!empty($certificateLinkInfo) && isset($certificateLinkInfo['certificate_link'])) {
                    $certificateLink .= '<span style="float:right"> ' . $certificateLinkInfo['certificate_link'] . "</span>";
                }
                $scoreinfo .= '<h4>' . get_lang('Total') . ' : ' . $scorecourse_display . $certificateLink . '</h4>';
            }
            Display::display_normal_message($scoreinfo, false);
        }
        // show navigation tree and buttons?
        if ($showtree == '1' || isset($_GET['studentoverview'])) {
            $header = '<div class="actions"><table>';
            $header .= '<tr>';
            if (!$selectcat == '0') {
                $header .= '<td><a href="' . api_get_self() . '?selectcat=' . $catobj->get_parent_id() . '">' . Display::return_icon('back.png', get_lang('BackTo') . ' ' . get_lang('RootCat'), '', ICON_SIZE_MEDIUM) . '</a></td>';
            }
            $header .= '<td>' . get_lang('CurrentCategory') . '</td>' . '<td><form name="selector"><select name="selectcat" onchange="document.selector.submit()">';
            $cats = Category::load();
            $tree = $cats[0]->get_tree();
            unset($cats);
            $line = null;
            foreach ($tree as $cat) {
                for ($i = 0; $i < $cat[2]; $i++) {
                    $line .= '&mdash;';
                }
                $line = isset($line) ? $line : '';
                if (isset($_GET['selectcat']) && $_GET['selectcat'] == $cat[0]) {
                    $header .= '<option selected value=' . $cat[0] . '>' . $line . ' ' . $cat[1] . '</option>';
                } else {
                    $header .= '<option value=' . $cat[0] . '>' . $line . ' ' . $cat[1] . '</option>';
                }
                $line = '';
            }
            $header .= '</select></form></td>';
            if (!empty($simple_search_form) && $message_resource === false) {
                $header .= '<td style="vertical-align: top;">' . $simple_search_form->toHtml() . '</td>';
            } else {
                $header .= '<td></td>';
            }
            if ($is_course_admin && $message_resource === false && $_GET['selectcat'] != 0) {
                /* $header .= '<td style="vertical-align: top;"><a href="gradebook_flatview.php?'.api_get_cidreq().'&selectcat=' . $catobj->get_id() . '"><img src="../img/view_list.gif" alt="' . get_lang('FlatView') . '" /> ' . get_lang('FlatView') . '</a>';
                   if ($is_course_admin && $message_resource===false) {
                   $header .= '<td style="vertical-align: top;"><a href="gradebook_scoring_system.php?'.api_get_cidreq().'&selectcat=' . $catobj->get_id() .'"><img src="../img/acces_tool.gif" alt="' . get_lang('ScoreEdit') . '" /> ' . get_lang('ScoreEdit') . '</a>';
                   } */
            } elseif (!isset($_GET['studentoverview'])) {
                if ($message_resource === false) {
                    //$header .= '<td style="vertical-align: top;"><a href="'.api_get_self().'?'.api_get_cidreq().'&studentoverview=&selectcat=' . $catobj->get_id() . '"><img src="../img/view_list.gif" alt="' . get_lang('FlatView') . '" /> ' . get_lang('FlatView') . '</a>';
                }
            } else {
                $header .= '<td style="vertical-align: top;"><a href="' . api_get_self() . '?' . api_get_cidreq() . '&studentoverview=&exportpdf=&selectcat=' . $catobj->get_id() . '" target="_blank">
							<img src="../img/icons/32/pdf.png" alt="' . get_lang('ExportPDF') . '" /> ' . get_lang('ExportPDF') . '</a>';
            }
            $header .= '</td></tr>';
            $header .= '</table></div>';
        }
        // for course admin & platform admin add item buttons are added to the header
        $header .= '<div class="actions">';
        $my_category = $catobj->shows_all_information_an_category($catobj->get_id());
        $user_id = api_get_user_id();
        $my_api_cidreq = api_get_cidreq();
        //$course_code = $my_category['course_code'];
        //$status_user = api_get_status_of_user_in_course ($user_id,$course_code);
        if (api_is_allowed_to_edit(null, true)) {
            if (empty($grade_model_id) || $grade_model_id == -1) {
                $header .= '<a href="gradebook_add_cat.php?' . api_get_cidreq() . '&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('new_folder.png', get_lang('AddGradebook'), array(), ICON_SIZE_MEDIUM) . '</a></td>';
            }
            if ($selectcat == '0') {
            } else {
                $my_category = $catobj->shows_all_information_an_category($catobj->get_id());
                if ($my_api_cidreq == '') {
                    $my_api_cidreq = 'cidReq=' . $my_category['course_code'];
                }
                if ($show_add_link && !$message_resource) {
                    $header .= '<td><a href="gradebook_add_eval.php?' . $my_api_cidreq . '&selectcat=' . $catobj->get_id() . '" >' . Display::return_icon('new_evaluation.png', get_lang('NewEvaluation'), '', ICON_SIZE_MEDIUM) . '</a>';
                    $cats = Category::load($selectcat);
                    if ($cats[0]->get_course_code() != null && !$message_resource) {
                        $header .= '<td><a href="gradebook_add_link.php?' . $my_api_cidreq . '&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('new_online_evaluation.png', get_lang('MakeLink'), '', ICON_SIZE_MEDIUM) . '</a>';
                    } else {
                        $header .= '<td><a href="gradebook_add_link_select_course.php?' . $my_api_cidreq . '&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('new_online_evaluation.png', get_lang('MakeLink'), '', ICON_SIZE_MEDIUM) . '</a>';
                    }
                }
                if (!$message_resource) {
                    $header .= '<td style="vertical-align: top;"><a href="gradebook_flatview.php?' . $my_api_cidreq . '&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('stats.png', get_lang('FlatView'), '', ICON_SIZE_MEDIUM) . '</a>';
                    $header .= '<td style="vertical-align: top;"><a href="gradebook_display_certificate.php?' . $my_api_cidreq . '&amp;cat_id=' . (int) $_GET['selectcat'] . '">' . Display::return_icon('certificate_list.png', get_lang('GradebookSeeListOfStudentsCertificates'), '', ICON_SIZE_MEDIUM) . '</a>';
                    // Right icons
                    $modify_icons = '<a href="gradebook_edit_cat.php?editcat=' . $catobj->get_id() . '&amp;cidReq=' . $catobj->get_course_code() . '&id_session=' . $catobj->get_session_id() . '">' . Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM) . '</a>';
                    $modify_icons .= '<a href="../document/document.php?curdirpath=/certificates&' . $my_api_cidreq . '&origin=gradebook&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('certificate.png', get_lang('AttachCertificate'), '', ICON_SIZE_MEDIUM) . '</a>';
                    if (empty($categories)) {
                        $modify_icons .= '<a href="gradebook_edit_all.php?id_session=' . api_get_session_id() . '&amp;' . $my_api_cidreq . '&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('percentage.png', get_lang('EditAllWeights'), '', ICON_SIZE_MEDIUM) . '</a>';
                    }
                    $score_display_custom = api_get_setting('gradebook_score_display_custom');
                    if (api_get_setting('teachers_can_change_score_settings') == 'true' && $score_display_custom['my_display_custom'] == 'true') {
                        $modify_icons .= '<a href="gradebook_scoring_system.php?' . $my_api_cidreq . '&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('ranking.png', get_lang('ScoreEdit'), '', ICON_SIZE_MEDIUM) . '</a>';
                    }
                    $header .= Display::div($modify_icons, array('class' => 'right'));
                }
            }
        } elseif (isset($_GET['search'])) {
            $header .= '<b>' . get_lang('SearchResults') . ' :</b>';
        }
        $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(api_get_user_id(), api_get_course_info());
        if ($isDrhOfCourse) {
            $header .= '<td style="vertical-align: top;"><a href="gradebook_flatview.php?' . $my_api_cidreq . '&selectcat=' . $catobj->get_id() . '">' . Display::return_icon('stats.png', get_lang('FlatView'), '', ICON_SIZE_MEDIUM) . '</a>';
            /*$header .= '<td style="vertical-align: top;"><a href="gradebook_display_certificate.php?' . $my_api_cidreq . '&amp;cat_id=' . (int) $_GET['selectcat'] . '">' .
              Display::return_icon('certificate_list.png', get_lang('GradebookSeeListOfStudentsCertificates'), '', ICON_SIZE_MEDIUM) . '</a>';*/
        }
        $header .= '</div>';
        echo $header;
        if (api_is_allowed_to_edit(null, true)) {
            $weight = intval($catobj->get_weight()) > 0 ? $catobj->get_weight() : 0;
            $weight = get_lang('TotalWeight') . ' : ' . $weight;
            $min_certification = intval($catobj->get_certificate_min_score() > 0) ? $catobj->get_certificate_min_score() : 0;
            $min_certification = get_lang('CertificateMinScore') . ' : ' . $min_certification;
            $edit_icon = '<a class="right_link" href="gradebook_edit_cat.php?editcat=' . $catobj->get_id() . '&amp;cidReq=' . $catobj->get_course_code() . '&id_session=' . $catobj->get_session_id() . '">' . Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . '</a>';
            //$msg = Display::tag('h3', $weight.' - '.$min_certification);
            $msg = Display::tag('h4', $weight . ' - ' . $min_certification . $edit_icon);
            //@todo show description
            $description = $catobj->get_description() == "" || is_null($catobj->get_description()) ? '' : '<strong>' . get_lang('GradebookDescriptionLog') . '</strong>' . ': ' . $catobj->get_description();
            Display::display_normal_message($msg, false);
            if (!empty($description)) {
                echo Display::div($description, array());
            }
        }
    }
 /**
  * Builds the course or platform admin icons to edit a category
  * @param Category $cat category
  * @param Category $selectcat id of selected category
  */
 public static function build_edit_icons_cat($cat, $selectcat)
 {
     $show_message = $cat->show_message_resource_delete($cat->get_course_code());
     $grade_model_id = $selectcat->get_grade_model_id();
     $selectcat = $selectcat->get_id();
     $modify_icons = null;
     if ($show_message === false) {
         $visibility_icon = $cat->is_visible() == 0 ? 'invisible' : 'visible';
         $visibility_command = $cat->is_visible() == 0 ? 'set_visible' : 'set_invisible';
         $modify_icons .= '<a class="view_children" data-cat-id="' . $cat->get_id() . '" href="javascript:void(0);">' . Display::return_icon('view_more_stats.gif', get_lang('Show'), '', ICON_SIZE_SMALL) . '</a>';
         if (!api_is_allowed_to_edit(null, true)) {
             $modify_icons .= Display::url(Display::return_icon('stats.png', get_lang('FlatView'), '', ICON_SIZE_SMALL), 'personal_stats.php?' . http_build_query(['selectcat' => $cat->get_id()]) . '&' . api_get_cidreq(), ['class' => 'ajax', 'data-title' => get_lang('FlatView')]);
         }
         if (api_is_allowed_to_edit(null, true)) {
             // Locking button
             if (api_get_setting('gradebook_locking_enabled') == 'true') {
                 if ($cat->is_locked()) {
                     if (api_is_platform_admin()) {
                         $modify_icons .= '&nbsp;<a onclick="javascript:if (!confirm(\'' . addslashes(get_lang('ConfirmToUnlockElement')) . '\')) return false;" href="' . api_get_self() . '?' . api_get_cidreq() . '&category_id=' . $cat->get_id() . '&action=unlock">' . Display::return_icon('lock.png', get_lang('UnLockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
                     } else {
                         $modify_icons .= '&nbsp;<a href="#">' . Display::return_icon('lock_na.png', get_lang('GradebookLockedAlert'), '', ICON_SIZE_SMALL) . '</a>';
                     }
                     $modify_icons .= '&nbsp;<a href="gradebook_flatview.php?export_pdf=category&selectcat=' . $cat->get_id() . '" >' . Display::return_icon('pdf.png', get_lang('ExportToPDF'), '', ICON_SIZE_SMALL) . '</a>';
                 } else {
                     $modify_icons .= '&nbsp;<a onclick="javascript:if (!confirm(\'' . addslashes(get_lang('ConfirmToLockElement')) . '\')) return false;" href="' . api_get_self() . '?' . api_get_cidreq() . '&category_id=' . $cat->get_id() . '&action=lock">' . Display::return_icon('unlock.png', get_lang('LockEvaluation'), '', ICON_SIZE_SMALL) . '</a>';
                     $modify_icons .= '&nbsp;<a href="#" >' . Display::return_icon('pdf_na.png', get_lang('ExportToPDF'), '', ICON_SIZE_SMALL) . '</a>';
                 }
             }
             if (empty($grade_model_id) || $grade_model_id == -1) {
                 if ($cat->is_locked() && !api_is_platform_admin()) {
                     $modify_icons .= Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
                 } else {
                     $modify_icons .= '<a href="gradebook_edit_cat.php?' . 'editcat=' . $cat->get_id() . '&cidReq=' . $cat->get_course_code() . '&id_session=' . $cat->get_session_id() . '">' . Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL) . '</a>';
                 }
             }
             $modify_icons .= '<a href="gradebook_edit_all.php?selectcat=' . $cat->get_id() . '&cidReq=' . $cat->get_course_code() . '&id_session=' . $cat->get_session_id() . '">' . Display::return_icon('percentage.png', get_lang('EditAllWeights'), '', ICON_SIZE_SMALL) . '</a>';
             $modify_icons .= '<a href="gradebook_flatview.php?selectcat=' . $cat->get_id() . '&cidReq=' . $cat->get_course_code() . '&id_session=' . $cat->get_session_id() . '">' . Display::return_icon('stats.png', get_lang('FlatView'), '', ICON_SIZE_SMALL) . '</a>';
             $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?visiblecat=' . $cat->get_id() . '&' . $visibility_command . '=&selectcat=' . $selectcat . '&cidReq=' . $cat->get_course_code() . '&id_session=' . $cat->get_session_id() . '">' . Display::return_icon($visibility_icon . '.png', get_lang('Visible'), '', ICON_SIZE_SMALL) . '</a>';
             //no move ability for root categories
             if ($cat->is_movable()) {
                 /* $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?movecat=' . $cat->get_id() . '&amp;selectcat=' . $selectcat . ' &amp;cidReq='.$cat->get_course_code().'">
                    <img src="../img/icons/22/move.png" border="0" title="' . get_lang('Move') . '" alt="" /></a>'; */
             } else {
                 //$modify_icons .= '&nbsp;<img src="../img/deplacer_fichier_na.gif" border="0" title="' . get_lang('Move') . '" alt="" />';
             }
             if ($cat->is_locked() && !api_is_platform_admin()) {
                 $modify_icons .= Display::return_icon('delete_na.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL);
             } else {
                 $modify_icons .= '&nbsp;<a href="' . api_get_self() . '?deletecat=' . $cat->get_id() . '&selectcat=' . $selectcat . '&cidReq=' . $cat->get_course_code() . '&id_session=' . $cat->get_session_id() . '" onclick="return confirmation();">' . Display::return_icon('delete.png', get_lang('DeleteAll'), '', ICON_SIZE_SMALL) . '</a>';
             }
         }
         return $modify_icons;
     }
 }