Skip to content

Commit d9ecec5

Browse files
Add a form to the issued certificate list to download a merged PDF of all shown certificates
1 parent 48f0913 commit d9ecec5

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

certificates.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@
6262

6363
$outputpage = new \tool_certificate\output\issues_page($template->get_id());
6464

65+
6566
$data = $outputpage->export_for_template($PAGE->get_renderer('core'));
67+
68+
$downloadform = new \tool_certificate\download_issues_form($template->get_id());
69+
if ($downloadissues = $downloadform->get_data()) {
70+
$outputpage->output_issues_pdf($template, $downloadissues);
71+
die();
72+
}
73+
$data['content'] .= $downloadform->render();
74+
6675
$data += ['heading' => get_string('issuedcertificates', 'tool_certificate')];
6776
if ($template->can_issue_to_anybody()) {
6877
$data += ['addbutton' => true, 'addbuttontitle' => get_string('issuecertificates', 'tool_certificate'),

classes/download_issues_form.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace tool_certificate;
4+
5+
class download_issues_form {
6+
public int $templateid;
7+
8+
public function __construct(int $templateid) {
9+
$this->templateid = $templateid;
10+
}
11+
12+
public function render(): string {
13+
return <<<HTML
14+
<form method="post" target="_blank" class="dataformatselector m-1">
15+
<div class="form-inline text-xs-right">
16+
<input type="hidden" name="templateid" value="$this->templateid">
17+
<label for="downloadissues_select" class="mr-1">Download issued PDFs as</label>
18+
<select name="downloadissues" id="downloadissues_select" class="form-control custom-select mr-1">
19+
<option value="pdf">Merged PDF</option>
20+
<option value="pdfdecollate">Merged PDF (De-collated)</option>
21+
</select>
22+
<button type="submit" class="btn btn-secondary">Download PDFs</button>
23+
</div>
24+
</form>
25+
HTML;
26+
}
27+
28+
public function get_data() {
29+
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
30+
return false;
31+
}
32+
33+
$downloadissues = required_param('downloadissues', PARAM_ALPHA);
34+
return $downloadissues;
35+
}
36+
37+
protected function definition(): void {
38+
$this->_form->setAttributes(['class' => 'form-inline']);
39+
40+
$templateid = $this->_customdata['templateid'];
41+
$this->_form->addElement('hidden', 'templateid', $templateid);
42+
$this->_form->setType('id', PARAM_INT);
43+
44+
$options =
45+
[
46+
'pdf' => 'Merged PDF',
47+
'pdfdecollate' => 'Merged PDF (De-collated)',
48+
];
49+
$this->_form->addElement('select', 'downloadissues', 'Download issued PDFs as', $options);
50+
$this->_form->setType('downloadissues', PARAM_TEXT);
51+
52+
$submit = $this->_form->addElement('submit', 'submit', 'Download PDFs');
53+
$submit->setAttributes(['class' => 'btn btn-secondary']);
54+
}
55+
}

classes/output/issues_page.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
class issues_page implements \templatable, \renderable {
3333
/** @var int */
3434
protected $templateid;
35+
public array $rows;
3536

3637
/**
3738
* templates_page constructor.
@@ -53,6 +54,76 @@ public function export_for_template(renderer_base $output): array {
5354
$report = system_report_factory::create(issues::class, $context,
5455
'', '', 0, ['templateid' => $this->templateid]);
5556

56-
return ['content' => $report->output()];
57+
$result = ['content' => $report->output()];
58+
$this->rows = $report->rows;
59+
return $result;
60+
}
61+
62+
/**
63+
* @throws \InvalidArgumentException
64+
* @throws \setasign\Fpdi\PdfParser\PdfParserException
65+
* @throws \setasign\Fpdi\PdfParser\PdfParserException
66+
*/
67+
public function output_issues_pdf(template $template, string $type): void {
68+
global $CFG;
69+
$files = [];
70+
$handles = [];
71+
foreach ($this->rows as $row) {
72+
$file = $template->get_issue_file($row);
73+
$files[] = $file;
74+
$handles[$file->get_id()] = $file->get_content_file_handle();
75+
}
76+
77+
require_once($CFG->libdir . '/pdflib.php');
78+
require_once($CFG->dirroot . '/mod/assign/feedback/editpdf/fpdi/autoload.php');
79+
80+
try {
81+
$pdf = new \setasign\Fpdi\Tcpdf\Fpdi();
82+
83+
if ($type == 'pdf') {
84+
foreach ($files as $file) {
85+
$filePages = $pdf->setSourceFile($handles[$file->get_id()]);
86+
for ($pageNumber = 1; $pageNumber <= $filePages; $pageNumber++) {
87+
$sourcePage = $pdf->importPage($pageNumber);
88+
$size = $pdf->getTemplateSize($sourcePage);
89+
$pdf->AddPage($size['orientation'], array($size['width'], $size['height']));
90+
91+
$pdf->useTemplate($sourcePage);
92+
}
93+
}
94+
95+
$pdf->Output('certificates.pdf');
96+
}
97+
else if ($type == 'pdfdecollate') {
98+
$pageCount = 1;
99+
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
100+
foreach ($files as $file) {
101+
$filePages = $pdf->setSourceFile($handles[$file->get_id()]);
102+
if ($pageNumber > $filePages) {
103+
continue;
104+
}
105+
if ($filePages > $pageCount) {
106+
$pageCount = $filePages;
107+
}
108+
109+
$sourcePage = $pdf->importPage($pageNumber);
110+
$size = $pdf->getTemplateSize($sourcePage);
111+
$pdf->AddPage($size['orientation'], array($size['width'], $size['height']));
112+
113+
$pdf->useTemplate($sourcePage);
114+
}
115+
}
116+
117+
$pdf->Output('certificates - ordered.pdf');
118+
}
119+
else {
120+
throw new \InvalidArgumentException("Unknown download type: $type");
121+
}
122+
}
123+
finally {
124+
foreach ($handles as $handle) {
125+
fclose($handle);
126+
}
127+
}
57128
}
58129
}

classes/reportbuilder/local/systemreports/issues.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,18 @@ protected function add_actions(): void {
245245
*/
246246
public function row_callback(stdClass $row): void {
247247
$this->userid = (int) $row->userid;
248+
249+
if (!isset($this->rows)) {
250+
$this->rows = [];
251+
}
252+
$this->rows[] = $row;
248253
}
249254

255+
/**
256+
* @var stdClass[]
257+
*/
258+
public array $rows;
259+
250260
/**
251261
* Callback for the fullname to display badge for archived issues.
252262
*

0 commit comments

Comments
 (0)