문제

I'm trying to retrieve a specific users unread posts total from a phpbb3 database directly. I'm using ezsql to make my life easier, and I've written the following code based on the following post: https://www.phpbb.com/community/viewtopic.php?f=46&t=2107403#p12881167

$unreadposts always seems to return a number thats higher than the actual number of unread posts. I've been working on this the last 12 hours and its tearing my hair out :)

Any help or suggestions would be most appreciated!

        // Step 1: get all topics the user has access to. Assuming all of them are unread until we prove otherwise
        $usertopicsallowed = $forumdb->get_results("SELECT DISTINCT t.topic_id, t.forum_id, t.topic_last_post_time FROM $forumdbname.phpbb_users u 
        INNER JOIN  $forumdbname.phpbb_user_group ug ON u.user_id = ug.user_id
        INNER JOIN  $forumdbname.phpbb_groups g ON g.group_id = ug.group_id
        INNER JOIN  $forumdbname.phpbb_acl_groups acl ON acl.group_id = ug.group_id
        INNER JOIN  $forumdbname.phpbb_forums f ON f.forum_id = acl.forum_id
        INNER JOIN  $forumdbname.phpbb_topics t ON f.forum_id = t.forum_id
        WHERE u.user_id = " . $forumuserid . ";");

        $usertopicsallowedcnt = count($usertopicsallowed);

        // Step 2: Return any topics for this user in topics_track
        $usertopicstrack = $forumdb->get_results("SELECT topic_id, mark_time FROM phpbb_topics_track WHERE user_id = " . $forumuserid . " ;");

        if (!empty($usertopicsallowed))
        {
            foreach($usertopicsallowed as $key => $usertopicallowed)
            {
                if (!empty($usertopicstrack))
                {
                    foreach($usertopicstrack as $key2 => $usertopictrack)
                    {
                        if ($usertopicsallowed[$key]->topic_id == $usertopicstrack[$key2]->topic_id)
                        {
                            if ($usertopicsallowed[$key]->topic_last_post_time < $usertopicstrack[$key2]->mark_time)
                            {
                                unset($usertopicsallowed[$key]);
                            }
                        }   
                    }
                }
            }
        }
        $usertopicsallowed2 = array_values($usertopicsallowed);
        $usertopicsallowed2cnt = count($usertopicsallowed2);

        // Step 3a: eturn any topics for this user in forums_track
        $userforumstrack = $forumdb->get_results("SELECT forum_id, mark_time FROM phpbb_forums_track WHERE user_id = " . $forumuserid . ";");

        // Step 3b: remove all topics before the forum tracks lastmark time
        if (!empty($usertopicsallowed2))
        {
            foreach($usertopicsallowed2 as $key => $usertopicsallow2)
            {
                if (!empty($userforumstrack))
                {
                    foreach($userforumstrack as $key2 => $userforumtrack)
                    {
                        if ($usertopicsallowed2[$key]->forum_id == $userforumstrack[$key2]->forum_id)
                        {
                            if ($usertopicsallowed2[$key]->topic_last_post_time < $userforumstrack[$key2]->mark_time)
                            {
                                unset($usertopicsallowed2[$key]);
                            }
                        }   
                    }
                }
            }
        }
        $usertopicsallowed3 = array_values($usertopicsallowed2);
        $usertopicsallowed3cnt = count($usertopicsallowed3);

        // Step 4: remove all topics before the user's lastmark time
        if (!empty($usertopicsallowed3))
        {
            foreach($usertopicsallowed3 as $key => $usertopicsallow3)
            {
                if ($usertopicsallowed3[$key]->topic_last_post_time < $forumuserlastmark)
                {
                    unset($usertopicsallowed3[$key]);
                }
            }
        }
        $usertopicsallowed4 = array_values($usertopicsallowed3);
        $usertopicsallowed4cnt = count($usertopicsallowed4);

        $unreadposts = count($usertopicsallowed4);
도움이 되었습니까?

해결책

OK so as I said in my comment above, after 12 hours failing to get this to work I found an answer within 15 minutes of posting here! Noting this reply so maybe it will help someone else in the future, and I'll also reply on the link as well.

This link gives the answer (https://www.phpbb.com/community/viewtopic.php?f=46&t=2092813#p12800435) but doesn't allow for permissions, so you need to add the code I wrote below in addition. The full code is therefore:

// Step 1: get all topics the user has access to. Assuing all of them are unread until we prove otherwise
        $usertopicsallowed = $forumdb->get_results("SELECT DISTINCT t.topic_id, t.forum_id, t.topic_last_post_time FROM $forumdbname.phpbb_users u 
        INNER JOIN  $forumdbname.phpbb_user_group ug ON u.user_id = ug.user_id
        INNER JOIN  $forumdbname.phpbb_groups g ON g.group_id = ug.group_id
        INNER JOIN  $forumdbname.phpbb_acl_groups acl ON acl.group_id = ug.group_id
        INNER JOIN  $forumdbname.phpbb_forums f ON f.forum_id = acl.forum_id
        INNER JOIN  $forumdbname.phpbb_topics t ON f.forum_id = t.forum_id
        WHERE u.user_id = " . $forumuserid . ";");

        // Step 2: Calculate unread posts in all forums (regardless of permissions)         
        $phpbbv2 = $forumdb->get_results("SELECT t.topic_id, t.topic_last_post_time, tt.mark_time as topic_mark_time, ft.mark_time as forum_mark_time FROM (phpbb_topics t) 
        LEFT JOIN phpbb_topics_track tt ON (tt.user_id = " . $forumuserid . " AND t.topic_id = tt.topic_id) 
        LEFT JOIN phpbb_forums_track ft ON (ft.user_id = " . $forumuserid . " AND t.forum_id = ft.forum_id) 
        WHERE ( (tt.mark_time IS NOT NULL AND t.topic_last_post_time > tt.mark_time) 
        OR (tt.mark_time IS NULL AND ft.mark_time IS NOT NULL AND t.topic_last_post_time > ft.mark_time) 
        OR (tt.mark_time IS NULL AND ft.mark_time IS NULL AND t.topic_last_post_time > " . $forumuserlastmark . ") ) AND t.topic_moved_id = 0 AND t.topic_approved = 1 ORDER BY t.topic_last_post_time DESC;");

        // Step 3: Loop through step 2 and only increment the counter for every topic the user has permission to view
        $unreadcounter=0;
        if (!empty($phpbbv2))
        {
            foreach($phpbbv2 as $key => $phpbbv2a)
            {
                if (!empty($usertopicsallowed))
                {
                    foreach($usertopicsallowed as $key2 => $usertopicallowed)
                    {
                        if ($phpbbv2[$key]->topic_id == $usertopicsallowed[$key2]->topic_id)
                        {
                            $unreadcounter++;
                        }   
                    }
                }
            }
        }
        $unreadpost=$unreadcounter;

I'm sure it could be better, I'm not the most experienced in php but its working for me.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top