Skip to content

Commit b2ee500

Browse files
committed
bug fix and obey keeplocalcopy setting
1 parent 86e95c0 commit b2ee500

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

downloadcerts.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @package mod_customcert
2323
* @author Gonzalo Romero
24-
* @author Giorgio Consorti <g.consorti@lynxalb.com>
24+
* @author Giorgio Consorti <g.consorti@lynxlab.com>
2525
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2626
*/
2727

@@ -53,31 +53,23 @@
5353
unset($cert);
5454
}
5555

56-
$context = $DB->get_record('context', ['contextlevel' => '50', 'instanceid' => $courseid]);
57-
$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);
60-
6156
// Build a list of files to zip.
6257
$filesforzipping = [];
6358

6459
foreach ($certs as $certid => $cert_fields) {
65-
$template = null;
66-
foreach ($users as $roleAssignmentId => $user_fields) {
67-
if (!$DB->get_record('customcert_issues', ['userid' => $user_fields->userid, 'customcertid' => $certid])) {
68-
continue;
69-
}
70-
if (is_null($template)) {
71-
$template = $DB->get_record('customcert_templates', ['id' => $cert_fields->templateid], '*', MUST_EXIST);
72-
$template = new \mod_customcert\template($template);
73-
}
74-
$lf = new \mod_customcert\localfile($template);
75-
if (false === $file = $lf->getPDF($user_fields->userid)) {
60+
$issues = $DB->get_records('customcert_issues', ['customcertid' => $certid]);
61+
list($userssql, $params) = $DB->get_in_or_equal(array_map(fn($i) => $i->userid, $issues), SQL_PARAMS_NAMED);
62+
$usersObjs = $DB->get_records_select('user', "id {$userssql}", $params);
63+
$template = $DB->get_record('customcert_templates', ['id' => $cert_fields->templateid], '*', MUST_EXIST);
64+
$template = new \mod_customcert\template($template);
65+
$lf = new \mod_customcert\localfile($template);
66+
foreach ($issues as $issue) {
67+
if (false === $file = $lf->getPDF($issue->userid)) {
7668
// must generate the pdf
77-
$pdf = $template->generate_pdf(false, $user_fields->userid, true);
69+
$pdf = $template->generate_pdf(false, $issue->userid, true);
7870
if (!empty($pdf)) {
7971
if ($cert_fields->keeplocalcopy) {
80-
$file = $lf->getPDF($user_fields->userid);
72+
$file = $lf->getPDF($issue->userid);
8173
} else {
8274
$file = [
8375
'content' => $pdf,
@@ -86,7 +78,7 @@
8678
}
8779
}
8880
if ($file) {
89-
$filename = \mod_customcert\localfile::buildFileName($usersObjs[$user_fields->userid]->username, $template->get_id(), $course->shortname);
81+
$filename = \mod_customcert\localfile::buildFileName($usersObjs[$issue->userid]->username, $template->get_id(), $course->shortname);
9082
$filesforzipping['/' . $course->shortname . '/' . $cert_fields->name . '/' . $filename] = $file;
9183
}
9284
}

0 commit comments

Comments
 (0)