Pārlūkot izejas kodu

Add certificate link + download certificate in a zip see BT#15017

when using the customcertificate plugin
Julio Montoya 6 gadi atpakaļ
vecāks
revīzija
f50ecb7105

+ 5 - 0
main/inc/lib/certificate.lib.php

@@ -391,6 +391,7 @@ class Certificate extends Model
             '((author_last_name))',
             '((score))',
             '((portal_name))',
+            '((certificate_link))',
         ];
 
         return $tags;
@@ -417,6 +418,8 @@ class Certificate extends Model
         }
 
         $currentUserInfo = api_get_user_info();
+        $url = api_get_path(WEB_PATH).'certificates/index.php?id='.$certificateInfo['id'].'&user_id='.$certificateInfo['user_id'];
+        $link = Display::url($url, $url);
 
         $replace = [
             $courseInfo['title'],
@@ -426,7 +429,9 @@ class Certificate extends Model
             $currentUserInfo['lastname'],
             $certificateInfo['score_certificate'],
             api_get_setting('Institution'),
+            $link
         ];
+
         $message = str_replace(self::notificationTags(), $replace, $message);
 
         MessageManager::send_message(

+ 0 - 1
plugin/customcertificate/src/CustomCertificatePlugin.php

@@ -259,7 +259,6 @@ class CustomCertificatePlugin extends Plugin
 
         if (api_get_plugin_setting('customcertificate', 'enable_plugin_customcertificate') === 'true') {
             $infoCertificate = self::getCertificateData($certId, $userId);
-            var_dump($infoCertificate);
             if (!empty($infoCertificate)) {
                 if ($certificate->user_id == api_get_user_id() && !empty($certificate->certificate_data)) {
                     $certificateId = $certificate->certificate_data['id'];

+ 4 - 4
plugin/customcertificate/src/index.php

@@ -108,13 +108,13 @@ if ($form->validate()) {
             'c_id' => $formValues['c_id'],
             'session_id' => $formValues['session_id'],
             'content_course' => $formValues['content_course'],
-            'contents_type' => intval($formValues['contents_type']),
+            'contents_type' => (int) $formValues['contents_type'],
             'contents' => $contents,
             'date_change' => intval($formValues['date_change']),
             'date_start' => date("Y-m-d", strtotime($date_start)),
             'date_end' => date("Y-m-d", strtotime($date_end)),
             'place' => $formValues['place'],
-            'type_date_expediction' => intval($formValues['type_date_expediction']),
+            'type_date_expediction' => (int) $formValues['type_date_expediction'],
             'day' => $formValues['day'],
             'month' => $formValues['month'],
             'year' => $formValues['year'],
@@ -122,8 +122,8 @@ if ($form->validate()) {
             'signature_text2' => $formValues['signature_text2'],
             'signature_text3' => $formValues['signature_text3'],
             'signature_text4' => $formValues['signature_text4'],
-            'margin_left' => intval($formValues['margin_left']),
-            'margin_right' => intval($formValues['margin_right']),
+            'margin_left' => (int) $formValues['margin_left'],
+            'margin_right' => (int) $formValues['margin_right'],
             'certificate_default' => 0,
         ];
 

+ 65 - 27
plugin/customcertificate/src/print_certificate.php

@@ -5,7 +5,7 @@ use Chamilo\CourseBundle\Entity\CLpCategory;
 
 $default = isset($_GET['default']) ? (int) $_GET['default'] : null;
 
-if ($default == 1) {
+if ($default === 1) {
     $cidReset = true;
 }
 
@@ -41,6 +41,8 @@ if (empty($courseCode)) {
     if (!empty($courseInfo)) {
         $courseId = $courseInfo['real_id'];
     }
+} else {
+    $courseInfo = api_get_course_info($courseCode);
 }
 
 if (empty($sessionId)) {
@@ -116,17 +118,21 @@ if (empty($infoCertificate)) {
 
 $workSpace = intval(297 - $infoCertificate['margin_left'] - $infoCertificate['margin_right']);
 $widthCell = intval($workSpace / 6);
-$htmlText = '<html>';
-$htmlText .= '
+$htmlList = [];
+
+$currentLocalTime = api_get_local_time();
+
+foreach ($userList as $userInfo) {
+    $htmlText = '<html>';
+    $htmlText .= '
     <link rel="stylesheet"
         type="text/css"
         href="'.api_get_path(WEB_PLUGIN_PATH).'customcertificate/resources/css/certificate.css">';
-$htmlText .= '
+    $htmlText .= '
     <link rel="stylesheet"
         type="text/css"
         href="'.api_get_path(WEB_CSS_PATH).'document.css">';
-$htmlText .= '<body>';
-foreach ($userList as $userInfo) {
+    $htmlText .= '<body>';
     $studentId = $userInfo['user_id'];
 
     if (empty($infoCertificate['background'])) {
@@ -248,8 +254,10 @@ foreach ($userList as $userInfo) {
                 );
             }
         } else {
-            $dateInfo = api_get_local_time($sessionInfo['access_end_date']);
-            $dateExpediction .= $plugin->get_lang('to').api_format_date($dateInfo, DATE_FORMAT_LONG);
+            if (!empty($sessionInfo)) {
+                $dateInfo = api_get_local_time($sessionInfo['access_end_date']);
+                $dateExpediction .= $plugin->get_lang('to').api_format_date($dateInfo, DATE_FORMAT_LONG);
+            }
         }
     }
 
@@ -465,24 +473,50 @@ foreach ($userList as $userInfo) {
         }
         $htmlText .= '</div>';
     }
+    $htmlText .= '</body></html>';
+    $fileName = 'certificate_'.$courseInfo['code'].'_'.$userInfo['complete_name'].'_'.$currentLocalTime;
+    $htmlList[$fileName] = $htmlText;
+}
+
+$fileList = [];
+$archivePath = api_get_path(SYS_ARCHIVE_PATH).'certificates/';
+if (!is_dir($archivePath)) {
+    mkdir($archivePath, api_get_permissions_for_new_directories());
+}
+
+foreach ($htmlList as $fileName => $content) {
+    $fileName = api_replace_dangerous_char($fileName);
+    $params = [
+        'filename' => $fileName,
+        'pdf_title' => 'Certificate',
+        'pdf_description' => '',
+        'format' => 'A4-L',
+        'orientation' => 'L',
+        'left' => 15,
+        'top' => 15,
+        'bottom' => 0,
+    ];
+    $pdf = new PDF($params['format'], $params['orientation'], $params);
+    if (count($htmlList) == 1) {
+        $pdf->content_to_pdf($content, '', $fileName, null, 'D', false, null, false, false, false);
+        exit;
+    } else {
+        $filePath = $archivePath.$fileName.'.pdf';
+        $pdf->content_to_pdf($content, '', $fileName, null, 'F', true, $filePath, false, false, false);
+        $fileList[] = $filePath;
+    }
+}
+
+if (!empty($fileList)) {
+    $zipFile = $archivePath.'certificates_'.api_get_unique_id().'.zip';
+    $zipFolder = new PclZip($zipFile);
+    foreach ($fileList as $file) {
+        $zipFolder->add($file, PCLZIP_OPT_REMOVE_ALL_PATH);
+    }
+    $name = 'certificates_'.$courseInfo['code'].'_'.$currentLocalTime.'.zip';
+    DocumentManager::file_send_for_download($zipFile, true, $name);
+    exit;
 }
-$htmlText .= '</body></html>';
-
-$fileName = 'certificate_'.date('Ymd_His');
-$params = [
-    'filename' => $fileName,
-    'pdf_title' => 'Certificate',
-    'pdf_description' => '',
-    'format' => 'A4-L',
-    'orientation' => 'L',
-    'left' => 15,
-    'top' => 15,
-    'bottom' => 0,
-];
-
-$pdf = new PDF($params['format'], $params['orientation'], $params);
-$pdf->content_to_pdf($htmlText, '', $fileName, null, 'D', false, null, false, false, false);
-exit;
 
 function getIndexFiltered($index)
 {
@@ -491,12 +525,16 @@ function getIndexFiltered($index)
     $lines = explode(chr(13).chr(10), $txt);
     $text1 = '';
     for ($x = 0; $x < 47; $x++) {
-        $text1 .= $lines[$x].chr(13).chr(10);
+        if (isset($lines[$x])) {
+            $text1 .= $lines[$x].chr(13).chr(10);
+        }
     }
 
     $text2 = '';
     for ($x = 47; $x < 94; $x++) {
-        $text2 .= $lines[$x].chr(13).chr(10);
+        if (isset($lines[$x])) {
+            $text2 .= $lines[$x].chr(13).chr(10);
+        }
     }
 
     $showLeft = str_replace(chr(13).chr(10), "<br/>", $text1);