@@ -458,23 +458,11 @@ public static function get_untracked_moodleoverflows($userid, $courseid) {
458458 */
459459 public static function moodleoverflow_count_unread_posts_moodleoverflow ($ cm ) {
460460 global $ DB , $ USER ;
461-
462- $ moodleoverflow = $ DB ->get_record_sql ("SELECT m.*, tm.id as hasdisabledtracking " .
463- "FROM {moodleoverflow} m " .
464- "LEFT JOIN {moodleoverflow_tracking} tm ON m.id = tm.moodleoverflowid AND tm.userid = :userid " .
465- "WHERE m.id = :moodleoverflowid " , ['userid ' => $ USER ->id , 'moodleoverflowid ' => $ cm ->instance ]);
466-
467461 // Return if tracking is off, or ((optional or forced, but forced disallowed by admin) and user has disabled tracking).
468- if (
469- $ moodleoverflow ->trackingtype == MOODLEOVERFLOW_TRACKING_OFF || (
470- ($ moodleoverflow ->trackingtype == MOODLEOVERFLOW_TRACKING_OPTIONAL || (
471- $ moodleoverflow ->trackingtype == MOODLEOVERFLOW_TRACKING_FORCED &&
472- !get_config ('moodleoverflow ' , 'allowforcedreadtracking ' )
473- )
474- ) && $ moodleoverflow ->hasdisabledtracking )
475- ) {
462+ if (self ::check_tracking_off ($ cm ->instance , $ USER ->id )) {
476463 return 0 ;
477464 }
465+
478466 // Get the current timestamp and the cutoffdate.
479467 $ now = round (time (), -2 );
480468 $ cutoffdate = $ now - (get_config ('moodleoverflow ' , 'oldpostdays ' ) * 24 * 60 * 60 );
@@ -490,4 +478,63 @@ public static function moodleoverflow_count_unread_posts_moodleoverflow($cm) {
490478 // Return the number of unread posts per moodleoverflow.
491479 return $ DB ->get_field_sql ($ sql , $ params );
492480 }
481+
482+ /**
483+ * Get amound of unread posts in a discussion
484+ * @param int $discussionid
485+ * @param int $userid
486+ * @return int
487+ */
488+ public static function moodleoverflow_count_unread_posts_discussion (int $ discussionid , int $ userid = 0 ) {
489+ global $ DB , $ USER ;
490+ if ($ userid == 0 ) {
491+ $ userid = $ USER ->id ;
492+ }
493+ $ discussion = $ DB ->get_record ('moodleoverflow_discussions ' , ['id ' => $ discussionid ]);
494+
495+ if (self ::check_tracking_off ($ discussion ->moodleoverflow , $ userid )) {
496+ return 0 ;
497+ }
498+
499+ // Get the current timestamp and the cutoffdate.
500+ $ now = round (time (), -2 );
501+ $ cutoffdate = $ now - (get_config ('moodleoverflow ' , 'oldpostdays ' ) * 24 * 60 * 60 );
502+
503+ // Define a sql-query.
504+ $ params = [$ USER ->id , $ discussion ->id , $ cutoffdate ];
505+ $ sql = "SELECT COUNT(p.id)
506+ FROM {moodleoverflow_posts} p
507+ JOIN {moodleoverflow_discussions} d ON p.discussion = d.id
508+ LEFT JOIN {moodleoverflow_read} r ON (r.postid = p.id AND r.userid = ?)
509+ WHERE d.id = ? AND p.modified >= ? AND r.id IS NULL " ;
510+
511+ // Return the number of unread posts per discussion.
512+ return $ DB ->get_field_sql ($ sql , $ params );
513+ }
514+
515+ /**
516+ * Helper function, checks if:
517+ * tracking is off, or ((optional or forced, but forced disallowed by admin) and user has disabled tracking).
518+ * @param int $moodleoverflowid Object from DB.
519+ * @param int $userid
520+ * @return bool
521+ */
522+ private static function check_tracking_off (int $ moodleoverflowid , int $ userid ): bool {
523+ global $ DB ;
524+ $ moodleoverflow = $ DB ->get_record_sql ("SELECT m.*, tm.id as hasdisabledtracking " .
525+ "FROM {moodleoverflow} m " .
526+ "LEFT JOIN {moodleoverflow_tracking} tm ON m.id = tm.moodleoverflowid AND tm.userid = :userid " .
527+ "WHERE m.id = :moodleoverflowid " , ['userid ' => $ userid , 'moodleoverflowid ' => $ moodleoverflowid ]);
528+
529+ return (
530+ $ moodleoverflow ->trackingtype == MOODLEOVERFLOW_TRACKING_OFF || (
531+ $ moodleoverflow ->hasdisabledtracking && (
532+ $ moodleoverflow ->trackingtype == MOODLEOVERFLOW_TRACKING_OPTIONAL || (
533+ $ moodleoverflow ->trackingtype == MOODLEOVERFLOW_TRACKING_FORCED &&
534+ !get_config ('moodleoverflow ' , 'allowforcedreadtracking ' )
535+ )
536+ )
537+ )
538+ );
539+ }
493540}
0 commit comments