@@ -145,38 +145,45 @@ public function execute() {
145
145
// Users with both mod/customcert:view and mod/customcert:receiveissue cabapilities.
146
146
$ userswithissueview = array_intersect_key ($ userswithissue , $ userswithview );
147
147
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 ) {
149
156
// 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 ))) {
151
158
continue ;
152
159
}
153
160
154
161
// 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 ))) {
156
163
continue ;
157
164
}
158
165
159
166
// 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 ];
161
168
if (!$ cm ->uservisible ) {
162
169
continue ;
163
170
}
164
171
165
172
// Check that they have passed the required time.
166
173
if (!empty ($ customcert ->requiredtime )) {
167
174
if (\mod_customcert \certificate::get_course_time ($ customcert ->courseid ,
168
- $ enroluser ->id ) < ($ customcert ->requiredtime * 60 )) {
175
+ $ filtereduser ->id ) < ($ customcert ->requiredtime * 60 )) {
169
176
continue ;
170
177
}
171
178
}
172
179
173
180
// Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition.
174
181
$ issue = $ DB ->get_record ('customcert_issues ' ,
175
- ['userid ' => $ enroluser ->id , 'customcertid ' => $ customcert ->id ], 'id, emailed ' );
182
+ ['userid ' => $ filtereduser ->id , 'customcertid ' => $ customcert ->id ], 'id, emailed ' );
176
183
177
184
// Ok, issue them the certificate.
178
185
$ 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 ;
180
187
181
188
// Validate issueid and one last check for emailed.
182
189
if (!empty ($ issueid ) && empty ($ issue ->emailed )) {
0 commit comments