/**
  *  Generates an HTML Certificate and fills the path_certificate field in the DB
  **/
 public function generate($params = array())
 {
     //The user directory should be set
     if (empty($this->certification_user_path) && $this->force_certificate_generation == false) {
         return false;
     }
     require_once api_get_path(SYS_CODE_PATH) . 'gradebook/lib/be.inc.php';
     require_once api_get_path(SYS_CODE_PATH) . 'gradebook/lib/gradebook_functions.inc.php';
     require_once api_get_path(SYS_CODE_PATH) . 'gradebook/lib/scoredisplay.class.php';
     $params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
     $my_category = Category::load($this->certificate_data['cat_id']);
     if (isset($my_category[0]) && $my_category[0]->is_certificate_available($this->user_id)) {
         $user = api_get_user_info($this->user_id);
         $scoredisplay = ScoreDisplay::instance();
         $scorecourse = $my_category[0]->calc_score($this->user_id);
         $scorecourse_display = isset($scorecourse) ? $scoredisplay->display_score($scorecourse, SCORE_AVERAGE) : get_lang('NoResultsAvailable');
         // Prepare all necessary variables:
         $organization_name = api_get_setting('Institution');
         //$portal_name         = api_get_setting('siteName');
         $stud_fn = $user['firstname'];
         $stud_ln = $user['lastname'];
         //@todo this code is not needed
         $certif_text = sprintf(get_lang('CertificateWCertifiesStudentXFinishedCourseYWithGradeZ'), $organization_name, $stud_fn . ' ' . $stud_ln, $my_category[0]->get_name(), $scorecourse_display);
         $certif_text = str_replace("\\n", "\n", $certif_text);
         //If the gradebook is related to skills we added the skills to the user
         $skill = new Skill();
         $skill->add_skill_to_user($this->user_id, $this->certificate_data['cat_id']);
         if (is_dir($this->certification_user_path)) {
             if (!empty($this->certificate_data)) {
                 $new_content_html = get_user_certificate_content($this->user_id, $my_category[0]->get_course_code(), false, $params['hide_print_button']);
                 if ($my_category[0]->get_id() == strval(intval($this->certificate_data['cat_id']))) {
                     $name = $this->certificate_data['path_certificate'];
                     $my_path_certificate = $this->certification_user_path . basename($name);
                     if (file_exists($my_path_certificate) && !empty($name) && !is_dir($my_path_certificate) && $this->force_certificate_generation == false) {
                         //Seems that the file was already generated
                         return true;
                     } else {
                         // Creating new name
                         $name = md5($this->user_id . $this->certificate_data['cat_id']) . '.html';
                         $my_path_certificate = $this->certification_user_path . $name;
                         $path_certificate = '/' . $name;
                         //Getting QR filename
                         $file_info = pathinfo($path_certificate);
                         $qr_code_filename = $this->certification_user_path . $file_info['filename'] . '_qr.png';
                         $my_new_content_html = str_replace('((certificate_barcode))', Display::img($this->certification_web_user_path . $file_info['filename'] . '_qr.png', 'QR'), $new_content_html['content']);
                         $my_new_content_html = mb_convert_encoding($my_new_content_html, 'UTF-8', api_get_system_encoding());
                         $result = @file_put_contents($my_path_certificate, $my_new_content_html);
                         if ($result) {
                             //Updating the path
                             self::update_user_info_about_certificate($this->certificate_data['cat_id'], $this->user_id, $path_certificate);
                             $this->certificate_data['path_certificate'] = $path_certificate;
                             if ($this->html_file_is_generated()) {
                                 if (!empty($file_info)) {
                                     $text = $this->parse_certificate_variables($new_content_html['variables']);
                                     $this->generate_qr($text, $qr_code_filename);
                                 }
                             }
                         }
                         return $result;
                     }
                 }
             }
         }
     }
     return false;
 }
Esempio n. 2
0
 /**
  * @param int $category_id
  * @param int $user_id
  * @return bool|string
  */
 public static function register_user_certificate($category_id, $user_id)
 {
     $courseId = api_get_course_int_id();
     $courseCode = api_get_course_id();
     $sessionId = api_get_session_id();
     // Generating the total score for a course
     $cats_course = Category::load($category_id, null, null, null, null, $sessionId, false);
     /** @var Category $category */
     $category = $cats_course[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 $subCategory) {
                 $sum_categories_weight_array[$subCategory->get_id()] = $subCategory->get_weight();
             }
         } else {
             $sum_categories_weight_array[$category_id] = $cats_course[0]->get_weight();
         }
     }
     $main_weight = $cats_course[0]->get_weight();
     $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);
     // A student always sees only the teacher's repartition
     $scoretotal_display = $scoredisplay->display_score($scoretotal, SCORE_DIV_PERCENT);
     if (!self::userFinishedCourse($user_id, $cats_course[0], 0, $courseCode, $sessionId, true)) {
         return false;
     }
     $skillToolEnabled = api_get_setting('skill.allow_skills_tool') == 'true';
     $userHasSkills = false;
     if ($skillToolEnabled) {
         if (!$category->getGenerateCertificates()) {
             $skill = new Skill();
             $skill->add_skill_to_user($user_id, $category_id, $courseId, $sessionId);
         }
         $objSkillRelUser = new SkillRelUser();
         $userSkills = $objSkillRelUser->get_user_skills($user_id, $courseId, $sessionId);
         $userHasSkills = !empty($userSkills);
         if (!$category->getGenerateCertificates() && $userHasSkills) {
             return ['badge_link' => Display::url(get_lang('DownloadBadges'), api_get_path(WEB_CODE_PATH) . "gradebook/get_badges.php?user={$user_id}", array('target' => '_blank', 'class' => 'btn btn-default'))];
         }
     }
     $my_certificate = GradebookUtils::get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
     if (empty($my_certificate)) {
         GradebookUtils::register_user_info_about_certificate($category_id, $user_id, $my_score_in_gradebook, api_get_utc_datetime());
         $my_certificate = GradebookUtils::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(Display::returnFontAwesomeIcon('download') . ' ' . get_lang('DownloadCertificate'), $url, array('target' => '_blank', 'class' => 'btn btn-sm btn-primary'));
             $exportToPDF = Display::url(Display::return_icon('pdf.png', get_lang('ExportToPDF'), array(), ICON_SIZE_MEDIUM), "{$url}&action=export");
             $hideExportLink = api_get_setting('hide_certificate_export_link');
             $hideExportLinkStudent = api_get_setting('hide_certificate_export_link_students');
             if ($hideExportLink === 'true' || api_is_student() && $hideExportLinkStudent === 'true') {
                 $exportToPDF = null;
             }
             $html = array('certificate_link' => $certificates, 'pdf_link' => $exportToPDF, 'pdf_url' => "{$url}&action=export");
             if ($skillToolEnabled && $userHasSkills) {
                 $html['badge_link'] = Display::url(get_lang('DownloadBadges'), api_get_path(WEB_CODE_PATH) . "gradebook/get_badges.php?user={$user_id}", array('target' => '_blank', 'class' => 'btn btn-default'));
             }
         }
         return $html;
     }
 }