static function register_user_certificate($category_id, $user_id)
 {
     // generating the total score for a course
     $cats_course = Category::load($category_id, null, null, null, null, null, false);
     $alleval_course = $cats_course[0]->get_evaluations($user_id, true);
     $alllink_course = $cats_course[0]->get_links($user_id, true);
     $evals_links = array_merge($alleval_course, $alllink_course);
     $item_total = 0;
     //@todo move these in a function
     $sum_categories_weight_array = array();
     if (isset($cats_course) && !empty($cats_course)) {
         $categories = Category::load(null, null, null, $category_id);
         if (!empty($categories)) {
             foreach ($categories as $category) {
                 $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
             }
         } else {
             $sum_categories_weight_array[$category_id] = $cats_course[0]->get_weight();
         }
     }
     $main_weight = $cats_course[0]->get_weight();
     $item_total_value = 0;
     $item_value = 0;
     for ($count = 0; $count < count($evals_links); $count++) {
         $item = $evals_links[$count];
         $score = $item->calc_score($user_id);
         $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 / $main_weight;
         $item_total_value += $item_value;
     }
     $item_total_value = (double) $item_total_value;
     $cattotal = Category::load($category_id);
     $scoretotal = $cattotal[0]->calc_score($user_id);
     //Do not remove this the gradebook/lib/fe/gradebooktable.class.php file load this variable as a global
     $scoredisplay = ScoreDisplay::instance();
     $my_score_in_gradebook = $scoredisplay->display_score($scoretotal, SCORE_SIMPLE);
     //Show certificate
     $certificate_min_score = $cats_course[0]->get_certificate_min_score();
     $scoretotal_display = $scoredisplay->display_score($scoretotal, SCORE_DIV_PERCENT);
     //a student always sees only the teacher's repartition
     if (isset($certificate_min_score) && $item_total_value >= $certificate_min_score) {
         $my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
         if (empty($my_certificate)) {
             register_user_info_about_certificate($category_id, $user_id, $my_score_in_gradebook, api_get_utc_datetime());
             $my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
         }
         if (!empty($my_certificate)) {
             $certificate_obj = new Certificate($my_certificate['id']);
             $url = Certificate::getCertificatePublicURL($my_certificate['id']);
             $certificates = Display::url(Display::return_icon('certificate.png', get_lang('Certificates'), array(), 32), $url, array('target' => '_blank'));
             $html = '<div class="actions" align="right">';
             $html .= Display::url($url, $url, array('target' => '_blank'));
             $html .= $certificates;
             $html .= '</div>';
             return $html;
         }
     } else {
         return false;
     }
 }
 /**
  * @param int $category_id
  * @param int $user_id
  * @return bool|string
  */
 public static function register_user_certificate($category_id, $user_id)
 {
     // Generating the total score for a course
     $cats_course = Category::load($category_id, null, null, null, null, api_get_session_id(), false);
     /** @var Category $category */
     $category = $cats_course[0];
     $alleval_course = $category->get_evaluations($user_id, true);
     $alllink_course = $category->get_links($user_id, true);
     $evals_links = array_merge($alleval_course, $alllink_course);
     //@todo move these in a function
     $sum_categories_weight_array = array();
     if (isset($cats_course) && !empty($cats_course)) {
         $categories = Category::load(null, null, null, $category_id);
         if (!empty($categories)) {
             foreach ($categories as $category) {
                 $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
             }
         } else {
             $sum_categories_weight_array[$category_id] = $cats_course[0]->get_weight();
         }
     }
     $main_weight = $cats_course[0]->get_weight();
     $item_total_value = 0;
     for ($count = 0; $count < count($evals_links); $count++) {
         /** @var AbstractLink $item */
         $item = $evals_links[$count];
         $score = $item->calc_score($user_id);
         $item_value = 0;
         if (!empty($score)) {
             $divide = $score[1] == 0 ? 1 : $score[1];
             $item_value = $score[0] / $divide * $item->get_weight();
         }
         $item_total_value += $item_value;
     }
     $item_total_value = (double) $item_total_value;
     $cattotal = Category::load($category_id);
     $scoretotal = $cattotal[0]->calc_score($user_id);
     //Do not remove this the gradebook/lib/fe/gradebooktable.class.php file load this variable as a global
     $scoredisplay = ScoreDisplay::instance();
     $my_score_in_gradebook = $scoredisplay->display_score($scoretotal, SCORE_SIMPLE);
     // Show certificate
     $certificate_min_score = $cats_course[0]->get_certificate_min_score();
     // A student always sees only the teacher's repartition
     $scoretotal_display = $scoredisplay->display_score($scoretotal, SCORE_DIV_PERCENT);
     if (isset($certificate_min_score) && $item_total_value >= $certificate_min_score) {
         $my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
         if (empty($my_certificate)) {
             register_user_info_about_certificate($category_id, $user_id, $my_score_in_gradebook, api_get_utc_datetime());
             $my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
         }
         $html = array();
         if (!empty($my_certificate)) {
             $certificate_obj = new Certificate($my_certificate['id']);
             $fileWasGenerated = $certificate_obj->html_file_is_generated();
             if (!empty($fileWasGenerated)) {
                 $url = api_get_path(WEB_PATH) . 'certificates/index.php?id=' . $my_certificate['id'];
                 $certificates = Display::url('&nbsp;' . get_lang('DownloadCertificate'), $url, array('target' => '_blank', 'class' => 'btn'));
                 $exportToPDF = Display::url(Display::return_icon('pdf.png', get_lang('ExportToPDF'), array(), ICON_SIZE_MEDIUM), "{$url}&action=export");
                 $html = array('certificate_link' => $certificates, 'pdf_link' => $exportToPDF);
             }
             return $html;
         }
     } else {
         return false;
     }
 }