@@ -125,7 +125,7 @@ public function execute() {
125
125
// Get the context.
126
126
$ context = \context::instance_by_id ($ customcert ->contextid );
127
127
128
- // Get a list of all the issues.
128
+ // Get a list of all the issues that are already emailed (skip these users) .
129
129
$ sql = "SELECT u.id
130
130
FROM {customcert_issues} ci
131
131
JOIN {user} u
@@ -140,57 +140,59 @@ public function execute() {
140
140
// Get the context of the Custom Certificate module.
141
141
$ cmcontext = \context_module::instance ($ cm ->id );
142
142
143
- // Now, get a list of users who can view and issue the certificate but have not yet.
144
143
// Get users with the mod/customcert:receiveissue capability in the Custom Certificate module context.
145
144
$ userswithissue = get_users_by_capability ($ cmcontext , 'mod/customcert:receiveissue ' );
146
145
// Get users with mod/customcert:view capability.
147
146
$ userswithview = get_users_by_capability ($ cmcontext , 'mod/customcert:view ' );
148
- // Users with both mod/customcert:view and mod/customcert:receiveissue cabapilities .
147
+ // Users with both mod/customcert:view and mod/customcert:receiveissue capabilities .
149
148
$ userswithissueview = array_intersect_key ($ userswithissue , $ userswithview );
150
149
151
- // Filter the remaining users by determining whether they can actually see the CM or not
152
- // (Note: filter_user_list only takes into account those availability condition which actually implement
153
- // this function, so the second check with get_fast_modinfo must be still performed - but we can reduce the
154
- // size of the users list here already).
150
+ // Filter remaining users by availability conditions.
155
151
$ infomodule = new \core_availability \info_module ($ cm );
156
152
$ filteredusers = $ infomodule ->filter_user_list ($ userswithissueview );
157
153
158
154
foreach ($ filteredusers as $ filtereduser ) {
159
- // Check if the user has already been issued and emailed.
155
+ // Skip if the user has already been issued and emailed.
160
156
if (in_array ($ filtereduser ->id , array_keys ((array )$ issuedusers ))) {
161
157
continue ;
162
158
}
163
159
164
- // Don't want to issue to teachers.
160
+ // Don't want to issue to teachers/managers .
165
161
if (in_array ($ filtereduser ->id , array_keys ((array )$ userswithmanage ))) {
166
162
continue ;
167
163
}
168
164
169
- // Now check if the certificate is not visible to the current user.
170
- $ cm = get_fast_modinfo ($ customcert ->courseid , $ filtereduser ->id )->instances ['customcert ' ][$ customcert ->id ];
171
- if (!$ cm ->uservisible ) {
165
+ // Check whether the CM is visible to this user.
166
+ $ usercm = get_fast_modinfo ($ customcert ->courseid , $ filtereduser ->id )->instances ['customcert ' ][$ customcert ->id ];
167
+ if (!$ usercm ->uservisible ) {
172
168
continue ;
173
169
}
174
170
175
- // Check that they have passed the required time.
171
+ // Check required time (if any) .
176
172
if (!empty ($ customcert ->requiredtime )) {
177
173
if (\mod_customcert \certificate::get_course_time ($ customcert ->courseid ,
178
174
$ filtereduser ->id ) < ($ customcert ->requiredtime * 60 )) {
179
175
continue ;
180
176
}
181
177
}
182
178
183
- // Ensure the cert hasn't already been issued, e.g via the UI (view.php) - a race condition .
179
+ // Ensure the cert hasn't already been issued; if not, issue it now .
184
180
$ issue = $ DB ->get_record ('customcert_issues ' ,
185
- ['userid ' => $ filtereduser ->id , 'customcertid ' => $ customcert ->id ], 'id, emailed ' );
186
-
187
- // Ok, issue them the certificate.
188
- $ issueid = empty ($ issue ) ?
189
- \mod_customcert \certificate::issue_certificate ($ customcert ->id , $ filtereduser ->id ) : $ issue ->id ;
181
+ ['userid ' => $ filtereduser ->id , 'customcertid ' => $ customcert ->id ],
182
+ 'id, emailed ' );
183
+
184
+ $ issueid = null ;
185
+ $ emailed = 0 ;
186
+ if (!empty ($ issue )) {
187
+ $ issueid = (int )$ issue ->id ;
188
+ $ emailed = (int )$ issue ->emailed ;
189
+ } else {
190
+ $ issueid = \mod_customcert \certificate::issue_certificate ($ customcert ->id , $ filtereduser ->id );
191
+ $ emailed = 0 ;
192
+ }
190
193
191
- // Validate issueid and one last check for emailed.
192
- if (!empty ($ issueid ) && empty ($ issue ->emailed )) {
193
- // We create a new adhoc task to send the email.
194
+ // If we have an issue and it has not been emailed yet, send it now.
195
+ if (!empty ($ issueid ) && $ emailed === 0 ) {
194
196
$ task = new \mod_customcert \task \email_certificate_task ();
195
197
$ task ->set_custom_data (['issueid ' => $ issueid , 'customcertid ' => $ customcert ->id ]);
196
198
$ useadhoc = get_config ('customcert ' , 'useadhoc ' );
0 commit comments