Skip to content

Commit

Permalink
Gradebook: Add CLI to export all certificates in course - refs BT#18279
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Jan 16, 2021
1 parent 0cc8382 commit 1ed046f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
32 changes: 32 additions & 0 deletions main/gradebook/cli/export_all_certificates.php
@@ -0,0 +1,32 @@
<?php

/* For licensing terms, see /license.txt */

/**
* Command to export certificates for a gradebook categori in course (or course session).
*
* <code>php main/gradebook/cli/export_all_certificated.php COURSE_CODE SESSION_ID CATEGORY_ID [USER_IDS]</code>
*/

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);
29 changes: 21 additions & 8 deletions main/gradebook/lib/be/category.class.php
Expand Up @@ -2265,11 +2265,21 @@ public static function generateCertificatesInUserList($catId, $userList)
}

/**
* @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';
Expand Down Expand Up @@ -2310,10 +2320,13 @@ public static function exportAllCertificates($catId, $userList = [])
// 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
);
}
}
Expand Down
15 changes: 12 additions & 3 deletions main/inc/lib/pdf.lib.php
Expand Up @@ -186,12 +186,15 @@ public function html_to_pdf_with_template(
* 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
*/
Expand All @@ -202,7 +205,8 @@ public function html_to_pdf(
$print_title = false,
$complete_style = true,
$addStyle = true,
$mainTitle = ''
$mainTitle = '',
$generateToFile = false
) {
if (empty($html_file_array)) {
return false;
Expand Down Expand Up @@ -365,7 +369,12 @@ public function html_to_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;
}

Expand Down

0 comments on commit 1ed046f

Please sign in to comment.