diff --git a/main/gradebook/cli/export_all_certificates.php b/main/gradebook/cli/export_all_certificates.php new file mode 100644 index 0000000000..6bd768318c --- /dev/null +++ b/main/gradebook/cli/export_all_certificates.php @@ -0,0 +1,32 @@ +php main/gradebook/cli/export_all_certificated.php COURSE_CODE SESSION_ID CATEGORY_ID [USER_IDS] + */ + +require_once __DIR__.'/../../inc/global.inc.php'; + +if (PHP_SAPI !== 'cli') { + die(get_lang('NotAllowed')); +} + +$courseCode = $argv[1]; +$sessionId = $argv[2]; +$categoryId = $argv[3]; +$userList = isset($argv[4]) ? explode(',', $argv[4]) : []; + +$date = api_get_utc_datetime(null, false, true); + +$pdfName = 'certs_'.$courseCode.'_'.$sessionId.'_'.$categoryId.'_'.$date->format('Y-m-d'); + +$finalFile = api_get_path(SYS_ARCHIVE_PATH)."$pdfName.pdf"; + +if (file_exists($finalFile)) { + unlink(api_get_path(SYS_ARCHIVE_PATH)."$pdfName.pdf"); +} + +Category::exportAllCertificates($categoryId, $userList, $courseCode, true, $pdfName); diff --git a/main/gradebook/lib/be/category.class.php b/main/gradebook/lib/be/category.class.php index 0eeb399fa1..e6d37fd0d4 100755 --- a/main/gradebook/lib/be/category.class.php +++ b/main/gradebook/lib/be/category.class.php @@ -2265,11 +2265,21 @@ class Category implements GradebookItem } /** - * @param int $catId - * @param array $userList - */ - public static function exportAllCertificates($catId, $userList = []) - { + * @param int $catId + * @param array $userList + * @param string|null $courseCode + * @param bool $generateToFile + * @param string $pdfName + * + * @throws \MpdfException + */ + public static function exportAllCertificates( + $catId, + $userList = [], + $courseCode = null, + $generateToFile = false, + $pdfName = '' + ) { $orientation = api_get_configuration_value('certificate_pdf_orientation'); $params['orientation'] = 'landscape'; @@ -2310,10 +2320,13 @@ class Category implements GradebookItem // stuff) and return as one multiple-pages PDF $pdf->html_to_pdf( $certificate_path_list, - get_lang('Certificates'), - null, + empty($pdfName) ? get_lang('Certificates') : $pdfName, + $courseCode, + false, false, - false + true, + '', + $generateToFile ); } } diff --git a/main/inc/lib/pdf.lib.php b/main/inc/lib/pdf.lib.php index 244922da4d..1632eb8ba2 100755 --- a/main/inc/lib/pdf.lib.php +++ b/main/inc/lib/pdf.lib.php @@ -186,12 +186,15 @@ class PDF * 1 => array('title'=>'Bye','path'=>'file2.html') * ); * @param string $pdf_name pdf name - * @param string $course_code (if you are using html that are located + * @param null $course_code (if you are using html that are located * in the document tool you must provide this) * @param bool $print_title add title * @param bool $complete_style show header and footer if true * @param bool $addStyle * @param string $mainTitle + * @param bool $generateToFile Optional. When it is TRUE, then the output file is move to app/cache + * + * @throws \MpdfException * * @return false|null */ @@ -202,7 +205,8 @@ class PDF $print_title = false, $complete_style = true, $addStyle = true, - $mainTitle = '' + $mainTitle = '', + $generateToFile = false ) { if (empty($html_file_array)) { return false; @@ -365,7 +369,12 @@ class PDF $output_file = $pdf_name.'.pdf'; } // F to save the pdf in a file - @$this->pdf->Output($output_file, 'D'); + @$this->pdf->Output($output_file, $generateToFile ? 'F' : 'D'); + + if ($generateToFile) { + rename(getcwd()."/$output_file", api_get_path(SYS_ARCHIVE_PATH).$output_file); + } + exit; }