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