Skip to content

Commit 8e3b9d8

Browse files
Claud10Rmdjnelson
Claud10R
authored andcommitted
Filter users before processing in the issue task (#634)
1 parent 12362ee commit 8e3b9d8

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

classes/task/issue_certificates_task.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,38 +145,45 @@ public function execute() {
145145
// Users with both mod/customcert:view and mod/customcert:receiveissue cabapilities.
146146
$userswithissueview = array_intersect_key($userswithissue, $userswithview);
147147

148-
foreach ($userswithissueview as $enroluser) {
148+
// Filter the remaining users by determining whether they can actually see the CM or not
149+
// (Note: filter_user_list only takes into account those availability condition which actually implement
150+
// this function, so the second check with get_fast_modinfo must be still performed - but we can reduce the
151+
// size of the users list here already).
152+
$infomodule = new \core_availability\info_module($cm);
153+
$filteredusers = $infomodule->filter_user_list($userswithissueview);
154+
155+
foreach ($filteredusers as $filtereduser) {
149156
// Check if the user has already been issued and emailed.
150-
if (in_array($enroluser->id, array_keys((array)$issuedusers))) {
157+
if (in_array($filtereduser->id, array_keys((array)$issuedusers))) {
151158
continue;
152159
}
153160

154161
// Don't want to issue to teachers.
155-
if (in_array($enroluser->id, array_keys((array)$userswithmanage))) {
162+
if (in_array($filtereduser->id, array_keys((array)$userswithmanage))) {
156163
continue;
157164
}
158165

159166
// Now check if the certificate is not visible to the current user.
160-
$cm = get_fast_modinfo($customcert->courseid, $enroluser->id)->instances['customcert'][$customcert->id];
167+
$cm = get_fast_modinfo($customcert->courseid, $filtereduser->id)->instances['customcert'][$customcert->id];
161168
if (!$cm->uservisible) {
162169
continue;
163170
}
164171

165172
// Check that they have passed the required time.
166173
if (!empty($customcert->requiredtime)) {
167174
if (\mod_customcert\certificate::get_course_time($customcert->courseid,
168-
$enroluser->id) < ($customcert->requiredtime * 60)) {
175+
$filtereduser->id) < ($customcert->requiredtime * 60)) {
169176
continue;
170177
}
171178
}
172179

173180
// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
174181
$issue = $DB->get_record('customcert_issues',
175-
['userid' => $enroluser->id, 'customcertid' => $customcert->id], 'id, emailed');
182+
['userid' => $filtereduser->id, 'customcertid' => $customcert->id], 'id, emailed');
176183

177184
// Ok, issue them the certificate.
178185
$issueid = empty($issue) ?
179-
\mod_customcert\certificate::issue_certificate($customcert->id, $enroluser->id) : $issue->id;
186+
\mod_customcert\certificate::issue_certificate($customcert->id, $filtereduser->id) : $issue->id;
180187

181188
// Validate issueid and one last check for emailed.
182189
if (!empty($issueid) && empty($issue->emailed)) {

0 commit comments

Comments
 (0)