@@ -49,16 +49,14 @@ public function get_name() {
49
49
public function execute () {
50
50
global $ DB ;
51
51
52
- // Get the certificatesPerRun, includeinnotvisiblecourses and certificateexecutionperiod configurations.
53
- $ certificatesPerRun = (int )get_config ('customcert ' , 'certificatesperrun ' );
54
- $ includeInNotVisibleCourses = (bool )get_config ('customcert ' , 'includeinnotvisiblecourses ' );
55
- $ certificateExecutionPeriod = (int )get_config ('customcert ' , 'certificateexecutionperiod ' );
56
-
52
+ // Get the certificatesperrun, includeinnotvisiblecourses, and certificateexecutionperiod configurations.
53
+ $ certificatesperrun = (int )get_config ('customcert ' , 'certificatesperrun ' );
54
+ $ includeinnotvisiblecourses = (bool )get_config ('customcert ' , 'includeinnotvisiblecourses ' );
55
+ $ certificateexecutionperiod = (int )get_config ('customcert ' , 'certificateexecutionperiod ' );
57
56
58
57
// Get the last processed batch and total certificates to process.
59
- $ taskProgress = $ DB ->get_record ('customcert_task_progress ' , ['taskname ' => 'email_certificate_task ' ]);
60
- $ lastProcessedBatch = $ taskProgress ->last_processed ;
61
-
58
+ $ taskprogress = $ DB ->get_record ('customcert_task_progress ' , ['taskname ' => 'email_certificate_task ' ]);
59
+ $ lastprocessed = $ taskprogress ->last_processed ;
62
60
63
61
// Get all the certificates that have requested someone get emailed.
64
62
$ emailotherslengthsql = $ DB ->sql_length ('c.emailothers ' );
@@ -79,16 +77,16 @@ public function execute() {
79
77
OR $ emailotherslengthsql >= 3) " ;
80
78
81
79
// Check the includeinnotvisiblecourses configuration.
82
- if (!$ includeInNotVisibleCourses ) {
80
+ if (!$ includeinnotvisiblecourses ) {
83
81
// Exclude certificates from hidden courses.
84
82
$ sql .= " AND co.visible = 1 AND cat.visible = 1 " ;
85
83
}
86
84
87
85
// Add condition based on certificate execution period.
88
- if ($ certificateExecutionPeriod > 0 ) {
86
+ if ($ certificateexecutionperiod > 0 ) {
89
87
// Include courses with no end date or end date greater than the specified period.
90
88
$ sql .= " AND (co.enddate = 0 OR co.enddate > :enddate) " ;
91
- $ params ['enddate ' ] = time () - $ certificateExecutionPeriod ;
89
+ $ params ['enddate ' ] = time () - $ certificateexecutionperiod ;
92
90
}
93
91
94
92
// Execute the SQL query.
@@ -102,26 +100,30 @@ public function execute() {
102
100
$ textrenderer = $ page ->get_renderer ('mod_customcert ' , 'email ' , 'textemail ' );
103
101
104
102
// Store the total count of certificates in the database.
105
- $ totalCertificatesToProcess = count ($ customcerts );
106
- $ DB ->set_field ('customcert_task_progress ' , 'total_certificate_to_process ' , $ totalCertificatesToProcess , [
103
+ $ totalcertificatestoprocess = count ($ customcerts );
104
+ $ DB ->set_field ('customcert_task_progress ' , 'total_certificate_to_process ' , $ totalcertificatestoprocess , [
107
105
'taskname ' => 'email_certificate_task ' ,
108
106
]);
109
107
110
-
111
108
// Check if we need to reset and start from the beginning.
112
- if ($ lastProcessedBatch >= count ($ customcerts )) {
113
- $ lastProcessedBatch = 0 ; // Reset to the beginning.
109
+ if ($ lastprocessed >= count ($ customcerts )) {
110
+ $ lastprocessed = 0 ; // Reset to the beginning.
114
111
}
115
112
116
- if ($ certificatesPerRun <= 0 ) {
113
+ if ($ certificatesperrun <= 0 ) {
117
114
// Process all certificates in a single run.
118
115
$ certificates = $ customcerts ;
119
116
} else {
120
117
// Process certificates in batches, starting from the last processed batch.
121
- $ certificates = array_slice ($ customcerts , $ lastProcessedBatch , $ certificatesPerRun );
118
+ $ certificates = array_slice ($ customcerts , $ lastprocessed , $ certificatesperrun );
122
119
}
123
120
124
121
foreach ($ certificates as $ customcert ) {
122
+ // Check if the certificate is hidden, quit early.
123
+ $ fastmoduleinfo = get_fast_modinfo ($ customcert ->courseid )->instances ['customcert ' ][$ customcert ->id ];
124
+ if (!$ fastmoduleinfo ->visible ) {
125
+ continue ;
126
+ }
125
127
// Do not process an empty certificate.
126
128
$ sql = "SELECT ce.*
127
129
FROM {customcert_elements} ce
@@ -163,27 +165,28 @@ public function execute() {
163
165
WHERE ci.customcertid = :customcertid " ;
164
166
$ issuedusers = $ DB ->get_records_sql ($ sql , ['customcertid ' => $ customcert ->id ]);
165
167
166
- // Now, get a list of users who can access the certificate but have not yet.
167
- $ enrolledusers = get_enrolled_users (\context_course::instance ($ customcert ->courseid ), 'mod/customcert:view ' );
168
- foreach ($ enrolledusers as $ enroluser ) {
169
- // Check if the user has already been issued.
170
- if (in_array ($ enroluser ->id , array_keys ((array ) $ issuedusers ))) {
171
- continue ;
172
- }
168
+ // Now, get a list of users who can Manage the certificate.
169
+ $ userswithmanage = get_users_by_capability ($ context , 'mod/customcert:manage ' , 'id ' );
173
170
174
- // Now check if the certificate is not visible to the current user.
175
- $ cm = get_fast_modinfo ($ customcert ->courseid , $ enroluser ->id )->instances ['customcert ' ][$ customcert ->id ];
176
- if (!$ cm ->uservisible ) {
177
- continue ;
178
- }
171
+ // Get the context of the Custom Certificate module.
172
+ $ cm = get_coursemodule_from_instance ('customcert ' , $ customcert ->id , $ customcert ->course );
173
+ $ context = \context_module::instance ($ cm ->id );
179
174
180
- // Don't want to email those with the capability to manage the certificate.
181
- if (has_capability ('mod/customcert:manage ' , $ context , $ enroluser ->id )) {
175
+ // Now, get a list of users who can Issue the certificate but have not yet.
176
+ // Get users with the specified capability in the Custom Certificate module context.
177
+ $ userwithissue = get_users_by_capability ($ context , 'mod/customcert:receiveissue ' , 'id, firstname, lastname, email ' );
178
+ $ infomodule = new \core_availability \info_module ($ fastmoduleinfo );
179
+ // Filter who can't access due to availability restriction, from the full list.
180
+ $ userscanissue = $ infomodule ->filter_user_list ($ userwithissue );
181
+
182
+ foreach ($ userscanissue as $ enroluser ) {
183
+ // Check if the user has already been issued.
184
+ if (in_array ($ enroluser ->id , array_keys ((array )$ issuedusers ))) {
182
185
continue ;
183
186
}
184
187
185
- // Only email those with the capability to receive the certificate.
186
- if (! has_capability ( ' mod/customcert:receiveissue ' , $ context , $ enroluser ->id )) {
188
+ // Don't want to email those with the capability to manage the certificate.
189
+ if (in_array ( $ enroluser ->id , array_keys (( array ) $ userswithmanage ) )) {
187
190
continue ;
188
191
}
189
192
@@ -216,7 +219,7 @@ public function execute() {
216
219
}
217
220
}
218
221
219
- // If there are no users to email we can return early.
222
+ // If there are no users to email, we can return early.
220
223
if (!$ issuedusers ) {
221
224
continue ;
222
225
}
@@ -227,7 +230,7 @@ public function execute() {
227
230
return ;
228
231
}
229
232
230
- $ issueIds = array () ;
233
+ $ issueids = [] ;
231
234
// Now, email the people we need to.
232
235
foreach ($ issuedusers as $ user ) {
233
236
// Set up the user.
@@ -301,17 +304,18 @@ public function execute() {
301
304
}
302
305
303
306
// Set the field so that it is emailed.
304
- $ issueIds [] = $ user ->issueid ;
305
- //$DB->set_field('customcert_issues', 'emailed', 1, ['id' => $user->issueid]);
307
+ $ issueids [] = $ user ->issueid ;
306
308
}
307
- if (!empty ($ issueIds )) {
308
- $ DB ->set_field_select ('customcert_issues ' , 'emailed ' , 1 , 'id IN ( ' . implode (', ' , $ issueIds ) . ') ' );
309
+ if (!empty ($ issueids )) {
310
+ $ DB ->set_field_select ('customcert_issues ' , 'emailed ' , 1 , 'id IN ( ' . implode (', ' , $ issueids ) . ') ' );
309
311
}
310
312
}
311
- // Update the last processed batch.
312
- $ newLastProcessedBatch = $ lastProcessedBatch + count ($ certificates );
313
- $ DB ->set_field ('customcert_task_progress ' , 'last_processed ' , $ newLastProcessedBatch , [
314
- 'taskname ' => 'email_certificate_task ' ,
315
- ]);
313
+ // Update the last processed position, if run in batches.
314
+ if ($ certificatesperrun > 0 ) {
315
+ $ newlastprocessed = $ lastprocessed + count ($ certificates );
316
+ $ DB ->set_field ('customcert_task_progress ' , 'last_processed ' , $ newlastprocessed , [
317
+ 'taskname ' => 'email_certificate_task ' ,
318
+ ]);
319
+ }
316
320
}
317
321
}
0 commit comments