Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow downloading a merged PDF of issued certificates #62

Draft
wants to merge 5 commits into
base: MOODLE_400_STABLE
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions certificates.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
$outputpage = new \tool_certificate\output\issues_page($template->get_id());

$data = $outputpage->export_for_template($PAGE->get_renderer('core'));

$downloadform = new \tool_certificate\download_issues_form($template->get_id());
if ($downloadissues = $downloadform->get_data()) {
$outputpage->output_issues_pdf($template, $downloadissues);
die();
}
$data['content'] .= $downloadform->render();

$data += ['heading' => get_string('issuedcertificates', 'tool_certificate')];
if ($template->can_issue_to_anybody()) {
$data += ['addbutton' => true, 'addbuttontitle' => get_string('issuecertificates', 'tool_certificate'),
Expand Down
56 changes: 56 additions & 0 deletions classes/download_issues_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace tool_certificate;

class download_issues_form {
public int $templateid;

public function __construct(int $templateid) {
$this->templateid = $templateid;
}

public function render(): string {
return <<<HTML
<form method="post" target="_blank" class="dataformatselector m-1">
<div class="form-inline text-xs-right">
<input type="hidden" name="templateid" value="$this->templateid" />
<input type="hidden" name="perpage" value="5000" />
<label for="downloadissues_select" class="mr-1">Download all filtered issued PDFs as</label>
<select name="downloadissues" id="downloadissues_select" class="form-control custom-select mr-1">
<option value="pdf">Merged PDF</option>
<option value="pdfdecollate">Merged PDF (De-collated)</option>
</select>
<button type="submit" class="btn btn-secondary">Download PDFs</button>
</div>
</form>
HTML;
}

public function get_data() {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
return false;
}

$downloadissues = required_param('downloadissues', PARAM_ALPHA);
return $downloadissues;
}

protected function definition(): void {
$this->_form->setAttributes(['class' => 'form-inline']);

$templateid = $this->_customdata['templateid'];
$this->_form->addElement('hidden', 'templateid', $templateid);
$this->_form->setType('id', PARAM_INT);

$options =
[
'pdf' => 'Merged PDF',
'pdfdecollate' => 'Merged PDF (De-collated)',
];
$this->_form->addElement('select', 'downloadissues', 'Download issued PDFs as', $options);
$this->_form->setType('downloadissues', PARAM_TEXT);

$submit = $this->_form->addElement('submit', 'submit', 'Download PDFs');
$submit->setAttributes(['class' => 'btn btn-secondary']);
}
}
113 changes: 112 additions & 1 deletion classes/output/issues_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class issues_page implements \templatable, \renderable {
/** @var int */
protected $templateid;

/**
* @var \stdClass[] The rows that were displayed in the table
*/
public array $rows;

/**
* templates_page constructor.
*
Expand All @@ -53,6 +58,112 @@ public function export_for_template(renderer_base $output): array {
$report = system_report_factory::create(issues::class, $context,
'', '', 0, ['templateid' => $this->templateid]);

return ['content' => $report->output()];
$result = ['content' => $report->output()];
if (isset($report->rows)) {
$this->rows = $report->rows;
}
else
{
$this->rows = [];
}
return $result;
}

/**
* @throws \InvalidArgumentException
* @throws \setasign\Fpdi\PdfParser\PdfParserException
* @throws \setasign\Fpdi\PdfParser\PdfParserException
*/
public function output_issues_pdf(template $template, string $type): void {
global $CFG;
$files = [];
$handles = [];
foreach ($this->rows as $row) {
$file = $template->get_issue_file($row);
$files[] = $file;
$handles[$file->get_id()] = $file->get_content_file_handle();
}

$debug = optional_param('debug', false, PARAM_BOOL);

require_once($CFG->libdir . '/pdflib.php');
require_once($CFG->dirroot . '/mod/assign/feedback/editpdf/fpdi/autoload.php');

// end all output buffers if any
while (ob_get_level())
{
ob_get_clean();
}

try {
$pdf = new \setasign\Fpdi\Tcpdf\Fpdi();
$count = count($files);
$name = clean_filename($template->get_name());
$at = date('Y-m-d H-i-s');
$name = "$name - $count certificate(s) - $at";

if ($type == 'pdf') {
$position = 0;
foreach ($files as $file) {
$position++;
$filePages = $pdf->setSourceFile($handles[$file->get_id()]);
for ($pageNumber = 1; $pageNumber <= $filePages; $pageNumber++) {
$sourcePage = $pdf->importPage($pageNumber);
$size = $pdf->getTemplateSize($sourcePage);
$pdf->AddPage($size['orientation'], array($size['width'], $size['height']));

$pdf->useTemplate($sourcePage);

if ($debug) {
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(200, 0, 0);
$pdf->SetXY(5, 5);
$pdf->Write(2, "PDF $position/$count, Page $pageNumber/$filePages");
}
}
}

$pdf->Output("$name.pdf");
}
else if ($type == 'pdfdecollate') {
$pageCount = 1;
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$position = 0;
foreach ($files as $file) {
$position++;
$filePages = $pdf->setSourceFile($handles[$file->get_id()]);
if ($pageNumber > $filePages) {
continue;
}
if ($filePages > $pageCount) {
$pageCount = $filePages;
}

$sourcePage = $pdf->importPage($pageNumber);
$size = $pdf->getTemplateSize($sourcePage);
$pdf->AddPage($size['orientation'], array($size['width'], $size['height']));

$pdf->useTemplate($sourcePage);

if ($debug) {
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(200, 0, 0);
$pdf->SetXY(5, 5);
$pdf->Write(2, "PDF $position/$count, Page $pageNumber/$filePages");
}
}
}

$pdf->Output("$name - ordered.pdf");
}
else {
throw new \InvalidArgumentException("Unknown download type: $type");
}
}
finally {
foreach ($handles as $handle) {
fclose($handle);
}
}
}
}
10 changes: 10 additions & 0 deletions classes/reportbuilder/local/systemreports/issues.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ protected function add_actions(): void {
*/
public function row_callback(stdClass $row): void {
$this->userid = (int) $row->userid;

if (!isset($this->rows)) {
$this->rows = [];
}
$this->rows[] = $row;
}

/**
* @var stdClass[]
*/
public array $rows;

/**
* Callback for the fullname to display badge for archived issues.
*
Expand Down
Loading