MyBB Community Forums

Full Version: Thread has a new post programmatically
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
so I just found a solution after reading the whole forumdisplay.php source code!!!!!!
it is too bad MyBB has no good documentation for programmers and it's API, anyway support forum still here and hope other people can use this:

First we need to grab the last thread read timestamp of user from database here is the query:

$query = $db->query("SELECT dateline FROM ".TABLE_PREFIX."threadsread WHERE tid='$tid' AND uid='$uid'");
$lastread = $db->fetch_field($query,"dateline");

you can add additional line of code if you want to work with guests too and have no empty $lastread variable, something like that:

$lastread = (!empty($lastread)? $lastread: my_get_array_cookie("threadread", $tid));

of course you need to have your thread last post timestamp too! you can use MyBB API in this case and no need to have additional query! here it is:


$thread = get_thread($tid);
$thread_lastpost = $thread['lastpost'];


and at the end, you need to compare these two values and do what you want:

if($thread_lastpost > $lastread )
{
//yourcode
}
else
{
//yourcode
}

In my case it will add an icon to the beginning of thread title so if the lamp icon was on this means user still not read thread after it's updated cause thread last read timestamp is smaller than thread last post timestamp, and if it's not it means user read thread after it's updated.


at the end you will have a code like this for both groups guests and users:


$query = $db->query("SELECT dateline FROM ".TABLE_PREFIX."threadsread WHERE tid='$tid' AND uid='$uid'");
$lastread = $db->fetch_field($query,"dateline");
$lastread = (!empty($lastread)? $lastread: my_get_array_cookie("threadread", $tid));
$thread = get_thread($tid);
$thread_lastpost = $thread['lastpost'];

if($thread_lastpost > $lastread )
{
//yourcode
}
else
{
//yourcode
}
That's a good start, but you seem to be missing things. For example, you query the threadsread table, but what about the forumsread table? You aren't querying that, but it is part of the logic in forumdisplay.php.
(2020-04-05, 11:06 AM)Laird Wrote: [ -> ]That's a good start, but you seem to be missing things. For example, you query the threadsread table, but what about the forumsread table? You aren't querying that, but it is part of the logic in forumdisplay.php.

My problem was only and only last threads read. for forums read MyBB do it by itself in very nice way Big Grin
cause of stats module to show which threads are still unread remains.
and this solution is for the people who want to know which threads are unreads. if  you're using ajax forum stats you will find what I mean.

https://community.mybb.com/mods.php?action=view&pid=93

It will show some informations about last posts, most viewed posts, popular users, etc ... on forum index page and it cannot show which topics are updated and which ones not.

unfortunately there is no screenshot but it is something like this one:

[Image: SinhVienIT.Net---v2xx.jpg]

such a module is very useful if its working correctly and optimized. and now I have optimized it. and removed a lot of it's features cause I only need latest posts, users with most posts and most viewed topics, all others are useless to me. and of course made it responsive.


Here is some screenshot from my forum to show how this part of code will works,
I just blured all titles and texts because they are wroted on persian and there is no need to show them. this code will change that small icon on threads and mark them as read or unread. so all users in the first visit of forum under heading section before forumdisplay starts will ses some statistic like that to follow new thread and posts easier.

[Image: attachment.php?aid=42720]

there is a lot of problem with this plugin, for example author was duplicate the whole code for every simple section instead of using classes and functions and reusing the code. and just edit queries!
and I just working on it to make it better and remove every duplicating codes, useless queries, use MyBB API instead of a new query every time and much more. but it customized based on my needs. maybe in future I working on a complete solution and upload it here, but unfortunately too busy currently Sad
(2020-04-05, 11:17 AM)sonixax Wrote: [ -> ]My problem was only and only last threads read.

Even so, I think you need to include the logic for forums read. Here's an example of why:

Let's say your user has some unread posts in a thread. But then, s/he marks the entire forum in which the thread appears as read. When s/he does this, the entry in the forumsread table is updated/created, but that in the threadsread table is not. So, your code, which checks only the threadsread table, will decide that the thread is unread - but if it had checked the forumsread table too, it would have realised that it was actually read, given that the user had marked the entire forum in which it exists as read.

Make sense?
(2020-04-05, 11:36 AM)Laird Wrote: [ -> ]
(2020-04-05, 11:17 AM)sonixax Wrote: [ -> ]My problem was only and only last threads read.

Even so, I think you need to include the logic for forums read. Here's an example of why:

Let's say your user has some unread posts in a thread. But then, s/he marks the entire forum in which the thread appears as read. When s/he does this, the entry in the forumsread table is updated/created, but that in the threadsread table is not. So, your code, which checks only the threadsread table, will decide that the thread is unread - but if it had checked the forumsread table too, it would have realised that it was actually read, given that the user had marked the entire forum in which it exists as read.

Make sense?

I just updated my last post, 
Please look into the screenshots. this module does not focused on forums. it's only focused on threads. I don't need such a feature but if anybody need it they need to edit the query and get both timestamps in one query using mysql subqueries feature or use some additional query like this


$fid = $thread['fid']; // get forum id from thread.
$query = $db->query("SELECT dateline FROM ".TABLE_PREFIX."forumsread WHERE fid='$fid' AND uid='$uid'");
$lastforumread = $db->fetch_field($query,"dateline");

also it is possible to use simple_select from MyBB API, but I don't want to use it cause if I need to write more complex queries later, I need to update whole code instead of my query only!
The pro stats plugin needs plenty of update work.
Pages: 1 2