Skip to content

Commit 86e95c0

Browse files
committed
fix to download certs when keeplocalcopy is false
1 parent c25341b commit 86e95c0

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

downloadcerts.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
die();
3737
}
3838

39+
/** @var \moodle_database $DB */
3940
global $DB;
4041

4142
// Increase the server timeout to handle the creation and sending of large zip files.
@@ -54,13 +55,15 @@
5455

5556
$context = $DB->get_record('context', ['contextlevel' => '50', 'instanceid' => $courseid]);
5657
$users = $DB->get_records('role_assignments', ['contextid' => $context->id]);
58+
list($userssql, $params) = $DB->get_in_or_equal(array_map(fn($u) => $u->userid, $users), SQL_PARAMS_NAMED);
59+
$usersObjs = $DB->get_records_select('user', "id {$userssql}", $params);
5760

5861
// Build a list of files to zip.
5962
$filesforzipping = [];
6063

6164
foreach ($certs as $certid => $cert_fields) {
6265
$template = null;
63-
foreach ($users as $userid => $user_fields) {
66+
foreach ($users as $roleAssignmentId => $user_fields) {
6467
if (!$DB->get_record('customcert_issues', ['userid' => $user_fields->userid, 'customcertid' => $certid])) {
6568
continue;
6669
}
@@ -73,11 +76,18 @@
7376
// must generate the pdf
7477
$pdf = $template->generate_pdf(false, $user_fields->userid, true);
7578
if (!empty($pdf)) {
76-
$file = $lf->getPDF($user_fields->userid);
79+
if ($cert_fields->keeplocalcopy) {
80+
$file = $lf->getPDF($user_fields->userid);
81+
} else {
82+
$file = [
83+
'content' => $pdf,
84+
];
85+
}
7786
}
7887
}
7988
if ($file) {
80-
$filesforzipping['/' . $course->shortname . '/' . $cert_fields->name . '/' .$file->get_filename()] = $file;
89+
$filename = \mod_customcert\localfile::buildFileName($usersObjs[$user_fields->userid]->username, $template->get_id(), $course->shortname);
90+
$filesforzipping['/' . $course->shortname . '/' . $cert_fields->name . '/' . $filename] = $file;
8191
}
8292
}
8393
}

0 commit comments

Comments
 (0)