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