Ver código fonte

Add custom certificate (cat_id = 0) see BT#11019

jmontoyaa 8 anos atrás
pai
commit
cdfe509e28

+ 206 - 100
main/inc/lib/certificate.lib.php

@@ -40,23 +40,21 @@ class Certificate extends Model
     /**
      * Constructor
      * @param int $certificate_id ID of the certificate.
+     * @param int $userId
      *
      * If no ID given, take user_id and try to generate one
      */
-    public function __construct($certificate_id = null)
+     public function __construct($certificate_id = 0, $userId = 0)
     {
         $this->table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
-        unset($this->certificate_data);
+        $this->user_id = !empty($userId) ? $userId : api_get_user_id();
 
-        if (isset($certificate_id)) {
+        if (!empty($certificate_id)) {
             $certificate = $this->get($certificate_id);
             if (!empty($certificate) && is_array($certificate)) {
                 $this->certificate_data = $certificate;
                 $this->user_id = $this->certificate_data['user_id'];
             }
-        } else {
-            //Try with the current user
-            $this->user_id = api_get_user_id();
         }
 
         if ($this->user_id) {
@@ -76,7 +74,7 @@ class Certificate extends Model
             }
         }
 
-        //Setting the qr and html variables
+        // Setting the qr and html variables
         if (isset($certificate_id) && !empty($this->certification_user_path)) {
             $pathinfo = pathinfo($this->certificate_data['path_certificate']);
             $this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
@@ -147,8 +145,11 @@ class Certificate extends Model
     }
 
     /**
-    *  Generates an HTML Certificate and fills the path_certificate field in the DB
-    **/
+     *  Generates an HTML Certificate and fills the path_certificate field in the DB
+     *
+     * @param array $params
+     * @return bool|int
+     */
     public function generate($params = array())
     {
         // The user directory should be set
@@ -160,106 +161,137 @@ class Certificate extends Model
 
         $params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
 
-        if (isset($this->certificate_data)) {
+        if (isset($this->certificate_data) && isset($this->certificate_data['cat_id'])) {
             $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
-
-            $courseId = api_get_course_int_id();
-            $sessionId = api_get_session_id();
-
-            $skill = new Skill();
-            $skill->add_skill_to_user(
-                $this->user_id,
-                $this->certificate_data['cat_id'],
-                $courseId,
-                $sessionId
-            );
-
-            if (is_dir($this->certification_user_path)) {
-                if (!empty($this->certificate_data)) {
-                    $new_content_html = GradebookUtils::get_user_certificate_content(
-                        $this->user_id,
-                        $my_category[0]->get_course_code(),
-                        $my_category[0]->get_session_id(),
-                        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
+            if (isset($my_category[0]) && $my_category[0]->is_certificate_available($this->user_id)) {
+                $courseId = api_get_course_int_id();
+                $sessionId = api_get_session_id();
+
+                $skill = new Skill();
+                $skill->add_skill_to_user(
+                    $this->user_id,
+                    $this->certificate_data['cat_id'],
+                    $courseId,
+                    $sessionId
+                );
+
+                if (is_dir($this->certification_user_path)) {
+                    if (!empty($this->certificate_data)) {
+                        $new_content_html = GradebookUtils::get_user_certificate_content(
+                            $this->user_id,
+                            $my_category[0]->get_course_code(),
+                            $my_category[0]->get_session_id(),
+                            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()
                                 );
-                                $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);
+                                $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 $result;
+                            }
                         }
                     }
                 }
             }
+        } else {
+            // General certificate
+
+            $name = md5($this->user_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';
+
+            $content = $this->generateCustomCertificate();
+
+            $my_new_content_html = str_replace(
+                '((certificate_barcode))',
+                Display::img(
+                    $this->certification_web_user_path.$file_info['filename'].'_qr.png',
+                    'QR'
+                ),
+                $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(
+                    0,
+                    $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;
@@ -279,8 +311,9 @@ class Certificate extends Model
     ) {
         $table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
         if (!UserManager::is_user_certified($cat_id, $user_id)) {
-            $sql='UPDATE '.$table_certificate.' SET path_certificate="'.Database::escape_string($path_certificate).'"
-                 WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
+            $sql = 'UPDATE '.$table_certificate.' SET 
+                        path_certificate="'.Database::escape_string($path_certificate).'"
+                    WHERE cat_id="'.intval($cat_id).'" AND user_id="'.intval($user_id).'" ';
             Database::query($sql);
         }
     }
@@ -429,4 +462,77 @@ class Certificate extends Model
         }
         exit;
     }
+
+    /**
+     * @return string
+     */
+    public function generateCustomCertificate()
+    {
+        $myCertificate = GradebookUtils::get_certificate_by_user_id(
+            0,
+            $this->user_id
+        );
+
+        if (empty($myCertificate)) {
+             GradebookUtils::register_user_info_about_certificate(
+                0,
+                $this->user_id,
+                100,
+                api_get_utc_datetime()
+            );
+        }
+
+        $userInfo = api_get_user_info($this->user_id);
+
+        $extraFieldValue = new ExtraFieldValue('user');
+        $value = $extraFieldValue->get_values_by_handler_and_field_variable($this->user_id, 'legal_accept');
+        $time = api_time_to_hms(Tracking::get_time_spent_on_the_platform($this->user_id));
+
+        $tplContent = new Template(null, false, false, false, false, false);
+        // variables for the default template
+        $tplContent->assign('complete_name', $userInfo['complete_name']);
+        $tplContent->assign('time_in_platform', $time);
+
+        $sessions = SessionManager::get_sessions_by_user($this->user_id);
+
+        $skill = new Skill();
+        $skills = $skill->getStudentSkills($this->user_id);
+
+        $tplContent->assign('terms_validation_date', api_get_local_time($value['value']));
+        $tplContent->assign('skills', $skills);
+        $tplContent->assign('sessions', $sessions);
+
+        $layoutContent = $tplContent->get_template('gradebook/custom_certificate.tpl');
+        $content = $tplContent->fetch($layoutContent);
+
+        return $content;
+    }
+
+    /**
+     *
+     */
+    public function generatePdfFromCustomCertificate()
+    {
+        $orientation = api_get_configuration_value('certificate_pdf_orientation');
+
+        $params['orientation'] = 'landscape';
+        if (!empty($orientation)) {
+            $params['orientation'] = $orientation;
+        }
+
+        $params['left'] = 0;
+        $params['right'] = 0;
+        $params['top'] = 0;
+        $params['bottom'] = 0;
+        $page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
+        $pdf = new PDF($page_format, $params['orientation'], $params);
+
+        $pdf->html_to_pdf(
+            $this->html_file,
+            get_lang('Certificates'),
+            null,
+            false,
+            false
+        );
+    }
 }

+ 14 - 0
main/inc/lib/skill.lib.php

@@ -1499,4 +1499,18 @@ class Skill extends Model
         return false;
     }
 
+    public function getStudentSkills($userId)
+    {
+        $sql = "SELECT s.name, sru.acquired_skill_at, c.title, c.directory
+                FROM {$this->table} s
+                INNER JOIN {$this->table_skill_rel_user} sru
+                ON s.id = sru.skill_id
+                INNER JOIN {$this->table_course} c
+                ON sru.course_id = c.id
+                WHERE sru.user_id = $userId";
+        $result = Database::query($sql);
+
+        return Database::store_result($result, 'ASSOC');
+    }
+
 }

+ 34 - 8
main/inc/lib/userportal.lib.php

@@ -354,6 +354,25 @@ class IndexManager
 
         $searchItem = null;
 
+
+
+        $myCertificate = GradebookUtils::get_certificate_by_user_id(
+            0,
+            $this->user_id
+        );
+
+        if ($myCertificate) {
+            $content .= Display::tag(
+                'li',
+                Display::url(
+                    Display::return_icon('skill-badges.png', get_lang('MyGeneralCertificate'), null, ICON_SIZE_SMALL).get_lang('MyGeneralCertificate'),
+                    api_get_path(WEB_CODE_PATH).'social/my_skills_report.php?a=generate_custom_skill'
+                )
+            );
+        }
+
+
+
         if (api_get_setting('allow_public_certificates') == 'true') {
             $searchItem = Display::tag(
                 'li',
@@ -373,7 +392,14 @@ class IndexManager
 
         if (api_get_setting('allow_skills_tool') == 'true') {
 
-            $content .= Display::tag('li', Display::url(Display::return_icon('skill-badges.png',get_lang('MySkills'),null,ICON_SIZE_SMALL).get_lang('MySkills'), api_get_path(WEB_CODE_PATH).'social/my_skills_report.php'));
+            $content .= Display::tag(
+                'li',
+                Display::url(
+                    Display::return_icon('skill-badges.png', get_lang('MySkills'), null, ICON_SIZE_SMALL).get_lang('MySkills'),
+                    api_get_path(WEB_CODE_PATH).'social/my_skills_report.php'
+                )
+            );
+
             $allowSkillsManagement = api_get_setting('allow_hr_skills_management') == 'true';
             if (($allowSkillsManagement && api_is_drh()) || api_is_platform_admin()) {
                 $content .= Display::tag('li',
@@ -1091,7 +1117,7 @@ class IndexManager
         if ($load_history) {
             // Load sessions in category in *history*
             $session_categories = UserManager::get_sessions_by_category($user_id, true);
-            
+
         } else {
             // Load sessions in category
             $session_categories = UserManager::get_sessions_by_category($user_id, false);
@@ -1105,7 +1131,7 @@ class IndexManager
                 $html .= get_lang('YouDoNotHaveAnySessionInItsHistory');
             }
         }
-        
+
         $sessionCount = 0;
         $courseCount = 0;
 
@@ -1125,7 +1151,7 @@ class IndexManager
 
             $this->tpl->assign('special_courses', $specialCourses);
             $this->tpl->assign('courses', $courses);
-            
+
             if (api_get_configuration_value('view_grid_courses') && ($courses || $specialCourses)) {
                 $listCourse = $this->tpl->fetch(
                 $this->tpl->get_template('/user_portal/grid_courses.tpl'));
@@ -1250,11 +1276,11 @@ class IndexManager
                             if (api_is_platform_admin()) {
                                 $actions = api_get_path(WEB_CODE_PATH) .'session/resume_session.php?id_session='.$session_id;
                             }
-                            
+
                             $coachId = $session_box['id_coach'];
                             $extraFieldValue = new ExtraFieldValue('session');
                             $imageField = $extraFieldValue->get_values_by_handler_and_field_variable($session_id, 'image');
-                            
+
                             $params['category_id'] = $session_box['category_id'];
                             $params['title'] = $session_box['title'];
                             //$params['subtitle'] = $extra_info;
@@ -1267,10 +1293,10 @@ class IndexManager
                             $params['duration'] = isset($session_box['duration']) ? ' ' . $session_box['duration'] : null;
                             $params['edit_actions'] = $actions;
                             $params['show_description'] = $session_box['show_description'];
-                            $params['description'] = $session_box['description'];   
+                            $params['description'] = $session_box['description'];
                             $params['visibility'] = $session_box['visibility'];
                             $params['show_simple_session_info'] = false;
-                            $params['course_list_session_style'] = $coursesListSessionStyle;                          
+                            $params['course_list_session_style'] = $coursesListSessionStyle;
                             $params['num_users'] = $session_box['num_users'];
                             $params['num_courses'] = $session_box['num_courses'];
                             $params['courses'] = $html_courses_session;

+ 6 - 0
main/lang/french/trad4all.inc.php

@@ -7705,4 +7705,10 @@ $TCCConnect = 'Se connecter';
 $ForMoreDetailsVisitX = "Pour que nous puissions mieux te connaître et te proposer un parcours sur mesure, tu dois d’abord remplir ce questionnaire %s.
 Si tu ne l’as pas encore fait, n’oublie pas non plus de remplir ton profil pour pouvoir accéder aux activités de la plateforme.";
 $NewStudentRegistered = "Inscription d'un nouvel apprenant";
+$UserHasParticipateDansDePlatformeXTheContratDateXCertificateDateXTimeX = 'a participé à une préparation linguistique en ligne sur la plateforme %s de l’Office Franco-allemand pour la Jeunesse.
+Du %s au %s.
+Cette préparation correspond à un volume horaire d’environ %s heures';
+$TheContentsAreValidated = 'Les contenus suivants ont été validés';
+$SkillsValidatedOfUserX = 'Sur cette plateforme l’investissement dans le réseau social est valorisé et %s a fait preuve des compétences suivantes';
+$MyGeneralCertificate = 'Mon certificat général';
 ?>

+ 17 - 0
main/mySpace/myStudents.php

@@ -170,6 +170,11 @@ if (isset($_GET['user_id']) && $_GET['user_id'] != "") {
 $action = isset($_GET['action']) ? $_GET['action'] : '';
 
 switch ($action) {
+    case 'generate_certificate':
+        $certificate = new Certificate(0, $student_id);
+        $certificate->generatePdfFromCustomCertificate();
+        exit;
+        break;
     case 'send_legal':
         LegalManager::sendLegal($student_id);
         break;
@@ -572,6 +577,18 @@ if (!empty($student_id)) {
             <?php
             }
 
+            $icon = ' '.Display::url(
+                get_lang('Generate'),
+                api_get_self().'?action=generate_certificate&student='.$student_id.'&course='.$course_code,
+                ['class' => 'btn btn-primary btn-xs']
+            );
+
+            echo '<tr>
+                <td align="right">';
+            echo get_lang('Certificate').' </td>  <td align="left">'.$icon;
+            echo '</td></tr>';
+
+
             if (api_get_setting('allow_terms_conditions') === 'true') {
                 $isBoss = UserManager::userIsBossOfStudent(api_get_user_id(), $student_id);
 

+ 8 - 0
main/social/my_skills_report.php

@@ -20,6 +20,14 @@ if (!$isStudent && !$isStudentBoss && !$isDRH) {
 
 $userId = api_get_user_id();
 
+$action = isset($_GET['a']) ? $_GET['a'] : '';
+switch ($action) {
+    case 'generate_custom_skill':
+        $certificate = new Certificate(0, api_get_user_id());
+        $certificate->generatePdfFromCustomCertificate();
+        break;
+}
+
 $skillTable = Database::get_main_table(TABLE_MAIN_SKILL);
 $skillRelUserTable = Database::get_main_table(TABLE_MAIN_SKILL_REL_USER);
 $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);

+ 25 - 0
main/template/rainbow/gradebook/custom_certificate.tpl

@@ -0,0 +1,25 @@
+<h2>{{ 'Certificate' | get_lang }}</h2>
+
+{{ complete_name }}
+
+{{ 'UserHasParticipateDansDePlatformeXTheContratDateXCertificateDateXTimeX' | get_lang | format(_s.site_name, terms_validation_date, 'xx', time_in_platform)}}
+
+{{ 'TheContentsAreValidated' | get_lang }}
+
+{% if sessions %}
+    <ul>
+    {% for session in sessions %}
+      <li>  {{ session.session_name }}</li>
+    {% endfor %}
+    </ul>
+{% endif %}
+
+{{ 'SkillsValidatedOfUserX' | get_lang | format(complete_name) }}
+
+{% if skills %}
+    <ul>
+    {% for skill in skills %}
+        <li>{{ skill.id }}</li>
+    {% endfor %}
+    </ul>
+{% endif %}