@@ -65,7 +65,7 @@ class mail_manager {
65
65
* @return bool
66
66
*/
67
67
public static function moodleoverflow_send_mails (): bool {
68
- global $ DB , $ CFG , $ PAGE ;
68
+ global $ CFG , $ DB , $ PAGE ;
69
69
70
70
// Get the course object of the top level site.
71
71
$ site = get_site ();
@@ -86,10 +86,8 @@ public static function moodleoverflow_send_mails(): bool {
86
86
$ moodleoverflows = [];
87
87
$ courses = [];
88
88
$ coursemodules = [];
89
- $ subscribedusers = [];
90
89
91
- // Posts older than x days will not be mailed.
92
- // This will avoid problems with the cron not ran for a long time.
90
+ // Posts older than x days will not be mailed. This will avoid problems with the cron not ran for a long time.
93
91
$ timenow = time ();
94
92
$ endtime = $ timenow - get_config ('moodleoverflow ' , 'maxeditingtime ' );
95
93
$ starttime = $ endtime - (get_config ('moodleoverflow ' , 'maxmailingtime ' ) * 60 * 60 );
@@ -102,7 +100,6 @@ public static function moodleoverflow_send_mails(): bool {
102
100
mtrace ('Errors occurred while trying to mark some posts as being mailed. ' );
103
101
return false ;
104
102
}
105
-
106
103
// Loop through all posts to be mailed.
107
104
foreach ($ posts as $ postid => $ post ) {
108
105
self ::check_post ($ post , $ mailcount , $ users , $ discussions , $ errorcount , $ posts , $ postid ,
@@ -126,18 +123,17 @@ public static function moodleoverflow_send_mails(): bool {
126
123
$ userto ->markposts = [];
127
124
128
125
// Cache the capabilities of the user.
129
- cron_setup_user ($ userto );
126
+ $ CFG -> branch >= 402 ? \ core \cron:: setup_user ( $ userto ) : cron_setup_user ($ userto );
130
127
131
128
// Reset the caches.
132
- foreach ($ coursemodules as $ moodleoverflowid => $ unused ) {
129
+ foreach ($ coursemodules as $ moodleoverflowid ) {
133
130
$ coursemodules [$ moodleoverflowid ]->cache = new stdClass ();
134
131
$ coursemodules [$ moodleoverflowid ]->cache ->caps = [];
135
132
unset($ coursemodules [$ moodleoverflowid ]->uservisible );
136
133
}
137
134
138
135
// Loop through all posts of this users.
139
136
foreach ($ posts as $ post ) {
140
-
141
137
self ::send_post ($ userto , $ post , $ coursemodules , $ errorcount ,
142
138
$ discussions , $ moodleoverflows , $ courses , $ mailcount , $ users , $ site , $ textout , $ htmlout );
143
139
}
@@ -148,18 +144,13 @@ public static function moodleoverflow_send_mails(): bool {
148
144
}
149
145
150
146
// Check for all posts whether errors occurred.
151
- if ($ posts ) {
152
-
153
- // Loop through all posts.
154
- foreach ($ posts as $ post ) {
155
-
156
- // Tracing information.
157
- mtrace ($ mailcount [$ post ->id ] . " users were sent post $ post ->id " );
147
+ foreach ($ posts as $ post ) {
148
+ // Tracing information.
149
+ mtrace ($ mailcount [$ post ->id ] . " users were sent post $ post ->id " );
158
150
159
- // Mark the posts with errors in the database.
160
- if ($ errorcount [$ post ->id ]) {
161
- $ DB ->set_field ('moodleoverflow_posts ' , 'mailed ' , self ::MOODLEOVERFLOW_MAILED_ERROR , ['id ' => $ post ->id ]);
162
- }
151
+ // Mark the posts with errors in the database.
152
+ if ($ errorcount [$ post ->id ]) {
153
+ $ DB ->set_field ('moodleoverflow_posts ' , 'mailed ' , self ::MOODLEOVERFLOW_MAILED_ERROR , ['id ' => $ post ->id ]);
163
154
}
164
155
}
165
156
@@ -232,7 +223,6 @@ public static function moodleoverflow_mark_old_posts_as_mailed($endtime) {
232
223
* @param stdClass $user
233
224
*/
234
225
public static function moodleoverflow_minimise_user_record (stdClass $ user ) {
235
-
236
226
// Remove all information for the mail generation that are not needed.
237
227
unset($ user ->institution );
238
228
unset($ user ->department );
@@ -268,61 +258,31 @@ public static function moodleoverflow_minimise_user_record(stdClass $user) {
268
258
private static function check_post ($ post , array &$ mailcount , array &$ users , array &$ discussions , array &$ errorcount ,
269
259
array &$ posts , int $ postid , array &$ moodleoverflows , array &$ courses ,
270
260
array &$ coursemodules ) {
271
- global $ DB ;
272
261
// Check the cache if the discussion exists.
273
262
$ discussionid = $ post ->discussion ;
274
- if (!isset ($ discussions [$ discussionid ])) {
275
- // Retrieve the discussion from the database.
276
- $ discussion = $ DB ->get_record ('moodleoverflow_discussions ' , ['id ' => $ post ->discussion ]);
277
-
278
- // If there is a record, update the cache. Else ignore the post.
279
- if ($ discussion ) {
280
- $ discussions [$ discussionid ] = $ discussion ;
281
- subscriptions::fill_subscription_cache ($ discussion ->moodleoverflow );
282
- subscriptions::fill_discussion_subscription_cache ($ discussion ->moodleoverflow );
283
- } else {
284
- mtrace ('Could not find discussion ' . $ discussionid );
285
- unset($ posts [$ postid ]);
286
- return ;
287
- }
263
+ if (!self ::cache_record ('moodleoverflow_discussions ' , $ discussionid , $ discussions ,
264
+ 'Could not find discussion ' , $ posts , $ postid , true )) {
265
+ return ;
288
266
}
289
267
290
268
// Retrieve the connected moodleoverflow instance from the database.
291
269
$ moodleoverflowid = $ discussions [$ discussionid ]->moodleoverflow ;
292
- if (!isset ($ moodleoverflows [$ moodleoverflowid ])) {
293
-
294
- // Retrieve the record from the database and update the cache.
295
- $ moodleoverflow = $ DB ->get_record ('moodleoverflow ' , ['id ' => $ moodleoverflowid ]);
296
- if ($ moodleoverflow ) {
297
- $ moodleoverflows [$ moodleoverflowid ] = $ moodleoverflow ;
298
- } else {
299
- mtrace ('Could not find moodleoverflow ' . $ moodleoverflowid );
300
- unset($ posts [$ postid ]);
301
- return ;
302
- }
270
+ if (!self ::cache_record ('moodleoverflow ' , $ moodleoverflowid , $ moodleoverflows ,
271
+ 'Could not find moodleoverflow ' , $ posts , $ postid , false )) {
272
+ return ;
303
273
}
304
274
305
275
// Retrieve the connected courses from the database.
306
276
$ courseid = $ moodleoverflows [$ moodleoverflowid ]->course ;
307
- if (!isset ($ courses [$ courseid ])) {
308
-
309
- // Retrieve the record from the database and update the cache.
310
- $ course = $ DB ->get_record ('course ' , ['id ' => $ courseid ]);
311
- if ($ course ) {
312
- $ courses [$ courseid ] = $ course ;
313
- } else {
314
- mtrace ('Could not find course ' . $ courseid );
315
- unset($ posts [$ postid ]);
316
- return ;
317
- }
277
+ if (!self ::cache_record ('course ' , $ courseid , $ courses ,
278
+ 'Could not find course ' , $ posts , $ postid , false )) {
279
+ return ;
318
280
}
319
281
320
282
// Retrieve the connected course modules from the database.
321
283
if (!isset ($ coursemodules [$ moodleoverflowid ])) {
322
-
323
284
// Retrieve the coursemodule and update the cache.
324
- $ cm = get_coursemodule_from_instance ('moodleoverflow ' , $ moodleoverflowid , $ courseid );
325
- if ($ cm ) {
285
+ if ($ cm = get_coursemodule_from_instance ('moodleoverflow ' , $ moodleoverflowid , $ courseid )) {
326
286
$ coursemodules [$ moodleoverflowid ] = $ cm ;
327
287
} else {
328
288
mtrace ('Could not find course module for moodleoverflow ' . $ moodleoverflowid );
@@ -333,14 +293,12 @@ private static function check_post($post, array &$mailcount, array &$users, arra
333
293
334
294
// Cache subscribed users of each moodleoverflow.
335
295
if (!isset ($ subscribedusers [$ moodleoverflowid ])) {
336
-
337
296
// Retrieve the context module.
338
297
$ modulecontext = context_module::instance ($ coursemodules [$ moodleoverflowid ]->id );
339
298
340
299
// Retrieve all subscribed users.
341
300
$ mid = $ moodleoverflows [$ moodleoverflowid ];
342
- $ subusers = subscriptions::get_subscribed_users ($ mid , $ modulecontext , 'u.* ' , true );
343
- if ($ subusers ) {
301
+ if ($ subusers = subscriptions::get_subscribed_users ($ mid , $ modulecontext , 'u.* ' , true )) {
344
302
// Loop through all subscribed users.
345
303
foreach ($ subusers as $ postuser ) {
346
304
// Save the user into the cache.
@@ -361,6 +319,39 @@ private static function check_post($post, array &$mailcount, array &$users, arra
361
319
$ errorcount [$ postid ] = 0 ;
362
320
}
363
321
322
+ /**
323
+ * Helper function for check_post(). Caches the a record exists in the database and caches the record if needed.
324
+ * @param string $table
325
+ * @param int $id
326
+ * @param array $cache
327
+ * @param string $errorMessage
328
+ * @param array $posts
329
+ * @param int $postid
330
+ * @param bool $fillsubscache If the subscription cache is being filled (only when checking discussion cache)
331
+ * @return bool
332
+ * @throws \dml_exception
333
+ */
334
+ private static function cache_record ($ table , $ id , &$ cache , $ errormessage , &$ posts , $ postid , $ fillsubscache ) {
335
+ global $ DB ;
336
+ // Check if cache if an record exists already in the cache.
337
+ if (!isset ($ cache [$ id ])) {
338
+ // If there is a record in the database, update the cache. Else ignore the post.
339
+ if ($ record = $ DB ->get_record ($ table , ['id ' => $ id ])) {
340
+ $ cache [$ id ] = $ record ;
341
+ if ($ fillsubscache ) {
342
+ subscriptions::fill_subscription_cache ($ record ->moodleoverflow );
343
+ subscriptions::fill_discussion_subscription_cache ($ record ->moodleoverflow );
344
+ }
345
+ } else {
346
+ mtrace ($ errormessage . $ id );
347
+ unset($ posts [$ postid ]);
348
+ return false ;
349
+ }
350
+ }
351
+ return true ;
352
+ }
353
+
354
+
364
355
/**
365
356
* Send the Mail with information of the post depending on theinformation available.
366
357
* E.g. anonymous post do not include names, users who want resumes do not get single mails.
@@ -427,11 +418,8 @@ private static function send_post($userto, $post, array &$coursemodules, array &
427
418
}
428
419
429
420
// Check whether the user is subscribed to the discussion.
430
- $ iscm = $ coursemodules [$ moodleoverflow ->id ];
431
421
$ uid = $ userto ->id ;
432
- $ did = $ post ->discussion ;
433
- $ issubscribed = subscriptions::is_subscribed ($ uid , $ moodleoverflow , $ modulecontext , $ did );
434
- if (!$ issubscribed ) {
422
+ if (!subscriptions::is_subscribed ($ uid , $ moodleoverflow , $ modulecontext , $ post ->discussion )) {
435
423
return ;
436
424
}
437
425
@@ -464,7 +452,7 @@ private static function send_post($userto, $post, array &$coursemodules, array &
464
452
}
465
453
466
454
// Setup roles and languages.
467
- cron_setup_user ($ userto , $ course );
455
+ $ CFG -> branch >= 402 ? \ core \cron:: setup_user ( $ userto , $ course ) : cron_setup_user ($ userto , $ course );
468
456
469
457
// Cache the users capability to view full names.
470
458
if (!isset ($ userto ->viewfullnames [$ moodleoverflow ->id ])) {
@@ -522,16 +510,7 @@ private static function send_post($userto, $post, array &$coursemodules, array &
522
510
}
523
511
524
512
// Format the data.
525
- $ data = new moodleoverflow_email (
526
- $ course ,
527
- $ cm ,
528
- $ moodleoverflow ,
529
- $ discussion ,
530
- $ post ,
531
- $ userfrom ,
532
- $ userto ,
533
- $ canreply
534
- );
513
+ $ data = new moodleoverflow_email ($ course , $ cm , $ moodleoverflow , $ discussion , $ post , $ userfrom , $ userto , $ canreply );
535
514
536
515
// Retrieve the unsubscribe-link.
537
516
$ userfrom ->customheaders [] = sprintf ('List-Unsubscribe: <%s> ' , $ data ->get_unsubscribediscussionlink ());
@@ -557,7 +536,6 @@ private static function send_post($userto, $post, array &$coursemodules, array &
557
536
558
537
// Check whether the post is a reply.
559
538
if ($ post ->parent ) {
560
-
561
539
// Add a reply header.
562
540
$ parentid = generate_email_messageid (hash ('sha256 ' , $ post ->parent . 'to ' . $ userto ->id ));
563
541
$ userfrom ->customheaders [] = "In-Reply-To: $ parentid " ;
0 commit comments